Skip to content

Commit

Permalink
Clean up a load of compiler nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 23, 2013
1 parent e273b2a commit f3b11ec
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 431 deletions.
13 changes: 6 additions & 7 deletions Rakefile
Expand Up @@ -30,21 +30,21 @@ require 'opal-sprockets'
# do this at the top level). We figure out which file we are including, and
# add it to our require list
class ::Opal::Parser
alias_method :mspec_process_call, :process_call
alias_method :mspec_handle_call, :handle_call

def process_call(sexp, level)
if sexp[1] == :language_version and @scope.top?
lang_type = sexp[2][2][1]
def handle_call(sexp)
if sexp[2] == :language_version and @scope.top?
lang_type = sexp[3][2][1]
target = "rubyspec/language/versions/#{lang_type}_1.9"

if File.exist?(target)
@requires << target
end

return f("nil")
return fragment("nil")
end

mspec_process_call sexp, level
mspec_handle_call sexp
end
end

Expand Down Expand Up @@ -138,7 +138,6 @@ task :build_specs do

env = SpecEnvironment.new
env.build
env.build_min
end

desc <<-DESC
Expand Down
2 changes: 2 additions & 0 deletions lib/opal/nodes.rb
Expand Up @@ -6,10 +6,12 @@
require 'opal/nodes/call_special'
require 'opal/nodes/class'
require 'opal/nodes/iter'
require 'opal/nodes/def'
require 'opal/nodes/if'
require 'opal/nodes/logic'
require 'opal/nodes/definitions'
require 'opal/nodes/yield'
require 'opal/nodes/rescue'
require 'opal/nodes/case'
require 'opal/nodes/super'
require 'opal/nodes/top'
38 changes: 23 additions & 15 deletions lib/opal/nodes/base.rb
Expand Up @@ -13,10 +13,12 @@ def self.children(*names)
end
end

def initialize(sexp, level, parser)
attr_reader :compiler

def initialize(sexp, level, compiler)
@sexp = sexp
@level = level
@parser = parser
@compiler = @parser = compiler
end

def type
Expand Down Expand Up @@ -46,9 +48,11 @@ def push(*strs)
end
end

def unshift(str)
str = fragment(str) if str.is_a?(String)
@fragments.unshift str
def unshift(*strs)
strs.reverse.each do |str|
str = fragment(str) if str.is_a?(String)
@fragments.unshift str
end
end

def wrap(pre, post)
Expand All @@ -61,15 +65,15 @@ def fragment(str)
end

def error(msg)
@parser.error msg
@compiler.error msg
end

def scope
@parser.scope
@compiler.scope
end

def s(*args)
@parser.s(*args)
@compiler.s(*args)
end

def expr?
Expand All @@ -84,16 +88,20 @@ def stmt?
@level == :stmt
end

def process(sexp, level = :expr)
@compiler.process sexp, level
end

def expr(sexp)
@parser.process sexp, :expr
@compiler.process sexp, :expr
end

def recv(sexp)
@parser.process sexp, :recv
@compiler.process sexp, :recv
end

def stmt(sexp)
@parser.process sexp, :stmt
@compiler.process sexp, :stmt
end

def expr_or_nil(sexp)
Expand All @@ -113,19 +121,19 @@ def add_temp(temp)
end

def helper(name)
@parser.helper name
@compiler.helper name
end

def with_temp(&block)
@parser.with_temp(&block)
@compiler.with_temp(&block)
end

def in_while?
@parser.in_while?
@compiler.in_while?
end

def while_loop
@parser.instance_variable_get(:@while_loop)
@compiler.instance_variable_get(:@while_loop)
end
end
end
Expand Down
94 changes: 94 additions & 0 deletions lib/opal/nodes/call.rb
@@ -0,0 +1,94 @@
require 'opal/nodes/base'

module Opal
class Parser
class CallNode < Node
children :recvr, :meth, :arglist, :iter

def compile
if handled = compiler.handle_call(@sexp)
push handled
return
end

mid = compiler.mid_to_jsid meth.to_s

compiler.method_calls << meth.to_sym

# trying to access an lvar in irb mode
if using_irb?
with_temp do |tmp|
lvar = meth.intern
lvar = "#{lvar}$" if Parser::RESERVED.include?(lvar)
call = s(:call, s(:self), meth.intern, s(:arglist))
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
end

return
end

case meth
when :block_given?
return push @compiler.js_block_given(@sexp, @level)
when :__method__, :__callee__
if scope.def?
return push(scope.mid.to_s.inspect)
else
return push("nil")
end
end

