Beispiel #1
0
        static void Test3()
        {
            Compiler      c     = new Compiler();
            List <string> lVars = new List <string>();

            lVars.Add("var int x;");
            lVars.Add("var int y;");
            lVars.Add("var int z;");
            List <VarDeclaration> vars = c.ParseVarDeclarations(lVars);

            string       s          = "let x = ((x + 5) + (y - z));";
            List <Token> lTokens    = c.Tokenize(s, 0);
            LetStatement assignment = c.ParseStatement(lTokens);

            List <LetStatement> lSimple   = c.SimplifyExpressions(assignment, vars);
            List <string>       lAssembly = c.GenerateCode(lSimple, vars);

            CPUEmulator cpu = new CPUEmulator();

            InitLCL(lAssembly);
            cpu.Code = lAssembly;
            cpu.Run(1000, false);
            if (cpu.M[20] != 5)
            {
                Console.WriteLine("BUGBUG");
            }
        }
Beispiel #2
0
        private void Simplfy(List <LetStatement> lState1, int funcName, LetStatement s, List <VarDeclaration> lVars)
        {
            bool Op = false;

            if (((BinaryOperationExpression)s.Value).Operand1 is BinaryOperationExpression)
            {
                Op = true;
                LetStatement x = new LetStatement();
                x.Value    = ((BinaryOperationExpression)s.Value).Operand1;
                x.Variable = "_" + funcName;
                VarDeclaration vd = new VarDeclaration("int", x.Variable);
                lVars.Add(vd);
                funcName++;
                Simplfy(lState1, funcName, x, lVars);
            }
            if (((BinaryOperationExpression)s.Value).Operand2 is BinaryOperationExpression)
            {
                Op = true;
                LetStatement y = new LetStatement();
                y.Value    = ((BinaryOperationExpression)s.Value).Operand2;
                y.Variable = "_" + funcName;
                VarDeclaration vd = new VarDeclaration("int", y.Variable);
                lVars.Add(vd);
                funcName++;
                Simplfy(lState1, funcName, y, lVars);
            }
            if (!Op)
            {
                lState1.Add(s);
            }
        }
        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();

            //add here code for computing a single let statement containing only a simple expression
            return(lAssembly);
        }
Beispiel #4
0
        //called after initVirtualIds(...)
        private void initLetStatements(Expression expression, Dictionary <Expression, int> registers, Stack <LetStatement> stack)
        {
            LetStatement letStatement = new LetStatement();

            letStatement.Variable = "_" + registers[expression];

            if (expression is VariableExpression || expression is NumericExpression)
            {
                letStatement.Value = expression;
                stack.Push(letStatement);
            }
            else
            {
                BinaryOperationExpression binExp = (BinaryOperationExpression)expression;

                BinaryOperationExpression value   = new BinaryOperationExpression();
                VariableExpression        varExp1 = new VariableExpression();
                VariableExpression        varExp2 = new VariableExpression();
                value.Operator     = binExp.Operator;
                varExp1.Name       = "_" + registers[binExp.Operand1];
                varExp2.Name       = "_" + registers[binExp.Operand2];
                value.Operand1     = varExp1;
                value.Operand2     = varExp2;
                letStatement.Value = value;
                stack.Push(letStatement);
                initLetStatements(binExp.Operand1, registers, stack);
                initLetStatements(binExp.Operand2, registers, stack);
            }
        }
Beispiel #5
0
        static void Test1()
        {
            Compiler      c     = new Compiler();
            List <string> lVars = new List <string>();

            lVars.Add("var int x;");
            List <VarDeclaration> vars = c.ParseVarDeclarations(lVars);

            string       s          = "let x = 5;";
            List <Token> lTokens    = c.Tokenize(s, 0);
            LetStatement assignment = c.ParseStatement(lTokens);

            if (assignment.ToString() != s)
            {
                Console.WriteLine("BUGBUG");
            }


            List <LetStatement> l = new List <LetStatement>();

            l.Add(assignment);
            List <string> lAssembly = c.GenerateCode(l, vars);
            CPUEmulator   cpu       = new CPUEmulator();

            InitLCL(lAssembly);
            cpu.Code = lAssembly;
            cpu.Run(1000, false);
            if (cpu.M[20] != 5)
            {
                Console.WriteLine("BUGBUG");
            }
        }
