Skip to content

Commit 1c198e4

Browse files
committed
Rewrite (+ 1 2) to (callm 1 + 2)
1 parent e1a6041 commit 1c198e4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

transform.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def rewrite_lambda(exp)
2929
end
3030
end
3131
end
32-
32+
3333

3434
# Re-write string constants outside %s() to
3535
# %s(call __get_string [original string constant])
@@ -78,6 +78,20 @@ def rewrite_fixnumconst(exp)
7878
end
7979

8080

81+
# Rewrite operators that should be treated as method calls
82+
# so that e.g. (+ 1 2) is turned into (callm 1 + 2)
83+
#
84+
def rewrite_operators(exp)
85+
exp.depth_first do |e|
86+
next :skip if e[0] == :sexp
87+
88+
if OPER_METHOD.member?(e[0].to_s)
89+
e[3] = e[2]
90+
e[2] = e[0]
91+
e[0] = :callm
92+
end
93+
end
94+
end
8195

8296
# 1. If I see an assign node, the variable on the left hand is defined
8397
# for the remainder of this scope and on any sub-scope.
@@ -199,6 +213,7 @@ def rewrite_let_env(exp)
199213
def preprocess exp
200214
rewrite_strconst(exp)
201215
rewrite_fixnumconst(exp)
216+
rewrite_operators(exp)
202217
rewrite_let_env(exp)
203218
rewrite_lambda(exp)
204219
end

0 commit comments

Comments
 (0)