Skip to content

Commit

Permalink
Clean up various helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 23, 2013
1 parent f3b11ec commit 0ee6da4
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 176 deletions.
14 changes: 12 additions & 2 deletions lib/opal/nodes/base.rb
Expand Up @@ -3,7 +3,17 @@
module Opal
class Parser
class Node
include NodeHelpers
include Helpers

def self.handlers
@handlers ||= {}
end

def self.handle(*types)
types.each do |type|
Node.handlers[type] = self
end
end

def self.children(*names)
names.each_with_index do |name, idx|
Expand All @@ -18,7 +28,7 @@ def self.children(*names)
def initialize(sexp, level, compiler)
@sexp = sexp
@level = level
@compiler = @parser = compiler
@compiler = compiler
end

def type
Expand Down
7 changes: 4 additions & 3 deletions lib/opal/nodes/call.rb
Expand Up @@ -3,6 +3,8 @@
module Opal
class Parser
class CallNode < Node
handle :call

children :recvr, :meth, :arglist, :iter

def compile
Expand All @@ -11,15 +13,14 @@ def compile
return
end

mid = compiler.mid_to_jsid meth.to_s
mid = 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)
lvar = variable(meth)
call = s(:call, s(:self), meth.intern, s(:arglist))
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
end
Expand Down
12 changes: 12 additions & 0 deletions lib/opal/nodes/call_special.rb
Expand Up @@ -5,6 +5,8 @@ class Parser
# recv.mid = rhs
# s(:recv, :mid=, s(:arglist, rhs))
class AttrAssignNode < Node
handle :attrasgn

children :recvr, :mid, :arglist

def compile
Expand All @@ -16,6 +18,8 @@ def compile
# lhs =~ rhs
# s(:match3, lhs, rhs)
class Match3Node < Node
handle :match3

children :lhs, :rhs

def compile
Expand All @@ -27,6 +31,8 @@ def compile
# a ||= rhs
# s(:op_asgn_or, s(:lvar, :a), s(:lasgn, :a, rhs))
class OpAsgnOrNode < Node
handle :op_asgn_or

children :recvr, :rhs

def compile
Expand All @@ -38,6 +44,8 @@ def compile
# a &&= rhs
# s(:op_asgn_and, s(:lvar, :a), s(:lasgn, a:, rhs))
class OpAsgnAndNode < Node
handle :op_asgn_and

children :recvr, :rhs

def compile
Expand All @@ -49,6 +57,8 @@ def compile
# lhs[args] ||= rhs
# s(:op_asgn1, lhs, args, :||, rhs)
class OpAsgn1Node < Node
handle :op_asgn1

children :lhs, :args, :op, :rhs

def first_arg
Expand All @@ -72,6 +82,8 @@ def compile
# lhs.b += rhs
# s(:op_asgn2, lhs, :b=, :+, rhs)
class OpAsgn2Node < Node
handle :op_asgn2

children :lhs, :mid, :op, :rhs

def meth
Expand Down
4 changes: 4 additions & 0 deletions lib/opal/nodes/case.rb
Expand Up @@ -3,6 +3,8 @@
module Opal
class Parser
class CaseNode < Node
handle :case

children :condition

def compile
Expand Down Expand Up @@ -51,6 +53,8 @@ def case_stmt
end

class WhenNode < Node
handle :when

children :whens, :body

def compile
Expand Down
6 changes: 6 additions & 0 deletions lib/opal/nodes/class.rb
Expand Up @@ -9,6 +9,8 @@ def in_scope(type, &block)
end

class SingletonClassNode < BaseScopeNode
handle :sclass

children :object, :body

def compile
Expand All @@ -27,6 +29,8 @@ def compile
end

class ModuleNode < BaseScopeNode
handle :module

children :cid, :body

def compile
Expand Down Expand Up @@ -66,6 +70,8 @@ def name_and_base
end

class ClassNode < ModuleNode
handle :class

children :cid, :sup, :body

def compile
Expand Down
12 changes: 12 additions & 0 deletions lib/opal/nodes/constants.rb
Expand Up @@ -3,6 +3,8 @@
module Opal
class Parser
class ConstNode < Node
handle :const

children :name

def compile
Expand All @@ -17,6 +19,8 @@ def compile
end

class ConstDeclarationNode < Node
handle :cdecl

children :name, :base

def compile
Expand All @@ -26,6 +30,8 @@ def compile
end

class ConstAssignNode < Node
handle :casgn

children :base, :name, :value

def compile
Expand All @@ -38,6 +44,8 @@ def compile
end

class ConstGetNode < Node
handle :colon2

children :base, :name

def compile
Expand All @@ -56,6 +64,8 @@ def compile
end

class TopConstNode < Node
handle :colon3

children :name

