coalesce operators '??' and '?=' now accepts function definitions too

This commit is contained in:
Celestino Amoroso 2024-04-20 09:40:07 +02:00
parent 6ae5ca34ed
commit d1122da566
2 changed files with 11 additions and 11 deletions

View File

@ -34,11 +34,11 @@ func evalNullCoalesce(ctx ExprContext, self *term) (v any, err error) {
if leftValue, exists := ctx.GetVar(leftTerm.source()); exists { if leftValue, exists := ctx.GetVar(leftTerm.source()); exists {
v = leftValue v = leftValue
} else if rightValue, err = self.children[1].compute(ctx); err == nil { } else if rightValue, err = self.children[1].compute(ctx); err == nil {
if _, ok := rightValue.(Functor); ok { // if _, ok := rightValue.(Functor); ok {
err = errCoalesceNoFunc(self.children[1]) // err = errCoalesceNoFunc(self.children[1])
} else { // } else {
v = rightValue v = rightValue
} // }
} }
return return
} }
@ -71,8 +71,8 @@ func evalAssignCoalesce(ctx ExprContext, self *term) (v any, err error) {
if leftValue, exists := ctx.GetVar(leftTerm.source()); exists { if leftValue, exists := ctx.GetVar(leftTerm.source()); exists {
v = leftValue v = leftValue
} else if rightValue, err = self.children[1].compute(ctx); err == nil { } else if rightValue, err = self.children[1].compute(ctx); err == nil {
if _, ok := rightValue.(Functor); ok { if functor, ok := rightValue.(Functor); ok {
err = errCoalesceNoFunc(self.children[1]) ctx.RegisterFunc(leftTerm.source(), functor, 0, -1)
} else { } else {
v = rightValue v = rightValue
ctx.setVar(leftTerm.source(), rightValue) ctx.setVar(leftTerm.source(), rightValue)
@ -82,9 +82,9 @@ func evalAssignCoalesce(ctx ExprContext, self *term) (v any, err error) {
} }
// utils // utils
func errCoalesceNoFunc(t *term) error { // func errCoalesceNoFunc(t *term) error {
return t.Errorf("the right operand of a coalescing operation cannot be a function definition") // return t.Errorf("the right operand of a coalescing operation cannot be a function definition")
} // }
// init // init
func init() { func init() {

View File

@ -134,7 +134,7 @@ func TestParser(t *testing.T) {
/* 113 */ {`import("test-funcs.expr"); (double(3+a) + 1) * two()`, int64(34), nil}, /* 113 */ {`import("test-funcs.expr"); (double(3+a) + 1) * two()`, int64(34), nil},
/* 114 */ {`x ?? "default"`, "default", nil}, /* 114 */ {`x ?? "default"`, "default", nil},
/* 115 */ {`x="hello"; x ?? "default"`, "hello", nil}, /* 115 */ {`x="hello"; x ?? "default"`, "hello", nil},
/* 116 */ {`x ?? func(){}`, nil, errors.New(`[1:14] the right operand of a coalescing operation cannot be a function definition`)}, /* 116 */ {`y=x ?? func(){4}; y()`, int64(4), nil},
/* 117 */ {`x ?= "default"; x`, "default", nil}, /* 117 */ {`x ?= "default"; x`, "default", nil},
/* 118 */ {`x="hello"; x ?= "default"; x`, "hello", nil}, /* 118 */ {`x="hello"; x ?= "default"; x`, "hello", nil},
/* 119 */ {`@x="hello"; @x`, nil, errors.New(`[1:3] variable references are not allowed in top level expressions: "@x"`)}, /* 119 */ {`@x="hello"; @x`, nil, errors.New(`[1:3] variable references are not allowed in top level expressions: "@x"`)},