Skip to content

Commit

Permalink
Sexps should use #type instead of dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 15, 2013
1 parent d2a17ad commit 54321ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
16 changes: 8 additions & 8 deletions lib/opal/grammar.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions lib/opal/grammar.y
Expand Up @@ -59,7 +59,7 @@ compstmt:
stmts opt_terms
{
comp = new_compstmt val[0]
if comp and comp[0] == :begin and comp.size == 2
if comp and comp.type == :begin and comp.size == 2
result = comp[1]
result.line = comp.line
else
Expand Down Expand Up @@ -325,7 +325,7 @@ mlhs_node:
| primary_value '[@' aref_args ']'
{
args = val[2]
args[0] = :arglist if args[0] == :array
args.type = :arglist if args.type == :array
result = s(:attrasgn, val[0], :[]=, args)
}
| primary_value '.' IDENTIFIER
Expand All @@ -346,7 +346,7 @@ lhs:
| primary_value '[@' aref_args ']'
{
args = val[2]
args[0] = :arglist if args[0] == :array
args.type = :arglist if args.type == :array
result = s(:attrasgn, val[0], :[]=, args)
}
| primary_value '.' IDENTIFIER
Expand Down Expand Up @@ -452,7 +452,7 @@ arg:
| primary_value '[@' aref_args ']' OP_ASGN arg
{
args = val[2]
args[0] = :arglist if args[0] == :array
args.type = :arglist if args.type == :array
result = s(:op_asgn1, val[0], val[2], val[4].intern, val[5])
result.line = val[0].line
}
Expand Down Expand Up @@ -504,15 +504,15 @@ arg:
| '+@' arg
{
result = new_call val[1], :"+@", s(:arglist)
result = val[1] if [:int, :float].include? val[1][0]
result = val[1] if [:int, :float].include? val[1].type
}
| '-@' arg
{
result = new_call val[1], :"-@", s(:arglist)
if val[1][0] == :int
if val[1].type == :int
val[1][1] = -val[1][1]
result = val[1]
elsif val[1][0] == :float
elsif val[1].type == :float
val[1][1] = -val[1][1].to_f
result = val[1]
end
Expand Down Expand Up @@ -1311,7 +1311,7 @@ word_list:
| word_list word SPACE
{
part = val[1]
part = s(:dstr, "", val[1]) if part[0] == :evstr
part = s(:dstr, "", val[1]) if part.type == :evstr
result = val[0] << part
}

Expand Down
70 changes: 35 additions & 35 deletions lib/opal/grammar_helpers.rb
Expand Up @@ -32,7 +32,7 @@ def new_body(compstmt, res, els, ens)
end

def new_defn(line, name, args, body)
body = s(:block, body) if body[0] != :block
body = s(:block, body) if body.type != :block
scope = s(:scope, body)
body << s(:nil) if body.size == 1
scope.line = body.line
Expand Down Expand Up @@ -158,7 +158,7 @@ def new_block_args(norm, opt, rest, block)

args = res.size == 2 && norm ? res[1] : s(:masgn, res)

if args[0] == :array
if args.type == :array
s(:masgn, args)
else
args
Expand All @@ -168,7 +168,7 @@ def new_block_args(norm, opt, rest, block)
def new_call(recv, meth, args = nil)
call = s(:call, recv, meth)
args = s(:arglist) unless args
args[0] = :arglist if args[0] == :array
args.type = :arglist if args.type == :array
call << args

if recv
Expand Down Expand Up @@ -211,7 +211,7 @@ def new_op_asgn(op, lhs, rhs)
end

def new_assign(lhs, rhs)
case lhs[0]
case lhs.type
when :iasgn, :cdecl, :lasgn, :gasgn, :cvdecl, :nth_ref
lhs << rhs
lhs
Expand All @@ -220,39 +220,39 @@ def new_assign(lhs, rhs)
lhs
when :colon2
lhs << rhs
lhs[0] = :casgn
lhs.type = :casgn
lhs
when :colon3
lhs << rhs
lhs[0] = :casgn3
lhs.type = :casgn3
lhs
else
raise "Bad lhs for new_assign: #{lhs[0]}"
raise "Bad lhs for new_assign: #{lhs.type}"
end
end

def new_assignable(ref)
case ref[0]
case ref.type
when :ivar
ref[0] = :iasgn
ref.type = :iasgn
when :const
ref[0] = :cdecl
ref.type = :cdecl
when :identifier
@scope.add_local ref[1] unless @scope.has_local? ref[1]
ref[0] = :lasgn
ref.type = :lasgn
when :gvar
ref[0] = :gasgn
ref.type = :gasgn
when :cvar
ref[0] = :cvdecl
ref.type = :cvdecl
else
raise "Bad new_assignable type: #{ref[0]}"
raise "Bad new_assignable type: #{ref.type}"
end

ref
end

def new_gettable(ref)
res = case ref[0]
res = case ref.type
when :lasgn
s(:lvar, ref[1])
when :iasgn
Expand All @@ -262,15 +262,15 @@ def new_gettable(ref)
when :cvdecl
s(:cvar, ref[1])
else
raise "Bad new_gettable ref: #{ref[0]}"
raise "Bad new_gettable ref: #{ref.type}"
end

res.line = ref.line
res
end

def new_var_ref(ref)
case ref[0]
case ref.type
when :self, :nil, :true, :false, :line, :file
ref
when :const
Expand All @@ -290,15 +290,15 @@ def new_var_ref(ref)
s(:call, nil, ref[1], s(:arglist))
end
else
raise "Bad var_ref type: #{ref[0]}"
raise "Bad var_ref type: #{ref.type}"
end
end

def new_super(args)
args = (args || s(:arglist))

if args[0] == :array
args[0] = :arglist
if args.type == :array
args.type = :arglist
end

s(:super, args)
Expand All @@ -311,9 +311,9 @@ def new_yield(args)

def new_xstr(str)
return s(:xstr, '') unless str
case str[0]
when :str then str[0] = :xstr
when :dstr then str[0] = :dxstr
case str.type
when :str then str.type = :xstr
when :dstr then str.type = :dxstr
when :evstr then str = s(:dxstr, '', str)
end

Expand All @@ -322,12 +322,12 @@ def new_xstr(str)

def new_dsym(str)
return s(:nil) unless str
case str[0]
case str.type
when :str
str[0] = :sym
str.type = :sym
str[1] = str[1].to_sym
when :dstr
str[0] = :dsym
str.type = :dsym
end

str
Expand All @@ -337,14 +337,14 @@ def new_str(str)
# cover empty strings
return s(:str, "") unless str
# catch s(:str, "", other_str)
if str.size == 3 and str[1] == "" and str[0] == :str
if str.size == 3 and str[1] == "" and str.type == :str
return str[2]
# catch s(:str, "content", more_content)
elsif str[0] == :str && str.size > 3
str[0] = :dstr
elsif str.type == :str && str.size > 3
str.type = :dstr
str
# top level evstr should be a dstr
elsif str[0] == :evstr
elsif str.type == :evstr
s(:dstr, "", str)
else
str
Expand All @@ -353,13 +353,13 @@ def new_str(str)

def new_regexp(reg, ending)
return s(:regexp, //) unless reg
case reg[0]
case reg.type
when :str
s(:regexp, Regexp.new(reg[1], ending))
when :evstr
s(:dregx, "", reg)
when :dstr
reg[0] = :dregx
reg.type = :dregx
reg
end
end
Expand All @@ -368,12 +368,12 @@ def str_append(str, str2)
return str2 unless str
return str unless str2

if str.first == :evstr
if str.type == :evstr
str = s(:dstr, "", str)
elsif str.first == :str
elsif str.type == :str
str = s(:dstr, str[1])
else
#puts str.first
#puts str.type
end
str << str2
str
Expand Down
8 changes: 8 additions & 0 deletions lib/opal/sexp.rb
Expand Up @@ -9,6 +9,14 @@ def initialize(args)
@array = args
end

def type
@array[0]
end

def type=(type)
@array[0] = type
end

def method_missing(sym, *args, &block)
@array.send sym, *args, &block
end
Expand Down

0 comments on commit 54321ed

Please sign in to comment.