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);
    }
    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")));
    }
    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 PlutoClass))
            {
                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, PlutoFunction> methods = new Dictionary <string, PlutoFunction>();

        foreach (FunctionStmt method in class_stmt.methods)
        {
            bool          is_constructor = ((string)method.name.value) == (string)class_stmt.name.value;
            PlutoFunction function       = new PlutoFunction(method, this.local_scope, is_constructor);
            methods.Add((string)method.name.value, function);
        }
        // Create the class object and assign it to the scope
        PlutoClass p_class = new PlutoClass((string)class_stmt.name.value, (PlutoClass)superclass, methods);

        if (superclass != null)
        {
            // Reset the scope
            local_scope = local_scope.enclosing_scope;
        }
        local_scope.assign(class_stmt.name, p_class);
    }
 public PlutoObject(PlutoClass the_class)
 {
     this.members   = new Dictionary <string, object>();
     this.the_class = the_class;
 }
Esempio n. 5
0
 public PlutoClass(string name, PlutoClass superclass, Dictionary <string, PlutoFunction> methods)
 {
     this.name       = name;
     this.superclass = superclass;
     this.methods    = methods;
 }