boolean and if/else
This commit is contained in:
59
ast/ast.go
59
ast/ast.go
@@ -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() {}
|
||||
|
||||
Reference in New Issue
Block a user