splat = arglist[1..-1].any? { |a| a.first == :splat }

if Sexp === arglist.last and arglist.last.type == :block_pass
block = expr(arglist.pop)
elsif iter
block = expr(iter)
end

tmpfunc = scope.new_temp if block
tmprecv = scope.new_temp if splat || tmpfunc

recv_code = recv(recv_sexp)
call_recv = s(:js_tmp, tmprecv || recv_code)

if tmpfunc and !splat
arglist.insert 1, call_recv
end

args = expr(arglist)

if tmprecv
push "(#{tmprecv} = ", recv_code, ")#{mid}"
else
push recv_code, mid
end

if tmpfunc
unshift "(#{tmpfunc} = "
push ", #{tmpfunc}._p = ", block, ", #{tmpfunc})"
end

if splat
push ".apply("
push(tmprecv || recv_code)
push ", ", args, ")"
elsif tmpfunc
push ".call(", args, ")"
else
push "(", args, ")"
end

scope.queue_temp tmpfunc if tmpfunc
end

def recv_sexp
recvr || s(:self)
end

def using_irb?
@compiler.irb_vars? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
end
end
end
end
4 changes: 2 additions & 2 deletions lib/opal/nodes/call_special.rb
Expand Up @@ -9,7 +9,7 @@ class AttrAssignNode < Node

def compile
sexp = s(:call, recvr, mid, arglist)
push @parser.process(sexp, @level)
push process(sexp, @level)
end
end

Expand All @@ -20,7 +20,7 @@ class Match3Node < Node

def compile
sexp = s(:call, lhs, :=~, s(:arglist, rhs))
push @parser.process(sexp, @level)
push process(sexp, @level)
end
end

Expand Down
12 changes: 6 additions & 6 deletions lib/opal/nodes/case.rb
Expand Up @@ -8,7 +8,7 @@ class CaseNode < Node
def compile
handled_else = false

@parser.in_case do
compiler.in_case do
if condition
case_stmt[:cond] = true
add_local '$case'
Expand All @@ -18,12 +18,12 @@ def compile

case_parts.each_with_index do |wen, idx|
if wen and wen.type == :when
@parser.returns(wen) if needs_closure?
compiler.returns(wen) if needs_closure?
push "else " unless idx == 0
push stmt(wen)
elsif wen # s(:else)
handled_else = true
wen = @parser.returns(wen) if needs_closure?
wen = compiler.returns(wen) if needs_closure?
push "else {", stmt(wen), "}"
end
end
Expand All @@ -46,7 +46,7 @@ def case_parts
end

def case_stmt
@parser.instance_variable_get(:@case_stmt)
compiler.case_stmt
end
end

Expand All @@ -73,15 +73,15 @@ def compile
end
end

push ") {", @parser.process(body_code, @level), "}"
push ") {", process(body_code, @level), "}"
end

def when_checks
whens.children
end

def case_stmt
@parser.instance_variable_get(:@case_stmt)
compiler.case_stmt
end

def body_code
Expand Down
4 changes: 2 additions & 2 deletions lib/opal/nodes/class.rb
Expand Up @@ -4,7 +4,7 @@ module Opal
class Parser
class BaseScopeNode < Node
def in_scope(type, &block)
indent { @parser.in_scope(type, &block) }
indent { compiler.in_scope(type, &block) }
end
end

Expand Down Expand Up @@ -97,7 +97,7 @@ def super_code

def body_code
body[1] = s(:nil) unless body[1]
stmt(@parser.returns(body))
stmt(compiler.returns(body))
end
end
end
Expand Down
14 changes: 2 additions & 12 deletions lib/opal/nodes/constants.rb
Expand Up @@ -5,13 +5,8 @@ class Parser
class ConstNode < Node
children :name

def initialize(*)
super
@const_missing = true
end

def compile
if @const_missing
if compiler.const_missing?
with_temp do |tmp|
push "((#{tmp} = $scope.#{name}) == null ? $opal.cm('#{name}') : #{tmp})"
end
Expand Down Expand Up @@ -45,13 +40,8 @@ def compile
class ConstGetNode < Node
children :base, :name

def initialize(*)
super
@const_missing = true
end

def compile
if @const_missing
if compiler.const_missing?
with_temp do |tmp|
push "((#{tmp} = ("
push expr(base)
Expand Down

0 comments on commit f3b11ec

Please sign in to comment.