コード例 #1
0
        public IEnumerable <AsmLine> Translate(BinExpression be)
        {
            foreach (var op in Translate(be.Right))
            {
                yield return(op);
            }
            var rightResReg = _regUsed;

            foreach (var op in Translate(be.Left))
            {
                yield return(op);
            }
            var leftResReg = _regUsed;

            if (Comparators.Contains(be.Operation))
            {
                yield return(new AsmLine("cmp", $"#{leftResReg}#", $"#{rightResReg}#"));

                yield return(new AsmLine(OpToInstr[be.Operation], NewReg(), null));
            }
            else
            {
                yield return(new AsmLine(OpToInstr[be.Operation], $"#{leftResReg}#", $"#{rightResReg}#"));
            }
        }
コード例 #2
0
        public IEnumerable <AsmLine> TranslateAssign(BinExpression be)
        {
            string leftOp;

            switch (be.Left)
            {
            case UnExpression ue when ue.Operation == "*":
            {
                foreach (var op in Translate(ue.Operand))
                {
                    yield return(op);
                }
                leftOp = $"[#{_regUsed}#]";
                break;
            }

            case VarExpression ve:
                leftOp = $"{ve}";
                break;

            default:
                throw new Exception("lvalue incorrect");
            }

            foreach (var op in Translate(be.Right))
            {
                yield return(op);
            }
            var rightResReg = _regUsed;

            yield return(new AsmLine(OpToInstr[be.Operation], leftOp, $"#{rightResReg}#"));
        }
コード例 #3
0
ファイル: CodeGen.cs プロジェクト: Lexus1982/Translator
    private void GenAssign(Expression expression)
    {
        if (expression is BinExpression)
        {
            BinExpression binExpression = (BinExpression)expression;
            //Запишем левую часть выражения (число/переменная)
            accum.Append(string.Format("{0}", GetExprValue(binExpression.Left)));
            //Запишем знак операции
            switch (binExpression.Operation)
            {
            case BinOperation.Add: accum.Append("+"); break;

            case BinOperation.Sub: accum.Append("-"); break;

            case BinOperation.Mul: accum.Append("*"); break;

            case BinOperation.Div: accum.Append("/"); break;
            }
            //Обработаем правую часть выражения
            this.GenAssign(binExpression.Right);
        }
        else if (expression is Expression)
        {
            Expression expr = (Expression)expression;
            accum.Append(string.Format("{0}", GetExprValue(expr)));
        }
        else
        {
            throw new System.Exception("Error 5");
        }
    }
コード例 #4
0
    private BinExpression ParseBinExpr(Expression expr)
    {
        if (this.index == this.tokens.Count)
        {
            throw new System.Exception("expected expression, got EOF");
        }

        BinExpression binResult = new BinExpression();

        //Запишем первый операнд
        binResult.Left = expr;

        // Запишем операцию
        if (this.tokens[this.index] == Scanner.Add) //Сложение
        {
            binResult.Operation = BinOperation.Add;
        }
        else if (this.tokens[this.index] == Scanner.Div) //Сложение
        {
            binResult.Operation = BinOperation.Div;
        }
        else if (this.tokens[this.index] == Scanner.Mul) //Сложение
        {
            binResult.Operation = BinOperation.Mul;
        }
        else if (this.tokens[this.index] == Scanner.Sub) //Сложение
        {
            binResult.Operation = BinOperation.Sub;
        }
        else
        {
            throw new System.Exception("missing bin operator");
        }

        // Запишем второй операнд
        this.index++;
        binResult.Right = this.ParseExpr();

        if (this.tokens[this.index] != Scanner.Semi && this.index < this.tokens.Count)
        {
            binResult.Right = this.ParseBinExpr(binResult.Right);
        }

        return(binResult);
    }