From 65b104ea55f400ea25d8d235b1eb29b85a65919a Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 7 Aug 2024 17:06:57 -0400 Subject: [PATCH] lang: ast: Split off helpers into util file --- lang/ast/structs.go | 53 --------------------------------------------- lang/ast/util.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 89539d35..83d369bb 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -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 - } -} diff --git a/lang/ast/util.go b/lang/ast/util.go index bdeac99b..11105b64 100644 --- a/lang/ast/util.go +++ b/lang/ast/util.go @@ -287,3 +287,56 @@ func CopyNodeMapping(in map[string]interfaces.Node) map[string]interfaces.Node { } return out } + +// 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 + } +}