Exemple #1
0
 public LoxFunction(Stmt.Function declaration, Environment closure,
                    bool isInitializer)
 {
     IsInitializer = isInitializer;
     Declaration   = declaration;
     Closure       = closure;
 }
Exemple #2
0
        public LoxVoid VisitFunctionStmt(Stmt.Function stmt)
        {
            LoxFunction function = new LoxFunction(stmt, _environment, false);

            _environment.Define(stmt.Name.Lexeme, function);
            return(null);
        }
Exemple #3
0
        private Stmt Function(string kind)
        {
            Lexeme name = Consume(LexemeType.IDENTIFIER, Peek());

            Consume(LexemeType.LEFT_PAREN, Peek());
            var typeMap = new List <TypeParameterMap>();

            if (!Check(LexemeType.RIGHT_PAREN))
            {
                do
                {
                    var parameterType = ConsumeKeyword();
                    var varName       = Expr();
                    if (parameterType.IsKeyWord)
                    {
                        typeMap.Add(new TypeParameterMap(parameterType, varName));
                    }
                }while (Match(LexemeType.COMMA));
            }

            Consume(LexemeType.RIGHT_PAREN, Peek());
            Consume(LexemeType.EQUAL, Peek());
            Consume(LexemeType.LEFT_BRACE, Peek());

            List <Stmt> statements = Block();

            var stmt = new Stmt.Function(name, typeMap, statements);

            return(stmt);
        }
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            var function = new LoxFunction(stmt.name, stmt.function, _environment, false);

            _environment.Define(stmt.name.Lexeme, function);
            return(null);
        }
Exemple #5
0
    public object VisitFunctionStmt(Stmt.Function stmt)
    {
        LoxFunction function = new LoxFunction(stmt, Environment, false);

        Environment.Define(stmt.Name.Lexeme, function);
        return(null);
    }
Exemple #6
0
        public object visitFunctionStmt(Stmt.Function stmt)
        {
            LoxFunction function = new LoxFunction(stmt, environment, false);

            environment.define(stmt.name.lexeme(), function);
            return(null);
        }
Exemple #7
0
 public object VisitFunctionStmt(Stmt.Function stmt)
 {
     Declare(stmt.Name);
     Define(stmt.Name);
     ResolveFunction(stmt, FunctionType.FUNCTION);
     return(null);
 }
Exemple #8
0
        Stmt Stmt.Visitor <Stmt> .VisitFunctionStmt(Stmt.Function stmt)
        {
            JukaFunction functionCallable = new JukaFunction(stmt, null, false);

            environment.Define(stmt.name.ToString(), functionCallable);
            return(null);
        }
Exemple #9
0
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            Function function = new Function(stmt);

            environment.Define(stmt.Name.Lexeme, function);
            return(null);
        }
Exemple #10
0
        public object VisitFunctionStmt(Stmt.Function stmt, object options)
        {
            StringBuilder builder = new();
            string        typestr;

            if (stmt.returnType != null)
            {
                typestr = (string)stmt.returnType.Accept(this, null);
            }
            else
            {
                typestr = "void";
            }
            builder.Append($"({typestr} {stmt.token.lexeme}(");
            foreach (var param in stmt.input)
            {
                if (param != stmt.input[0])
                {
                    builder.Append(", ");
                }
                builder.Append(param.Accept(this, null));
            }
            builder.Append(") ");
            foreach (Stmt body in stmt.body)
            {
                builder.Append(body.Accept(this, null));
            }
            builder.Append(')');
            return(builder.ToString());
        }
Exemple #11
0
 public Unit VisitFunctionStmt(Stmt.Function stmt)
 {
     Declare(stmt.Name);
     Define(stmt.Name);
     ResolveFunction(stmt);
     return(new Unit());
 }
