Esempio n. 1
0
        public object visitReturnStmt(Stmt.Return returnStmt)
        {
            captureToken(returnStmt.keyword);

            if (current.type == FunctionType.TYPE_SCRIPT)
            {
                error("Cannot return from top-level code.");
            }

            if (returnStmt.value == null)
            {
                emitReturn();
            }
            else
            {
                if (current.type == FunctionType.TYPE_INITIALIZER)
                {
                    error("Cannot return a value from an initializer.");
                }

                compile(returnStmt.value);
                emitByte(OpCode.OP_RETURN);
            }

            return(null);
        }
Esempio n. 2
0
 public IEnumerable <string> VisitReturnStmt(Stmt.Return stmt)
 {
     yield return(stmt.Value.Match(
                      some: value => $"return {value};",
                      none: () => "return;"
                      ));
 }
Esempio n. 3
0
        public override VoidObject VisitReturnStmt(Stmt.Return stmt)
        {
            bool sanityCheckFailed = false;

            if (stmt.Value != null)
            {
                if (!stmt.Value.TypeReference.IsResolved)
                {
                    TypeValidationErrorCallback(new TypeValidationError(
                                                    stmt.Keyword,
                                                    $"Internal compiler error: return {stmt.Value} inference has not been attempted"
                                                    ));

                    sanityCheckFailed = true;
                }
            }
            else
            {
                // No return value - ensure that the return type of the current function is 'void'.
            }

            if (sanityCheckFailed)
            {
                return(VoidObject.Void);
            }

            // TODO: Validate that return type in stmt.TypeReference matches that of the associated function.

            return(VoidObject.Void);
        }
Esempio n. 4
0
        public bool VisitReturnStatement(Stmt.Return statement)
        {
            if (statement.Value == null)
            {
                if (CurrentFunction.ReturnType.Type == TypeEnum.VOID)
                {
                    return(true);
                }

                return(false);
            }

            var sr = Examine(statement.Value);

            if (sr.ImplicitCastableTo(CurrentFunction.ReturnType))
            {
                return(true);
            }

            if (sr != CurrentFunction.ReturnType)
            {
                Console.WriteLine("Mismatched return type");
                return(false);
            }

            return(true);
        }
Esempio n. 5
0
        public LoxVoid VisitReturnStmt(Stmt.Return stmt)
        {
            object value = stmt.Value.HasValue
                ? Evalutate(stmt.Value.ValueOrFailure())
                : null;

            throw new Return(value);
        }
Esempio n. 6
0
 public Unit VisitReturnStmt(Stmt.Return stmt)
 {
     if (stmt.Value != null)
     {
         Resolve(stmt.Value);
     }
     return(new Unit());
 }
Esempio n. 7
0
 public object VisitReturnStmt(Stmt.Return stmt, object options)
 {
     if (stmt.value == null)
     {
         return("(return)");
     }
     return(Parenthesize("return", stmt.value));
 }
Esempio n. 8
0
        public string VisitReturnStmt(Stmt.Return stmt)
        {
            if (stmt.Value == null)
            {
                return("(return)");
            }

            return(Parenthesize("return", stmt.Value));
        }
Esempio n. 9
0
        public object VisitReturnStmt(Stmt.Return stmt)
        {
            object value = null;

            if (stmt.Value != null)
            {
                value = Evaluate(stmt.Value);
            }
            throw new Return(value);
        }
Esempio n. 10
0
        public object Visit(Stmt.Return stmt)
        {
            // Set to null if nothing returned
            object value = null;

            if (stmt.Value != null)
            {
                value = Evaluate(stmt.Value);
            }

            throw new ReturnException(value);
        }
Esempio n. 11
0
 public Unit VisitReturnStmt(Stmt.Return stmt)
 {
     if (CurrentFunction == FunctionType.NONE)
     {
         CSLox.Error(stmt.Keyword, "Can't return from top-level code");
     }
     if (stmt.Value != null)
     {
         Resolve(stmt.Value);
     }
     return(new Unit());
 }
Esempio n. 12
0
        public VoidObject VisitReturnStmt(Stmt.Return stmt)
        {
            if (currentFunction == FunctionType.NONE)
            {
                nameResolutionErrorHandler(new NameResolutionError("Cannot return from top-level code.", stmt.Keyword));
            }

            if (stmt.Value != null)
            {
                Resolve(stmt.Value);
            }

            return(VoidObject.Void);
        }
Esempio n. 13
0
        public LoxVoid VisitReturnStmt(Stmt.Return stmt)
        {
            if (_currentFunction is FunctionType.None)
            {
                Lox.Error(stmt.Keyword, "Cannot return from top-level code.");
            }

            stmt.Value.MatchSome(expr => {
                if (_currentFunction == FunctionType.Initializer)
                {
                    Lox.Error(stmt.Keyword, "Cannot return a value from an initializer");
                }

                Resolve(expr);
            });
            return(null);
        }
