lib/AstRewritter.rb in maroon-0.7.0 vs lib/AstRewritter.rb in maroon-0.7.1

- old
+ new

@@ -1,8 +1,9 @@ class AstRewritter def initialize(ast, interpretation_context) @ast = Production.new(ast, interpretation_context) + @roles = interpretation_context.roles end def rewrite!() ast.each do |production| case production.type @@ -10,46 +11,55 @@ data = production.data production[2] = ((("self_" + data[1].to_s) + "_") + data[0].to_s).to_sym production[1] = nil when Tokens.block_with_bind then block = production.last - must_b_sym = "aliased_role must be a Symbol".to_sym - local_must_b_sym = "local must be a Symbol".to_sym - raise(must_b_sym) unless aliased_role.instance_of?(Symbol) - raise(local_must_b_sym) unless local.instance_of?(Symbol) - aliased_field = ("@" + aliased_role.to_s).to_sym - temp_symbol = ("temp____" + aliased_role.to_s).to_sym - assignment = Sexp.new - assignment[0] = :iasgn - assignment[1] = aliased_field - load_arg = Sexp.new - load_arg[0] = :lvar - load_arg[1] = local - assignment[2] = load_arg - block.insert(1, assignment) - assignment = Sexp.new - assignment[0] = :lasgn - assignment[1] = temp_symbol - load_field = Sexp.new - load_field[0] = :ivar - load_field[1] = aliased_field - assignment[2] = load_field - block.insert(1, assignment) - assignment = Sexp.new - assignment[0] = :iasgn - assignment[1] = aliased_field - load_temp = Sexp.new - load_temp[0] = :lvar - load_temp[1] = temp_symbol - assignment[2] = load_temp - block[block.length] = assignment + block.delete_at(1) + production.data.each do |local, aliased_role| + must_b_sym = "aliased_role must be a Symbol".to_sym + local_must_b_sym = "local must be a Symbol".to_sym + raise(must_b_sym) unless aliased_role.instance_of?(Symbol) + raise(local_must_b_sym) unless local.instance_of?(Symbol) + unless @roles.has_key?(aliased_role) then + role_names = [] + @interpretation_context.each { |k, v| (role_names << k.to_s) } + raise(((aliased_role.to_s + " is not a role. Available roles are ") + role_names.join(","))) + end + aliased_field = ("@" + aliased_role.to_s).to_sym + temp_symbol = ("temp____" + aliased_role.to_s).to_sym + assignment = Sexp.new + assignment[0] = :iasgn + assignment[1] = aliased_field + load_arg = Sexp.new + load_arg[0] = :lvar + load_arg[1] = local + assignment[2] = load_arg + block.insert(1, assignment) + assignment = Sexp.new + assignment[0] = :lasgn + assignment[1] = temp_symbol + load_field = Sexp.new + load_field[0] = :ivar + load_field[1] = aliased_field + assignment[2] = load_field + block.insert(1, assignment) + assignment = Sexp.new + assignment[0] = :iasgn + assignment[1] = aliased_field + load_temp = Sexp.new + load_temp[0] = :lvar + load_temp[1] = temp_symbol + assignment[2] = load_temp + block[block.length] = assignment + end else # do nothing end end end private - attr_reader :ast + + attr_reader :ast end \ No newline at end of file