def compile
Expand All @@ -67,6 +77,8 @@ def compile
end

class TopConstAssignNode < Node
handle :casgn3

children :name, :value

def compile
Expand Down
8 changes: 6 additions & 2 deletions lib/opal/nodes/def.rb
Expand Up @@ -4,10 +4,12 @@ module Opal
class Parser
# FIXME: needs rewrite
class DefNode < BaseScopeNode
handle :def

children :recvr, :mid, :args, :stmts

def compile
jsid = compiler.mid_to_jsid mid.to_s
jsid = mid_to_jsid mid.to_s
params = nil
scope_name = nil

Expand Down Expand Up @@ -66,7 +68,7 @@ def compile

opt[1..-1].each do |o|
next if o[2][2] == :undefined
line "if (#{compiler.lvar_to_js o[1]} == null) {"
line "if (#{variable(o[1])} == null) {"
line ' ', expr(o)
line "}"
end if opt
Expand Down Expand Up @@ -138,6 +140,8 @@ def arity_check(args, opt, splat, block_name, mid)

# FIXME: needs rewrite
class ArglistNode < Node
handle :arglist

def compile
code, work = [], []

Expand Down
26 changes: 23 additions & 3 deletions lib/opal/nodes/definitions.rb
Expand Up @@ -4,6 +4,8 @@ module Opal
class Parser

class SvalueNode < Node
handle :svalue

children :value

def compile
Expand All @@ -14,6 +16,8 @@ def compile
# :scope nodes are actually inside scopes (e.g. :module, :class).
# These are not actually the scopes themselves.
class ScopeNode < Node
handle :scope

children :body

def compile
Expand All @@ -24,23 +28,27 @@ def compile
end

class UndefNode < Node
handle :undef

children :mid

# FIXME: we should be setting method to a stub method here
def compile
push "delete #{scope.proto}#{compiler.mid_to_jsid mid[1].to_s}"
push "delete #{scope.proto}#{mid_to_jsid mid[1].to_s}"
end
end

class AliasNode < Node
handle :alias

children :new_name, :old_name

def new_mid
compiler.mid_to_jsid new_name[1].to_s
mid_to_jsid new_name[1].to_s
end

def old_mid
compiler.mid_to_jsid old_name[1].to_s
mid_to_jsid old_name[1].to_s
end

def compile
Expand All @@ -54,6 +62,8 @@ def compile
end

class BeginNode < Node
handle :begin

children :body

def compile
Expand All @@ -67,6 +77,8 @@ def compile
end

class ParenNode < Node
handle :paren

children :body

def compile
Expand All @@ -85,6 +97,8 @@ def compile
end

class RescueModNode < Node
handle :rescue_mod

children :lhs, :rhs

def body
Expand All @@ -103,6 +117,8 @@ def compile
end

class BlockNode < Node
handle :block

def compile
return push "nil" if children.empty?

Expand Down Expand Up @@ -184,6 +200,8 @@ def find_inline_yield(stmt)
end

class WhileNode < Node
handle :while

children :test, :body

def compile
Expand Down Expand Up @@ -230,6 +248,8 @@ def wrap_in_closure?
end

class UntilNode < WhileNode
handle :until

def while_open
"while (!("
end
Expand Down
36 changes: 33 additions & 3 deletions lib/opal/nodes/helpers.rb
@@ -1,17 +1,47 @@
module Opal
class Parser
module NodeHelpers
module Helpers

# Reserved javascript keywords - we cannot create variables with the
# same name
RESERVED = %w[
break case catch continue debugger default delete do else finally for
function if in instanceof new return switch this throw try typeof var let
void while with class enum export extends import super true false native
const static
]

def property(name)
reserved?(name) ? "['#{name}']" : ".#{name}"
end

def reserved?(name)
Opal::Parser::RESERVED.include? name
RESERVED.include? name
end

def variable(name)
reserved?(name) ? "#{name}$" : name
reserved?(name.to_s) ? "#{name}$" : name
end

# Converts a ruby lvar/arg name to a js identifier. Not all ruby names
# are valid in javascript. A $ suffix is added to non-valid names.
# varibales
def lvar_to_js(var)
var = "#{var}$" if Parser::Helpers::RESERVED.include? var.to_s
var.to_sym
end

# Converts a ruby method name into its javascript equivalent for
# a method/function call. All ruby method names get prefixed with
# a '$', and if the name is a valid javascript identifier, it will
# have a '.' prefix (for dot-calling), otherwise it will be
# wrapped in brackets to use reference notation calling.
def mid_to_jsid(mid)
if /\=|\+|\-|\*|\/|\!|\?|\<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s
"['$#{mid}']"
else
'.$' + mid
end
end

def indent(&block)
Expand Down

0 comments on commit 0ee6da4

Please sign in to comment.