From 2ddf8c7c42154cc31fad8de10d21b649a5a383bb Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Sun, 8 Nov 2026 14:20:05 -0600 Subject: [PATCH] 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 --- core/expr.gd | 15 ++++++++++++++- docs/LOGIC.md | 3 +++ tests/test_expr_if.gd | 31 +++++++++++++++++++++++++++++++ tests/test_expr_if.gd.uid | 1 + 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/test_expr_if.gd create mode 100644 tests/test_expr_if.gd.uid diff --git a/core/expr.gd b/core/expr.gd index 4ca7104..385ecc8 100644 --- a/core/expr.gd +++ b/core/expr.gd @@ -311,7 +311,17 @@ static func _rewrite(src: String) -> Dictionary: while k < n and src[k] == " ": k += 1 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 toks.append(["op" if word == "in" else "other", word]) else: @@ -388,6 +398,9 @@ class _Base: func dm(x: Variant) -> int: 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: return Expr.has_item(list, item) diff --git a/docs/LOGIC.md b/docs/LOGIC.md index 5a28dc2..43792cd 100644 --- a/docs/LOGIC.md +++ b/docs/LOGIC.md @@ -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. - `has(list, item)`: whether a comma-separated list contains an item. - `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 `2d6 + dm(@end)` or `@terms`. Values beyond the lowest or highest range clamp to that range, diff --git a/tests/test_expr_if.gd b/tests/test_expr_if.gd new file mode 100644 index 0000000..15d15c3 --- /dev/null +++ b/tests/test_expr_if.gd @@ -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) diff --git a/tests/test_expr_if.gd.uid b/tests/test_expr_if.gd.uid new file mode 100644 index 0000000..eff6de3 --- /dev/null +++ b/tests/test_expr_if.gd.uid @@ -0,0 +1 @@ +uid://b7jjpn8bqosv7