Beispiel #6
0
        public LetStatement makeLetStatement(string var, Expression value)
        {
            LetStatement output = new LetStatement();

            output.Variable = var;
            output.Value    = value;
            return(output);
        }
Beispiel #7
0
        public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
        {
            //add here code to simply expressins in a statement.
            //add var declarations for artificial variables.
            var        newLetStatements = new List <LetStatement>();
            Expression expression       = s.Value;

            s.Value = SimplifyExpressions(expression, newLetStatements, lVars, true);
            newLetStatements.Add(s);
            return(newLetStatements);
        }
        public LetStatement ParseStatement(List <Token> lTokens)
        {
            TokensStack sTokens = new TokensStack();

            for (int i = lTokens.Count - 1; i >= 0; i--)
            {
                sTokens.Push(lTokens[i]);
            }
            LetStatement s = new LetStatement();

            s.Parse(sTokens);
            return(s);
        }
Beispiel #9
0
        public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
        {
            List <LetStatement> letStatements = new List <LetStatement>();

            //LetStatement temp = s;
            //add here code to simply expressins in a statement.
            //add var declarations for artificial variables.


            if (s.Value is NumericExpression || s.Value is VariableExpression)
            {
                letStatements.Add(s);
            }
            else if (s.Value is BinaryOperationExpression)
            {
                if (((BinaryOperationExpression)s.Value).Operand1 is BinaryOperationExpression)
                {
                    VarDeclaration var_dec = new VarDeclaration("int", "_" + (lVars.Count + 1));
                    lVars.Add(var_dec);
                    LetStatement left_let_statement = new LetStatement();
                    left_let_statement.Variable = var_dec.Name;
                    left_let_statement.Value    = ((BinaryOperationExpression)s.Value).Operand1;

                    VariableExpression _var = new VariableExpression();
                    _var.Name = var_dec.Name;
                    ((BinaryOperationExpression)s.Value).Operand1 = _var;
                    letStatements.AddRange(SimplifyExpressions(left_let_statement, lVars));
                }


                if (((BinaryOperationExpression)s.Value).Operand2 is BinaryOperationExpression)
                {
                    VarDeclaration var_dec = new VarDeclaration("int", "_" + (lVars.Count + 1));
                    lVars.Add(var_dec);
                    LetStatement left_let_statement = new LetStatement();
                    left_let_statement.Variable = var_dec.Name;
                    left_let_statement.Value    = ((BinaryOperationExpression)s.Value).Operand2;



                    VariableExpression _var = new VariableExpression();
                    _var.Name = var_dec.Name;
                    ((BinaryOperationExpression)s.Value).Operand2 = _var;
                    letStatements.AddRange(SimplifyExpressions(left_let_statement, lVars));
                }
                Console.WriteLine("" + s.ToString());
                letStatements.Add(s);
            }
            return(letStatements);
        }
Beispiel #10
0
        public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
        {
            //add here code to simply expressins in a statement.
            List <LetStatement> lState1 = new List <LetStatement>();

            if (s.Value is BinaryOperationExpression)
            {
                Simplfy(lState1, 1, s, lVars);
            }
            else
            {
                lState1.Add(s);
            }
            return(lState1);

            //add var declarations for artificial variables
        }
Beispiel #11
0
        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();

            //add here code for computing a single let statement containing only a simple expression

            CopyToVirtualRegister(lAssembly, dSymbolTable, aSimple.Value, "RESULT");
            PutLocalVarAddressToRegister(lAssembly, dSymbolTable, aSimple.Variable, "D", aSimple.VariableToken);
            lAssembly.Add("@ADDRESS");
            lAssembly.Add("M=D");
            lAssembly.Add("@RESULT");
            lAssembly.Add("D=M");
            lAssembly.Add("@ADDRESS");
            lAssembly.Add("A=M");
            lAssembly.Add("M=D");

            return(lAssembly);
        }
        public List <LetStatement> ParseAssignments(List <string> lLines)
        {
            List <LetStatement> lParsed = new List <LetStatement>();
            List <Token>        lTokens = Tokenize(lLines);
            TokensStack         sTokens = new TokensStack();

            for (int i = lTokens.Count - 1; i >= 0; i--)
            {
                sTokens.Push(lTokens[i]);
            }
            while (sTokens.Count > 0)
            {
                LetStatement ls = new LetStatement();
                ls.Parse(sTokens);
                lParsed.Add(ls);
            }
            return(lParsed);
        }
