Permalink
Browse files

Compile maps

  • Loading branch information...
1 parent d6af242 commit 87c93b611082c11965c9aee5c45e863a7c45adc8 @txus committed May 12, 2012
Showing with 73 additions and 1 deletion.
  1. +31 −0 lib/lambra/bytecode_compiler.rb
  2. +4 −0 lib/lambra/syntax/ast.rb
  3. +0 −1 spec/custom/matchers/compile_as.rb
  4. +38 −0 spec/syntax/ast_spec.rb
@@ -64,6 +64,11 @@ def visit_Nil(o)
g.push_nil
end
+ def visit_Keyword(o)
+ set_line(o)
+ g.push_literal o.name
+ end
+
def visit_Sequence(o)
set_line(o)
@@ -83,6 +88,32 @@ def visit_Vector(o)
g.make_array count
end
+ def visit_Map(o)
+ set_line(o)
+
+ ary = o.to_a
+ count = ary.size
+ i = 0
+
+ g.push_cpath_top
+ g.find_const :Hash
+ g.push count # / 2
+ g.send :new_from_literal, 1
+
+ while i < count
+ k = ary[i].first
+ v = ary[i].last
+
+ g.dup
+ k.accept(self)
+ v.accept(self)
+ g.send :[]=, 2
+ g.pop
+
+ i += 1
+ end
+ end
+
def finalize
# g.local_names = s.variables
# g.local_count = s.variables.size
@@ -70,6 +70,10 @@ def to_sexp
[sexp_name, elements]
end
+
+ def to_a
+ @elements.to_a
+ end
end
class Set < Node
@@ -8,7 +8,6 @@ def matches?(actual)
visitor = Lambra::BytecodeCompiler.new
visitor.compile(ast)
-
@actual = visitor.generator
@expected.literals == @actual.literals &&
@expected.stream == @actual.stream
@@ -168,6 +168,15 @@
[:number, 43.0],
[:string, "hey"]]
}
+
+ # compile do |g|
+ # g.push_literal ???
+ # g.push_literal ???
+ # g.push_literal 43
+ # g.push_literal "hey"
+ # g.make_array 4
+ # g.ret
+ # end
end
relates "[hello \n\t (world 43) \"hey\"]" do
@@ -185,10 +194,39 @@
describe "The Map node" do
relates '{}' do
parse { [:map] }
+
+ compile do |g|
+ g.push_cpath_top
+ g.find_const :Hash
+ g.meta_push_0
+ g.send :new_from_literal, 1
+ g.ret
+ end
end
relates '{:a 1 :b 2}' do
parse { [:map, {[:keyword, :a] => [:number, 1], [:keyword, :b] => [:number, 2]}] }
+
+ compile do |g|
+ g.push_cpath_top
+ g.find_const :Hash
+ g.meta_push_2
+ g.send :new_from_literal, 1
+
+ g.dup_top
+ g.push_literal :a
+ g.push_literal 1
+ g.send :[]=, 2
+ g.pop
+
+ g.dup_top
+ g.push_literal :b
+ g.push_literal 2
+ g.send :[]=, 2
+ g.pop
+
+ g.ret
+ end
end
end

0 comments on commit 87c93b6

Please sign in to comment.