examples/sicp_scheme/evaluator.rb in rubasteme-0.1.3 vs examples/sicp_scheme/evaluator.rb in rubasteme-0.1.4

- old
+ new

@@ -41,10 +41,13 @@ when *EV_DEFINITION eval_definition(exp, env) when *EV_IF eval_if(exp, env) when *EV_LAMBDA + internal_definitions(exp).each { |d| + eval_definition(d, env) + } make_procedure(lambda_parameters(exp), lambda_body(exp), env) when *EV_BEGIN eval_sequence(begin_actions(exp), env) @@ -197,12 +200,13 @@ arguments) eval_sequence(procedure_body(procedure), extended_env) end def begin_actions(exp) - # exp = [:ast_begin, [:ast_*_1], [:ast_*_2], ... ] - exp[1..-1] + # exp = [:ast_begin, [:ast_sequence]] + # sequence = [:ast_sequence, [:ast_*_1], [:ast_*_2], ...] + exp[1][1..-1] end def eval_sequence(exps, env) if last_exp?(exps) self.eval(first_exp(exps), env) @@ -239,18 +243,27 @@ def definition_value(exp) # exp = [:ast_identifier_definition, [:ast_identifier], [:ast_*]] exp[2] end + def internal_definitions(exp) + # exp = [:ast_lambda_expression, [:ast_formals], [:ast_body]] + # body = [:ast_body, [:ast_internal_definitions], [:ast_sequence]] + # internal_definitions = [:ast_intern_definitions, [:ast_definition], ...] + exp[2][1][1..-1] + end + def lambda_parameters(exp) - # exp = [:ast_lambda_expression, [:ast_formals], [:ast_*_1] ...] + # exp = [:ast_lambda_expression, [:ast_formals], [:ast_body]] formals = exp[1][1..-1] formals.map{|node| identifier(node)} end def lambda_body(exp) - # exp = [:ast_lambda_expression, [:ast_formals], [:ast_*_1] ...] - exp[2..-1] + # exp = [:ast_lambda_expression, [:ast_formals], [:ast_body]] + # body = [:ast_body, [:ast_internal_definitions], [:ast_sequence]] + # sequence = [:ast_sequence, [:ast_*_1], [:ast_*_2], ...] + exp[2][2][1..-1] end def make_procedure(parameters, body, env) # parameters = [:ast_formals, [:ast_identifier_1], [:ast_identifier_2] ...] # body = [[:ast_*_1], [:ast_*_2], ...]