Esempio n. 1
0
        private void SplitTheExp(ref List <AssignmentStatement> l, BinaryOperationExpression binaryOp)
        {
            string replaceLeft = "", replaceRight = "";

            if (binaryOp.Operand1 is BinaryOperationExpression)
            {
                SplitTheExp(ref l, (BinaryOperationExpression)binaryOp.Operand1);
                replaceLeft = ((AssignmentStatement)l.Last()).Variable.Name;
            }
            if (binaryOp.Operand2 is BinaryOperationExpression)
            {
                SplitTheExp(ref l, (BinaryOperationExpression)binaryOp.Operand2);
                replaceRight = ((AssignmentStatement)l.Last()).Variable.Name;
            }
            tempLocals++;
            string             s   = "_" + tempLocals;
            VariableExpression var = new VariableExpression();

            var.Name = s;
            AssignmentStatement toAdd = new AssignmentStatement();

            toAdd.Variable = var;
            if (replaceLeft == "" && replaceRight == "")
            {
                toAdd.Value = binaryOp;
            }
            else
            {
                BinaryOperationExpression change = new BinaryOperationExpression();
                change.Operator = binaryOp.Operator;
                if (replaceLeft != "")
                {
                    VariableExpression var2 = new VariableExpression();
                    var2.Name       = replaceLeft;
                    change.Operand1 = var2;
                }
                else
                {
                    change.Operand1 = binaryOp.Operand1;
                }
                if (replaceRight != "")
                {
                    VariableExpression var2 = new VariableExpression();
                    var2.Name       = replaceRight;
                    change.Operand2 = var2;
                }
                else
                {
                    change.Operand2 = binaryOp.Operand1;
                }
                toAdd.Value = change;
            }
            l.Add(toAdd);
        }
Esempio n. 2
0
        private List <string> isNumaricAssigment(List <string> AssemblyList, AssignmentStatement aSimple)
        {
            NumericExpression Num = (NumericExpression)(aSimple.Value);
            int value             = Num.Value;

            AssemblyList.Add("@" + value);
            AssemblyList.Add("D = A");
            AssemblyList.Add("@RETURN");
            AssemblyList.Add("M = D");
            computeVariabletoR0(AssemblyList, aSimple.Variable.Name);
            updateDestinationWithResult(AssemblyList);
            return(AssemblyList);
        }
Esempio n. 3
0
 private List <string> isVariableAssigment(List <string> AssemblyList, AssignmentStatement aSimple)
 {
     AssemblyList.Add("@" + symbolTable[((VariableExpression)aSimple.Value).Name]);
     AssemblyList.Add("D = A");
     AssemblyList.Add("@LOCAL");
     AssemblyList.Add("A = D + M");
     AssemblyList.Add("D = M");
     AssemblyList.Add("@RESULT");
     AssemblyList.Add("M = D");
     computeVariabletoR0(AssemblyList, ((VariableExpression)aSimple.Variable).Name);
     updateDestinationWithResult(AssemblyList);
     return(AssemblyList);
 }
Esempio n. 4
0
        //Compile a single line containing only an assignment of the form "let <var> = <expression>;"
        public List <string> Compile(string sAssignment)
        {
            //Tokenize the string into a stack of tokens
            Stack <Token> sTokens = Tokenize(sAssignment);
            //Parse the tokens into objects representing the meaning of the statement
            AssignmentStatement s = Parse(sTokens);
            //Simplify complex expressions in order for the code generation to be simpler
            List <AssignmentStatement> lSimpleAssignments = SimplifyExpressions(s);

            //Compute the symbol table here
            ComputeSymbolTable(lSimpleAssignments);
            //Generate the actual code
            List <string> lAssembly = GenerateCode(lSimpleAssignments);

            return(lAssembly);
        }
Esempio n. 5
0
        static void Test1()
        {
            Console.WriteLine("test1");
            Compiler      c       = new Compiler();
            string        s       = "let x = 5;";
            Stack <Token> sTokens = c.Tokenize(s);

            string[]     aTokens = new string[] { "let", "x", "=", "5", ";" };
            List <Token> answer  = new List <Token>();

            for (int i = 0; i < aTokens.Length; i++)
            {
                Token sToken = sTokens.Pop();
                answer.Add(sToken);
                bool print = false;
                if (sToken.ToString() != aTokens[i])
                {
                    print = true;
                }
                if (print)
                {
                    Console.WriteLine("tokens should be: " + aTokens + " but your answer is: " + answer);
                }
            }
            sTokens = c.Tokenize(s);
            AssignmentStatement assignment = c.Parse(sTokens);

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

            List <AssignmentStatement> lSimple = c.SimplifyExpressions(assignment);

            if (lSimple.Count != 1 || lSimple[0].ToString() != assignment.ToString())
            {
                Console.WriteLine("BUGBUG");
            }

            //List<string> lAssembly = c.GenerateCode(lSimple);
            c.Compile(s);
        }
Esempio n. 6
0
        //Simplify an expression by creating artificial local variables, and using them for intermidiate computations.
        //For example, let x = (+ (- y 2) (- 5 z)); will be simplified into:
        //let _1 = (- y 2);
        //let _2 = (- 5 z);
        //let x = (+ _1 _2);
        public List <AssignmentStatement> SimplifyExpressions(AssignmentStatement s)
        {
            //your code here
            List <AssignmentStatement> ListToReturn = new List <AssignmentStatement>();

            if ((!(s.Value is BinaryOperationExpression)) || (!(((BinaryOperationExpression)s.Value).Operand1 is BinaryOperationExpression) &&
                                                              !(((BinaryOperationExpression)s.Value).Operand2 is BinaryOperationExpression)))
            {
                ListToReturn.Add(s);
            }
            else
            {
                SplitTheExp(ref ListToReturn, (BinaryOperationExpression)s.Value);
                VariableExpression temp = new VariableExpression();
                temp.Name = "_" + tempLocals;
                s.Value   = temp;
                ListToReturn.Add(s);
            }
            return(ListToReturn);
        }
Esempio n. 7
0
        static void TestCompile()
        {
            Compiler c = new Compiler();
            string   wrongToken;

            Console.WriteLine("");

            string s = "let x == 1;";

            Stack <Token> sTokens = c.Tokenize(s);

            wrongToken = "="; bool answerWasWrong; bool testPerfect;
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);
                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else
                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }

            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let x = = 2;;";
            wrongToken = "=";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;

            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }


            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------

            Console.WriteLine("");

            s          = "let 1 = 3;";
            wrongToken = "1";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }

            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let x = 4;;";
            wrongToken = ";";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let let x = 5;";
            wrongToken = "let";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let 1BadName = 5;";
            wrongToken = "1BadName";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let x = ( (+ x 5) (- y z));";
            wrongToken = "(";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");

            s          = "let x = (+ (- (+ x 5) (- y z));";
            wrongToken = "(";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot catch that there are more '(' then ')'");
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }

            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ (* x 5) (- y z));";
            wrongToken = "*";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ 5 y z);";
            wrongToken = "z";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }

            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = ( 5 + z );";
            wrongToken = "5";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ (; x 5) (- y z));";
            wrongToken = ";";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ (+ x 5); (- y z));";
            wrongToken = ";";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "x = (+ (+ x 5) (- y z));";
            wrongToken = "x";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ (+ x 5) WOW (- y z));";
            wrongToken = ")";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+(-(+ (+ x 5) (- y z));";
            wrongToken = "(";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
            //________________________________________________________________________________________________
            //------------------------------------------------------------------------------------------------
            Console.WriteLine("");
            s          = "let x = (+ (+ x 5)-)+) (- y z));";
            wrongToken = ")";
            Console.WriteLine("testing: " + s);
            answerWasWrong = true; testPerfect = true;
            try
            {
                sTokens = c.Tokenize(s);

                AssignmentStatement assignment = c.Parse(sTokens);
            }
            catch (SyntaxErrorException e)
            {
                answerWasWrong = false;
                if (e.Token == null)
                {
                    Console.WriteLine("you throwed an Exception as needed, but your Token is NULL");
                    testPerfect = false;
                }
                else

                if (e.Token.Name != wrongToken)
                {
                    Console.WriteLine("token in Exception should be " + wrongToken + " but you sent: " + e.Token.Name);
                    testPerfect = false;
                }
            }
            if (answerWasWrong)
            {
                Console.WriteLine("you didnot throw Exception on: " + wrongToken);
            }
            else if (testPerfect)
            {
                Console.WriteLine("good work, this test was perfect");
            }
        }
Esempio n. 8
0
        //Generates assembly code for a simple assignment statement. Can accept only the following:
        //let <var> = <number>; e.g. let x = 5;
        //let <var> = <var>; e.g. let x = y;
        //let <var> = (<operator> <operand1> <operand2>); where operand1 and operand2 can only by either numbers or variables, but not nested expressions. e.g. let x = (- y 5);
        public List <string> GenerateCode(AssignmentStatement aSimple)
        {
            List <string> AssemblyList = new List <string>();

            if (aSimple.Value is NumericExpression)
            {
                AssemblyList = isNumaricAssigment(AssemblyList, aSimple);
            }
            else if (aSimple.Value is VariableExpression)
            {
                VariableExpression var = (VariableExpression)aSimple.Value;
                if (!symbolTable.ContainsKey(var.Name))
                {
                    throw new SyntaxErrorException("Undefined variable, please define the variable " + var.Name + " first.", new Token());
                }
                else
                {
                    AssemblyList = isVariableAssigment(AssemblyList, aSimple);
                }
            }
            else
            {
                BinaryOperationExpression temp = new BinaryOperationExpression();
                temp = (BinaryOperationExpression)aSimple.Value;
                if (temp.Operand1 is NumericExpression)
                {
                    AssemblyList.Add("@" + ((NumericExpression)temp.Operand1).Value);
                    AssemblyList.Add("D = A");
                    AssemblyList.Add("@OPERAND1");
                    AssemblyList.Add(" M = D");
                }
                else
                {
                    AssemblyList.Add("@" + symbolTable[((VariableExpression)temp.Operand1).Name]);
                    AssemblyList.Add("D = A");
                    AssemblyList.Add("@LOCAL");
                    AssemblyList.Add("D = D + M");
                    AssemblyList.Add("A = D");
                    AssemblyList.Add("D = M");
                    AssemblyList.Add("@OPERAND1");
                    AssemblyList.Add("M = D");
                }
                if (temp.Operand2 is NumericExpression)
                {
                    AssemblyList.Add("@" + ((NumericExpression)temp.Operand2).Value);
                    AssemblyList.Add("D = A");
                    AssemblyList.Add("@OPERAND2");
                    AssemblyList.Add("M = D");
                }
                else
                {
                    AssemblyList.Add("@" + symbolTable[((VariableExpression)temp.Operand2).Name]);
                    AssemblyList.Add("D = A");
                    AssemblyList.Add("@LOCAL");
                    AssemblyList.Add("D = D + M");
                    AssemblyList.Add("A = D");
                    AssemblyList.Add("D = M");
                    AssemblyList.Add("@OPERAND2");
                    AssemblyList.Add("M = D");
                }
                AssemblyList.Add("@OPERAND1");
                AssemblyList.Add(" D = M");
                AssemblyList.Add("@OPERAND2");
                if (temp.Operator == "+")
                {
                    AssemblyList.Add("D = D + M");
                }
                else
                {
                    AssemblyList.Add("D = D - M");
                }
                AssemblyList.Add("@RESULT");
                AssemblyList.Add("M = D");

                computeVariabletoR0(AssemblyList, aSimple.Variable.Name);
                updateDestinationWithResult(AssemblyList);
            }


            return(AssemblyList);
        }
