def compile(self, path, filename, system_class, universe): fname = path + os.sep + filename + ".som" try: input_file = open_file_as_stream(fname, "r") try: self._parser = Parser(input_file, fname, universe) result = self._compile(system_class, universe) finally: input_file.close() except OSError: raise IOError() except ParseError as e: from som.vm.universe import error_println error_println(str(e)) universe.exit(1) return None cname = result.get_name() cnameC = cname.get_string() if filename != cnameC: from som.vm.universe import error_println error_println("File name %s does not match class name %s." % (filename, cnameC)) universe.exit(1) return result
def load_primitives(self, display_warning): from som.primitives.known import (primitives_for_class, PrimitivesNotFound) try: prim_class = primitives_for_class(self) prim_class(self._universe, display_warning).install_primitives_in(self) except PrimitivesNotFound: if display_warning: from som.vm.universe import error_println error_println("Loading of primitives failed for %s. Currently, " "we support primitives only for known classes" % self.get_name())
def start(self): from som.vm.universe import std_println, error_println counter = 0 it = self._universe.nilObject std_println("SOM Shell. Type \"quit\" to exit.\n") # Create a fake bootstrap frame current_frame = self._interpreter.new_frame(None, self._bootstrap_method, None) while True: try: # Read a statement from the keyboard stmt = raw_input("---> ") if stmt == "quit" or stmt == "": return elif stmt == "\n": continue # Generate a temporary class with a run method stmt = ("Shell_Class_" + str(counter) + " = ( run: it = ( | tmp | tmp := (" + stmt + " ). 'it = ' print. ^tmp println ) )") counter += 1 # Compile and load the newly generated class my_class = self._universe.load_shell_class(stmt) # If success if my_class: current_frame.reset_stack_pointer() # Create and push a new instance of our class on the stack my_object = self._universe.new_instance(my_class) current_frame.push(my_object) # Push the old value of "it" on the stack current_frame.push(it) # Lookup the run: method initialize = my_class.lookup_invokable( self._universe.symbol_for("run:")) # Invoke the run method initialize.invoke(current_frame, self._interpreter) # Save the result of the run method it = current_frame.pop() except Exception as e: if not we_are_translated(): # this cannot be done in rpython import traceback traceback.print_exc() error_println("Caught exception: %s" % e)
def load_primitives(self, display_warning): from som.primitives.known import (primitives_for_class, PrimitivesNotFound) try: prims = primitives_for_class(self) prims(self._universe).install_primitives_in(self) except PrimitivesNotFound: if display_warning: from som.vm.universe import error_println error_println( "Loading of primitives failed for %s. Currently, " "we support primitives only for known classes" % self.get_name())
def dump(clazz): for i in range(0, clazz.get_number_of_instance_invokables()): inv = clazz.get_instance_invokable(i) # output header and skip if the Invokable is a Primitive error_print(str(clazz.get_name()) + ">>" + str(inv.get_signature()) + " = ") if inv.is_primitive(): error_println("<primitive>") continue # output actual method dump_method(inv, "\t")
def dump(clazz): for i in range(0, clazz.get_number_of_instance_invokables()): inv = clazz.get_instance_invokable(i) # output header and skip if the Invokable is a Primitive error_print( str(clazz.get_name()) + ">>" + str(inv.get_signature()) + " = ") if inv.is_primitive(): error_println("<primitive>") continue # output actual method dump_method(inv, "\t")
def start(self): from som.vm.universe import std_println, error_println counter = 0 it = nilObject std_println("SOM Shell. Type \"quit\" to exit.\n") while True: try: # Read a statement from the keyboard stmt = raw_input("---> ") if stmt == "quit" or stmt == "": return elif stmt == "\n": continue # Generate a temporary class with a run method stmt = ("Shell_Class_" + str(counter) + " = ( run: it = ( | tmp | tmp := (" + stmt + " ). 'it = ' print. ^tmp println ) )") counter += 1 # Compile and load the newly generated class my_class = self._universe.load_shell_class(stmt) # If success if my_class: # Create and push a new instance of our class on the stack my_object = self._universe.new_instance(my_class) # Lookup the run: method initialize = my_class.lookup_invokable( self._universe.symbol_for("run:")) # Invoke the run method it = initialize.invoke(my_object, [it], False) except Exception as e: if not we_are_translated(): # this cannot be done in rpython import traceback traceback.print_exc() error_println("Caught exception: %s" % e)
def dump_method(m, indent): error_println("(") error_println(indent + indent + "TODO") error_println(indent + ")")
import sys from som.compiler.parser import ParseError from som.vm.universe import main, Exit try: main(sys.argv) except Exit, e: sys.exit(e.code) except ParseError as e: from som.vm.universe import error_println error_println(str(e)) sys.exit(1) sys.exit(0)
import sys from som.compiler.parse_error import ParseError from som.vm.universe import main, Exit try: main(sys.argv) except Exit, e: sys.exit(e.code) except ParseError as e: from som.vm.universe import error_println error_println(str(e)) sys.exit(1) sys.exit(0)
def dump_method(m, indent): error_println("(") # output stack information error_println("%s<%d locals, %d stack, %d bc_count>" % (indent, m.get_number_of_locals().get_embedded_integer(), m.get_maximum_number_of_stack_elements().get_embedded_integer(), m.get_number_of_bytecodes())) # output bytecodes b = 0 while b < m.get_number_of_bytecodes(): error_print(indent) # bytecode index if b < 10: error_print(" ") if b < 100: error_print(" ") error_print(" %d:" % b) # mnemonic bytecode = m.get_bytecode(b) error_print(bytecode_as_str(bytecode) + " ") # parameters (if any) if bytecode_length(bytecode) == 1: error_println() b += 1 continue if bytecode == Bytecodes.push_local: error_println("local: " + str(m.get_bytecode(b + 1)) + ", context: " + str(m.get_bytecode(b + 2))) elif bytecode == Bytecodes.push_argument: error_println("argument: " + str(m.get_bytecode(b + 1)) + ", context " + str(m.get_bytecode(b + 2))) elif bytecode == Bytecodes.push_field: error_println("(index: " + str(m.get_bytecode(b + 1)) + ") field: " + str(m.get_holder().get_instance_field_name(m.get_bytecode(b + 1)))) elif bytecode == Bytecodes.push_block: error_print("block: (index: " + str(m.get_bytecode(b + 1)) + ") ") dump_method(m.get_constant(b), indent + "\t") elif bytecode == Bytecodes.push_constant: constant = m.get_constant(b) error_println("(index: " + str(m.get_bytecode(b + 1)) + ") value: (" + str(constant.get_class(get_current()).get_name()) + ") " + str(constant)) elif bytecode == Bytecodes.push_global: error_println("(index: " + str(m.get_bytecode(b + 1)) + ") value: " + str(m.get_constant(b))) elif bytecode == Bytecodes.pop_local: error_println("local: " + str(m.get_bytecode(b + 1)) + ", context: " + str(m.get_bytecode(b + 2))) elif bytecode == Bytecodes.pop_argument: error_println("argument: " + str(m.get_bytecode(b + 1)) + ", context: " + str(m.get_bytecode(b + 2))) elif bytecode == Bytecodes.pop_field: error_println("(index: " + str(m.get_bytecode(b + 1)) + ") field: " + str(m.get_holder().get_instance_field_name(m.get_bytecode(b + 1)))) elif bytecode == Bytecodes.send: error_println("(index: " + str(m.get_bytecode(b + 1)) + ") signature: " + str(m.get_constant(b))) elif bytecode == Bytecodes.super_send: error_println("(index: " + str(m.get_bytecode(b + 1)) + ") signature: " + str(m.get_constant(b))) else: error_println("<incorrect bytecode>") b += bytecode_length(m.get_bytecode(b)) error_println(indent + ")")