Ejemplo n.º 1
0
    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);
    }
Ejemplo n.º 2
0
    public static void interrupt_wavy_exception(WavyObject exception_object)
    {
        // First check if the exception is valid by checking it extends the base Exception WavyClass
        WavyClass exception = (WavyClass)WavyNamespace.get_var_in_namespace(interpreter.local_scope, NAMESPACE_LOCATION, BASE_EXCEPTION_CLASS);
        // First check if it extends 'Exception'
        WavyClass super = exception_object.the_class;

        while (super != exception && super != null)
        {
            super = super.superclass;
        }
        if (super != exception)
        {
            throw new RuntimeException("Class must be an Exception to be able to interrupt");
        }
        // Then throw the interrupt
        throw new InterruptException(exception_object, (string)exception_object.get("message"));
    }
Ejemplo n.º 3
0
    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);
    }
Ejemplo n.º 4
0
 public WavyClass(string name, WavyClass superclass, Dictionary <string, WavyFunction> methods)
 {
     this.name       = name;
     this.superclass = superclass;
     this.methods    = methods;
 }
Ejemplo n.º 5
0
 public WavyObject(WavyClass the_class)
 {
     this.members   = new Dictionary <string, object>();
     this.the_class = the_class;
 }