Skip to content

Commit 2fc6b85

Browse files
author
webdev778
committed
Add debugging facilities to Python compiler
1 parent 3e618fb commit 2fc6b85

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/interscript/compiler/python.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def indent
3838

3939
def emit(code)
4040
@code << (" " * @indent) << code << "\n"
41+
code
4142
end
4243

4344
def compile(map, debug: false)
@@ -69,7 +70,7 @@ def compile(map, debug: false)
6970
emit "_PTREE_#{k} = #{escape v}"
7071
end
7172
@parallel_regexps.each do |k,v|
72-
v = "[\"#{v[0]}\", #{escape v[1]}]"
73+
v = %{["#{v[0]}", #{escape v[1]}]}
7374
emit "_PRE_#{k} = #{v}"
7475
end
7576
end
@@ -87,12 +88,15 @@ def compile_rule(r, map = @map, wrapper = false)
8788
return if r.reverse_run == true
8889
case r
8990
when Interscript::Node::Stage
90-
#c += "$map_debug ||= []\n" if @debug
91+
if @debug
92+
emit "if not hasattr(interscript, 'map_debug'):"
93+
indent { emit "interscript.map_debug = []" }
94+
end
9195
emit "def _stage_#{r.name}(s):"
9296
indent do
9397
r.children.each do |t|
94-
compile_rule(t, map)
95-
#c += %{$map_debug << [s.dup, #{@map.name.to_s.inspect}, #{r.name.to_s.inspect}, #{t.inspect.inspect}, #{comp.inspect}]\n} if @debug
98+
comp = compile_rule(t, map)
99+
emit %{interscript.map_debug.append([s, #{escape @map.name.to_s}, #{escape r.name.to_s}, #{escape t.inspect}, #{escape comp}])} if @debug
96100
end
97101
emit "return s\n"
98102
end
@@ -317,10 +321,10 @@ def call(str, stage=:main)
317321
end
318322

319323
def self.read_debug_data
320-
$map_debug || []
324+
(ctx['map_debug'] || []).map(&:to_a).to_a
321325
end
322326

323327
def self.reset_debug_data
324-
$map_debug = []
328+
ctx['map_debug'].clear
325329
end
326330
end

0 commit comments

Comments
 (0)