diff --git a/go/parser-experiments/two/temp.bnf b/go/parser-experiments/two/temp.bnf index fbec7ad3e..1df446db8 100644 --- a/go/parser-experiments/two/temp.bnf +++ b/go/parser-experiments/two/temp.bnf @@ -30,7 +30,7 @@ SubroutineCallsite : "call" SubroutineName "(" ")" << dsl.NewASTNodeZary( - $0, + $1, dsl.NodeTypeSubroutineCallsite, ) >> @@ -50,7 +50,7 @@ SubroutineCallsite // * StringLiteral "b" << dsl.AdoptChildren( dsl.NewASTNodeNestable( - $0, + $1, dsl.NodeTypeSubroutineCallsite, ), $3, diff --git a/go/reg-test/cases/case-subr.sh b/go/reg-test/cases/case-subr.sh index a93c68a8a..5ffca03a3 100644 --- a/go/reg-test/cases/case-subr.sh +++ b/go/reg-test/cases/case-subr.sh @@ -1,12 +1,14 @@ # Check for BNF/AST errors, with minimal CST involvement -run_mlr -n put -v 'call s()' +mlr_expect_fail -n put -v 'call s()' mlr_expect_fail -n put -v 'call s' -run_mlr -n put -v 'call s(1,2,3)' +mlr_expect_fail -n put -v 'call s(1,2,3)' run_mlr -n put -v 'subr s() {}' run_mlr -n put -v 'subr s() {x=1}' run_mlr -n put -v 'subr s() {return}' mlr_expect_fail -n put -v 'subr s() {return 2}' mlr_expect_fail -n put 'subr s()' +run_mlr -n put -v 'subr s() {}; call s()' +run_mlr -n put -v 'call s(); subr s() {}' run_mlr -n put -v 'subr s(a) {print "HELLO, ".a."!"} call s("WORLD")' @@ -16,6 +18,13 @@ run_mlr --from $indir/2.dkvp put 'subr s() {return}' mlr_expect_fail --from $indir/2.dkvp put 'call s()' -run_mlr --from $indir/2.dkvp put 'subr s(a) {print "HELLO, ".a."!"} call s("WORLD")' - -#run_mlr --from $indir/2.dkvp put 'call s()' +run_mlr --from $indir/2.dkvp put -v -q ' + func s(x) { + return x*2; + } + subr s(a) { + print "HELLO, ".a."!" + } + print s(NR); + call s("WORLD"); +' diff --git a/go/reg-test/expected/case-subr.sh.out b/go/reg-test/expected/case-subr.sh.out index ad258a187..5006cd213 100644 --- a/go/reg-test/expected/case-subr.sh.out +++ b/go/reg-test/expected/case-subr.sh.out @@ -4,7 +4,7 @@ DSL EXPRESSION: call s() RAW AST: * statement block - * subroutine callsite "call" + * subroutine callsite "s" mlr -n put -v call s @@ -14,7 +14,7 @@ DSL EXPRESSION: call s(1,2,3) RAW AST: * statement block - * subroutine callsite "call" + * subroutine callsite "s" * int literal "1" * int literal "2" * int literal "3" @@ -68,6 +68,28 @@ RAW AST: mlr -n put subr s() +mlr -n put -v subr s() {}; call s() +DSL EXPRESSION: +subr s() {}; call s() +RAW AST: +* statement block + * subroutine definition "s" + * parameter list + * statement block + * subroutine callsite "s" + + +mlr -n put -v call s(); subr s() {} +DSL EXPRESSION: +call s(); subr s() {} +RAW AST: +* statement block + * subroutine callsite "s" + * subroutine definition "s" + * parameter list + * statement block + + mlr -n put -v subr s(a) {print "HELLO, ".a."!"} call s("WORLD") DSL EXPRESSION: subr s(a) {print "HELLO, ".a."!"} call s("WORLD") @@ -84,7 +106,7 @@ RAW AST: * string literal "HELLO, " * local variable "a" * string literal "!" - * subroutine callsite "call" + * subroutine callsite "s" * string literal "WORLD" @@ -98,6 +120,57 @@ a=eks,b=pan,i=2,x=0.7586799647899636,y=0.5221511083334797 mlr --from ./reg-test/input/2.dkvp put call s() -mlr --from ./reg-test/input/2.dkvp put subr s(a) {print "HELLO, ".a."!"} call s("WORLD") -Exit status was 1; expected 0. +mlr --from ./reg-test/input/2.dkvp put -v -q + func s(x) { + return x*2; + } + subr s(a) { + print "HELLO, ".a."!" + } + print s(NR); + call s("WORLD"); + +DSL EXPRESSION: + + func s(x) { + return x*2; + } + subr s(a) { + print "HELLO, ".a."!" + } + print s(NR); + call s("WORLD"); + +RAW AST: +* statement block + * function definition "s" + * parameter list + * parameter + * parameter name "x" + * statement block + * return "return" + * operator "*" + * local variable "x" + * int literal "2" + * subroutine definition "s" + * parameter list + * parameter + * parameter name "a" + * statement block + * print statement "print" + * operator "." + * operator "." + * string literal "HELLO, " + * local variable "a" + * string literal "!" + * print statement "print" + * function callsite "s" + * context variable "NR" + * subroutine callsite "s" + * string literal "WORLD" + +2 +HELLO, WORLD! +4 +HELLO, WORLD! diff --git a/go/src/miller/dsl/cst/root.go b/go/src/miller/dsl/cst/root.go index 476695ae5..7df2f2e18 100644 --- a/go/src/miller/dsl/cst/root.go +++ b/go/src/miller/dsl/cst/root.go @@ -55,6 +55,11 @@ func Build( return nil, err } + err = cstRoot.resolveSubroutineCallsites() + if err != nil { + return nil, err + } + return cstRoot, nil } @@ -185,16 +190,16 @@ func (this *RootNode) resolveSubroutineCallsites() error { this.unresolvedSubroutineCallsites.Front(), ).(*UDSCallsite) - functionName := unresolvedSubroutineCallsite.uds.signature.funcOrSubrName + subroutineName := unresolvedSubroutineCallsite.uds.signature.funcOrSubrName callsiteArity := unresolvedSubroutineCallsite.uds.signature.arity - uds, err := this.udsManager.LookUp(functionName, callsiteArity) + uds, err := this.udsManager.LookUp(subroutineName, callsiteArity) if err != nil { return err } if uds == nil { return errors.New( - "Miller: function name not found: " + functionName, + "Miller: subroutine name not found: " + subroutineName, ) } diff --git a/go/src/miller/dsl/cst/uds.go b/go/src/miller/dsl/cst/uds.go index b49a42fd7..6f6f19e5b 100644 --- a/go/src/miller/dsl/cst/uds.go +++ b/go/src/miller/dsl/cst/uds.go @@ -233,17 +233,6 @@ func (this *RootNode) BuildAndInstallUDS(astNode *dsl.ASTNode) error { parameterListASTNode := astNode.Children[0] subroutineBodyASTNode := astNode.Children[1] - returnValueTypeName := "any" - if len(astNode.Children) == 3 { - typeNode := astNode.Children[2] - lib.InternalCodingErrorIf(typeNode.Type != dsl.NodeTypeTypedecl) - returnValueTypeName = string(typeNode.Token.Lit) - } - typeGatedReturnValue, err := types.NewTypeGatedMlrvalName( - "subroutine return value", - returnValueTypeName, - ) - lib.InternalCodingErrorIf(parameterListASTNode.Type != dsl.NodeTypeParameterList) lib.InternalCodingErrorIf(parameterListASTNode.Children == nil) arity := len(parameterListASTNode.Children) @@ -274,7 +263,7 @@ func (this *RootNode) BuildAndInstallUDS(astNode *dsl.ASTNode) error { typeGatedParameterNames[i] = typeGatedParameterName } - signature := NewSignature(functionName, arity, typeGatedParameterNames, typeGatedReturnValue) + signature := NewSignature(functionName, arity, typeGatedParameterNames, nil) subroutineBody, err := this.BuildStatementBlockNode(subroutineBodyASTNode) if err != nil { diff --git a/go/src/miller/parsing/mlr.bnf b/go/src/miller/parsing/mlr.bnf index 5f34bd03c..4956e526b 100644 --- a/go/src/miller/parsing/mlr.bnf +++ b/go/src/miller/parsing/mlr.bnf @@ -1329,7 +1329,7 @@ FcnArgs SubroutineCallsite : call SubroutineName "(" ")" << dsl.NewASTNodeZary( - $0, + $1, dsl.NodeTypeSubroutineCallsite, ) >> @@ -1349,7 +1349,7 @@ SubroutineCallsite // * StringLiteral "b" << dsl.AdoptChildren( dsl.NewASTNodeNestable( - $0, + $1, dsl.NodeTypeSubroutineCallsite, ), $3, diff --git a/go/src/miller/parsing/parser/productionstable.go b/go/src/miller/parsing/parser/productionstable.go index 7b5b5b730..d3e535712 100644 --- a/go/src/miller/parsing/parser/productionstable.go +++ b/go/src/miller/parsing/parser/productionstable.go @@ -2532,7 +2532,7 @@ var productionsTable = ProdTab{ }, ProdTabEntry{ String: `SubroutineCallsite : call SubroutineName "(" ")" << dsl.NewASTNodeZary( - X[0], + X[1], dsl.NodeTypeSubroutineCallsite, ) >>`, Id: "SubroutineCallsite", @@ -2541,7 +2541,7 @@ var productionsTable = ProdTab{ NumSymbols: 4, ReduceFunc: func(X []Attrib) (Attrib, error) { return dsl.NewASTNodeZary( - X[0], + X[1], dsl.NodeTypeSubroutineCallsite, ) }, @@ -2549,7 +2549,7 @@ var productionsTable = ProdTab{ ProdTabEntry{ String: `SubroutineCallsite : call SubroutineName "(" FcnArgs ")" << dsl.AdoptChildren( dsl.NewASTNodeNestable( - X[0], + X[1], dsl.NodeTypeSubroutineCallsite, ), X[3], @@ -2561,7 +2561,7 @@ var productionsTable = ProdTab{ ReduceFunc: func(X []Attrib) (Attrib, error) { return dsl.AdoptChildren( dsl.NewASTNodeNestable( - X[0], + X[1], dsl.NodeTypeSubroutineCallsite, ), X[3],