boolean and if/else

This commit is contained in:
Chuck Smith
2024-01-18 12:08:25 -05:00
parent 3d0247a7bb
commit fee3e38896
3 changed files with 263 additions and 0 deletions

View File

@@ -81,6 +81,23 @@ type InfixExpression struct {
Right Expression
}
type Boolean struct {
Token token.Token
Value bool
}
type BlockStatement struct {
Token token.Token // the { token
Statements []Statement
}
type IfExpression struct {
Token token.Token // The 'if' token
Condition Expression
Consequence *BlockStatement
Alternative *BlockStatement
}
func (ls *LetStatement) statementNode() {
}
func (ls *LetStatement) TokenLiteral() string {
@@ -180,3 +197,45 @@ func (ie *InfixExpression) String() string {
return out.String()
}
func (b Boolean) TokenLiteral() string {
return b.Token.Literal
}
func (b Boolean) String() string {
return b.Token.Literal
}
func (b Boolean) expressionNode() {}
func (bs BlockStatement) TokenLiteral() string {
return bs.Token.Literal
}
func (bs BlockStatement) String() string {
var out bytes.Buffer
for _, s := range bs.Statements {
out.WriteString(s.String())
}
return out.String()
}
func (bs BlockStatement) statementNode() {}
func (ie IfExpression) TokenLiteral() string {
return ie.Token.Literal
}
func (ie IfExpression) String() string {
var out bytes.Buffer
out.WriteString("if")
out.WriteString(ie.Condition.String())
out.WriteString(" ")
out.WriteString(ie.Consequence.String())
if ie.Alternative != nil {
out.WriteString("else ")
out.WriteString(ie.Alternative.String())
}
return out.String()
}
func (ie IfExpression) expressionNode() {}