Skip to content

Commit 3b85bdd

Browse files
author
webdev778
committed
Add composability, ref: #465
1 parent 4a12385 commit 3b85bdd

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

README.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ It is then easy to see the exact differences in rendering between the systems.
108108
diff bgnpcgn-rus-Latn.txt bas-rus-Latn.txt
109109
----
110110

111+
You can also run some maps in reverse (warning: not all maps are made to be reversed),
112+
either by changing an order of character sets in a name (for bas-rus-`Cyrl-Latn`-2017-bss
113+
it would be bas-rus-`Latn-Cyrl`-2017-bss) or appending `-reverse` to their name:
114+
115+
[source,sh]
116+
----
117+
interscript my_jamos.txt \
118+
--system=var-kor-Hang-Hang-jamo-reverse \
119+
--output=my_hangul.txt
120+
----
121+
122+
You can also compose some maps, by joining their names with `|` (similar to how bash
123+
pipe works). For instance:
124+
125+
[source,sh]
126+
----
127+
interscript my_file.txt \
128+
--system='var-map1-Xxxx-Xxxx|var-map2-Xxxx-Xxxx' \
129+
--output=my_output.txt
130+
----
131+
111132
If you use Interscript from the Git repository, you would call the following command
112133
instead of `interscript`:
113134

ruby/lib/interscript/dsl.rb

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,39 @@
22

33
module Interscript::DSL
44
@cache = {}
5-
def self.parse(map_name)
5+
def self.parse(map_name, reverse: true)
66
# map name aliases? here may be a place to wrap it
77

88
return @cache[map_name] if @cache[map_name]
99

10-
reverse = false
10+
# This is a composition, so let's make a new virtual map
11+
# that calls all maps in a sequence.
12+
if map_name.include? "|"
13+
map_parts = map_name.split("|").map(&:strip)
14+
15+
doc = Interscript::DSL::Document.new(map_name) do
16+
map_parts.each_with_index do |i, idx|
17+
dependency i, as: :"part#{idx}"
18+
end
19+
20+
stage {
21+
map_parts.each_with_index do |i, idx|
22+
run map[:"part#{idx}"].stage.main
23+
end
24+
}
25+
end.node
26+
27+
return @cache[map_name] = doc
28+
end
29+
1130
path = begin
1231
Interscript.locate(map_name)
1332
rescue Interscript::MapNotFoundError => e
33+
# But maybe we called the map in a reversed fashion?
1434
begin
15-
reverse = map_name
16-
Interscript.locate(Interscript::Node::Document.reverse_name(map_name))
35+
raise e if reverse == false # Protect from an infinite loop
36+
reverse_name = Interscript::Node::Document.reverse_name(map_name)
37+
return @cache[map_name] = parse(reverse_name, reverse: false).reverse
1738
rescue Interscript::MapNotFoundError
1839
raise e
1940
end
@@ -66,12 +87,6 @@ def self.parse(map_name)
6687
obj.node.metadata = md.node
6788

6889
@cache[map_name] = obj.node
69-
70-
if reverse
71-
@cache[reverse] = @cache[map_name].reverse
72-
else
73-
@cache[map_name]
74-
end
7590
end
7691
end
7792

ruby/lib/interscript/utils/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def call(str, stage=:main, compiler=$compiler || Interscript::Interpreter, **kwa
2828
module Interscript::DSL
2929
class << self
3030
alias original_parse parse
31-
def parse(map_name)
31+
def parse(map_name, **kwargs)
3232
if $documents && $documents[map_name]
3333
$documents[map_name]
3434
else
35-
original_parse(map_name)
35+
original_parse(map_name, **kwargs)
3636
end
3737
end
3838
end

ruby/spec/composability_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
RSpec.describe "composability" do
2+
it "can depend on reversed maps" do
3+
a = document("part-1-One-Two") {
4+
stage {
5+
sub "a", "b"
6+
}
7+
}
8+
9+
b = document("part-2-One-Two") {
10+
stage {
11+
sub "c", "d"
12+
}
13+
}
14+
15+
c = document("composed") {
16+
dependency "part-1-Two-One", as: twoone
17+
dependency "part-2-One-Two", as: onetwo
18+
19+
stage {
20+
run map.twoone.stage.main
21+
run map.onetwo.stage.main
22+
}
23+
}
24+
25+
expect(c.("abcd")).to eq("aadd")
26+
end
27+
28+
it "can seamlessly compose two maps" do
29+
a = document("part1") {
30+
stage {
31+
sub "a", "b"
32+
}
33+
}
34+
b = document("part2") {
35+
stage {
36+
sub "c", "d"
37+
}
38+
}
39+
40+
c = document("composed2") {
41+
dependency "part1|part2", as: composed
42+
43+
stage {
44+
run map.composed.stage.main
45+
}
46+
}
47+
48+
expect(c.("abcd")).to eq("bbdd")
49+
end
50+
end

0 commit comments

Comments
 (0)