Exemple #12
0
        public string VisitFunctionStmt(Stmt.Function stmt)
        {
            StringBuilder builder = new();

            builder.Append("(fun " + stmt.Name.Lexeme + "(");

            foreach (Parameter param in stmt.Parameters)
            {
                if (param != stmt.Parameters[0])
                {
                    builder.Append(' ');
                }

                builder.Append(param.Name.Lexeme);
            }

            builder.Append(") ");

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

            builder.Append(')');

            return(builder.ToString());
        }
Exemple #13
0
 public LoxFunction(Stmt.Function declaration,
                    Environment closure,
                    bool isInitializer)
 {
     this.declaration   = declaration;
     this.closure       = closure;
     this.isInitializer = isInitializer;
 }
Exemple #14
0
        public object visitFunctionStmt(Stmt.Function stmt)
        {
            declare(stmt.name);
            define(stmt.name);

            resolveFunction(stmt, FunctionType.TYPE_FUNCTION);
            return(null);
        }
Exemple #15
0
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            Declare(stmt.name);
            Define(stmt.name);

            ResolveFunction(stmt, FunctionType.FUNCTION);
            return(new Stmt.DefaultStatement());
        }
        void function(Stmt.Function funStmt, FunctionType type)
        {
            /* clox: funDeclaration */
            byte global = 0;

            if (type == FunctionType.TYPE_FUNCTION)
            {
                global = parseVariable(funStmt.name, "Expect function name.");
                markInitialized();
            }


            /* clox: function */
            Compiler_t compiler = new Compiler_t();

            initCompiler(ref compiler, type, funStmt.name);

            captureToken(funStmt.name);

            beginScope();

            // Compile parameter list.
            if (funStmt.params_.Count > 255)
            {
                errorAtCurrent("Cannot have more than 255 parameters.");
            }
            foreach (Token param in funStmt.params_)
            {
                byte paramConstant = parseVariable(param, "Expect parameter name.");
                defineVariable(paramConstant);
            }
            current.function.arity = funStmt.params_.Count;


            // Compile the function body.
            foreach (Stmt stmt in funStmt.body)
            {
                compile(stmt);
            }

            // Create the function object.
            ObjFunction function = endCompiler();

            emitBytes((byte)OpCode.OP_CLOSURE, makeConstant(Value.OBJ_VAL(function)));

            for (int i = 0; i < function.upvalueCount; i++)
            {
                emitByte((byte)(compiler.upvalues[i].isLocal ? 1 : 0));
                emitByte(compiler.upvalues[i].index);
            }

            /* clox: funDeclaration */
            if (type == FunctionType.TYPE_FUNCTION)
            {
                defineVariable(global);
            }
        }
Exemple #17
0
        /// <summary>
        /// Resolve a function declaration
        /// </summary>
        /// <param name="stmt">The statement</param>
        public object Visit(Stmt.Function stmt)
        {
            // Declare and define the function name
            Declare(stmt.Name);
            Define(stmt.Name);

            ResolveFunction(stmt, FunctionType.FUNCTION);
            return(null);
        }
Exemple #18
0
        public LoxVoid VisitFunctionStmt(Stmt.Function stmt)
        {
            Declare(stmt.Name);
            Define(stmt.Name);

            ResolveFunction(stmt.Parameter, stmt.Body, FunctionType.Function);

            return(null);
        }
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            Declare(stmt.name);
            Define(stmt.name);

            Resolve(stmt.function);

            return(null);
        }
Exemple #20
0
        public VoidObject VisitFunctionStmt(Stmt.Function stmt)
        {
            Declare(stmt.Name);
            DefineFunction(stmt.Name, stmt.ReturnTypeReference, stmt);

            ResolveFunction(stmt, FunctionType.FUNCTION);

            return(VoidObject.Void);
        }
Exemple #21
0
 private void ResolveFunction(Stmt.Function function)
 {
     BeginScope();
     foreach (var param in function.Params)
     {
         Declare(param);
         Define(param);
     }
     Resolve(function.Body);
     EndScope();
 }
