Beispiel #1
0
    protected void ExecuteExprStmt(AstExprStmt stmt)
    {
        if (HadErrorOrReturn())
        {
            return;
        }

        EvaluateExpr(stmt.m_expr);
    }
 private void GenerateExprStatement(AstExprStmt expr)
 {
     GenerateExpression(expr.Expr, false);
     if (expr.Destructions != null)
     {
         foreach (var dest in expr.Destructions)
         {
             GenerateStatement(dest);
         }
     }
 }
        private AstExprStmt AnalyseExprStatement(AstExprStmt expr, bool allow_any_expr = false, bool infer_types = true)
        {
            expr.Expr.AttachTo(expr);

            if (infer_types)
            {
                expr.Expr = InferType(expr.Expr, null);
            }

            if (!allow_any_expr)
            {
                switch (expr.Expr)
                {
                case AstIfExpr _:
                case AstBlockExpr _:
                case AstCallExpr _:
                case AstCompCallExpr _:
                case AstMatchExpr _:
                case AstEmptyExpr _:
                case AstBreakExpr _:
                case AstContinueExpr _:
                case AstMoveAssignExpr _:
                    break;

                default:
                    ReportError(expr.Expr, $"This type of expression is not allowed here");
                    break;
                }
            }

            if (expr.Expr.Type.IsComptimeOnly)
            {
                ReportError(expr.Expr, $"This type of expression is not allowed here");
            }

            expr.SetFlag(StmtFlags.Returns, expr.Expr.GetFlag(ExprFlags.Returns));
            expr.SetFlag(StmtFlags.Breaks, expr.Expr.GetFlag(ExprFlags.Breaks));

            return(expr);
        }
Beispiel #4
0
 public override NodeFinderResult VisitExpressionStmt(AstExprStmt stmt, int data = 0)
 {
     return(stmt.Expr.Accept(this, data));
 }
Beispiel #5
0
 public virtual ReturnType VisitExpressionStmt(AstExprStmt stmt, DataType data          = default) => default;
Beispiel #6
0
 protected void ResolveExprStmt(AstExprStmt stmt)
 {
     ResolveExpr(stmt.m_expr);
 }
        private AstStatement AnalyseForStatement(AstForStmt fo)
        {
            fo.SubScope = new Scope("for", fo.Scope);

            fo.Collection.SetFlag(ExprFlags.ValueRequired, true);
            fo.Collection.AttachTo(fo);
            fo.Collection = InferType(fo.Collection, null);
            ConvertLiteralTypeToDefaultType(fo.Collection, null);

            if (fo.Collection.Type.IsErrorType)
            {
                return(fo);
            }

            fo.Body.AttachTo(fo);
            fo.Body.Scope = fo.SubScope;
            fo.Body       = InferType(fo.Body, CheezType.Code);

            var fors = fo.Scope.GetForExtensions(fo.Collection.Type);


            var matches = fors.Select(func =>
            {
                var args = new List <AstArgument>
                {
                    new AstArgument(fo.Collection, Location: fo.Collection),
                    new AstArgument(fo.Body, Location: fo.Body)
                };
                if (fo.Arguments != null)
                {
                    args.AddRange(fo.Arguments);
                }

                var par = func.Parameters.Select(p => (p.Name?.Name, p.Type, p.DefaultValue)).ToArray();
                if (CheckAndMatchArgsToParams(args, par, false))
                {
                    return(func, args);
                }
                return(null, null);
            }).Where(a => a.func != null).ToList();

            if (matches.Count == 0)
            {
                var candidates = fors.Select(f => ("Tried this candidate:", f.ParameterLocation));
                ReportError(fo.Collection, $"No for extension matches type '{fo.Collection.Type}'", candidates);
                return(fo);
            }
            else if (matches.Count > 1)
            {
                var candidates = matches.Select(f => ("This matches:", f.func.ParameterLocation));
                ReportError(fo.Collection, $"Multiple for extensions match type '{fo.Collection.Type}'", candidates);
                return(fo);
            }
            else
            {
                AstVariableDecl CreateLink(AstIdExpr name, AstExpression expr, ILocation location)
                {
                    var link = new AstCompCallExpr(
                        new AstIdExpr("link", false, location),
                        new List <AstArgument> {
                        new AstArgument(expr, Location: expr.Location)
                    },
                        location);

                    var type = mCompiler.ParseExpression($"@typeof(@link({expr}))", new Dictionary <string, AstExpression>
                    {
                        { "it", name }
                    });

                    var varDecl = new AstVariableDecl(name, type, link, true, Location: location);

                    return(varDecl);
                }

                var(func, args) = matches[0];
                var code  = args[1].Expr;
                var links = new List <AstStatement>();

                var it       = new AstIdExpr("it", false, fo.Location);
                var it_index = new AstIdExpr("it_index", false, fo.Location);

                // create links for it and it_index
                if (fo.VarName != null)
                {
                    links.Add(CreateLink(fo.VarName, it, fo.VarName.Location));
                }
                else
                {
                    links.Add(CreateLink(it, it.Clone(), it.Location));
                }

                if (fo.IndexName != null)
                {
                    links.Add(CreateLink(fo.IndexName, it_index, fo.IndexName.Location));
                }
                else
                {
                    links.Add(CreateLink(it_index, it_index.Clone(), it_index.Location));
                }

                // set break and continue
                if (fo.Label != null)
                {
                    var setBreakAndContinue = mCompiler.ParseStatement($"@set_break_and_continue({fo.Label.Name})");
                    links.Add(setBreakAndContinue);
                }

                // set value to null because it is not a code anymore
                code.TypeInferred = false;
                code.Value        = null;
                links.Add(new AstExprStmt(code, code.Location));
                args[1].Expr = new AstBlockExpr(links, Location: fo.Body.Location);

                var call     = new AstCallExpr(new AstFunctionRef(func, null, fo.Location), args, fo.Location);
                var exprStmt = new AstExprStmt(call, fo.Body.Location);
                exprStmt.Parent = fo.Parent;
                exprStmt.Scope  = fo.SubScope;
                var result = AnalyseStatement(exprStmt, out var ns);
                Debug.Assert(ns == null);
                return(result);
            }
        }
Beispiel #8
0
 public override string VisitExpressionStmt(AstExprStmt stmt, int indentLevel = 0)
 {
     return(stmt.Expr.Accept(this));
 }