Skip to content

Commit

Permalink
chore(minor): cleanup ast code
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedsaheed committed Feb 16, 2024
1 parent 701c4be commit 463318d
Showing 1 changed file with 92 additions and 84 deletions.
176 changes: 92 additions & 84 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ type Expression interface {
expressionNode()
}

/*
IntegerLiteral represents an integer literal.
For example, `5`.
*/
type IntegerLiteral struct {
Token token.Token
Value int64
}

type Program struct {
Statements []Statement
}

/*
Identifier represents an identifier.
For example, `x`.
Expand All @@ -50,78 +37,14 @@ type Identifier struct {
Value string
}

/*
ExpressionStatement represents an expression statement.
For example, `5 + 5;`.
*/
type ExpressionStatement struct {
Token token.Token
Expression Expression
}

/*
LetStatement represents a let statement.
For example, `let x = 5;`.
*/
type LetStatement struct {
Token token.Token // token.LET
Name *Identifier
Value Expression
}

/*
ReturnStatement represents a return statement.
For example, `return 5;`.
*/
type ReturnStatement struct {
Token token.Token
ReturnValue Expression
}

/*
PrefixExpression represents a prefix expression.
For example, the `-` in `-5`.
*/
type PrefixExpression struct {
Token token.Token
Operator string
Right Expression
}

/*
InfixExpression represents an infix expression.
For example, the `+` in `5 + 5`.
*/
type InfixExpression struct {
Token token.Token
Left Expression
Operator string
Right Expression
}

/*
Boolean represents a boolean value.
`true` or `false`.
*/
type Boolean struct {
Token token.Token
Value bool
}

/*
BlockStatement represents a block statement.
A block statement is a sequence of statements enclosed in braces.
*/
type BlockStatement struct {
Token token.Token
Statements []Statement
}

func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string { return i.Token.Literal }
func (i *Identifier) String() string { return i.Value }

type Program struct {
Statements []Statement
}

func (p *Program) TokenLiteral() string {
if len(p.Statements) > 0 {
return p.Statements[0].TokenLiteral()
Expand All @@ -137,6 +60,16 @@ func (p *Program) String() string {
return out.String()
}

/*
LetStatement represents a let statement.
For example, `let x = 5;`.
*/
type LetStatement struct {
Token token.Token // token.LET
Name *Identifier
Value Expression
}

func (ls *LetStatement) TokenLiteral() string { return ls.Token.Literal }
func (ls *LetStatement) statementNode() {}
func (ls *LetStatement) String() string {
Expand All @@ -153,6 +86,15 @@ func (ls *LetStatement) String() string {
return out.String()
}

/*
ReturnStatement represents a return statement.
For example, `return 5;`.
*/
type ReturnStatement struct {
Token token.Token
ReturnValue Expression
}

func (rs *ReturnStatement) statementNode() {}
func (rs *ReturnStatement) TokenLiteral() string { return rs.Token.Literal }
func (rs *ReturnStatement) String() string {
Expand All @@ -166,6 +108,15 @@ func (rs *ReturnStatement) String() string {
return out.String()
}

/*
ExpressionStatement represents an expression statement.
For example, `5 + 5;`.
*/
type ExpressionStatement struct {
Token token.Token
Expression Expression
}

func (es *ExpressionStatement) statementNode() {}
func (es *ExpressionStatement) TokenLiteral() string { return es.Token.Literal }
func (es *ExpressionStatement) String() string {
Expand All @@ -175,10 +126,29 @@ func (es *ExpressionStatement) String() string {
return ""
}

/*
IntegerLiteral represents an integer literal.
For example, `5`.
*/
type IntegerLiteral struct {
Token token.Token
Value int64
}

func (il *IntegerLiteral) expressionNode() {}
func (il *IntegerLiteral) TokenLiteral() string { return il.Token.Literal }
func (il *IntegerLiteral) String() string { return il.Token.Literal }

/*
PrefixExpression represents a prefix expression.
For example, the `-` in `-5`.
*/
type PrefixExpression struct {
Token token.Token
Operator string
Right Expression
}

func (pe *PrefixExpression) expressionNode() {}
func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
func (pe *PrefixExpression) String() string {
Expand All @@ -191,6 +161,17 @@ func (pe *PrefixExpression) String() string {
return out.String()
}

/*
InfixExpression represents an infix expression.
For example, the `+` in `5 + 5`.
*/
type InfixExpression struct {
Token token.Token
Left Expression
Operator string
Right Expression
}

func (ie *InfixExpression) expressionNode() {}
func (ie *InfixExpression) TokenLiteral() string { return ie.Token.Literal }
func (ie *InfixExpression) String() string {
Expand All @@ -205,10 +186,29 @@ func (ie *InfixExpression) String() string {
return out.String()
}

/*
Boolean represents a boolean value.
`true` or `false`.
*/
type Boolean struct {
Token token.Token
Value bool
}

func (b *Boolean) expressionNode() {}
func (b *Boolean) TokenLiteral() string { return b.Token.Literal }
func (b *Boolean) String() string { return b.Token.Literal }

/*
BlockStatement represents a block statement.
A block statement is a sequence of statements enclosed in braces.
*/
type BlockStatement struct {
Token token.Token
Statements []Statement
}

func (bs *BlockStatement) statementNode() {}
func (bs *BlockStatement) TokenLiteral() string { return bs.Token.Literal }
func (bs *BlockStatement) String() string {
Expand All @@ -221,11 +221,15 @@ func (bs *BlockStatement) String() string {
return out.String()
}

/*
IfExpression represents an if expression.
For example, `if (x < y) { x } else { y }`.
*/
type IfExpression struct {
Token token.Token
Condition Expression
Consequence *BlockStatement
Alternative *BlockStatement
Condition Expression // The condition to be evaluated
Consequence *BlockStatement // The block statement to be executed if the condition is true
Alternative *BlockStatement // The block statement to be executed if the condition is false
}

func (ie *IfExpression) expressionNode() {}
Expand All @@ -246,6 +250,10 @@ func (ie *IfExpression) String() string {
return out.String()
}

/*
FunctionLiteral represents a function literal.
For example, `fn(x, y) { x + y; }`.
*/
type FunctionLiteral struct {
Token token.Token
Parameters []*Identifier
Expand Down

0 comments on commit 463318d

Please sign in to comment.