Exemple #22
0
        public Unit VisitFunctionStmt(Stmt.Function stmt)
        {
            // Function names are variable types, to be decalared and defined in the same step.
            // THis allows recursion to handle self name resoluion.
            // nb: the funciton name and, arguments are all in the same scope.
            // the body not.
            Declare(stmt.Name);
            Define(stmt.Name);
            ResolveFunction(stmt, FunctionType.FUNCTION);

            return(new Unit());
        }
Exemple #23
0
        public FunctionBinding(Stmt.Function function, ITypeReference typeReference, int distance, Expr referringExpr)
            : base(typeReference, referringExpr)
        {
            // Likewise, the Function property is permitted to be null for variable bindings (but not for Call
            // bindings)
            if (referringExpr is Expr.Call)
            {
                Function = function ?? throw new PerlangInterpreterException(
                                     "When referringExpr is an Expr.Call instance, function cannot be null");
            }

            Distance = distance;
        }
Exemple #24
0
        public IEnumerable <string> VisitFunctionStmt(Stmt.Function stmt)
        {
            string parameters = string.Join(", ", stmt.Parameter.Select(p => p.Lexeme));

            string[] body = stmt.Body.SelectMany(s => PrintStmt(s)).ToArray();

            yield return($"fun {stmt.Name}({parameters}) {{");

            foreach (string bodyStmt in body)
            {
                yield return($"  {bodyStmt}");
            }
            yield return("}");
        }
Exemple #25
0
        private void ResolveFunction(Stmt.Function function, FunctionType type)
        {
            FunctionType enclosingFunction = CurrentFunction;

            CurrentFunction = type;
            BeginScope();
            foreach (Token param in function.Params)
            {
                Declare(param);
                Define(param);
            }
            Resolve(function.Body);
            EndScope();
            CurrentFunction = enclosingFunction;
        }
Exemple #26
0
        private void resolveFunction(Stmt.Function function, FunctionType type)
        {
            FunctionType enclosingFunction = currentFunction;

            currentFunction = type;

            beginScope();
            foreach (Token param in function.params_)
            {
                declare(param);
                define(param);
            }
            resolve(function.body);
            endScope();
            currentFunction = enclosingFunction;
        }
Exemple #27
0
        private void ResolveFunction(Stmt.Function function, FunctionType type)
        {
            var enclosingFunction = currentFunction;

            currentFunction = type;

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

            currentFunction = enclosingFunction;
        }
Exemple #28
0
        /// <summary>
        /// Resolve a function, creating a scope and its parameters
        /// </summary>
        /// <param name="function">The function</param>
        private void ResolveFunction(Stmt.Function function, FunctionType type)
        {
            // Keep track of functions
            FunctionType enclosing_function = _current_function;

            _current_function = type;

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

            _current_function = enclosing_function;
        }
Exemple #29
0
        private void ResolveFunction(Stmt.Function function, FunctionType type)
        {
            FunctionType enclosingFunction = currentFunction;

            currentFunction = type;

            BeginScope();

            foreach (Parameter param in function.Parameters)
            {
                Declare(param.Name);
                Define(param.Name, new TypeReference(param.TypeSpecifier));
            }

            Resolve(function.Body);
            EndScope();

            currentFunction = enclosingFunction;
        }
Exemple #30
0
        private void DefineFunction(Token name, ITypeReference typeReference, Stmt.Function function)
        {
            if (typeReference == null)
            {
                throw new ArgumentException("typeReference cannot be null");
            }

            if (IsEmpty(scopes))
            {
                globals[name.Lexeme] = new FunctionBindingFactory(typeReference, function);
                return;
            }

            // We set the variable’s value in the scope map to mark it as fully initialized and available for
            // use. It’s alive! As an extra bonus, we store the type reference of the initializer (if present), or the
            // function return type and function statement (in case of a function being defined). These details are
            // useful later on, in the static type analysis.
            scopes.Last()[name.Lexeme] = new FunctionBindingFactory(typeReference, function);
        }