def run(self): try: main = self.get_current_scope().get_var(ast.Identifier('main', -1)) except VarNotFound: print( f"FATAL: missing entry point function 'main' in file {self.filename}", file=sys.stderr) sys.exit(1) try: with self.new_scope(): # print(main, dir(main)) if len(main.params) == 1: main([self.argv], runner=self) else: main([], runner=self) except StatementError as e: if self.debug: raise print(f"ERROR:{e.stmt.lineno}:", e) sys.exit(1) except Exception as e: if self.debug: raise print(f"ERROR:{main.lineno}:", e) sys.exit(1)
def reassign_var(self, identifier, value, *, const=None): # pointless const argument, just for safety # print("reassign", identifier, value) if identifier in self.const_vars: raise CannotReassignConst(identifier) # cannot reassign consts orig = ast.TypeId( ast.Identifier(self.vars[identifier].__class__.__name__, identifier.lineno)) orig.type_check(value, self.runner) self.vars[identifier] = value
def get_var_noid(self, name): return self.get_var(ast.Identifier(name, -1))
def _exec_std_import(self, stmt): # print(stmt) form = stmt.from_module name = form.transform_to_string() vi_file_name = os.path.join("violet", *name.split(".")) + ".vi" is_vi_file = os.path.exists(vi_file_name) if is_vi_file: try: module = Runner.open(vi_file_name) module.interpret() except Exception as e: raise StatementError( stmt, f'module {stmt.from_module.name!r} does not exist') else: for identifier in stmt.importing: if identifier.name == "*": vars = module.get_current_scope().vars scope = self.get_current_scope() for ident, value in vars.items(): scope.set_var(ident, value) break else: try: var = module.get_current_scope().get_var( identifier) except Exception as e: raise StatementError( stmt, f'failed to import {identifier.name!r} from {name}' ) else: self.get_current_scope().set_var(identifier, var) else: try: module = importlib.import_module('violet.' + name) except ImportError: raise StatementError(stmt, f'module {name!r} does not exist') else: for iport in stmt.importing: if iport.name == "*": if not hasattr(module, "__all__"): importables = ( x for x in dir(module) if x.isalpha() or not x.startswith("__")) else: importables = module.__all__ scope = self.get_current_scope() for vname in importables: scope.set_var(ast.Identifier(vname, stmt.lineno), getattr(module, vname)) break if not hasattr(module, iport.name): raise StatementError( stmt, f'failed to import {iport.name!r} from {name!r}') else: self.get_current_scope().set_var( iport, getattr(module, iport.name))