public void Repeat01() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("i", IrisType.Integer); globals.Add("a", IrisType.Integer); string input = @" repeat a := 0; i := i + 1; until i = 10;"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"L0: ldc.i4.0 stsfld 1 ldsfld 0 ldc.i4.1 add stsfld 0 ldsfld 0 ldc.i4.s 10 bne.un L0 "; Assert.AreEqual(expected, output); }
public void SemanticError10() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"a[0]"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 1) Symbol 'a' is not an array, but is being used as an array."); }
public void For01() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("i", IrisType.Integer); globals.Add("a", IrisType.Integer); string input = @"for i := 0 to 10 do a := 0;"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldc.i4.0 stsfld 0 L0: ldsfld 0 ldc.i4.s 10 bgt L1 ldc.i4.0 stsfld 1 ldsfld 0 ldc.i4.1 add stsfld 0 br L0 L1: "; Assert.AreEqual(expected, output); }
public void If02() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"if 1 > 0 or 1 < 0 then a := 0 else a := 1"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldc.i4.1 ldc.i4.0 cgt ldc.i4.1 ldc.i4.0 clt or brfalse L0 ldc.i4.0 stsfld 0 br L1 L0: ldc.i4.1 stsfld 0 L1: "; Assert.AreEqual(expected, output); }
public void SemanticError06() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", Procedure.Create(new Variable[0])); string input = @"not a"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 5) Expecting boolean expression."); }
public void SemanticError08() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", Procedure.Create(new Variable[] { new Variable(IrisType.Boolean, "b") })); string input = @"a(true) + 1"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 9) Cannot apply operator to procedure call."); }
public void Expression17() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"a + 1 < 7 and a - 1 > 2"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldsfld 0 ldc.i4.1 add ldc.i4.7 clt ldsfld 0 ldc.i4.1 sub ldc.i4.2 cgt and "; Assert.AreEqual(expected, output); }
public void SemanticError13() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a + a"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 3) Operator requires a primitive type (boolean, integer, or string)."); }
public void SyntaxError10() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a[0];"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 5) Expecting ':='."); }
public void SemanticError21() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a := 1"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 6) Cannot assign to 'a' (type mismatch error)."); }
public void SemanticError39() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer })); string input = @"method"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 1) Wrong number of arguments for function 'method'. 1 expected. 0 provided."); }
public void SemanticError31() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"a(false)"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 2) Symbol 'a' is not a procedure or function."); }
public void SyntaxError08() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"else a := 1"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 6) Cannot start statement with 'else' or unexpected ';' after if statement."); }
public void SemanticError29() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer.MakeByRefType() })); string input = @"method(false)"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 8) Cannot take address of constant, call, or expression."); }
public void SemanticError19() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a[true]"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 3) Expecting integer value as array index."); }
public void SemanticError18() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer })); string input = @"method(false)"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 8) Argument type doesn't match parameter 'p0' of function 'method'"); }
public static void TestStatementParserWithError(string compiland, GlobalSymbolList symbols, string expectedError) { using (TestCompilerContext context = TestCompilerContext.Create(compiland, symbols, CompilationFlags.NoDebug)) { context.ParseStatement(); Assert.IsTrue(context.ErrorCount > 0, "Expecting compiler error"); Assert.AreEqual(expectedError, context.FirstError); } }
public static string TestStatementParser(string compiland, GlobalSymbolList symbols = null) { using (TestCompilerContext context = TestCompilerContext.Create(compiland, symbols, CompilationFlags.NoDebug)) { context.ParseStatement(); Assert.AreEqual(0, context.ErrorCount, context.FirstError.ToString()); return context.GetCompilerOutput(); } }
public void SemanticError38() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", Procedure.Create(new Variable[0])); globals.Add("b", IrisType.Integer); string input = @"b := a"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 3) Cannot use procedure in assignment statement."); }
public void SemanticError34() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); globals.Add("b", IrisType.Integer); string input = @"for a := 0 to 'test' do b := 1"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 15) Expecting integer expression."); }
public static string TestStatementParser(string compiland, GlobalSymbolList symbols = null) { using (TestCompilerContext context = TestCompilerContext.Create(compiland, symbols, CompilationFlags.NoDebug)) { context.ParseStatement(); Assert.AreEqual(0, context.ErrorCount, context.FirstError.ToString()); return(context.GetCompilerOutput()); } }
public void SemanticError37() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", Procedure.Create(new Variable[0])); globals.Add("b", IrisType.Integer); string input = @"a := b"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 3) Cannot assign to result of function or procedure call."); }
public void SyntaxError24() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"for 'test' := 0 to 'test' do a := 1"; TestHelpers.TestStatementParserWithError(input, globals, @"(1, 5) Expecting integer identifier."); }
public void SimpleCall() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", Function.Create(IrisType.Integer, new Variable[0])); string input = @"method()"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"call _Unknown "; Assert.AreEqual(expected, output); }
public void CallProcedure02() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("p", Procedure.Create(new Variable[0])); string input = @"p();"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"call _Unknown "; Assert.AreEqual(expected, output); }
public void CallFunction02() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("f", Function.Create(IrisType.Integer, new Variable[0])); string input = @"f();"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"call _Unknown pop "; Assert.AreEqual(expected, output); }
public void If03() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"if 0 > 1 then a := 0 else if 1 < 1 then a := 1 else if 2 < 1 then a := 2 else a := 3"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldc.i4.0 ldc.i4.1 ble L0 ldc.i4.0 stsfld 0 br L1 L0: ldc.i4.1 ldc.i4.1 bge L2 ldc.i4.1 stsfld 0 br L3 L2: ldc.i4.2 ldc.i4.1 bge L4 ldc.i4.2 stsfld 0 br L5 L4: ldc.i4.3 stsfld 0 L5: L3: L1: "; Assert.AreEqual(expected, output); }
public void Assign02() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeByRefType()); string input = @"a := 1;"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldsfld 0 ldc.i4.1 stind.i4 "; Assert.AreEqual(expected, output); }
public void Expression18() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a[7]"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldsfld 0 ldc.i4.7 ldelem.i4 "; Assert.AreEqual(expected, output); }
public void Expression25() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer.MakeByRefType() })); globals.Add("a", IrisType.Integer); string input = @"method((a))"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldsflda 0 call _Unknown "; Assert.AreEqual(expected, output); }
public static TestCompilerContext Create(string compiland, GlobalSymbolList globals, CompilationFlags flags) { byte[] buffer = Encoding.Default.GetBytes(compiland); MemoryStream input = new MemoryStream(buffer); StreamReader reader = new StreamReader(input); MemoryStream output = new MemoryStream(); TextEmitter emitter = new TextEmitter(output); TestCompilerContext testContext = new TestCompilerContext(input, reader, output, emitter, flags); if (globals != null) { foreach (var symbol in globals.Items) testContext.SymbolTable.Add(symbol.Item1, symbol.Item2, StorageClass.Global, null); } return testContext; }
public void Expression23() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeByRefType()); string input = @"a + 1"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldsfld 0 ldind.i4 ldc.i4.1 add "; Assert.AreEqual(expected, output); }
public void Assign01() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer.MakeArrayType()); string input = @"a[0] := a[1];"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldsfld 0 ldc.i4.0 ldsfld 0 ldc.i4.1 ldelem.i4 stelem.i4 "; Assert.AreEqual(expected, output); }
public void SimpleWithParams() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("global", IrisType.Integer); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer, IrisType.Integer, IrisType.Boolean, IrisType.Integer })); string input = @"method(1, -2, false, global)"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldc.i4.1 ldc.i4.2 neg ldc.i4.0 ldsfld 0 call _Unknown "; Assert.AreEqual(expected, output); }
public void If01() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"if 1 > 0 then a := 0;"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"ldc.i4.1 ldc.i4.0 ble L0 ldc.i4.0 stsfld 0 L0: "; Assert.AreEqual(expected, output); }
public void Expression19() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("strcmp", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer, IrisType.Integer })); globals.Add("a", IrisType.String); globals.Add("b", IrisType.String); string input = @"a = b"; string output = TestHelpers.TestExpressionParser(input, globals); string expected = @"ldsfld 0 ldsfld 1 call _Unknown ldc.i4.0 ceq "; Assert.AreEqual(expected, output); }
public void While01() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("a", IrisType.Integer); string input = @"while 1 > 0 do a := 0;"; string output = TestHelpers.TestStatementParser(input, globals); string expected = @"L0: ldc.i4.1 ldc.i4.0 ble L1 ldc.i4.0 stsfld 0 br L0 L1: "; Assert.AreEqual(expected, output); }
public void SemanticError41() { GlobalSymbolList globals = new GlobalSymbolList(); globals.Add("method", TestHelpers.MakeTestFunction(IrisType.Integer, new IrisType[] { IrisType.Integer.MakeByRefType() })); globals.Add("a", IrisType.Integer); string input = @"method(a + 1)"; TestHelpers.TestExpressionParserWithError(input, globals, @"(1, 10) Cannot take address of expression."); }