// Bind this function to an object instance
    public PlutoFunction bind(PlutoObject obj)
    {
        // Inside of the function
        Scope bind_scope = new Scope(this.scope);

        // Define "this" as the object we are binding to
        bind_scope.define("this", obj);
        return(new PlutoFunction(this.function, bind_scope, this.is_constructor));
    }
Example #2
0
    // Create an instance of the class here
    public object call(Interpreter interpreter, List <object> args)
    {
        PlutoObject obj = new PlutoObject(this);

        // Check if the class has a constructor
        if (this.methods.ContainsKey(this.name))
        {
            // Bind the function to this instance and call the constructor
            this.methods[this.name].bind(obj).call(interpreter, args);
        }
        return(obj);
    }
    public object visit_super(SuperExpr super_expr)
    {
        // Get the superclass and the 'this' object
        PlutoClass  superclass = (PlutoClass)local_scope.get(new Token(Token.Type.Super, "super"));
        PlutoObject obj        = (PlutoObject)local_scope.get(new Token(Token.Type.This, "this"));
        // Get the method from the superclass
        PlutoFunction 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);
    }
Example #4
0
 public PlutoFunction find_method(PlutoObject obj, string identifier)
 {
     // Find method in this class
     if (this.methods.ContainsKey(identifier))
     {
         return(methods[identifier].bind(obj));
     }
     // Find method in superclass
     if (superclass != null)
     {
         return(superclass.find_method(obj, identifier));
     }
     return(null);
 }
    public void visit_interrupt(InterruptStmt interrupt_stmt)
    {
        PlutoObject exception_obj = (PlutoObject)evaluate(interrupt_stmt.expression);
        PlutoClass  exception     = (PlutoClass)namespaces["exception"].get(new Token(Token.Type.Identifier, "Exception"));
        // First check if it extends 'Exception'
        PlutoClass super = exception_obj.the_class.superclass;

        while (super != exception && super != null)
        {
            super = super.superclass;
        }
        if (super != exception)
        {
            throw new RuntimeException("Class must be an Exception to be able to interrupt");
        }
        throw new InterruptException(exception_obj, (string)exception_obj.get(new Token(Token.Type.Identifier, "message")));
    }
Example #6
0
 public InterruptException(PlutoObject obj, string error_msg)
 {
     this.obj       = obj;
     this.error_msg = error_msg;
 }