예제 #1
0
파일: Resolver.cs 프로젝트: mhorskaya/cslox
 public object VisitVariableExpr(Expr.VariableExpr expr)
 {
     if (Scopes.Any())
     {
         var scope = Scopes.Peek();
         if (scope.ContainsKey(expr.Name.Lexeme) && !scope[expr.Name.Lexeme])
         {
             Lox.Error(expr.Name, "Cannot read local variable in its own initializer.");
         }
     }
     ResolveLocal(expr, expr.Name);
     return(null);
 }
예제 #2
0
        private Stmt ClassDeclaration()
        {
            var name = Consume(IDENTIFIER, "Expect class name.");

            Expr.VariableExpr superclass = null;
            if (Match(LESS))
            {
                Consume(IDENTIFIER, "Expect superclass name.");
                superclass = new Expr.VariableExpr(Previous());
            }

            Consume(LEFT_BRACE, "Expect '{' before class body.");

            var methods = new List <Stmt.FunctionStmt>();

            while (!Check(RIGHT_BRACE) && !IsAtEnd())
            {
                methods.Add(Function("method"));
            }

            Consume(RIGHT_BRACE, "Expect '}' after class body.");

            return(new Stmt.ClassStmt(name, superclass, methods));
        }
예제 #3
0
 public ClassStmt(Token name, Expr.VariableExpr superclass, List <FunctionStmt> methods)
 {
     Name       = name;
     Superclass = superclass;
     Methods    = methods;
 }
예제 #4
0
 public string VisitVariableExpr(Expr.VariableExpr expr)
 {
     return(expr.Name.Lexeme);
 }