Permalink
Browse files

Implement interop with Ruby methods

  • Loading branch information...
1 parent 291e257 commit fab156c0d617867cb04790e6ae21149d327a62c0 @txus committed Mar 5, 2014
Showing with 26 additions and 4 deletions.
  1. +16 −0 lib/lambra/bytecode_compiler.rb
  2. +1 −1 lib/lambra/parser/lambra.kpeg
  3. +3 −3 lib/lambra/parser/parser.rb
  4. +6 −0 spec/compiler_spec.rb
@@ -66,6 +66,7 @@ def visit_List(o)
return visit_SpecialForm(car.name, cdr) if car.respond_to?(:name) && special_form?(car.name)
return visit_PrimitiveForm(car.name, cdr) if car.respond_to?(:name) && primitive_form?(car.name)
+ return visit_InteropForm(car.name, cdr) if car.respond_to?(:name) && interop_form?(car.name)
args = cdr.count
@@ -78,6 +79,17 @@ def visit_List(o)
g.send :call, args
end
+ def visit_InteropForm(car, cdr)
+ method = car[1..-1].to_sym
+ receiver = cdr.shift
+ receiver.accept(self)
+ cdr.each do |argument|
+ argument.accept(self)
+ end
+
+ g.send method, cdr.count, false
+ end
+
def visit_SpecialForm(car, cdr)
case car.to_s
when 'def'
@@ -376,6 +388,10 @@ def primitive_form?(name)
PRIMITIVE_FORMS.include?(name.to_s)
end
+ def interop_form?(name)
+ !!(name =~ /^./)
+ end
+
def new_block_generator(g, arguments)
blk = g.class.new
blk.name = g.state.name || :__block__
@@ -39,7 +39,7 @@ true = "true" ~true_value(current_line, current_column)
false = "false" ~false_value(current_line, current_column)
nil = "nil" ~nil_value(current_line, current_column)
-word = < /[a-zA-Z0-9_\-\*\+\-\/]+/ > { text }
+word = < /\.?[a-zA-Z0-9_\-\*\+\-\/]+/ > { text }
symbol = word:w ~symbol(current_line, current_column, w.to_sym)
keyword = ":" word:w ~keyword(current_line, current_column, w.to_sym)
@@ -862,13 +862,13 @@ def _nil
return _tmp
end
- # word = < /[a-zA-Z0-9_\-\*\+\-\/]+/ > { text }
+ # word = < /\.?[a-zA-Z0-9_\-\*\+\-\/]+/ > { text }
def _word
_save = self.pos
while true # sequence
_text_start = self.pos
- _tmp = scan(/\A(?-mix:[a-zA-Z0-9_\-\*\+\-\/]+)/)
+ _tmp = scan(/\A(?-mix:\.?[a-zA-Z0-9_\-\*\+\-\/]+)/)
if _tmp
text = get_text(_text_start)
end
@@ -1681,7 +1681,7 @@ def _root
Rules[:_true] = rule_info("true", "\"true\" {true_value(current_line, current_column)}")
Rules[:_false] = rule_info("false", "\"false\" {false_value(current_line, current_column)}")
Rules[:_nil] = rule_info("nil", "\"nil\" {nil_value(current_line, current_column)}")
- Rules[:_word] = rule_info("word", "< /[a-zA-Z0-9_\\-\\*\\+\\-\\/]+/ > { text }")
+ Rules[:_word] = rule_info("word", "< /\\.?[a-zA-Z0-9_\\-\\*\\+\\-\\/]+/ > { text }")
Rules[:_symbol] = rule_info("symbol", "word:w {symbol(current_line, current_column, w.to_sym)}")
Rules[:_keyword] = rule_info("keyword", "\":\" word:w {keyword(current_line, current_column, w.to_sym)}")
Rules[:_string] = rule_info("string", "\"\\\"\" < /[^\\\\\"]*/ > \"\\\"\" {string_value(current_line, current_column, text)}")
View
@@ -88,4 +88,10 @@
}.should eval_to "hello world"
end
end
+
+ describe 'interop' do
+ it 'works with methods' do
+ "(.succ 3)".should eval_to 4
+ end
+ end
end

0 comments on commit fab156c

Please sign in to comment.