lang: ast: Split off helpers into util file

This commit is contained in:
James Shubin
2024-08-07 17:06:57 -04:00
parent 562eb643fc
commit 65b104ea55
2 changed files with 53 additions and 53 deletions

View File

@@ -9981,56 +9981,3 @@ func (obj *ExprIf) Value() (types.Value, error) {
}
return obj.ElseBranch.Value()
}
// getScope pulls the local stored scope out of an Expr, without needing to add
// a similarly named method to the Expr interface. This is private and not part
// of the interface, because it is only used internally.
// TODO: we could extend this to include Stmt's if it was ever useful
func getScope(node interfaces.Expr) (*interfaces.Scope, error) {
//if _, ok := node.(interfaces.Expr); !ok {
// return nil, fmt.Errorf("unexpected: %+v", node)
//}
switch expr := node.(type) {
case *ExprBool:
return expr.scope, nil
case *ExprStr:
return expr.scope, nil
case *ExprInt:
return expr.scope, nil
case *ExprFloat:
return expr.scope, nil
case *ExprList:
return expr.scope, nil
case *ExprMap:
return expr.scope, nil
case *ExprStruct:
return expr.scope, nil
case *ExprFunc:
return expr.scope, nil
case *ExprCall:
return expr.scope, nil
case *ExprVar:
return expr.scope, nil
case *ExprIf:
return expr.scope, nil
default:
return nil, fmt.Errorf("unexpected: %+v", node)
}
}
// trueCallee is a helper function because ExprTopLevel and ExprSingleton are
// sometimes added around builtins. This makes it difficult for the type checker
// to check if a particular builtin is the callee or not. This function removes
// the ExprTopLevel and ExprSingleton wrappers, if they exist.
func trueCallee(apparentCallee interfaces.Expr) interfaces.Expr {
switch x := apparentCallee.(type) {
case *ExprTopLevel:
return trueCallee(x.Definition)
case *ExprSingleton:
return trueCallee(x.Definition)
default:
return apparentCallee
}
}