Implemented if statements
This commit is contained in:
@ -37,6 +37,15 @@ func (node *Define) String() string {
|
||||
return fmt.Sprintf("(= %s %s)", node.Name.Text(), node.Value)
|
||||
}
|
||||
|
||||
type If struct {
|
||||
Condition *expression.Expression
|
||||
Body AST
|
||||
}
|
||||
|
||||
func (node *If) String() string {
|
||||
return fmt.Sprintf("(if %s %s)", node.Condition, node.Body)
|
||||
}
|
||||
|
||||
type Loop struct {
|
||||
Body AST
|
||||
}
|
||||
|
@ -47,6 +47,22 @@ func toASTNode(tokens token.List) (Node, error) {
|
||||
tree, err := Parse(tokens[blockStart:blockEnd])
|
||||
return &Loop{Body: tree}, err
|
||||
|
||||
case keyword.If:
|
||||
blockStart := tokens.IndexKind(token.BlockStart) + 1
|
||||
blockEnd := tokens.LastIndexKind(token.BlockEnd)
|
||||
|
||||
if blockStart == -1 {
|
||||
return nil, errors.New(errors.MissingBlockStart, nil, tokens[0].End())
|
||||
}
|
||||
|
||||
if blockEnd == -1 {
|
||||
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
|
||||
}
|
||||
|
||||
condition := expression.Parse(tokens[1:token.BlockStart])
|
||||
tree, err := Parse(tokens[blockStart:blockEnd])
|
||||
return &If{Condition: condition, Body: tree}, err
|
||||
|
||||
default:
|
||||
return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, nil, tokens[0].Position)
|
||||
}
|
||||
|
Reference in New Issue
Block a user