lib/minjs/lex/function.rb in minjs-0.4.0 vs lib/minjs/lex/function.rb in minjs-0.4.1

- old
+ new

@@ -14,37 +14,33 @@ # @note # The function declaration in statement(block) is not permitted by ECMA262. # However, almost all implementation permit it, so minjs cannot raise # exception even if function declarataion in block. # - def func_declaration(context) + def func_declaration(var_env) # FunctionDeclaration : # function Identifier ( FormalParameterListopt ) { FunctionBody } - return nil if lex.eql_lit?(ECMA262::ID_FUNCTION).nil? + return nil if eql_lit?(ECMA262::ID_FUNCTION).nil? - new_context = ECMA262::Context.new - new_context.lex_env = context.lex_env.new_declarative_env() - new_context.var_env = context.var_env.new_declarative_env() + new_var_env = ECMA262::LexEnv.new(outer: var_env) - if id=identifier(context) and - lex.eql_lit?(ECMA262::PUNC_LPARENTHESIS) and - args = formal_parameter_list(new_context) and - lex.eql_lit?(ECMA262::PUNC_RPARENTHESIS) and - lex.eql_lit?(ECMA262::PUNC_LCURLYBRAC) and - b=func_body(new_context) and lex.eql_lit?(ECMA262::PUNC_RCURLYBRAC) - f = ECMA262::StFunc.new(new_context, id, args, b, {:decl => true}) + if id=identifier(var_env) and + eql_lit?(ECMA262::PUNC_LPARENTHESIS) and + args = formal_parameter_list(new_var_env) and + eql_lit?(ECMA262::PUNC_RPARENTHESIS) and + eql_lit?(ECMA262::PUNC_LCURLYBRAC) and + b=func_body(new_var_env) and eql_lit?(ECMA262::PUNC_RCURLYBRAC) + f = ECMA262::StFunc.new(new_var_env, id, args, b, {:decl => true}) - context.var_env.record.create_mutable_binding(id, nil) - context.var_env.record.set_mutable_binding(id, f, nil) - context.lex_env.record.create_mutable_binding(id, nil) - context.lex_env.record.set_mutable_binding(id, f, nil) + var_env.record.create_mutable_binding(id, nil) + var_env.record.set_mutable_binding(id, f, nil) f else if b - raise ParseError.new("No `}' at end of function", lex) + raise ParseError.new("No `}' at end of function", self) else - raise ParseError.new("Bad function declaration", lex) + raise ParseError.new("Bad function declaration", self) end end end # Tests next literal is FunctionExpression or not. @@ -58,72 +54,66 @@ # # @note # The function expression and declaration uses same class # for convenience. # - def func_exp(context) + def func_exp(var_env) # FunctionExpression : # function Identifieropt ( FormalParameterListopt ) { FunctionBody } - return nil if lex.eql_lit?(ECMA262::ID_FUNCTION).nil? + return nil if eql_lit?(ECMA262::ID_FUNCTION).nil? @logger.debug "*** func_exp" - id_opt = identifier(context) - new_context = ECMA262::Context.new - new_context.lex_env = context.lex_env.new_declarative_env() - new_context.var_env = context.var_env.new_declarative_env() + id_opt = identifier(var_env) + new_var_env = ECMA262::LexEnv.new(outer: var_env) - if lex.eql_lit?(ECMA262::PUNC_LPARENTHESIS) and - args = formal_parameter_list(new_context) and - lex.eql_lit?(ECMA262::PUNC_RPARENTHESIS) and - lex.eql_lit?(ECMA262::PUNC_LCURLYBRAC) and - b = func_body(new_context) and lex.eql_lit?(ECMA262::PUNC_RCURLYBRAC) - f = ECMA262::StFunc.new(new_context, id_opt, args, b) + if eql_lit?(ECMA262::PUNC_LPARENTHESIS) and + args = formal_parameter_list(new_var_env) and + eql_lit?(ECMA262::PUNC_RPARENTHESIS) and + eql_lit?(ECMA262::PUNC_LCURLYBRAC) and + b = func_body(new_var_env) and eql_lit?(ECMA262::PUNC_RCURLYBRAC) + f = ECMA262::StFunc.new(new_var_env, id_opt, args, b) + #new_var_env.func = f if id_opt - new_context.var_env.record.create_mutable_binding(id_opt, nil) - new_context.var_env.record.set_mutable_binding(id_opt, f, nil) - new_context.lex_env.record.create_mutable_binding(id_opt, nil) - new_context.lex_env.record.set_mutable_binding(id_opt, f, nil) - id_opt.context = new_context + var_env.record.create_mutable_binding(id_opt, nil) + var_env.record.set_mutable_binding(id_opt, f, nil) end f else if b - raise ParseError.new("No `}' at end of function", lex) + raise ParseError.new("No `}' at end of function", self) else - raise ParseError.new("Bad function expression", lex) + raise ParseError.new("Bad function expression", self) end end end - def formal_parameter_list(context) + def formal_parameter_list(var_env) ret = [] - unless lex.peek_lit(nil).eql? ECMA262::PUNC_RPARENTHESIS + unless peek_lit(nil).eql? ECMA262::PUNC_RPARENTHESIS while true - if arg = identifier(context) + if arg = identifier(var_env) ret.push(arg) else - raise ParseError.new("unexpceted token", lex) + raise ParseError.new("unexpceted token", self) end - if lex.peek_lit(nil).eql? ECMA262::PUNC_RPARENTHESIS + if peek_lit(nil).eql? ECMA262::PUNC_RPARENTHESIS break - elsif lex.eql_lit? ECMA262::PUNC_COMMA + elsif eql_lit? ECMA262::PUNC_COMMA ; else - raise ParseError.new("unexpceted token", lex) + raise ParseError.new("unexpceted token", self) end end end ret.each do |argName| - context.var_env.record.create_mutable_binding(argName, nil) - context.var_env.record.set_mutable_binding(argName, :undefined, nil, {:_parameter_list => true}) - context.lex_env.record.create_mutable_binding(argName, nil) - context.lex_env.record.set_mutable_binding(argName, :undefined, nil, {:_parameter_list => true}) + var_env.record.create_mutable_binding(argName, nil) + var_env.record.set_mutable_binding(argName, :undefined, nil, _parameter_list: true) end ret end - def func_body(context) - source_elements(context) + def func_body(var_env) + source_elements(var_env) end private :func_body, :formal_parameter_list end end