twostroke 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/lib/twostroke/ast/array.rb +8 -0
  2. data/lib/twostroke/ast/assignment.rb +7 -0
  3. data/lib/twostroke/ast/binary_operators.rb +7 -0
  4. data/lib/twostroke/ast/body.rb +6 -0
  5. data/lib/twostroke/ast/break.rb +4 -0
  6. data/lib/twostroke/ast/call.rb +7 -0
  7. data/lib/twostroke/ast/case.rb +9 -1
  8. data/lib/twostroke/ast/continue.rb +11 -0
  9. data/lib/twostroke/ast/declaration.rb +4 -0
  10. data/lib/twostroke/ast/delete.rb +6 -0
  11. data/lib/twostroke/ast/do_while.rb +7 -0
  12. data/lib/twostroke/ast/false.rb +11 -0
  13. data/lib/twostroke/ast/for_in.rb +8 -0
  14. data/lib/twostroke/ast/for_loop.rb +10 -1
  15. data/lib/twostroke/ast/function.rb +6 -0
  16. data/lib/twostroke/ast/if.rb +8 -0
  17. data/lib/twostroke/ast/index.rb +7 -0
  18. data/lib/twostroke/ast/member_access.rb +6 -0
  19. data/lib/twostroke/ast/multi_expression.rb +7 -0
  20. data/lib/twostroke/ast/new.rb +7 -0
  21. data/lib/twostroke/ast/null.rb +4 -0
  22. data/lib/twostroke/ast/number.rb +4 -0
  23. data/lib/twostroke/ast/object_literal.rb +6 -0
  24. data/lib/twostroke/ast/regexp.rb +4 -0
  25. data/lib/twostroke/ast/return.rb +6 -0
  26. data/lib/twostroke/ast/string.rb +4 -0
  27. data/lib/twostroke/ast/switch.rb +7 -0
  28. data/lib/twostroke/ast/ternary.rb +8 -0
  29. data/lib/twostroke/ast/this.rb +4 -0
  30. data/lib/twostroke/ast/throw.rb +6 -0
  31. data/lib/twostroke/ast/true.rb +11 -0
  32. data/lib/twostroke/ast/try.rb +8 -0
  33. data/lib/twostroke/ast/unary_operators.rb +7 -1
  34. data/lib/twostroke/ast/variable.rb +4 -0
  35. data/lib/twostroke/ast/while.rb +8 -1
  36. data/lib/twostroke/ast/with.rb +16 -0
  37. data/lib/twostroke/compiler/javascript.rb +396 -0
  38. data/lib/twostroke/compiler/tsasm.rb +595 -0
  39. data/lib/twostroke/compiler.rb +8 -0
  40. data/lib/twostroke/parser.rb +47 -9
  41. data/lib/twostroke/runtime/lib/array.js +144 -0
  42. data/lib/twostroke/runtime/lib/array.rb +71 -0
  43. data/lib/twostroke/runtime/lib/boolean.rb +23 -0
  44. data/lib/twostroke/runtime/lib/console.rb +13 -0
  45. data/lib/twostroke/runtime/lib/date.rb +65 -0
  46. data/lib/twostroke/runtime/lib/error.rb +36 -0
  47. data/lib/twostroke/runtime/lib/function.js +0 -0
  48. data/lib/twostroke/runtime/lib/function.rb +45 -0
  49. data/lib/twostroke/runtime/lib/math.rb +36 -0
  50. data/lib/twostroke/runtime/lib/number.rb +65 -0
  51. data/lib/twostroke/runtime/lib/object.js +0 -0
  52. data/lib/twostroke/runtime/lib/object.rb +55 -0
  53. data/lib/twostroke/runtime/lib/regexp.rb +28 -0
  54. data/lib/twostroke/runtime/lib/string.rb +86 -0
  55. data/lib/twostroke/runtime/lib/undefined.rb +7 -0
  56. data/lib/twostroke/runtime/lib.rb +42 -0
  57. data/lib/twostroke/runtime/scope.rb +120 -0
  58. data/lib/twostroke/runtime/types/array.rb +79 -0
  59. data/lib/twostroke/runtime/types/boolean.rb +23 -0
  60. data/lib/twostroke/runtime/types/boolean_object.rb +25 -0
  61. data/lib/twostroke/runtime/types/function.rb +83 -0
  62. data/lib/twostroke/runtime/types/null.rb +11 -3
  63. data/lib/twostroke/runtime/types/number.rb +31 -0
  64. data/lib/twostroke/runtime/types/number_object.rb +25 -0
  65. data/lib/twostroke/runtime/types/object.rb +169 -20
  66. data/lib/twostroke/runtime/types/regexp.rb +31 -0
  67. data/lib/twostroke/runtime/types/string.rb +16 -0
  68. data/lib/twostroke/runtime/types/string_object.rb +52 -0
  69. data/lib/twostroke/runtime/types/undefined.rb +11 -3
  70. data/lib/twostroke/runtime/types/value.rb +14 -0
  71. data/lib/twostroke/runtime/types.rb +133 -4
  72. data/lib/twostroke/runtime/vm.rb +25 -0
  73. data/lib/twostroke/runtime/vm_frame.rb +459 -0
  74. data/lib/twostroke/runtime.rb +6 -5
  75. data/lib/twostroke/tokens.rb +20 -8
  76. data/lib/twostroke.rb +3 -1
  77. metadata +41 -7
  78. data/lib/twostroke/runtime/context.rb +0 -33
  79. data/lib/twostroke/runtime/environment.rb +0 -13
  80. data/lib/twostroke/runtime/types/basic_type.rb +0 -5