Esempio n. 14
0
        public Stmt VisitReturnStatement(Stmt.Return statement)
        {
            if (currentFunction.ReturnType.Type == TypeEnum.VOID)
            {
                LLVM.BuildRetVoid(_builder);
            }
            else
            {
                var type  = Visit(statement.Value);
                var value = _valueStack.Pop();

                var casted = CheckedCast(value, type, currentFunction.ReturnType);
                LLVM.BuildRet(_builder, casted);
            }

            return(null);
        }
Esempio n. 15
0
        public Unit VisitReturnStmt(Stmt.Return stmt)
        {
            if (CurrentFunction == FunctionType.NONE)
            {
                CSLox.Error(stmt.Keyword, "Can't return from top-level code");
            }
            if (stmt.Value != null)
            {
                // Small hack to stop 'erberts return from a constructor.
                // Terminates current instancec
                if (CurrentFunction == FunctionType.INITIALISER)
                {
                    CSLox.Error(stmt.Keyword, "Can't return a value from an initialiser.");
                }

                Resolve(stmt.Value);
            }
            return(new Unit());
        }
Esempio n. 16
0
        public object VisitReturnStmt(Stmt.Return stmt)
        {
            if (_currentFunction == FunctionType.None)
            {
                Lox.Error(stmt.keyword, "Can't return from top-level code");
            }

            if (_currentFunction == FunctionType.Initializer && stmt.value is not null)
            {
                Lox.Error(stmt.keyword, "Can't return a value from an initializer.");
            }

            if (stmt.value != null)
            {
                Resolve(stmt.value);
            }

            return(null);
        }
Esempio n. 17
0
        public object VisitReturnStmt(Stmt.Return stmt)
        {
            if (currentFunction == FunctionType.NONE)
            {
                this.compilerError?.AddError("Can't reach return. No function defined");
            }

            if (stmt.expr != null)
            {
                if (currentFunction == FunctionType.INITIALIZER)
                {
                    this.compilerError?.AddError("can't return from an initializer function");
                }

                Resolve(stmt.expr);
            }

            return(new Stmt.DefaultStatement());
        }
Esempio n. 18
0
        public object visitReturnStmt(Stmt.Return stmt)
        {
            if (currentFunction == FunctionType.TYPE_NONE)
            {
                Interpreter.error(stmt.keyword, "Cannot return from top-level code.");
            }

            if (stmt.value != null)
            {
                if (currentFunction == FunctionType.TYPE_INITIALIZER)
                {
                    Interpreter.error(stmt.keyword, "Cannot return a value from an initializer.");
                }

                resolve(stmt.value);
            }

            return(null);
        }
Esempio n. 19
0
        /// <summary>
        /// Resolve a return statement
        /// </summary>
        /// <param name="stmt">The statement</param>
        /// <returns></returns>
        public object Visit(Stmt.Return stmt)
        {
            // Make sure we are in a function
            if (_current_function == FunctionType.NONE)
            {
                _error_handler.Error(stmt.Keyword, "Cannot return from top-level code.");
            }


            if (stmt.Value != null)
            {
                // Check this is not in a initializer
                if (_current_function == FunctionType.INITIALIZER)
                {
                    _error_handler.Error(stmt.Keyword, "Cannot return from an initializer.");
                }

                Resolve(stmt.Value);
            }
            return(null);
        }
Esempio n. 20
0
        public object VisitReturnStmt(Stmt.Return stmt)
        {
            if (currentFunction == FunctionType.NONE)
            {
                throw new InterpretingException(stmt.Keyword,
                                                "Can't return from top-level code.");
            }

            if (stmt.Value != null)
            {
                if (currentFunction == FunctionType.INITIALIZER)
                {
                    throw new InterpretingException(stmt.Keyword,
                                                    "Can't return a value from an initializer.");
                }

                Resolve(stmt.Value);
            }

            return(null);
        }
Esempio n. 21
0
 public Stmt VisitReturnStmt(Stmt.Return stmt)
 {
     throw new NotImplementedException();
 }
Esempio n. 22
0
 public static TrashObject ReturnStmt(this Interpreter interpreter, Stmt.Return stmt)
 {
     return(interpreter.Evaluate(stmt.Value));
 }
Esempio n. 23
0
 public TrashObject VisitReturnStmt(Stmt.Return stmt)
 {
     return(this.ReturnStmt(stmt));
 }
Esempio n. 24
0
 public object VisitReturnStmt(Stmt.Return stmt, object options = null)
 {
     return(null);
 }
Esempio n. 25
0
 public object VisitBreakStmt(Stmt.Break stmt)
 {
     Stmt.Return returnStatement = new Stmt.Return(null, null);
     return(VisitReturnStmt(returnStatement));
 }