Created
July 13, 2023 13:47
-
-
Save thebergamo/df180ed416be75766fe164f34c8cf120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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