public object visit_super(SuperExpr super_expr) { // Get the superclass and the 'this' object WavyClass superclass = (WavyClass)local_scope.get("super"); WavyObject obj = (WavyObject)local_scope.get("this"); // Get the method from the superclass WavyFunction method = superclass.find_method(obj, (string)super_expr.identifier.value); if (method == null) { throw new RuntimeException("Undefined method of superclass '" + super_expr.identifier.value + "'"); } return(method); }
public object get(string identifier) { // Check class methods WavyFunction method = the_class.find_method(this, identifier); if (method != null) { return(method); } // Check object members object member = find_member(identifier); if (member != null) { return(member); } throw new RuntimeException("Class member/method cannot be found '" + identifier + "'"); }
public void visit_trycatch(TryCatchStmt trycatch_stmt) { try { execute(trycatch_stmt.try_body); }catch (WavyException exception) { WavyFunction function = new WavyFunction(trycatch_stmt.catch_body, this.local_scope, false); // We want to create a new instance of a WavyException here Callable exception_class = (Callable)WavyNamespace.get_var_in_namespace(this.local_scope, "exception", "Exception"); function.call(this, new List <object>() { exception_class.call(this, new List <object>() { "[Caught Exception] " + exception.errror_msg }) } ); } }
public void visit_class(ClassStmt class_stmt) { object superclass = null; // Check if we have a superclass, and if so, evaluate that if (class_stmt.superclass != null) { superclass = evaluate(class_stmt.superclass); if (!(superclass is WavyClass)) { throw new RuntimeException("Superclass must be a class type"); } } this.local_scope.define((string)class_stmt.name.value, null); if (class_stmt.superclass != null) { // Create a new scope for the superclass class local_scope = new Scope(local_scope); // Define super as the superclass local_scope.define("super", superclass); } // Create a dictionary of methods for the class Dictionary <string, WavyFunction> methods = new Dictionary <string, WavyFunction>(); foreach (FunctionStmt method in class_stmt.methods) { bool is_constructor = ((string)method.name.value) == (string)class_stmt.name.value; WavyFunction function = new WavyFunction(method, this.local_scope, is_constructor); methods.Add((string)method.name.value, function); } // Create the class object and assign it to the scope WavyClass p_class = new WavyClass((string)class_stmt.name.value, (WavyClass)superclass, methods); if (superclass != null) { // Reset the scope local_scope = local_scope.enclosing_scope; } local_scope.assign(class_stmt.name, p_class); }
public void visit_function(FunctionStmt function_stmt) { WavyFunction func = new WavyFunction(function_stmt, this.local_scope, false); local_scope.define((string)function_stmt.name.value, func); }