Skip to content

Commit b37027d

Browse files
committed
This is a preliminary, experimental, broken first stab at some basic inline support, not currently enabled anywhere (it'd need to be added the transformation stages in transform.rb, but the current form *does not work*)
1 parent 80f1543 commit b37027d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

compiler.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Compiler
4747
:preturn, :proc, :stackframe, :deref, :include,
4848
:protected, :array, :splat, :mod, :or_assign, :break, :next,
4949
:__compiler_internal, # See `compile_pragma.rb`
50+
:__inline # See `inline.rb`
5051
]
5152

5253
Keywords = @@keywords

inline.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
class Compiler
3+
# Allow `inline` keyword
4+
def compile_inline(*args)
5+
Value.new(:global, :nil)
6+
end
7+
8+
def find_inline(exps)
9+
@inline_functions = {}
10+
11+
exps.depth_first(:defun) do |e|
12+
if Array(e[3]).first == :__inline
13+
body = [].concat(e[4..-1])
14+
@inline_functions[e[1]] = Function.new(e[1], e[2], body, @e.get_local, false)
15+
end
16+
end
17+
18+
#@inline_classes = {}
19+
#exps.depth_first(:class) do |e|
20+
# #STDERR.puts e
21+
#end
22+
end
23+
24+
# Stupid simple inlining, as starting point.
25+
def rewrite_inline(exps)
26+
find_inline(exps)
27+
28+
#STDERR.puts(@inline_functions.inspect)
29+
exps.depth_first(:call) do |e|
30+
f = @inline_functions[e[1]]
31+
if f
32+
vars = e[2..-1]
33+
i = 0
34+
assigns = []
35+
while (i < f.args.length)
36+
assigns << [:assign, f.args[i].name, [:sexp].concat(vars[i])]
37+
i = i + 1
38+
end
39+
body = [:let, f.args.map{|a| a.name}].concat(assigns).concat(f.body)
40+
#preprocess(body)
41+
e.replace(body)
42+
:skip
43+
else
44+
end
45+
end
46+
exps
47+
end
48+
end

0 commit comments

Comments
 (0)