-
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1727d0c
commit 7a16fb2
Showing
7 changed files
with
346 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
require 'opal/nodes/base' | ||
|
||
module Opal | ||
class Parser | ||
# recv.mid = rhs | ||
# s(:recv, :mid=, s(:arglist, rhs)) | ||
class AttrAssignNode < Node | ||
children :recvr, :mid, :arglist | ||
|
||
def compile | ||
sexp = s(:call, recvr, mid, arglist) | ||
push @parser.process(sexp, @level) | ||
end | ||
end | ||
|
||
# lhs =~ rhs | ||
# s(:match3, lhs, rhs) | ||
class Match3Node < Node | ||
children :lhs, :rhs | ||
|
||
def compile | ||
sexp = s(:call, lhs, :=~, s(:arglist, rhs)) | ||
push @parser.process(sexp, @level) | ||
end | ||
end | ||
|
||
# a ||= rhs | ||
# s(:op_asgn_or, s(:lvar, :a), s(:lasgn, :a, rhs)) | ||
class OpAsgnOrNode < Node | ||
children :recvr, :rhs | ||
|
||
def compile | ||
sexp = s(:or, recvr, rhs) | ||
push expr(sexp) | ||
end | ||
end | ||
|
||
# a &&= rhs | ||
# s(:op_asgn_and, s(:lvar, :a), s(:lasgn, a:, rhs)) | ||
class OpAsgnAndNode < Node | ||
children :recvr, :rhs | ||
|
||
def compile | ||
sexp = s(:and, recvr, rhs) | ||
push expr(sexp) | ||
end | ||
end | ||
|
||
# lhs[args] ||= rhs | ||
# s(:op_asgn1, lhs, args, :||, rhs) | ||
class OpAsgn1Node < Node | ||
children :lhs, :args, :op, :rhs | ||
|
||
def first_arg | ||
args[1] | ||
end | ||
|
||
def compile | ||
with_temp do |a| # args | ||
with_temp do |r| # recv | ||
aref = s(:call, s(:js_tmp, r), :[], s(:arglist, s(:js_tmp, a))) | ||
aset = s(:call, s(:js_tmp, r), :[]=, s(:arglist, s(:js_tmp, a), rhs)) | ||
orop = s(:or, aref, aset) | ||
|
||
push "(#{a} = ", expr(first_arg), ", #{r} = ", expr(lhs) | ||
push ", ", expr(orop), ")" | ||
end | ||
end | ||
end | ||
end | ||
|
||
# lhs.b += rhs | ||
# s(:op_asgn2, lhs, :b=, :+, rhs) | ||
class OpAsgn2Node < Node | ||
children :lhs, :mid, :op, :rhs | ||
|
||
def meth | ||
mid.to_s[0..-2] | ||
end | ||
|
||
def compile | ||
case op.to_s | ||
when '||' then compile_or | ||
when '&&' then compile_and | ||
else compile_operator | ||
end | ||
end | ||
|
||
def compile_or | ||
with_temp do |tmp| | ||
getr = s(:call, s(:js_tmp, tmp), meth, s(:arglist)) | ||
asgn = s(:call, s(:js_tmp, tmp), mid, s(:arglist, rhs)) | ||
orop = s(:or, getr, asgn) | ||
|
||
push "(#{tmp} = ", expr(lhs), ", ", expr(orop), ")" | ||
end | ||
end | ||
|
||
def compile_and | ||
with_temp do |tmp| | ||
getr = s(:call, s(:js_tmp, tmp), meth, s(:arglist)) | ||
asgn = s(:call, s(:js_tmp, tmp), mid, s(:arglist, rhs)) | ||
andop = s(:and, getr, asgn) | ||
|
||
push "(#{tmp} = ", expr(lhs), ", ", expr(andop), ")" | ||
end | ||
end | ||
|
||
def compile_operator | ||
with_temp do |tmp| | ||
getr = s(:call, s(:js_tmp, tmp), meth, s(:arglist)) | ||
oper = s(:call, getr, op, s(:arglist, rhs)) | ||
asgn = s(:call, s(:js_tmp, tmp), mid, s(:arglist, oper)) | ||
|
||
push "(#{tmp} = ", expr(lhs), ", ", expr(asgn), ")" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'opal/nodes/base' | ||
|
||
module Opal | ||
class Parser | ||
class BaseScopeNode < Node | ||
def in_scope(type, &block) | ||
@parser.in_scope(type, &block) | ||
end | ||
end | ||
|
||
class SingletonClassNode < BaseScopeNode | ||
children :object, :body | ||
|
||
def compile | ||
in_scope(:sclass) do | ||
add_temp '$scope = self._scope' | ||
add_temp 'def = self._proto' | ||
|
||
push scope.to_vars | ||
push stmt(body) | ||
end | ||
|
||
push "})(" | ||
push recv(object) | ||
wrap "(function(self) {", ".$singleton_class())" | ||
end | ||
end | ||
|
||
class ModuleNode < BaseScopeNode | ||
children :cid, :body | ||
|
||
def compile | ||
helper :module | ||
|
||
push pre_code | ||
|
||
@parser.indent do | ||
in_scope(:module) do | ||
scope.name = module_name | ||
add_temp "#{scope.proto} = self._proto" | ||
add_temp '$scope = self._scope' | ||
|
||
body_code = stmt(body) | ||
|
||
push "#{@indent}", scope.to_vars, "\n\n#{@indent}" | ||
push body_code | ||
push "\n#{@indent}", scope.to_donate_methods | ||
end | ||
end | ||
|
||
push "\n#{@indent}})(", module_base, ")" | ||
end | ||
|
||
def pre_code | ||
name = module_name | ||
"(function($base) {\n#{@indent} var self = $module($base, '#{name}');\n" | ||
end | ||
|
||
def module_base | ||
if Symbol === cid or String === cid | ||
'self' | ||
elsif cid.type == :colon2 | ||
expr(cid[1]) | ||
elsif cid.type == :colon3 | ||
'$opal.Object' | ||
else | ||
raise "Bad receiver in module" | ||
end | ||
end | ||
|
||
def module_name | ||
if Symbol === cid or String === cid | ||
cid.to_s | ||
elsif cid.type == :colon2 | ||
cid[2].to_s | ||
elsif cid.type == :colon3 | ||
cid[1].to_s | ||
else | ||
raise "Bad receiver in module" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.