rlwhitcomb / utilities

Some of my personal utility programs
MIT License
2 stars 0 forks source link

Add possibility of named parameters to Calc user-defined functions #664

Closed rlwhitcomb closed 3 months ago

rlwhitcomb commented 3 months ago

Consider this:

define f(r = 0, R = 0, n = 0, N=0) = { ... }

which would like to invoke with a choice of parameters:

f(r=1.5, n= 20)

or

f(R=.5, N=15)

And since all the parameters have default values, we could omit all the values, so having named parameters means we don't have to figure out the order, simply attach the expressions via the parameter name.

rlwhitcomb commented 3 months ago

Getting an exception now:

Defining function call(m, n = 0, r = 1, p = -1, ...) = { $echo m ; $echo n ; _* }
> call(2,3,4,5,6,7)
Exception in thread "main" java.lang.NullPointerException
    at info.rlwhitcomb.calc.FunctionDeclaration.getConstantFlag(FunctionDeclaration.java:209)
    at info.rlwhitcomb.calc.FunctionScope.setParameterValue(FunctionScope.java:119)
    at info.rlwhitcomb.calc.FunctionScope.setParameterValue(FunctionScope.java:105)
    at info.rlwhitcomb.calc.FunctionDeclaration.setupFunctionCall(FunctionDeclaration.java:333)
    at info.rlwhitcomb.calc.LValueContext.getLValue(LValueContext.java:593)
    at info.rlwhitcomb.calc.CalcObjectVisitor.getLValue(CalcObjectVisitor.java:1233)
    at info.rlwhitcomb.calc.CalcObjectVisitor.visitVarExpr(CalcObjectVisitor.java:3997)
    at info.rlwhitcomb.calc.CalcParser$VarExprContext.accept(CalcParser.java:4888)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
    at info.rlwhitcomb.calc.CalcObjectVisitor.evaluate(CalcObjectVisitor.java:1631)
    at info.rlwhitcomb.calc.CalcObjectVisitor.visitExprStmt(CalcObjectVisitor.java:2607)
    at info.rlwhitcomb.calc.CalcParser$ExprStmtContext.accept(CalcParser.java:438)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
    at info.rlwhitcomb.calc.CalcObjectVisitor.internalVisitStatements(CalcObjectVisitor.java:2557)
    at info.rlwhitcomb.calc.CalcObjectVisitor.visitStmtOrExpr(CalcObjectVisitor.java:2571)
    at info.rlwhitcomb.calc.CalcParser$StmtOrExprContext.accept(CalcParser.java:311)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
    at info.rlwhitcomb.calc.CalcObjectVisitor.internalVisitStatements(CalcObjectVisitor.java:2557)
    at info.rlwhitcomb.calc.CalcObjectVisitor.visitProg(CalcObjectVisitor.java:2566)
    at info.rlwhitcomb.calc.CalcParser$ProgContext.accept(CalcParser.java:224)
    at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
    at info.rlwhitcomb.calc.Calc.process(Calc.java:2062)
    at info.rlwhitcomb.calc.Calc.main(Calc.java:2743)

I think it's on the "..." parameter.

rlwhitcomb commented 3 months ago

Need to determine how exactly to order the "setupFunctionCall" if there are mixed named and positional parameters, along with "..." and parameters with initial value expressions.

rlwhitcomb commented 3 months ago

Okay, another wrinkle here: we have to define the parameters in the order of declaration always because of the _* array, because it remembers the order and must mimic the declaration order, or it is unusable. Take for example:

define call(m, n = 0, r = 1, p = -1, ...) = { $echo m ; $echo n ; _* }
call(2,3,4,5,6,7)
call(m=23)
call(p=14)

The resulting parameter array looks like for the last example:

call(p = 14) -> [ 14, <null>, 0, 1 ]

which is actually parameters p, m, n, r, and NOT m, n, r, p as it should be.