Exemple #1
0
        public void Emit(IEmitter emit, Statement s)
        {
            if (s is WhileStatement ws)
            {
                var loop = emit.CreateLabel();
                var end  = emit.CreateLabel();

                emit.MarkLabel(loop);

                Emit(emit, ws.Condition);

                emit.BranchIfZero(end);

                Emit(emit, ws.Body);

                emit.Branch(loop);

                emit.MarkLabel(end);
            }
            else if (s is IfStatement ifs)
            {
                var falseLabel = emit.CreateLabel();
                var endLabel   = emit.CreateLabel();

                Emit(emit, ifs.Condition);

                emit.BranchIfZero(falseLabel);

                Emit(emit, ifs.TrueBody);

                if (ifs.FalseBody.Any())
                {
                    emit.Branch(endLabel);

                    emit.MarkLabel(falseLabel);

                    Emit(emit, ifs.FalseBody);

                    emit.MarkLabel(endLabel);
                }
                else
                {
                    emit.MarkLabel(falseLabel);
                    emit.MarkLabel(endLabel);
                }
            }
            else if (s is ReturnStatement ret)
            {
                if (ret.Result != null)
                {
                    Emit(emit, ret.Result);
                }

                emit.Return(ret.GetFunction());
            }
            else if (s is ExpressionStatement e)
            {
                Emit(emit, e.Expression);

                EmitPop(emit, ResolveOneType(e.Expression, e.Expression.ReturnType));
            }
            else if (!(s is LocalDeclarationStatement))
            {
                throw new SourceError(s, SourceError.UnknownStatement);
            }
        }