Ejemplo n.º 1
0
        public string VisitFunctionStmt(Stmt.FunctionStmt stmt)
        {
            var builder = new StringBuilder();

            builder.Append($"(fun {stmt.Name.Lexeme}(");

            foreach (var param in stmt.Params)
            {
                if (param != stmt.Params[0])
                {
                    builder.Append(" ");
                }
                builder.Append(param.Lexeme);
            }

            builder.Append(") ");

            foreach (var body in stmt.Body)
            {
                builder.Append(body.Accept(this));
            }

            builder.Append(")");
            return(builder.ToString());
        }
Ejemplo n.º 2
0
 public object VisitFunctionStmt(Stmt.FunctionStmt stmt)
 {
     Declare(stmt.Name);
     Define(stmt.Name);
     ResolveFunction(stmt, FunctionType.FUNCTION);
     return(null);
 }
Ejemplo n.º 3
0
        private void ResolveFunction(Stmt.FunctionStmt function, FunctionType type)
        {
            var enclosingFunction = _currentFunction;

            _currentFunction = type;

            BeginScope();
            foreach (var param in function.Params)
            {
                Declare(param);
                Define(param);
            }
            Resolve(function.Body);
            EndScope();

            _currentFunction = enclosingFunction;
        }
Ejemplo n.º 4
0
 public LoxFunction(Stmt.FunctionStmt declaration, Environment closure, bool isInitializer)
 {
     Declaration   = declaration;
     Closure       = closure;
     IsInitializer = isInitializer;
 }