-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refactor-str-seq' of https://github.com/arr-ai/arrai in…
…to refactor-str-seq
- Loading branch information
Showing
9 changed files
with
279 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package rel | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/arr-ai/wbnf/parser" | ||
) | ||
|
||
const safeCallOp = "safe_call" | ||
|
||
type SafeTailExpr struct { | ||
ExprScanner | ||
fallbackValue, base Expr | ||
tailExprs []func(Expr) Expr | ||
} | ||
|
||
func NewSafeTailExpr( | ||
scanner parser.Scanner, | ||
fallback, base Expr, | ||
tailExprs []func(Expr) Expr, | ||
) Expr { | ||
if len(tailExprs) == 0 { | ||
panic("exprs cannot be empty") | ||
} | ||
return &SafeTailExpr{ExprScanner{scanner}, fallback, base, tailExprs} | ||
} | ||
|
||
func (s *SafeTailExpr) Eval(local Scope) (value Value, err error) { | ||
value, err = s.base.Eval(local) | ||
if err != nil { | ||
return nil, wrapContext(err, s) | ||
} | ||
for _, t := range s.tailExprs { | ||
expr := t(value) | ||
if call, isCall := expr.(*BinExpr); isCall && call.op == "safe_call" { | ||
value, err = call.Eval(local) | ||
if err != nil { | ||
return nil, wrapContext(err, s) | ||
} | ||
|
||
for e, i := value.(Set).Enumerator(), 1; e.MoveNext(); i++ { | ||
if i > 1 { | ||
return s.fallbackValue.Eval(local) | ||
} | ||
} | ||
if !value.IsTrue() { | ||
return s.fallbackValue.Eval(local) | ||
} | ||
value = SetAny(value.(Set)) | ||
} else if safeDot, isSafeDot := expr.(*SafeDotExpr); isSafeDot { | ||
value, err = safeDot.Eval(local) | ||
if err != nil { | ||
if _, isMissingAttr := err.(missingAttrError); isMissingAttr { | ||
return s.fallbackValue.Eval(local) | ||
} | ||
return nil, wrapContext(err, s) | ||
} | ||
} else { | ||
value, err = expr.Eval(local) | ||
if err != nil { | ||
return nil, wrapContext(err, s) | ||
} | ||
} | ||
} | ||
return value, err | ||
} | ||
|
||
func (s *SafeTailExpr) String() string { | ||
finalExpr := s.tailExprs[0](s.base) | ||
if len(s.tailExprs) > 1 { | ||
for _, e := range s.tailExprs[1:] { | ||
finalExpr = e(finalExpr) | ||
} | ||
} | ||
return finalExpr.String() + fmt.Sprintf(":%s", s.fallbackValue.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package syntax | ||
|
||
import "testing" | ||
|
||
func TestSafeTail(t *testing.T) { | ||
t.Parallel() | ||
|
||
AssertCodesEvalToSameValue(t, `1 `, `(a: 1).a?:42 `) | ||
AssertCodesEvalToSameValue(t, `42`, `(a: 1).b?:42 `) | ||
AssertCodesEvalToSameValue(t, `1 `, `{"a": 1}("a")?:42 `) | ||
AssertCodesEvalToSameValue(t, `42`, `{"a": 1}("b")?:42 `) | ||
AssertCodesEvalToSameValue(t, `1 `, `(a: (b: 1)).a?.b:42 `) | ||
AssertCodesEvalToSameValue(t, `1 `, `{"a": {"b": 1}}("a")?("b"):42 `) | ||
AssertCodesEvalToSameValue(t, `1 `, `let a = (b: (c: (d: (e: 1)))); a.b.c?.d.e?:42`) | ||
AssertCodesEvalToSameValue(t, `1 `, `let a = (b: (c: (d: (e: 1)))); a.b.c?.d?.e:42`) | ||
AssertCodesEvalToSameValue(t, `42`, `let a = (b: (c: (d: (e: 1)))); a.b.c?.d.f?:42`) | ||
AssertCodesEvalToSameValue(t, `42`, `let a = (b: (c: (d: (e: 1)))); a.b.c?.f?.e:42`) | ||
AssertCodesEvalToSameValue(t, | ||
`1`, | ||
`let a = {"b": {"c": {"d": {"e": 1}}}}; a("b")("c")?("d")("e")?:42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`1`, | ||
`let a = {"b": {"c": {"d": {"e": 1}}}}; a("b")("c")?("d")?("e"):42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`42`, | ||
`let a = {"b": {"c": {"d": {"e": 1}}}}; a("b")("c")?("d")("f")?:42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`42`, | ||
`let a = {"b": {"c": {"d": {"e": 1}}}}; a("b")("c")?("f")?("e"):42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`1`, | ||
`let a = {"b": (c: (d: {"e": 1}))}; a("b").c?.d("e")?:42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`42`, | ||
`let a = {"b": (c: (d: {"e": 1}))}; a("b").c?.d("f")?:42 `) | ||
AssertCodesEvalToSameValue(t, | ||
`42`, | ||
`let a = {"b": (c: (d: {"e": 1}))}; a("b").c?.e?("f")?:42 `) | ||
|
||
AssertCodeErrors(t, `(a: 1).a?.c:42 `, `(1).c: lhs must be a Tuple, not rel.Number`) | ||
AssertCodeErrors(t, `(a: (b: 1)).a?.c:42 `, `Missing attr c`) | ||
AssertCodeErrors(t, | ||
`{"a": {"b": 1}}("a")?("c"):42`, | ||
`unexpected panic: Call: no return values from set {b: 1}`) | ||
} |
Oops, something went wrong.