Beispiel #13
0
        static void TestSimplifyLetStatement()
        {
            List <VarDeclaration> varDecs = new List <VarDeclaration>();

            char chr = 'a';

            while (chr <= 'g')
            {
                VarDeclaration varDec = new VarDeclaration(null, "" + chr);
                varDecs.Add(varDec);
                chr++;
            }
            VarDeclaration x = new VarDeclaration(null, "x");

            varDecs.Add(x);
            string strExpression = "(((a + b) - (c - d)) + (e - (f + g)))";

            Compiler     c          = new Compiler();
            List <Token> tokensList = c.Tokenize(strExpression, 0);
            TokensStack  tokens     = new TokensStack();

            for (int i = tokensList.Count - 1; i >= 0; i--)
            {
                tokens.Push(tokensList[i]);
            }
            LetStatement example    = new LetStatement();
            Expression   expression = Expression.Create(tokens);

            expression.Parse(tokens);
            LetStatement let = new LetStatement();

            let.Variable = "x";
            let.Value    = expression;
            List <LetStatement> letStatements = c.SimplifyExpressions(let, varDecs);

            for (int i = 0; i < letStatements.Count; i++)
            {
                Console.WriteLine(letStatements[i]);
            }

            Console.WriteLine("Simplifying Expressions completetd");
        }
 public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
 {
     //add here code to simply expressins in a statement.
     //add var declarations for artificial variables.
     return(null);
 }
Beispiel #15
0
        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();

            lAssembly.Add("//generation started" + aSimple);

            //add here code for computing a single let statement containing only a simple expression
            Expression value    = aSimple.Value;
            string     variable = aSimple.Variable;

            if (value is NumericExpression | value is VariableExpression)
            {
                lAssembly.AddRange(GenerateCodeForUnaryAssignment(aSimple, dSymbolTable));
            }
            else if (value is BinaryOperationExpression)
            {
                lAssembly.Add("//Binary Expression" + aSimple);
                //
                //Example: a = b <op> c
                //first let a=b
                //next  let a=a <op> c
                //
                BinaryOperationExpression binExp = (BinaryOperationExpression)value;
                Expression   operand1            = binExp.Operand1;
                Expression   operand2            = binExp.Operand2;
                LetStatement letAEqualB          = makeLetStatement(variable, operand1);

                //let a=b
                lAssembly.AddRange(GenerateCodeForUnaryAssignment(letAEqualB, dSymbolTable));
                lAssembly.Add("@" + RESULT);
                lAssembly.Add("D=M");
                lAssembly.Add("@" + OPERAND1);
                lAssembly.Add("M=D");


                //let a = a <op> c
                if (operand2 is NumericExpression)
                {
                    lAssembly.Add("//Opearand 2 is Numeric " + aSimple);

                    NumericExpression num = (NumericExpression)operand2;
                    lAssembly.Add("@" + num.Value);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@_a");
                    lAssembly.Add("M=M" + binExp.Operator + "D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@" + RESULT);
                    lAssembly.Add("M=D");
                }
                else if (operand2 is VariableExpression)
                {
                    lAssembly.Add("//Opearand 2 is a Variable " + aSimple);

                    VariableExpression c = (VariableExpression)operand2;
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@" + dSymbolTable[c.Name]);
                    lAssembly.Add("D=M");
                    lAssembly.Add("@" + dSymbolTable[variable]);
                    lAssembly.Add("M=M" + binExp.Operator + "D");
                    lAssembly.Add("D=M");

                    lAssembly.Add("@" + RESULT);
                    lAssembly.Add("M=D");
                }
            }


            return(lAssembly);
        }