@@ -10,5 +10,13 @@ module Twostroke::AST
10
10
  def collapse
11
11
  self.class.new items: items.map(&:collapse)
12
12
  end
13
+
14
+ def walk(&bk)
15
+ if yield self
16
+ items.each do |item|
17
+ item.walk &bk
18
+ end
19
+ end
20
+ end
13
21
  end
14
22
  end
@@ -5,5 +5,12 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new left: left.collapse, right: right.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ left.walk &bk
12
+ right.walk &bk
13
+ end
14
+ end
8
15
  end
9
16
  end
@@ -16,6 +16,13 @@ module Twostroke::AST
16
16
  def collapse
17
17
  self.class.new left: left.collapse, right: right.collapse, assign_result_left: assign_result_left
18
18
  end
19
+
20
+ def walk(&bk)
21
+ if yield self
22
+ left.walk &bk
23
+ right.walk &bk
24
+ end
25
+ end
19
26
  end
20
27
  const_set op, klass
21
28
  end
@@ -10,5 +10,11 @@ module Twostroke::AST
10
10
  def collapse
11
11
  self.class.new statements: statements.reject(&:nil?).map(&:collapse)
12
12
  end
13
+
14
+ def walk(&bk)
15
+ if yield self
16
+ statements.each { |s| s.walk &bk }
17
+ end
18
+ end
13
19
  end
14
20
  end
@@ -3,5 +3,9 @@ module Twostroke::AST
3
3
  def collapse
4
4
  self
5
5
  end
6
+
7
+ def walk
8
+ yield self
9
+ end
6
10
  end
7
11
  end
@@ -10,5 +10,12 @@ module Twostroke::AST
10
10
  def collapse
11
11
  self.class.new callee: callee.collapse, arguments: arguments.map(&:collapse)
12
12
  end
13
+
14
+ def walk(&bk)
15
+ if yield self
16
+ callee.walk &bk
17
+ arguments.each { |arg| arg.walk &bk }
18
+ end
19
+ end
13
20
  end
14
21
  end
@@ -3,10 +3,18 @@ module Twostroke::AST
3
3
  attr_accessor :expression, :statements
4
4
  def initialize(*args)
5
5
  @statements = []
6
+ @is_default = false
6
7
  super *args
7
8
  end
8
9
  def collapse
9
- self.class.new expression: expression.collapse, statements: statements.collect(&:collapse)
10
+ self.class.new expression: expression && expression.collapse, statements: statements.collect(&:collapse)
11
+ end
12
+
13
+ def walk(&bk)
14
+ if yield self
15
+ expression.walk &bk
16
+ statements.each { |s| s.walk &bk }
17
+ end
10
18
  end
11
19
  end
12
20
  end
@@ -0,0 +1,11 @@
1
+ module Twostroke::AST
2
+ class Continue < Base
3
+ def collapse
4
+ self
5
+ end
6
+
7
+ def walk
8
+ yield self
9
+ end
10
+ end
11
+ end
@@ -5,5 +5,9 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self
7
7
  end
