Refactored code structure

This commit is contained in:
2024-07-03 11:39:24 +02:00
parent ed03f6a802
commit feebfe65bb
54 changed files with 583 additions and 450 deletions

View File

@ -1,36 +1,54 @@
package ast
import (
"fmt"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
type Node interface{}
type Node fmt.Stringer
type AST []Node
type Assign struct {
Value *expression.Expression
Name token.Token
Value *expression.Expression
Name token.Token
Operator token.Token
}
func (node *Assign) String() string {
return fmt.Sprintf("(= %s %s)", node.Name.Text(), node.Value)
}
type Call struct {
Expression *expression.Expression
}
func (node *Call) String() string {
return node.Expression.String()
}
type Define struct {
Value *expression.Expression
Name token.Token
}
type If struct {
Condition *expression.Expression
Body AST
func (node *Define) String() string {
return fmt.Sprintf("(= %s %s)", node.Name.Text(), node.Value)
}
type Loop struct {
Body AST
}
func (node *Loop) String() string {
return fmt.Sprintf("(loop %s)", node.Body)
}
type Return struct {
Value *expression.Expression
}
func (node *Return) String() string {
return fmt.Sprintf("(return %s)", node.Value)
}

View File

@ -1,10 +1,10 @@
package ast
import (
"git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/keyword"
"git.akyoto.dev/cli/q/src/build/token"
"git.akyoto.dev/cli/q/src/errors"
)
// Parse generates an AST from a list of tokens.
@ -75,7 +75,7 @@ func toASTNode(tokens token.List) (Node, error) {
name := expr.Children[0].Token
value := expr.Children[1]
return &Assign{Name: name, Value: value}, nil
return &Assign{Name: name, Value: value, Operator: expr.Token}, nil
case IsFunctionCall(expr):
return &Call{Expression: expr}, nil