Skip to content

Instantly share code, notes, and snippets.

@thebergamo
Created July 13, 2023 13:47
Show Gist options
  • Save thebergamo/df180ed416be75766fe164f34c8cf120 to your computer and use it in GitHub Desktop.
Save thebergamo/df180ed416be75766fe164f34c8cf120 to your computer and use it in GitHub Desktop.
type IfStatNode struct {
Condition Node
ThenBranch Node
ElseBranch Node
}
func (n *IfStatNode) Interpret(i *Interpreter) (Node, error) {
conditionNode, err := n.Condition.Interpret(i)
if err != nil {
return nil, err
}
conditional, ok := conditionNode.(*BoolLiteral)
if !ok {
return nil, fmt.Errorf("expected conditional to be boolean literal, got %T", conditionNode)
}
var node Node
var e error
if conditional.Value {
node, e = n.ThenBranch.Interpret(i)
} else if n.ElseBranch != nil {
node, e = n.ElseBranch.Interpret(i)
}
return node, e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment