-
-
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
7f8d2e2
commit 1727d0c
Showing
6 changed files
with
157 additions
and
115 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
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
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,18 @@ | ||
module Opal | ||
class Parser | ||
module NodeHelpers | ||
|
||
def property(name) | ||
reserved?(name) ? "['#{name}']" : ".#{name}" | ||
end | ||
|
||
def reserved?(name) | ||
Opal::Parser::RESERVED.include? name | ||
end | ||
|
||
def variable(name) | ||
reserved?(name) ? "#{name}$" : name | ||
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,78 @@ | ||
require 'opal/nodes/base' | ||
|
||
module Opal | ||
class Parser | ||
class BaseYieldNode < Node | ||
def compile_call(children, level) | ||
scope.uses_block! | ||
|
||
if yields_single_arg?(children) | ||
push expr(children.first) | ||
wrap "$opal.$yield1(#{block_name}, ", ')' | ||
else | ||
push @parser.process_arglist(children, level) | ||
|
||
if uses_splat?(children) | ||
wrap "$opal.$yieldX(#{block_name}, ", ')' | ||
else | ||
wrap "$opal.$yieldX(#{block_name}, [", '])' | ||
end | ||
end | ||
end | ||
|
||
def block_name | ||
scope.block_name || '$yield' | ||
end | ||
|
||
def yields_single_arg?(children) | ||
!uses_splat?(children) and children.size == 1 | ||
end | ||
|
||
def uses_splat?(children) | ||
children.any? { |child| child.type == :splat } | ||
end | ||
end | ||
|
||
class YieldNode < BaseYieldNode | ||
def compile | ||
compile_call(children, @level) | ||
|
||
if stmt? | ||
wrap 'if (', ' === $breaker) return $breaker.$v' | ||
else | ||
with_temp do |tmp| | ||
wrap "(((#{tmp} = ", ") === $breaker) ? $breaker.$v : #{tmp})" | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
# special opal yield assign, for `a = yield(arg1, arg2)` to assign | ||
# to a temp value to make yield expr into stmt. | ||
# | ||
# level will always be stmt as its the reason for this to exist | ||
# | ||
# s(:yasgn, :a, s(:yield, arg1, arg2)) | ||
class YasgnNode < BaseYieldNode | ||
children :var_name, :yield_args | ||
|
||
def compile | ||
compile_call(s(*yield_args[1..-1]), :stmt) | ||
wrap "if ((#{var_name} = ", ") === $breaker) return $breaker.$v" | ||
end | ||
end | ||
|
||
# Created by `#returns()` for when a yield statement should return | ||
# it's value (its last in a block etc). | ||
class ReturnableYieldNode < BaseYieldNode | ||
def compile | ||
compile_call children, @level | ||
|
||
with_temp do |tmp| | ||
wrap "return #{tmp} = ", ", #{tmp} === $breaker ? #{tmp} : #{tmp}" | ||
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