definstance_var_keys @instance_vars.sort_by(&:last).map {|k, _|k} end
defadd_parents(parent_name, parent_table) @parent = parent_name unless parent_table.is_a? ClassTable raise "parent_table should be a ClassTable" end parent_table.instance_vars.each do|var_name, _| unless@instance_vars.include? var_name add_instance_var(var_name) end end end
分支
最近才发现我还没有做分支以及循环的内容
AST
1 2 3 4
classIf # stmt_list: [[if_cond, stmt], [elsif_cond, stmt]*] attr_reader:stmt_list, :else_stmts end
defon_if(node) list = node.stmt_list.map do|cond, stmt| c = visit(cond) s = [visit(stmt), JumpAfterIf.new].flatten cmp_and_jmp = push_eq_jmp(s.size) [c, cmp_and_jmp, s].flatten end.flatten els = visit(node.else_stmts) list = list + els list.each_with_index do|inst, index| if inst.is_a? JumpAfterIf list[index] = RelativeJump.new(list.size - index + 1) end end list end
defpush_eq_jmp(true_branch_size) [Push.new(1), EQ.new, JumpFalse.new(true_branch_size + 1)] end