minjs 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ module Minjs
2
+ module ECMA262
3
+ class Base
4
+ def to_js(options = {})
5
+ self.class.to_s + "??"
6
+ end
7
+
8
+ def to_s
9
+ to_js({})
10
+ end
11
+
12
+ def concat(options, *args)
13
+ prev = nil
14
+ j = []
15
+ args.flatten.each do|x|
16
+ sep = ''
17
+ nl = ''
18
+ if x.kind_of? Base
19
+ js = x.to_js(options);
20
+ else
21
+ js = x.to_s
22
+ end
23
+ if prev
24
+ if prev.match(/[\w\$]\z/) and js.match(/^[\w\$]/)
25
+ sep = ' '
26
+ elsif prev.match(/;\z/) and js == "}"
27
+ prev.sub!(/;\z/, "")
28
+ elsif prev.match(/;\z/) and js == ";" and !options[:for_args]
29
+ prev.sub!(/;\z/, "")
30
+ elsif prev.match(/[\-]\z/) and js.match(/^\-/)
31
+ sep = ' '
32
+ elsif prev.match(/[\+]\z/) and js.match(/^\+/)
33
+ sep = ' '
34
+ end
35
+ end
36
+ #for debug
37
+ if false and js.match(/;\z/) and !options[:for_args]
38
+ nl = "\n"
39
+ end
40
+ js = "#{sep}#{js}#{nl}";
41
+ j.push(js)
42
+ prev = js
43
+ end
44
+ j.join("")
45
+ end
46
+ end
47
+
48
+ class Prog < Base
49
+ attr_reader :source_elements
50
+ attr_reader :context
51
+
52
+ def initialize(context, source_elements)
53
+ @source_elements = source_elements
54
+ @context = context
55
+ end
56
+
57
+ def traverse(parent, &block)
58
+ @source_elements.each do |s|
59
+ s.traverse(self, &block)
60
+ end
61
+ yield self, parent
62
+ end
63
+
64
+ def to_js(options = {})
65
+ tt = ''
66
+ vars = @context.var_env.record.binding.find_all {|k, v|
67
+ v and v[:_parameter_list].nil? and !v[:value].kind_of?(StFunc)
68
+ }.collect{|x|x[0]}
69
+
70
+ tt = concat(options, tt, @source_elements)
71
+ end
72
+
73
+ def grouping
74
+ sl = @source_elements
75
+ i = 0
76
+ while i < sl.length
77
+ st = sl[i]
78
+ i0 = i
79
+ prev = nil
80
+ t = nil
81
+ while st and st.to_exp?
82
+ if prev and prev.to_exp?
83
+ t = ECMA262::ExpComma.new(t, st.to_exp({}))
84
+ elsif prev.nil?
85
+ t = st.to_exp({})
86
+ else
87
+ break
88
+ end
89
+ prev = st
90
+ i += 1
91
+ st = sl[i]
92
+ end
93
+ if i0 != i and i - i0 >= 2
94
+ sl[i0...i] = StExp.new(t)
95
+ i = (i - i0 + 1)
96
+ else
97
+ i += 1
98
+ end
99
+ end
100
+ end
101
+
102
+ def replace(from, to)
103
+ idx = @source_elements.index(from)
104
+ if idx
105
+ @source_elements[idx] = to
106
+ end
107
+ end
108
+
109
+ def remove(st)
110
+ @source_elements.delete(st)
111
+ end
112
+
113
+ def each(&block)
114
+ @source_elements.each(&block)
115
+ end
116
+
117
+ def [](i)
118
+ @source_elements[i]
119
+ end
120
+
121
+ def index(st)
122
+ @source_elements.index(st)
123
+ end
124
+
125
+ end
126
+ end
127
+ end
128
+
@@ -0,0 +1,78 @@
1
+ module Minjs
2
+ module ECMA262
3
+ class EnvRecord
4
+ attr_reader :binding
5
+ attr_reader :options
6
+
7
+ def initialize(options = {})
8
+ @binding = {}
9
+ @options = {}
10
+ end
11
+ def create_mutable_binding(n, d, options = {})
12
+ if n.kind_of? IdentifierName
13
+ n = n.val
14
+ end
15
+ @binding[n] = {:value => nil}
16
+ end
17
+ def set_mutable_binding(n, v, s, options = {})
18
+ if n.kind_of? IdentifierName
19
+ n = n.val
20
+ end
21
+ @binding[n][:value] = v
22
+ @binding[n].merge!(options)
23
+ end
24
+ end
25
+
26
+ class ExObject
27
+ def initialize(options = {})
28
+ @attr = options[:attr] || {}
29
+ @prop = options[:prop] || {}
30
+ end
31
+ end
32
+
33
+ class LexEnv
34
+ attr_reader :record
35
+ attr_reader :outer
36
+
37
+ def initialize(options = {})
38
+ @outer = options[:outer]
39
+ @record = EnvRecord.new
40
+ end
41
+
42
+ def new_declarative_env(outer = nil)
43
+ LexEnv.new(outer: (outer || self))
44
+ end
45
+
46
+ def debug
47
+ STDERR.puts @record.binding
48
+ end
49
+ end
50
+
51
+ class Context
52
+ attr_accessor :lex_env
53
+ attr_accessor :var_env
54
+ attr_accessor :this_binding
55
+
56
+ def initialize(options = {})
57
+ @var_env = @lex_env = LexEnv.new(options)
58
+ @this_binding = ExObject.new(
59
+ {
60
+ attr: {
61
+ writable: true,
62
+ enumerable: false,
63
+ configurable: true
64
+ }
65
+ }
66
+ )
67
+ end
68
+
69
+ def debug
70
+ @var_env.debug
71
+ end
72
+
73
+ def inspect
74
+ @var_env.record.binding.to_s
75
+ end
76
+ end
77
+ end
78
+ end