8
+
9
+ def walk
10
+ yield self
11
+ end
8
12
  end
9
13
  end
@@ -5,5 +5,11 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new expression: expression.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ expression.walk &bk
12
+ end
13
+ end
8
14
  end
9
15
  end
@@ -5,5 +5,12 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new body: body.collapse, condition: condition.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ condition.walk &bk
12
+ body.walk &bk
13
+ end
14
+ end
8
15
  end
9
16
  end
@@ -0,0 +1,11 @@
1
+ module Twostroke::AST
2
+ class False < Base
3
+ def collapse
4
+ self
5
+ end
6
+
7
+ def walk(&bk)
8
+ yield self
9
+ end
10
+ end
11
+ end
@@ -5,5 +5,13 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new lval: lval.collapse, object: object.collapse, body: body.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ lval.walk &bk
12
+ object.walk &bk
13
+ body.walk &bk
14
+ end
15
+ end
8
16
  end
9
17
  end
@@ -6,7 +6,16 @@ module Twostroke::AST
6
6
  self.class.new initializer: initializer && initializer.collapse,
7
7
  condition: condition && condition.collapse,
8
8
  increment: increment && increment.collapse,
9
- body: body.collapse
9
+ body: body && body.collapse
10
+ end
11
+
12
+ def walk(&bk)
13
+ if yield self
14
+ initializer.walk(&bk) if initializer
15
+ condition.walk(&bk) if condition
16
+ increment.walk(&bk) if increment
17
+ body.walk &bk if body
18
+ end
10
19
  end
11
20
  end
12
21
  end
@@ -11,5 +11,11 @@ module Twostroke::AST
11
11
  def collapse
12
12
  self.class.new name: name, arguments: arguments, statements: statements.reject(&:nil?).map(&:collapse)
13
13
  end
14
+
15
+ def walk(&bk)
16
+ if yield self
17
+ statements.each { |s| s.walk &bk }
18
+ end
19
+ end
14
20
  end
15
21
  end
@@ -5,5 +5,13 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new condition: condition.collapse, then: @then.collapse, else: @else && @else.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ condition.walk &bk
12
+ @then.walk &bk
13
+ @else.walk &bk if @else
14
+ end
15
+ end
8
16
  end
9
17
  end
@@ -5,5 +5,12 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new object: object.collapse, index: index.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ object.walk &bk
12
+ index.walk &bk
13
+ end
14
+ end
8
15
  end
9
16
  end
@@ -5,5 +5,11 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new object: object.collapse, member: member
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ object.walk &bk
12
+ end
13
+ end
8
14
  end
9
15
  end
@@ -5,5 +5,12 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new left: left.collapse, right: right.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ left.walk &bk
12
+ right.walk &bk
13
+ end
14
+ end
8
15
  end
9
16
  end
@@ -10,5 +10,12 @@ module Twostroke::AST
10
10
  def collapse
11
11
  self.class.new callee: callee.collapse, arguments: arguments.map(&:collapse)
12
12
  end
13
+
14
+ def walk(&bk)
15
+ if yield self
16
+ callee.walk &bk
17
+ arguments.each { |a| a.walk &bk }
18
+ end
19
+ end
13
20
  end
14
21
  end
@@ -3,5 +3,9 @@ module Twostroke::AST
3
3
  def collapse
4
4
  self
5
5
  end
6
+
7
+ def walk
8
+ yield self
9
+ end
6
10
  end
7
11
  end
@@ -5,5 +5,9 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self
7
7
  end
8
+
9
+ def walk
10
+ yield self
11
+ end
8
12
  end
9
13
  end
@@ -11,5 +11,11 @@ module Twostroke::AST
11
11
  collapsed = items.map { |k,v| [k, v.collapse] }
12
12
  self.class.new items: collapsed
13
13
  end
14
+
15
+ def walk(&bk)
16
+ if yield self
17
+ items.each { |i| i[1].walk &bk }
18
+ end
19
+ end
14
20
  end
15
21
  end
@@ -5,5 +5,9 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self
7
7
  end
8
+
9
+ def walk(&bk)
10
+ yield self
11
+ end
8
12
  end
