Esempio n. 1
0
        object Stmt.Visitor <object> .visitClassStmt(Class stmt)
        {
            //Console.WriteLine("visitClassStmt");
            Object superclass = null;

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

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

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

            Dictionary <String, TypeScriptNativeFunction> methods =
                new Dictionary <String, TypeScriptNativeFunction>();

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

            TypeScriptNativeClass klass =
                new TypeScriptNativeClass(
                    stmt.name.lexeme,
                    (TypeScriptNativeClass)superclass,
                    methods
                    );

            if (superclass != null)
            {
                environment = environment.enclosing;
            }

            environment.assign(stmt.name, klass);
            return(null);
        }
Esempio n. 2
0
        // Expressions Visitor
        object Expr.Visitor <object> .visitAssignExpr(Assign expr)
        {
            Object value = evaluate(expr.value);

            int outDistance;
            var found    = locals.TryGetValue(expr, out outDistance);
            int distance = found ? outDistance : 0;

            if (locals.ContainsKey(expr))
            {
                environment.assignAt(distance, expr.name, value);
            }
            else
            {
                globals.assign(expr.name, value);
            }

            return(value);
        }