Add if(condition, then, else) and refuse the broken inline ternary
Godot's Expression parses "a if c else b" but returns the wrong branch, so boards silently computed nonsense. Expressions now offer a spreadsheet-style if() and reject the inline form with a hint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8feaf2f217
commit
2ddf8c7c42
4 changed files with 49 additions and 1 deletions
15
core/expr.gd
15
core/expr.gd
|
|
@ -311,7 +311,17 @@ static func _rewrite(src: String) -> Dictionary:
|
||||||
while k < n and src[k] == " ":
|
while k < n and src[k] == " ":
|
||||||
k += 1
|
k += 1
|
||||||
var is_call := k < n and src[k] == "("
|
var is_call := k < n and src[k] == "("
|
||||||
if is_call or _KEYWORDS.has(word):
|
if word == "if" and is_call:
|
||||||
|
# Spreadsheet-style if(condition, then, else). Both branches
|
||||||
|
# are evaluated, so dice in either one are rolled.
|
||||||
|
out += "_if"
|
||||||
|
toks.append(["other", word])
|
||||||
|
elif word == "if" or word == "else":
|
||||||
|
# Godot's Expression parses "a if c else b" but returns the
|
||||||
|
# wrong branch, so refuse it rather than give silent nonsense.
|
||||||
|
return {"text": "", "inputs": [], "vars": [], "text_side": [],
|
||||||
|
"error": "Write if(condition, then, else) instead of \"… if … else …\""}
|
||||||
|
elif is_call or _KEYWORDS.has(word):
|
||||||
out += word
|
out += word
|
||||||
toks.append(["op" if word == "in" else "other", word])
|
toks.append(["op" if word == "in" else "other", word])
|
||||||
else:
|
else:
|
||||||
|
|
@ -388,6 +398,9 @@ class _Base:
|
||||||
func dm(x: Variant) -> int:
|
func dm(x: Variant) -> int:
|
||||||
return Expr.dm(x)
|
return Expr.dm(x)
|
||||||
|
|
||||||
|
func _if(cond: Variant, then_value: Variant, else_value: Variant) -> Variant:
|
||||||
|
return then_value if Expr.truthy(cond) else else_value
|
||||||
|
|
||||||
func has(list: Variant, item: Variant) -> bool:
|
func has(list: Variant, item: Variant) -> bool:
|
||||||
return Expr.has_item(list, item)
|
return Expr.has_item(list, item)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,9 @@ evaluated with Godot's `Expression`, after a small rewrite.
|
||||||
0 for 6–8, +1 for 9–11, +2 for 12–14, and +3 for 15 or more.
|
0 for 6–8, +1 for 9–11, +2 for 12–14, and +3 for 15 or more.
|
||||||
- `has(list, item)`: whether a comma-separated list contains an item.
|
- `has(list, item)`: whether a comma-separated list contains an item.
|
||||||
- `count(prefix)`: the number of set variables whose names start with `prefix`.
|
- `count(prefix)`: the number of set variables whose names start with `prefix`.
|
||||||
|
- `if(condition, then, else)`: picks a value. Both branches are evaluated, so dice in
|
||||||
|
either are rolled. The inline `a if c else b` form is refused, because Godot's
|
||||||
|
Expression returns the wrong branch for it.
|
||||||
|
|
||||||
Ranged `roll` tables keep plain dice (`2d6`) and also accept any expression, such as
|
Ranged `roll` tables keep plain dice (`2d6`) and also accept any expression, such as
|
||||||
`2d6 + dm(@end)` or `@terms`. Values beyond the lowest or highest range clamp to that range,
|
`2d6 + dm(@end)` or `@terms`. Values beyond the lowest or highest range clamp to that range,
|
||||||
|
|
|
||||||
31
tests/test_expr_if.gd
Normal file
31
tests/test_expr_if.gd
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
extends SceneTree
|
||||||
|
## if(condition, then, else) in expressions, and the refused inline ternary.
|
||||||
|
## Run: godot --headless --path . -s res://tests/test_expr_if.gd -- --sample
|
||||||
|
|
||||||
|
var _failures := 0
|
||||||
|
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
_value("if(1 > 2, 10, 20)", 20)
|
||||||
|
_value("if(3 > 2, 10, 20)", 10)
|
||||||
|
_value("if(1 = 1, \"yes\", \"no\")", "yes")
|
||||||
|
_value("if(0, 1, if(1, 2, 3))", 2)
|
||||||
|
_value("5 + if(2 >= 2, 1, 0)", 6)
|
||||||
|
for src in ["10 if 1 > 2 else 20", "1 if true else 2"]:
|
||||||
|
var r := Expr.evaluate(src, null)
|
||||||
|
_check(str(r.get("error", "")).contains("if(condition"), "\"%s\" is refused with a hint (got %s)" % [src, r])
|
||||||
|
_check(Expr.check(src) != "", "Expr.check flags \"%s\"" % src)
|
||||||
|
_check(Expr.check("if(@str > 8, 1, 0)") == "", "if() with a variable passes the check")
|
||||||
|
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
|
||||||
|
quit(1 if _failures else 0)
|
||||||
|
|
||||||
|
|
||||||
|
func _value(src: String, want: Variant) -> void:
|
||||||
|
var r := Expr.evaluate(src, null)
|
||||||
|
_check(str(r.get("error", "")) == "" and r.get("value") == want, "%s = %s (got %s)" % [src, want, r])
|
||||||
|
|
||||||
|
|
||||||
|
func _check(ok: bool, what: String) -> void:
|
||||||
|
if not ok:
|
||||||
|
_failures += 1
|
||||||
|
print(" ✗ ", what)
|
||||||
1
tests/test_expr_if.gd.uid
Normal file
1
tests/test_expr_if.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://b7jjpn8bqosv7
|
||||||
Loading…
Reference in a new issue