9
13
  end
@@ -5,5 +5,11 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new expression: expression && expression.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ expression.walk &bk if expression
12
+ end
13
+ end
8
14
  end
9
15
  end
@@ -5,5 +5,9 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self
7
7
  end
8
+
9
+ def walk
10
+ yield self
11
+ end
8
12
  end
9
13
  end
@@ -8,5 +8,12 @@ module Twostroke::AST
8
8
  def collapse
9
9
  self.class.new expression: expression.collapse, cases: cases.collect(&:collapse)
10
10
  end
11
+
12
+ def walk(&bk)
13
+ if yield self
14
+ expression.walk &bk
15
+ cases.each { |c| c.walk &bk }
16
+ end
17
+ end
11
18
  end
12
19
  end
@@ -5,5 +5,13 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new condition: condition.collapse, if_true: if_true.collapse, if_false: if_false.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ condition.walk &bk
12
+ if_true.walk &bk
13
+ if_false.walk &bk
14
+ end
15
+ end
8
16
  end
9
17
  end
@@ -1,5 +1,9 @@
1
1
  module Twostroke::AST
2
2
  class This < Base
3
3
  def collapse; self; end
4
+
5
+ def walk
6
+ yield self
7
+ end
4
8
  end
5
9
  end
@@ -5,5 +5,11 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self.class.new expression: expression.collapse
7
7
  end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ expression.walk &bk
12
+ end
13
+ end
8
14
  end
9
15
  end
@@ -0,0 +1,11 @@
1
+ module Twostroke::AST
2
+ class True < Base
3
+ def collapse
4
+ self
5
+ end
6
+
7
+ def walk(&bk)
8
+ yield self
9
+ end
10
+ end
11
+ end
@@ -7,5 +7,13 @@ module Twostroke::AST
7
7
  catch_statements: (catch_statements && catch_statements.map(&:collapse)),
8
8
  finally_statements: (finally_statements && finally_statements.map(&:collapse))
9
9
  end
10
+
11
+ def walk(&bk)
12
+ if yield self
13
+ try_statements.each { |s| s.walk &bk }
14
+ catch_statements.each { |s| s.walk &bk } if catch_statements
15
+ finally_statements.each { |s| s.walk &bk } if finally_statements
16
+ end
17
+ end
10
18
  end
11
19
  end
@@ -1,12 +1,18 @@
1
1
  module Twostroke::AST
2
2
  [ :PostIncrement, :PreIncrement, :PostDecrement, :PreDecrement,
3
- :BinaryNot, :UnaryPlus, :Negation, :TypeOf, :Not, :Void ].each do |op|
3
+ :BinaryNot, :UnaryPlus, :Negation, :TypeOf, :Not, :Void, :BracketedExpression ].each do |op|
4
4
  klass = Class.new Base do
5
5
  attr_accessor :value
6
6
 
7
7
  def collapse
8
8
  self.class.new value: value.collapse
9
9
  end
10
+
11
+ def walk(&bk)
12
+ if yield self
13
+ value.walk &bk
14
+ end
15
+ end
10
16
  end
11
17
  const_set op, klass
12
18
  end
@@ -5,5 +5,9 @@ module Twostroke::AST
5
5
  def collapse
6
6
  self
7
7
  end
8
+
9
+ def walk
10
+ yield self
11
+ end
8
12
  end
9
13
  end
@@ -3,7 +3,14 @@ module Twostroke::AST
3
3
  attr_accessor :condition, :body
4
4
 
5
5
  def collapse
6
- self.class.new condition: condition.collapse, body: body.collapse
6
+ self.class.new condition: condition.collapse, body: body && body.collapse
7
+ end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ condition.walk &bk
12
+ body.walk &bk if body
13
+ end
7
14
  end
8
15
  end
9
16
  end
@@ -0,0 +1,16 @@
1
+ module Twostroke::AST
2
+ class With < Base
3
+ attr_accessor :object, :statement
4
+
5
+ def collapse
6
+ self.class.new object: object.collapse, statement: statement && statement.collapse
7
+ end
8
+
9
+ def walk(&bk)
10
+ if yield self
11
+ object.walk &bk
12
+ statement.walk &bk
13
+ end
14
+ end
15
+ end
16
+ end