diff options
| author | Bobby <[email protected]> | 2024-01-24 21:03:26 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-01-24 21:03:26 +0000 |
| commit | c07b5f8fd0cf8bd1654825dcf7401d670fda4c0a (patch) | |
| tree | 21630a57b47598ddf58f9428e265157ce9206fc8 /ast/ast.go | |
| parent | b7d832da734647049f28c6994292d77b6e2c60eb (diff) | |
| download | mana-c07b5f8fd0cf8bd1654825dcf7401d670fda4c0a.tar.xz mana-c07b5f8fd0cf8bd1654825dcf7401d670fda4c0a.zip | |
If-Else Parsing
Diffstat (limited to 'ast/ast.go')
| -rw-r--r-- | ast/ast.go | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -88,6 +88,24 @@ func (i *Identifier) String() string { return i.Value } +// BlockStatement represents a block statement. +type BlockStatement struct { + Token tokens.Token // the token.LBRACE token + Statements []Statement +} + +func (bs *BlockStatement) statementNode() {} +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() +} + // ReturnStatement represents a return statement. type ReturnStatement struct { Token tokens.Token // the token.RETURN token @@ -204,3 +222,28 @@ func (b *Boolean) TokenLiteral() string { func (b *Boolean) String() string { return b.Token.Literal } + +type IfExpression struct { + Token tokens.Token // the 'if' token + Condition Expression + Consequence *BlockStatement + Alternative *BlockStatement +} + +func (ie *IfExpression) expressionNode() {} +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() +} |
