def _increment_field(self, name): self._push_field_value_onto_stack(name) self.code.extend([ instructions.bipush(1), instructions.iadd(), ]) self._set_field_with_value_from_top_of_stack(name)
def _input_to_field(self, name, as_char): self.code.append(instructions.aload(0)) self._push_field_value_onto_stack(Builder.INPUT_INDEX) self.code.append(instructions.aaload()) if as_char: self.code.append(instructions.bipush(0)) char_at = self.output_class.pool.add_method_ref( "java/lang/String", "charAt", "(I)C") self.code.append(instructions.invokevirtual(char_at)) else: parse_int = self.output_class.pool.add_method_ref( "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I") self.code.append(instructions.invokestatic(parse_int)) self._set_field_with_value_from_top_of_stack(name) self._increment_field(Builder.INPUT_INDEX)
def asl_dump(self, asl): mapping = { ast.Goto: lambda: self.code.append(Goto(item.name)), ast.ConditionalGoto: lambda: self._add_conditional_goto(item.name), ast.Label: lambda: self.code.append(Label(item.name)), ast.BinaryOperator: lambda: self._add_operator_instruction_from_node(item), ast.Value: lambda: self.code.append(instructions.bipush(item.value)), ast.DynamicValue: lambda: self._push_field_value_onto_stack(item.field), ast.Assign: lambda: self._set_field_with_value_from_top_of_stack(item.var), ast.PrintVariable: lambda: self._print_field(item.field, item.as_char), ast.InputVariable: lambda: self._input_to_field(item.field, item.as_char), ast.NoOp: lambda: None, ast.Compare: lambda: self._compare(item.var1, item.var2), } for item in asl: for node_type in mapping.keys(): if isinstance(item, node_type): mapping[node_type]() break else: raise CompilationError("No rule to map {}".format(item)) return self
def _set_field(self, name, value): """ Sets a field with a constant value. """ self.code.append(instructions.bipush(value)) self._set_field_with_value_from_top_of_stack(name)
def _multiply_integer_at_top_of_stack_by_two(self): self.code.extend([ instructions.bipush(2), instructions.imul(), ])