Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions interscript-py/src/interscript/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion interscript-py/src/interscript/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions interscript-py/tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions lib/interscript/dsl/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions lib/interscript/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/interscript/stdlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/interscript/stdlib/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 92 additions & 0 deletions spec/isc/fail_loud_spec.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/stdlib_functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading