mirror of
https://github.com/hanzoai/numscript.git
synced 2026-10-30 18:26:57 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers, and the interpreter's parseArg, evaluateExprAs and evaluateOptExprAs are now generic methods on the parser and program state they take. omitzero is not applied, since it changes what the JSON says. Co-Authored-By: Claude Opus 5 <[email protected]>
44 lines
822 B
Go
44 lines
822 B
Go
package interpreter
|
|
|
|
import "github.com/formancehq/numscript/internal/parser"
|
|
|
|
type argsParser struct {
|
|
parsedArgsCount int
|
|
args []Value
|
|
err InterpreterError
|
|
}
|
|
|
|
func NewArgsParser(args []Value) *argsParser {
|
|
return &argsParser{
|
|
args: args,
|
|
}
|
|
}
|
|
|
|
func (p *argsParser) parseArg[T any](r parser.Range, expect func(Value, parser.Range) (*T, InterpreterError)) *T {
|
|
index := p.parsedArgsCount
|
|
p.parsedArgsCount++
|
|
|
|
if p.err != nil || index >= len(p.args) {
|
|
return nil
|
|
}
|
|
|
|
arg := p.args[index]
|
|
parsed, err := expect(arg, r)
|
|
if err != nil {
|
|
p.err = err
|
|
return nil
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func (p *argsParser) parse() InterpreterError {
|
|
if len(p.args) != p.parsedArgsCount {
|
|
p.err = BadArityErr{
|
|
ExpectedArity: p.parsedArgsCount,
|
|
GivenArguments: len(p.args),
|
|
}
|
|
}
|
|
|
|
return p.err
|
|
}
|