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.
data/lib/minjs/func.rb ADDED
@@ -0,0 +1,86 @@
1
+ module Minjs
2
+ module Func
3
+ #
4
+ #13
5
+ #
6
+ def func_declaration(lex, context)
7
+ return nil if lex.match_lit(ECMA262::ID_FUNCTION).nil?
8
+ lex.eval_lit {
9
+ new_env = context.lex_env.new_declarative_env()
10
+ new_context = ECMA262::Context.new
11
+ new_context.lex_env = new_env
12
+ new_context.var_env = new_env
13
+
14
+ if id=identifier(lex, context) and
15
+ lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and
16
+ args = formal_parameter_list(lex, new_context) and
17
+ lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and
18
+ lex.match_lit(ECMA262::PUNC_LCURLYBRAC) and
19
+ b=func_body(lex, new_context) and lex.match_lit(ECMA262::PUNC_RCURLYBRAC)
20
+ f = ECMA262::StFunc.new(new_context, id, args, b, true)
21
+
22
+ context.var_env.record.create_mutable_binding(id, nil)
23
+ context.var_env.record.set_mutable_binding(id, f, nil)
24
+ f
25
+ else
26
+ nil
27
+ end
28
+ }
29
+ end
30
+
31
+ def func_exp(lex, context)
32
+ return nil if lex.match_lit(ECMA262::ID_FUNCTION).nil?
33
+ STDERR.puts "*** func_exp" if @debug
34
+ lex.debug_lit if @debug
35
+
36
+ lex.eval_lit {
37
+ id_opt = identifier(lex, context)
38
+ new_env = context.lex_env.new_declarative_env()
39
+ new_context = ECMA262::Context.new
40
+ new_context.lex_env = new_env
41
+ new_context.var_env = new_env
42
+
43
+ if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and
44
+ args = formal_parameter_list(lex, new_context) and
45
+ lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and
46
+ lex.match_lit(ECMA262::PUNC_LCURLYBRAC) and
47
+ b = func_body(lex, new_context) and lex.match_lit(ECMA262::PUNC_RCURLYBRAC)
48
+ f = ECMA262::StFunc.new(new_context, id_opt, args, b)
49
+ if id_opt
50
+ new_context.var_env.record.create_mutable_binding(id_opt, nil)
51
+ new_context.var_env.record.set_mutable_binding(id_opt, f, nil)
52
+ id_opt.context = new_context
53
+ end
54
+ f
55
+ else
56
+ lex.debug_lit
57
+ raise 'error'
58
+ end
59
+ }
60
+ end
61
+
62
+ def formal_parameter_list(lex, context)
63
+ lex.eval_lit{
64
+ ret = []
65
+ while true
66
+ a = identifier(lex, context)
67
+ if a
68
+ ret.push(a)
69
+ break if lex.match_lit(ECMA262::PUNC_COMMA).nil?
70
+ else
71
+ break
72
+ end
73
+ end
74
+ ret.each do |argName|
75
+ context.var_env.record.create_mutable_binding(argName, nil)
76
+ context.var_env.record.set_mutable_binding(argName, :undefined, nil, {:_parameter_list => true})
77
+ end
78
+ ret
79
+ }
80
+ end
81
+
82
+ def func_body(lex, context)
83
+ source_elements(lex, context)
84
+ end
85
+ end
86
+ end