Permalink
Browse files

Implement interop with Ruby constants

  • Loading branch information...
1 parent fab156c commit 6edbc77bb34b08f5512d6c107af44de3d91b79af @txus committed Mar 5, 2014
Showing with 21 additions and 4 deletions.
  1. +9 −0 lib/lambra/bytecode_compiler.rb
  2. +1 −1 lib/lambra/parser/lambra.kpeg
  3. +3 −3 lib/lambra/parser/parser.rb
  4. +8 −0 spec/compiler_spec.rb
@@ -190,6 +190,8 @@ def visit_PrimitiveForm(car, cdr)
def visit_Symbol(o)
if o.name.to_sym == :self
return visit_Self(o)
+ elsif o.name =~ /^[A-Z]/
+ return visit_Constant(o)
end
set_line(o)
@@ -207,6 +209,13 @@ def visit_Self(o)
g.send :pid, 0
end
+ def visit_Constant(o)
+ g.push_cpath_top
+ o.name.to_s.split('.').each do |part|
+ g.find_const(part.to_sym)
+ end
+ end
+
def visit_Number(o)
set_line(o)
g.push_literal o.value
@@ -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-Z\.]?[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-Z\.]?[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-Z\.]?[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-Z\\.]?[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
@@ -93,5 +93,13 @@
it 'works with methods' do
"(.succ 3)".should eval_to 4
end
+
+ it 'works with simple constants' do
+ "Set".should eval_to Set
+ end
+
+ it 'works with qualified constants' do
+ "Lambra.Parser".should eval_to Lambra::Parser
+ end
end
end

0 comments on commit 6edbc77

Please sign in to comment.