static void Test2() { Console.WriteLine("test2"); Compiler c = new Compiler(); string s = "let x = (+ (+ x 5) (- y z));"; Stack <Token> sTokens = c.Tokenize(s); string[] aTokens = new string[] { "let", "x", "=", "(", "+", "(", "+", "x", "5", ")", "(", "-", "y", "z", ")", ")", ";" }; for (int i = 0; i < aTokens.Length; i++) { Token sToken = sTokens.Pop(); if (sToken.ToString() != aTokens[i]) { Console.WriteLine("BUGBUG"); } } sTokens = c.Tokenize(s); AssignmentStatement assignment = c.Parse(sTokens); if (assignment.ToString() != s) { Console.WriteLine("BUGBUG"); } List <AssignmentStatement> lSimple = c.SimplifyExpressions(assignment); string[] aSimple = new string[] { "let _3 = (+ x 5);", "let _4 = (- y z);", "let x = (+ _3 _4);" }; for (int i = 0; i < aSimple.Length; i++) { if (lSimple[i].ToString() != aSimple[i]) { Console.WriteLine("BUGBUG"); } } c.Compile(s); }
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"); } }
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"); } }
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); }
static bool Test4() { try { Compiler c = new Compiler(); List <string> lCodeLines = c.ReadFile(@"GCDErr.Jack"); List <Token> lTokens = c.Tokenize(lCodeLines); } catch (SyntaxErrorException e) { return(true); } return(false); }
private static bool TestParse() { try { Compiler sc = new Compiler(); List <string> lLines = sc.ReadFile(@"Program.Jack"); List <Token> lTokens = sc.Tokenize(lLines); TokensStack sTokens = new TokensStack(); for (int i = lTokens.Count - 1; i >= 0; i--) { sTokens.Push(lTokens[i]); } JackProgram prog = new JackProgram(); prog.Parse(sTokens); string sAfterParsing = prog.ToString(); sAfterParsing = sAfterParsing.Replace(" ", ""); sAfterParsing = sAfterParsing.Replace("\t", ""); sAfterParsing = sAfterParsing.Replace("\n", ""); sAfterParsing = sAfterParsing.ToLower(); string sAllTokens = ""; foreach (Token t in lTokens) { sAllTokens += GetName(t).ToLower(); } for (int i = 0; i < sAllTokens.Length; i++) { if (sAllTokens[i] != sAfterParsing[i]) { return(false); } } return(true); } catch (Exception e) { Console.WriteLine(e); } return(false); }
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"); }
static bool Test3() { Compiler c = new Compiler(); List <string> lCodeLines = c.ReadFile(@"BinarySearch.Jack"); List <Token> lTokens = c.Tokenize(lCodeLines); Token t = lTokens[0]; if (t.ToString() != "function" || !(t is Statement)) { return(false); } t = lTokens[20]; if (t.ToString() != "end" || !(t is Identifier) || t.Line != 2 || t.Position != 9) { return(false); } t = lTokens[50]; if (t.ToString() != "+" || !(t is Operator) || t.Line != 9 || t.Position != 22) { return(false); } return(true); }
static bool Test2() { Compiler c = new Compiler(); List <string> lCodeLines = c.ReadFile(@"Fib.Jack"); List <Token> lTokens = c.Tokenize(lCodeLines); Token t = lTokens[0]; if (t.ToString() != "function" || !(t is Statement)) { return(false); } t = lTokens[20]; if (t.ToString() != "2" || !(t is Number) || t.Line != 5 || t.Position != 8) { return(false); } t = lTokens[50]; if (t.ToString() != "}" || !(t is Parentheses) || t.Line != 12 || t.Position != 0) { return(false); } return(true); }
static bool Test1() { Compiler c = new Compiler(); List <string> lCodeLines = c.ReadFile(@"GCD.Jack"); List <Token> lTokens = c.Tokenize(lCodeLines); Token t = lTokens[0]; if (t.ToString() != "function" || !(t is Statement)) { return(false); } t = lTokens[20]; if (t.ToString() != "=" || t.Line != 3 || t.Position != 11) { return(false); } t = lTokens[50]; if (t.ToString() != "}" || !(t is Parentheses) || t.Line != 7 || t.Position != 1) { return(false); } return(true); }
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"); } }