diff --git a/interscript-py/src/interscript/executor.py b/interscript-py/src/interscript/executor.py index a699bd9b..4d74409d 100644 --- a/interscript-py/src/interscript/executor.py +++ b/interscript-py/src/interscript/executor.py @@ -76,12 +76,13 @@ def execute_stage( ) -> str: """Run one stage by name; returns the transformed string. - A missing stage is a no-op (returns the input unchanged), matching - the TS runtime. `language` is the ISC-WF-1 W3 caller selection (§5.1). + A missing stage raises MapLogicError (WO18 G4 parity audit: the old + silent no-op hid map typos; mirrors rs/ruby). `language` is the + ISC-WF-1 W3 caller selection (§5.1). """ stage = map_.stage(stage_name) if stage is None: - return text + raise MapLogicError(f'stage "{stage_name}" not found in {map_.system_code}') ctx = ExecutionContext(map_, text, loader, language) execute_stage_rules(stage, ctx) return ctx.current diff --git a/interscript-py/src/interscript/items.py b/interscript-py/src/interscript/items.py index bb382974..34de3c16 100644 --- a/interscript-py/src/interscript/items.py +++ b/interscript-py/src/interscript/items.py @@ -97,8 +97,13 @@ def compile_item( a = _alias_compiled(item.name) return _compiled(a.re, a.literal) resolved = ctx.resolve_alias(item.name) + # WO18 G3 (parity audit): an unresolved alias is map-logic + # damage, not an empty matcher. Compiling re="" matched at every + # string boundary and silently spliced the replacement text + # ("a" -> "XaX"); ruby raises SystemConversionError and rs + # fails at load. if resolved is None: - return _compiled("", "") + raise MapLogicError(f"Alias {item.name} not found") if ( class_ctx and item.name not in ctx.map.aliases diff --git a/interscript-py/tests/test_executor.py b/interscript-py/tests/test_executor.py index 07166bc1..4c90e228 100644 --- a/interscript-py/tests/test_executor.py +++ b/interscript-py/tests/test_executor.py @@ -55,9 +55,13 @@ def test_plain_substitution(self): m = make_map([stage("main", [sub("a", "b")])]) assert execute_stage(m, "main", "aaa") == "bbb" - def test_missing_stage_is_noop(self): + def test_missing_stage_raises(self): + # WO18 G4 (parity audit): a missing stage used to be a silent + # no-op returning the input, hiding map typos. Canon (rs, and + # ruby post-fix) is a named MapLogicError. m = make_map([stage("other", [sub("a", "b")])]) - assert execute_stage(m, "main", "aaa") == "aaa" + with pytest.raises(MapLogicError, match='stage "main" not found'): + execute_stage(m, "main", "aaa") def test_before_after_constraints(self): rules = [ @@ -245,6 +249,17 @@ def test_stdlib_boundary_alias(self): m = make_map([stage("main", [rule])]) assert execute_stage(m, "main", "ab cd") == "|ab| |cd|" + def test_unresolved_alias_raises(self): + # WO18 G3 (parity audit): an unresolved alias used to compile to + # the EMPTY matcher, which matches at every string boundary and + # silently spliced the replacement text ("a" -> "XaX"). Canon + # (ruby SystemConversionError / rs load error) is fail loud. + m = make_map( + [stage("main", [sub(AliasItem(name="nowhere"), "X")])] + ) + with pytest.raises(MapLogicError, match="Alias nowhere not found"): + execute_stage(m, "main", "a") + class TestItemKinds: def test_char_class_range(self): diff --git a/lib/interscript/dsl/group.rb b/lib/interscript/dsl/group.rb index c49a1f85..1c022f87 100644 --- a/lib/interscript/dsl/group.rb +++ b/lib/interscript/dsl/group.rb @@ -35,6 +35,12 @@ def downcase end Interscript::Stdlib.available_functions.each do |fun| + # upcase stays the to-symbol builder (`sub "a", upcase`): the + # Interpreter special-cases `to == :upcase` on the compiled rule, + # and dsl_stage_spec pins that path. The ISC surface's upcase + # directive does not go through this DSL. + next if fun == :upcase + define_method fun do |**kwargs| puts "funcall(#{fun}, #{kwargs.inspect}) from #{inspect}" if $DEBUG diff --git a/lib/interscript/interpreter.rb b/lib/interscript/interpreter.rb index 0e123587..938c48c4 100644 --- a/lib/interscript/interpreter.rb +++ b/lib/interscript/interpreter.rb @@ -6,7 +6,12 @@ def compile(map, _: nil) end def call(str, stage = :main, each: false, language: nil, &block) + stage_name = stage stage = @map.stages[stage] + # WO18 G4 (parity audit): a missing stage used to crash with an + # accidental NoMethodError (nil flowed into execute_rule). rs's + # named error is canon. + raise Interscript::MapLogicError, %(stage "#{stage_name}" not found in #{@map.name}) unless stage if each e = Enumerator.new do |yielder| options = [] @@ -203,12 +208,17 @@ def execute_rule r when Interscript::Node::Rule::Funcall @str = Interscript::Stdlib::Functions.public_send(r.name, @str, **r.kwargs) when Interscript::Node::Rule::Run + # WO18 G4 (parity audit): a missing run-target stage used to + # crash with `NoMethodError undefined method 'reverse_run' for + # nil`; raise the named error instead (rs canon). if r.stage.map doc = @map.dep_aliases[r.stage.map].document stage = doc.imported_stages[r.stage.name] + raise Interscript::MapLogicError, %(stage "#{r.stage.name}" not found in #{doc.name}) unless stage @str = Stage.new(doc, @str, language: @language).execute_rule(stage) else stage = @map.imported_stages[r.stage.name] + raise Interscript::MapLogicError, %(stage "#{r.stage.name}" not found in #{@map.name}) unless stage @str = Stage.new(@map, @str, language: @language).execute_rule(stage) end end diff --git a/lib/interscript/stdlib.rb b/lib/interscript/stdlib.rb index 81cbdb17..92d89f58 100644 --- a/lib/interscript/stdlib.rb +++ b/lib/interscript/stdlib.rb @@ -176,7 +176,10 @@ def self.deterministic_sort_by_max_length(ary) end def self.available_functions - %i[title_case downcase compose decompose separate unseparate secryst rababa rababa_reverse] + # WO18 G8 (parity audit): the ISC fn literals upcase/reverse/strip/ + # swapcase are part of the grammar surface; declare them alongside + # the classic set (Stdlib::Functions implements all four). + %i[title_case downcase upcase compose decompose separate unseparate reverse strip swapcase secryst rababa rababa_reverse] end def self.reverse_function diff --git a/lib/interscript/stdlib/functions.rb b/lib/interscript/stdlib/functions.rb index 4885b030..4580addc 100644 --- a/lib/interscript/stdlib/functions.rb +++ b/lib/interscript/stdlib/functions.rb @@ -18,6 +18,27 @@ def self.downcase(output, word_separator: nil) end end + # WO18 G8 (parity audit): the ISC surface's fn literals. upcase + # mirrors the ts/py/rs directive semantics (String#upcase); the ISC + # grammars carry reverse/strip/swapcase as fn literals too, so the + # Funcall dispatcher needs them (plain String semantics — no other + # runtime executes them yet). + def self.upcase(output, _: nil) + output.upcase + end + + def self.reverse(output, _: nil) + output.reverse + end + + def self.strip(output, _: nil) + output.strip + end + + def self.swapcase(output, _: nil) + output.swapcase + end + def self.compose(output, _: nil) output.unicode_normalize(:nfc) end diff --git a/spec/isc/fail_loud_spec.rb b/spec/isc/fail_loud_spec.rb new file mode 100644 index 00000000..21ada696 --- /dev/null +++ b/spec/isc/fail_loud_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +# WO18 G4/G8 — fail-loud + funcall parity from the merged four-runtime +# audit (maps docs/parity/README.md). +# +# G4: requesting a stage the map doesn't have used to crash with a +# NoMethodError (accidental nil dereference through execute_rule). +# Canon (rs) is a named MapLogicError at both sites — the requested +# main stage and a run's target stage. +# +# G8: the ISC surface includes the upcase/reverse/strip/swapcase fn +# literals (grammar `fn_lit` / stage directives); ruby's +# Stdlib::Functions was missing all four, so the `upcase` directive +# crashed with NoMethodError. ts/py/rs semantics are the reference. + +require "tmpdir" + +RSpec.describe "WO18 — interpreter fail-loud behavior" do + def with_map(name, body) + dir = Dir.mktmpdir + File.write(File.join(dir, name), body) + Interscript.load_path.unshift(dir) + yield + ensure + Interscript.load_path.delete(dir) if dir + end + + describe "G4 — missing stage" do + it "requesting an absent stage raises MapLogicError (was: NoMethodError)" do + with_map("tst-g4-main.isc", <<~ISC) do + system "T:tst-g4-main:1" { + stage main { sub "a" "b" } + } + ISC + interp = Interscript::Interpreter.call("tst-g4-main") + expect { interp.call("abc", :mahn) }.to raise_error( + Interscript::MapLogicError, + /stage "mahn" not found/ + ) + end + end + + it "running an absent stage target raises MapLogicError (was: NoMethodError)" do + with_map("tst-g4-run.isc", <<~ISC) do + system "T:tst-g4-run:1" { + stage main { run stage.nope } + } + ISC + interp = Interscript::Interpreter.call("tst-g4-run") + expect { interp.call("abc", :main) }.to raise_error( + Interscript::MapLogicError, + /stage "nope" not found/ + ) + end + end + + it "a present stage still executes" do + with_map("tst-g4-ok.isc", <<~ISC) do + system "T:tst-g4-ok:1" { + stage main { sub "a" "b" } + } + ISC + expect(Interscript::Interpreter.call("tst-g4-ok").call("abc", :main)).to eq("bbc") + end + end + end + + describe "G8 — fn literals execute (the audit's probe shapes)" do + it "the upcase stage directive runs (probe tst-upcase-exec)" do + with_map("tst-wo18-upcase.isc", <<~ISC) do + # P15: upcase funcall execution (ruby Stdlib::Functions had no upcase). + system "T:tst-wo18-upcase:1" { + stage main { + parallel { sub "§" " " } + upcase + } + } + ISC + expect(Interscript.transliterate("tst-wo18-upcase", "§abc")).to eq(" ABC") + end + end + + it "reverse/strip/swapcase dispatch through the Funcall path" do + # reverse/strip/swapcase are fn literals (`to` position) in the ISC + # grammar — not stage directives — so the executable surface is the + # Funcall rule; the legacy DSL builds one per available_function. + expect(stage { reverse }.call("abc")).to eq("cba") + expect(stage { strip }.call(" a b ")).to eq("a b") + expect(stage { swapcase }.call("aBc")).to eq("AbC") + end + end +end diff --git a/spec/stdlib_functions_spec.rb b/spec/stdlib_functions_spec.rb index 5021c534..88c8a2c1 100644 --- a/spec/stdlib_functions_spec.rb +++ b/spec/stdlib_functions_spec.rb @@ -9,6 +9,37 @@ expect(Interscript::Stdlib.available_functions).to include(:rababa, :secryst, :rababa_reverse) end + # WO18 G8 (parity audit): the ISC surface's fn literals — + # upcase/reverse/strip/swapcase — must be declared and implemented, + # not NoMethodError at dispatch (probe funcall-upcase-exec). + it "lists the four ISC fn literals as available functions" do + expect(Interscript::Stdlib.available_functions).to include(:upcase, :reverse, :strip, :swapcase) + end + + describe ".upcase" do + it "uppercases (ts semantics: to_uppercase)" do + expect(described_class.upcase("abc")).to eq("ABC") + end + end + + describe ".reverse" do + it "reverses the string" do + expect(described_class.reverse("abc")).to eq("cba") + end + end + + describe ".strip" do + it "strips leading and trailing whitespace" do + expect(described_class.strip(" a b ")).to eq("a b") + end + end + + describe ".swapcase" do + it "swaps character case" do + expect(described_class.swapcase("aBc")).to eq("AbC") + end + end + it "reverse-maps rababa to rababa_reverse" do expect(Interscript::Stdlib.reverse_function[:rababa]).to eq(:rababa_reverse) expect(Interscript::Stdlib.reverse_function[:rababa_reverse]).to eq(:rababa)