Beispiel #16
0
        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();

            if (!dSymbolTable.ContainsKey(aSimple.Variable))
            {
                throw new SyntaxErrorException("the var isn't exist", new Token());
            }
            if (aSimple.Value is BinaryOperationExpression)
            {
                //case 1: number,number-
                if (AllIsNumeric(((BinaryOperationExpression)aSimple.Value).Operand1 + "") && AllIsNumeric(((BinaryOperationExpression)aSimple.Value).Operand2 + ""))
                {
                    CopyToOperand1(((BinaryOperationExpression)aSimple.Value).Operand1 + "", lAssembly);
                    CopyToOperand2(((BinaryOperationExpression)aSimple.Value).Operand2 + "", lAssembly);
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    if (((BinaryOperationExpression)aSimple.Value).Operator == "+")
                    {
                        lAssembly.Add("D=D+M");
                    }
                    else
                    {
                        lAssembly.Add("D=M-D");
                    }
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
                //case 2: variable,number-
                if (AllStringIsChars((((BinaryOperationExpression)aSimple.Value).Operand1 + "")[0] + "") && AllStringIsCharsOr_OrNumber((((BinaryOperationExpression)aSimple.Value).Operand1 + "").Remove(0, 1)) && AllIsNumeric(((BinaryOperationExpression)aSimple.Value).Operand2 + ""))
                {
                    CopyToOperand1(((BinaryOperationExpression)aSimple.Value).Operand1 + "", lAssembly);
                    CopyVarToOperand2(((BinaryOperationExpression)aSimple.Value).Operand2 + "", dSymbolTable, lAssembly);
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    if (((BinaryOperationExpression)aSimple.Value).Operator == "+")
                    {
                        lAssembly.Add("D=D+M");
                    }
                    else
                    {
                        lAssembly.Add("D=M-D");
                    }
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
                //case 3: number,variable-
                if (AllStringIsChars((((BinaryOperationExpression)aSimple.Value).Operand2 + "")[0] + "") && AllStringIsCharsOr_OrNumber((((BinaryOperationExpression)aSimple.Value).Operand2 + "").Remove(0, 1)) && AllIsNumeric(((BinaryOperationExpression)aSimple.Value).Operand1 + ""))
                {
                    CopyVarToOperand1(((BinaryOperationExpression)aSimple.Value).Operand1 + "", dSymbolTable, lAssembly);
                    CopyToOperand2(((BinaryOperationExpression)aSimple.Value).Operand2 + "", lAssembly);
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    if (((BinaryOperationExpression)aSimple.Value).Operator == "+")
                    {
                        lAssembly.Add("D=D+M");
                    }
                    else
                    {
                        lAssembly.Add("D=M-D");
                    }
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
                //case 4: variable,variable-
                if (AllStringIsChars((((BinaryOperationExpression)aSimple.Value).Operand2 + "")[0] + "") && AllStringIsCharsOr_OrNumber((((BinaryOperationExpression)aSimple.Value).Operand2 + "").Remove(0, 1)) && AllStringIsChars((((BinaryOperationExpression)aSimple.Value).Operand1 + "")[0] + "") && AllStringIsCharsOr_OrNumber((((BinaryOperationExpression)aSimple.Value).Operand1 + "").Remove(0, 1)))
                {
                    CopyVarToOperand1(((BinaryOperationExpression)aSimple.Value).Operand1 + "", dSymbolTable, lAssembly);
                    CopyVarToOperand2(((BinaryOperationExpression)aSimple.Value).Operand2 + "", dSymbolTable, lAssembly);
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    if (((BinaryOperationExpression)aSimple.Value).Operator == "+")
                    {
                        lAssembly.Add("D=D+M");
                    }
                    else
                    {
                        lAssembly.Add("D=M-D");
                    }
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
            }

            return(lAssembly);
        }
Beispiel #17
0
        public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
        {
            //add here code to simply expressins in a statement.
            //add var declarations for artificial variables.
            //check that all vars appear in vardeclarations
            List <LetStatement> output = new List <LetStatement>();
            //first check that all vars in expression were declared;
            List <string> varNames = new List <string>();

            foreach (VarDeclaration varDec in lVars)
            {
                varNames.Add(varDec.Name);
            }
            if (!varNames.Contains(s.Variable))
            {
                throw new SyntaxErrorException(" the variable " + s.Variable + " was never defined in expression " + s, new Token());
            }
            CheckThatAllVariablesWereDeclared(s.Value, varNames);

            if (s.Value is BinaryOperationExpression)
            {
                //init data
                numVirtualVars = 0;
                Dictionary <Expression, int> registers = new Dictionary <Expression, int>();
                initVirtualIds(s.Value, registers);
                Stack <LetStatement> lets = new Stack <LetStatement>();
                initLetStatements(s.Value, registers, lets);


                BinaryOperationExpression binExp = (BinaryOperationExpression)(s.Value);
                BinaryOperationExpression binVal = new BinaryOperationExpression();

                // let OPERATOR1 = _xxx
                VariableExpression var1 = makeVariable("_" + registers[binExp.Operand1]);
                binVal.Operand1 = var1;
                LetStatement letOP1 = makeLetStatement(OPERAND1, var1);

                // let OPERATOR2 = _yyy
                VariableExpression var2 = makeVariable("_" + registers[binExp.Operand2]);
                binVal.Operand2 = var2;
                LetStatement letOP2 = makeLetStatement(OPERAND2, var2);

                // let RESULT= _1 and variable=RESULT
                LetStatement letResult = makeLetStatement(RESULT, makeVariable("_" + registers[s.Value]));
                LetStatement letVar    = makeLetStatement(s.Variable, makeVariable(RESULT));



                while (lets.Count > 0)
                {
                    output.Add(lets.Pop());
                }

                output.Add(letOP1);
                output.Add(letOP2);
                output.Add(letResult);
                output.Add(letVar);
            }
            else
            {
                //need to put in RESULT???
                output.Add(s);
            }

            return(output);
        }
Beispiel #18
0
        private List <string> GenerateCodeForUnaryAssignment(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();
            //add here code for computing a single let statement containing only a simple expression
            Expression value = aSimple.Value;

            if (value is NumericExpression)
            {
                lAssembly.Add("//Numeric Value " + aSimple);

                NumericExpression numExp = (NumericExpression)value;

                //next 8 lines rep @var
                lAssembly.Add("@LCL");
                lAssembly.Add("D=M");
                lAssembly.Add("@" + dSymbolTable[aSimple.Variable]);
                lAssembly.Add("D=A+D");//D=a's address
                lAssembly.Add("@_a");
                lAssembly.Add("M=D");
                lAssembly.Add("@" + RESULT);
                lAssembly.Add("M=D");

                lAssembly.Add("@" + numExp.Value);
                lAssembly.Add("D=A");
                lAssembly.Add("@_a");
                lAssembly.Add("A=M");
                lAssembly.Add("M=D");
                lAssembly.Add("@" + RESULT);
                lAssembly.Add("M=D");
            }
            else if (value is VariableExpression)
            {
                lAssembly.Add("//Variable value" + aSimple);

                //Example: let a = b;
                //LCL[a]=LCL[b]
                VariableExpression valExp = (VariableExpression)value;
                int valuesIndex           = dSymbolTable[valExp.ToString()];

                //first store value of LCL[b] in RESULT
                lAssembly.Add("@LCL");
                lAssembly.Add("D=M");
                lAssembly.Add("@" + dSymbolTable[valExp.ToString()]);
                lAssembly.Add("A=D+A");
                lAssembly.Add("D=M");
                lAssembly.Add("@" + RESULT);
                lAssembly.Add("M=D");

                //next store a's address in virtual register "_a"
                lAssembly.Add("@LCL");
                lAssembly.Add("D=M");
                lAssembly.Add("@" + dSymbolTable[valExp.ToString()]);
                lAssembly.Add("D=D+A");
                lAssembly.Add("@_a");
                lAssembly.Add("M=D");

                //update "a"
                lAssembly.Add("@" + RESULT);
                lAssembly.Add("D=M");
                lAssembly.Add("@_a");
                lAssembly.Add("A=M");
                lAssembly.Add("M=D");
            }

            return(lAssembly);
        }
Beispiel #19
0
        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            //Console.WriteLine("HI------------------------------------------------------------");
            List <string> lAssembly = new List <string>();


            if (aSimple.Value is NumericExpression)
            {
                lAssembly.Add("@" + ((NumericExpression)aSimple.Value).Value.ToString());
                lAssembly.Add("D=A");
                lAssembly.Add("@RESULT");
                lAssembly.Add("M=D");
            }

            else if (aSimple.Value is BinaryOperationExpression)
            {
                if ((((BinaryOperationExpression)aSimple.Value).Operand1 is NumericExpression) && (((BinaryOperationExpression)aSimple.Value).Operand2 is NumericExpression))
                {
                    lAssembly.Add("@" + ((BinaryOperationExpression)aSimple.Value).Operand1);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("M=D");

                    lAssembly.Add("@" + ((BinaryOperationExpression)aSimple.Value).Operand2);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@OPERAND2");
                    lAssembly.Add("M=D");
                }
                else if ((((BinaryOperationExpression)aSimple.Value).Operand1 is VariableExpression) && (((BinaryOperationExpression)aSimple.Value).Operand2 is VariableExpression))
                {
                    if (!dSymbolTable.ContainsKey(((BinaryOperationExpression)aSimple.Value).Operand1.ToString()))
                    {
                        Identifier id = new Identifier(((BinaryOperationExpression)aSimple.Value).Operand1.ToString(), 0, 0);
                        throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                    }
                    lAssembly.Add("@" + dSymbolTable[((BinaryOperationExpression)aSimple.Value).Operand1.ToString()]);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=D+M");
                    lAssembly.Add("A=D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("M=D");


                    if (!dSymbolTable.ContainsKey(((BinaryOperationExpression)aSimple.Value).Operand2.ToString()))
                    {
                        Identifier id = new Identifier(((BinaryOperationExpression)aSimple.Value).Operand2.ToString(), 0, 0);
                        throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                    }
                    lAssembly.Add("@" + dSymbolTable[((BinaryOperationExpression)aSimple.Value).Operand2.ToString()]);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=D+M");
                    lAssembly.Add("A=D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND2");
                    lAssembly.Add("M=D");
                }
                else if ((((BinaryOperationExpression)aSimple.Value).Operand1 is VariableExpression) && (((BinaryOperationExpression)aSimple.Value).Operand2 is NumericExpression))
                {
                    if (!dSymbolTable.ContainsKey(((BinaryOperationExpression)aSimple.Value).Operand1.ToString()))
                    {
                        Identifier id = new Identifier(((BinaryOperationExpression)aSimple.Value).Operand1.ToString(), 0, 0);
                        throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                    }
                    lAssembly.Add("@" + dSymbolTable[((BinaryOperationExpression)aSimple.Value).Operand1.ToString()]);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=D+M");
                    lAssembly.Add("A=D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("M=D");


                    lAssembly.Add("@" + ((BinaryOperationExpression)aSimple.Value).Operand2);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@OPERAND2");
                    lAssembly.Add("M=D");
                }
                else if ((((BinaryOperationExpression)aSimple.Value).Operand1 is NumericExpression) && (((BinaryOperationExpression)aSimple.Value).Operand2 is VariableExpression))
                {
                    lAssembly.Add("@" + ((BinaryOperationExpression)aSimple.Value).Operand1);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("M=D");

                    if (!dSymbolTable.ContainsKey(((BinaryOperationExpression)aSimple.Value).Operand2.ToString()))
                    {
                        Identifier id = new Identifier(((BinaryOperationExpression)aSimple.Value).Operand2.ToString(), 0, 0);
                        throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                    }
                    lAssembly.Add("@" + dSymbolTable[((BinaryOperationExpression)aSimple.Value).Operand2.ToString()]);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=D+M");
                    lAssembly.Add("A=D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND2");
                    lAssembly.Add("M=D");
                }
                else
                {
                    throw new Exception("This kind of expressions is not supported yet!");
                }



                /* This part is fixed! */
                lAssembly.Add("@OPERAND1");
                lAssembly.Add("D=M");
                lAssembly.Add("@OPERAND2");
                lAssembly.Add("D=D" + ((BinaryOperationExpression)aSimple.Value).Operator + "M");
                lAssembly.Add("@RESULT");
                lAssembly.Add("M=D");
            }

            else if (aSimple.Value is UnaryOperatorExpression)
            {
                if (((UnaryOperatorExpression)aSimple.Value).Operand is VariableExpression)
                {
                    if (!dSymbolTable.ContainsKey(((UnaryOperatorExpression)aSimple.Value).Operand.ToString()))
                    {
                        Identifier id = new Identifier(((UnaryOperatorExpression)aSimple.Value).Operand.ToString(), 0, 0);
                        throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                    }
                    lAssembly.Add("@" + dSymbolTable[(((UnaryOperatorExpression)aSimple.Value).Operand).ToString()]);
                    lAssembly.Add("D=A");
                    lAssembly.Add("@LCL");
                    lAssembly.Add("D=D+M");
                    lAssembly.Add("A=D");
                    lAssembly.Add("D=M");
                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("M=D");


                    lAssembly.Add("@OPERAND1");
                    lAssembly.Add("D=M");
                    lAssembly.Add("D=" + ((UnaryOperatorExpression)aSimple.Value).Operator + "D");
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
                else if (((UnaryOperatorExpression)aSimple.Value).Operand is NumericExpression)
                {
                    lAssembly.Add("@" + (((UnaryOperatorExpression)aSimple.Value).Operand).ToString());
                    lAssembly.Add("D=A");



                    lAssembly.Add("D=A");
                    lAssembly.Add("D=" + ((UnaryOperatorExpression)aSimple.Value).Operator + "D");
                    lAssembly.Add("@RESULT");
                    lAssembly.Add("M=D");
                }
                else
                {
                    throw new Exception("This kind of expressions is not supported yet!");
                }
            }

            else if (aSimple.Value is VariableExpression)
            {
                //throw new Exception("Heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey!");

                if (!dSymbolTable.ContainsKey(((VariableExpression)aSimple.Value).Name.ToString()))
                {
                    Identifier id = new Identifier(((VariableExpression)aSimple.Value).Name.ToString(), 0, 0);
                    throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
                }
                lAssembly.Add("@" + dSymbolTable[((VariableExpression)aSimple.Value).Name]);
                lAssembly.Add("D=A");
                lAssembly.Add("@LCL");
                lAssembly.Add("D=D+M");
                lAssembly.Add("A=D");
                lAssembly.Add("D=M");
                lAssembly.Add("@RESULT");
                lAssembly.Add("M=D");
            }
            else
            {
                throw new Exception("This kind of expressions is not supported yet!");
            }


            if (!dSymbolTable.ContainsKey(aSimple.Variable))
            {
                Identifier id = new Identifier(aSimple.Variable, 0, 0);
                throw new SyntaxErrorException(id.ToString() + "$was not found!", id);
            }
            lAssembly.Add("@" + dSymbolTable[aSimple.Variable]);
            lAssembly.Add("D=A");
            lAssembly.Add("@LCL");
            lAssembly.Add("D=D+M");
            lAssembly.Add("@ADDRESS");
            lAssembly.Add("M=D");
            lAssembly.Add("D=M");
            lAssembly.Add("@RESULT");
            lAssembly.Add("D=M");
            lAssembly.Add("@ADDRESS");
            lAssembly.Add("A=M");
            lAssembly.Add("M=D");

            return(lAssembly);
        }
Beispiel #20
0
/*
 *
 *
 * Impelementtt!!!!
 */


        public List <string> GenerateCode(LetStatement aSimple, Dictionary <string, int> dSymbolTable)
        {
            List <string> lAssembly = new List <string>();

            ///////////////NumericExpression
            if (aSimple.Value is NumericExpression)
            {
                int exp = aSimple.Value;

                lAssembly.Add("@" + exp);

                lAssembly.Add("D=A");

                lAssembly.Add("@RESULT");

                lAssembly.Add("M=D");

                lAssembly.AddRange(ResultTOtable(aSimple.Variable, dSymbolTable));
            }
//////////////////VariableExpression
            if (aSimple.Value is VariableExpression)
            {
                int exp = ComputeExpression(aSimple.Value, dSymbolTable);

                lAssembly.AddRange(VariableAddressToADDRESS(dSymbolTable.ElementAt(exp).Key, dSymbolTable));

                lAssembly.Add("@ADDRESS");

                lAssembly.Add("A=M");

                lAssembly.Add("D=M");

                lAssembly.Add("@RESULT");

                lAssembly.Add("M=D");

                lAssembly.AddRange(ResultTOtable(aSimple.Variable, dSymbolTable));
            }
///////////////////BinaryOperationExpression
            if (aSimple.Value is BinaryOperationExpression)
            {
                int exp = ComputeExpression(aSimple.Value, dSymbolTable);
                BinaryOperationExpression be = (BinaryOperationExpression)aSimple.Value;
                if (be.Operand1 is NumericExpression)
                {
                    lAssembly.Add("@" + be.Operand1);

                    lAssembly.Add("D=A");

                    lAssembly.Add("@OPERAND1");

                    lAssembly.Add("M=D");
                }
                if (be.Operand1 is VariableExpression)
                {
                    lAssembly.AddRange(VariableAddressToADDRESS(be.Operand1.ToString(), dSymbolTable));

                    lAssembly.Add("@ADDRESS");

                    lAssembly.Add("A=M");

                    lAssembly.Add("D=M");

                    lAssembly.Add("@OPERAND1");

                    lAssembly.Add("M=D");
                }
                if (be.Operand2 is NumericExpression)
                {
                    lAssembly.Add("@" + be.Operand2);

                    lAssembly.Add("D=A");

                    lAssembly.Add("@OPERAND2");

                    lAssembly.Add("M=D");
                }
                if (be.Operand2 is VariableExpression)
                {
                    lAssembly.AddRange(VariableAddressToADDRESS(be.Operand2.ToString(), dSymbolTable));

                    lAssembly.Add("@ADDRESS");

                    lAssembly.Add("A=M");

                    lAssembly.Add("D=M");

                    lAssembly.Add("@OPERAND2");

                    lAssembly.Add("M=D");
                }

                lAssembly.Add("@OPERAND1");

                lAssembly.Add("D=M");

                lAssembly.Add("@OPERAND2");

                lAssembly.Add("D=D" + be.Operator + "M");

                lAssembly.Add("@RESULT");

                lAssembly.Add("M=D");

                lAssembly.AddRange(ResultTOtable(aSimple.Variable, dSymbolTable));
            }
            return(lAssembly);
        }
Beispiel #21
0
/*
 *
 *
 * Implement!!!!!
 */
        public List <LetStatement> SimplifyExpressions(LetStatement s, List <VarDeclaration> lVars)
        {
            List <LetStatement> letAfterSimplfy = new List <LetStatement>();
            string str   = s.Value.ToString();
            int    count = 0;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '(')
                {
                    count++;
                }
            }


            int index = 1;

            //complex expression
            while (count > 1)
            {
                String substr = "";
                int    i;
                for (i = str.IndexOf(')'); str[i] != '('; i--)
                {
                    substr = str[i] + substr;
                }
                substr = "(" + substr;

                List <string> lLines = new List <string>();
                lLines.Add(substr);
                List <Token>  lTokens = Tokenize(lLines);
                Stack <Token> sTokens = new Stack <Token>();
                for (int j = lTokens.Count - 1; j >= 0; j--)
                {
                    sTokens.Push(lTokens[j]);
                }

                Expression exp = Expression.Create(sTokens);
                exp.Parse(sTokens);
                LetStatement l = new LetStatement();
                l.Value    = exp;
                l.Variable = "_" + index;
                letAfterSimplfy.Add(l);

                String newexp = "";
                newexp = str.Substring(0, i) + "_" + index + str.Substring(str.IndexOf(')') + 1);
                index++;
                str = newexp;
                count--;
            }



            List <string> listLines = new List <string>();

            listLines.Add(str);
            List <Token>  listTokens  = Tokenize(listLines);
            Stack <Token> stackTokens = new Stack <Token>();

            for (int j = listTokens.Count - 1; j >= 0; j--)
            {
                stackTokens.Push(listTokens[j]);
            }

            Expression expi = Expression.Create(stackTokens);

            expi.Parse(stackTokens);
            LetStatement lett = new LetStatement();

            lett.Value    = expi;
            lett.Variable = ((LetStatement)s).Variable;
            letAfterSimplfy.Add(lett);
            return(letAfterSimplfy);
        }