示例#1
0
        public object visitSuperExpr(Expr.Super expr)
        {
            int         distance   = locals[expr];
            JingleClass superclass = (JingleClass)environment.getAt(distance, "super");

            Instance object_ = (Instance)environment.getAt(distance - 1, "this");

            JingleFunc method = superclass.findMethod(expr.method.lexeme);

            if (method == null)
            {
                throw new RuntimeError(expr.method, "Undefined property '" + expr.method.lexeme + "'.");
            }

            return(method.bind(object_));
        }
示例#2
0
        public object visitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

            if (stmt.superclass != null)
            {
                superclass = evaluate(stmt.superclass);
                if (!(superclass is JingleClass))
                {
                    throw new RuntimeError(stmt.superclass.name, "Superclass must be a class.");
                }
            }

            environment.define(stmt.name.lexeme, null);

            if (stmt.superclass != null)
            {
                environment = new Environment(environment);
                environment.define("super", superclass);
            }

            Dictionary <string, JingleFunc> methods = new Dictionary <string, JingleFunc>();

            foreach (Stmt.Function method in stmt.methods)
            {
                JingleFunc function = new JingleFunc(method, environment, method.name.lexeme.Equals("init"));
                methods.Add(method.name.lexeme, function);
            }

            JingleClass jnclass = new JingleClass(stmt.name.lexeme, (JingleClass)superclass, methods);

            if (superclass != null)
            {
                environment = environment.enclosing;
            }
            environment.assign(stmt.name, jnclass);
            return(null);
        }
示例#3
0
 public Instance(JingleClass jnclass)
 {
     this.jnclass = jnclass;
 }
示例#4
0
 public JingleClass(string name, JingleClass superclass, Dictionary <string, JingleFunc> methods)
 {
     this.superclass = superclass;
     this.name       = name;
     this.methods    = methods;
 }