Esempio n. 9
0
        //Parses a stack of tokens, containing a single assignment statement.
        //The structure must be "let <var> = <expression>;" where expression can be of an arbitrary complexity, i.e., any complex expression is allowed.
        //Parsing must detect syntax problems (e.g. "let" or "=" are missing, opened parantheses are not closed, sentence does not end with a ;, and so forth).
        //When syntax errors are detected, a SyntaxErrorException must be thrown, with an appropriate message explaining the problem.
        public AssignmentStatement Parse(Stack <Token> sTokens)
        {
            Token popedToken = new Token();

            popedToken = sTokens.Pop();
            AssignmentStatement StatmentToReturn  = new AssignmentStatement();
            VariableExpression  variableWorkingOn = new VariableExpression(),
                                SecondVariable    = new VariableExpression();
            NumericExpression num = new NumericExpression();

            if (popedToken.Type != Token.TokenType.Keyword)
            {
                throw new SyntaxErrorException("We accept only LL(0) type, Unrecogmized KeyWord", popedToken);
            }
            popedToken = sTokens.Pop();
            if ((popedToken.Type == Token.TokenType.ID) && (popedToken.Name.ElementAt(0) >= '0') && (popedToken.Name.ElementAt(0) <= '9'))
            {
                throw new SyntaxErrorException("All variables can not start with a number", popedToken);
            }
            else if (popedToken.Name.Contains("?") || popedToken.Name.Contains("#") || popedToken.Name.Contains("@"))
            {
                throw new SyntaxErrorException("Variable can not contain the '?,#,@' signs", popedToken);
            }
            else if ((popedToken.Type != Token.TokenType.ID))
            {
                throw new SyntaxErrorException("Expecting a variable name", popedToken);
            }
            else
            {
                variableWorkingOn.Name    = popedToken.Name;
                StatmentToReturn.Variable = variableWorkingOn;
            }
            popedToken = sTokens.Pop();
            if (!popedToken.Name.Equals("="))
            {
                throw new SyntaxErrorException("Expecting a '=' sign", popedToken);
            }
            popedToken = sTokens.Pop();
            if (Char.IsDigit(popedToken.Name, 0))
            {
                bool success = false;
                success = Int32.TryParse(popedToken.Name, out num.Value);
                if (!success)
                {
                    throw new SyntaxErrorException("Invalid number", popedToken);
                }
                StatmentToReturn.Value = num;
            }
            else if (Char.IsLetter(popedToken.Name, 0))
            {
                SecondVariable.Name    = popedToken.Name;
                StatmentToReturn.Value = SecondVariable;
            }
            else if (popedToken.Name == "(")
            {
                StatmentToReturn.Value = BinaryExpressionCreate(new BinaryOperationExpression(), ref sTokens);
                if ((sTokens.Count > 0))
                {
                    if (sTokens.Pop().Name != ")")
                    {
                        throw new SyntaxErrorException("Missing ')' symbol", popedToken);
                    }
                }
            }
            else
            {
                throw new SyntaxErrorException("Invalid value", popedToken);
            }
            if (sTokens.Count > 1)
            {
                throw new SyntaxErrorException("Invalid value", popedToken);
            }
            if (sTokens.Count == 0 || (sTokens.Count == 1 && (sTokens.Pop().Name != ";")))
            {
                throw new SyntaxErrorException("Expecting ';' at the end of the statement", popedToken);
            }
            if (sTokens.Count > 0)
            {
                throw new SyntaxErrorException("No code after ';'", popedToken);
            }
            return(StatmentToReturn);
        }