public void TestReservedWordIssue23() { const string source = @"MODULE Test; VAR int: INTEGER; BEGIN int := 1; WriteInt(int); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void RepeatTest() { const string source = @"MODULE Test; VAR i: INTEGER; BEGIN i := 1; REPEAT WriteInt(i); WriteLn; i := i+1; UNTIL i > 5 END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n2\n3\n4\n5\n", output.ToString().NlFix()); }
public void SimpleArraySetAndGetValueGlobal() { const string source = @"MODULE Array3; VAR a: ARRAY 32 OF INTEGER; n: INTEGER; BEGIN a[1] := 1; n := a[1]; WriteInt(n); WriteLn END Array3."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void TestIssue25Repeat() { const string source = @"MODULE Issue25; VAR x: BOOLEAN; BEGIN x := FALSE; REPEAT WriteString('Yes'); x := TRUE UNTIL x; WriteLn END Issue25."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("Yes\n", output.ToString().NlFix()); }
public void Test1() { const string source = @"MODULE Test; TYPE rType = RECORD a: INTEGER; b: STRING END; VAR demo: rType; BEGIN demo.a := 1; WriteInt(demo.a); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1\n", output.ToString().NlFix()); }
public void SystemToStringReal() { const string source = @"MODULE ToStringTest; VAR s: STRING; BEGIN s := ToString(12.5); WriteString(s); WriteLn END ToStringTest."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("12.5\n", output.ToString().NlFix()); }
public void TestAddVarConst() { const string source = @"MODULE Array; VAR a: INTEGER; BEGIN a := 1; WriteInt(a+2); WriteLn END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("3\n", output.ToString().NlFix()); }
public void TestReadBoolVar() { string source = @"MODULE Array; VAR a,b: BOOLEAN; BEGIN ReadBool(a); ReadBool(b); WriteBool(a); WriteLn; WriteBool(b); WriteLn; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader("true" + Environment.NewLine + "false" + Environment.NewLine)); Assert.Equal($"{true}\n{false}\n", output.ToString().NlFix()); }
public void TestReadIntVar() { const string source = @"MODULE Array; VAR a: INTEGER; BEGIN ReadInt(a); WriteInt(a); WriteLn; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader("12" + Environment.NewLine)); Assert.Equal("12\n", output.ToString().NlFix()); }
public void TestEpsilon() { string source = @"MODULE Test; CONST expected = 10.8511834932; VAR r, s, z: REAL; b, c: BOOLEAN; BEGIN r := 1.5; s := 7.2341223288; z := r * s; b := (r - expected) < EPSILON; c := r = expected; WriteBool(b); WriteString(','); WriteBool(c); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true},{false}\n", output.ToString().NlFix()); }
public void TestNegRealVar() { string source = @"MODULE Array; VAR a: REAL; BEGIN a := 1.5; a := -a; WriteReal(a); WriteLn; WriteReal(-a); WriteLn END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{-1.5}\n{1.5}\n", output.ToString().NlFix()); }
public void TestArrayCallByValue() { string source = @"MODULE Test; TYPE aType = ARRAY 5 OF INTEGER; VAR arr: aType; PROCEDURE TestArray(a: aType); BEGIN IF (a[1] # 1) THEN WriteString('a is 0') END; a[1] := 2 END TestArray; BEGIN arr[1] := 1; TestArray(arr); WriteBool(arr[1] = 1); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}\n", output.ToString().NlFix()); }
public void SimpleArrayDefinition() { const string source = @"MODULE Array; VAR a: ARRAY 32 OF INTEGER; b: ARRAY 32 OF BOOLEAN; END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); Assert.True(syntaxTree.CompileAndLoadAssembly(cg) != null); }
public void Multiply(int a, int b) { string source = @" MODULE MultiplyTest; VAR x, y, z: INTEGER; PROCEDURE Multiply(x: INTEGER; y: INTEGER; VAR z: INTEGER); VAR negate : BOOLEAN; BEGIN negate := x < 0; IF (negate) THEN x := -x END; z := 0; WHILE x > 0 DO IF x MOD 2 = 1 THEN z := z + y END; y := 2*y; x := x DIV 2 END ; IF (negate) THEN z := -z END; END Multiply; BEGIN ReadInt(x); ReadInt(y); Multiply(x, y, z); WriteInt(z); WriteLn END MultiplyTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader($"{a}{Environment.NewLine}{b}{Environment.NewLine}")); Assert.Equal($"{a * b}\n", output.ToString().NlFix()); }
public void TestProcedureBoolean() { string source = @" MODULE ProcTest; CONST true_ = TRUE; VAR x, y: BOOLEAN; i, j: BOOLEAN; PROCEDURE Test(x: BOOLEAN; y: BOOLEAN; VAR a: BOOLEAN; b: BOOLEAN); BEGIN x := FALSE; y := true_; a := TRUE; b := FALSE; END Test; BEGIN x := TRUE; y := x = true_; i := FALSE; j := FALSE; Test(y, x, i, j); WriteBool(x); WriteString(', '); WriteBool(y); WriteString(', '); WriteBool(i); WriteString(', '); WriteBool(j); WriteLn END ProcTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}, {true}, {true}, {false}\n", output.ToString().NlFix()); }
public void TestRecordCallByValue() { string source = @"MODULE Test; TYPE rType = RECORD a: INTEGER; b: REAL; c: BOOLEAN END; VAR r: rType; PROCEDURE TestRecord(r: rType); BEGIN IF (r.a # 1) THEN WriteString(' a error') END; IF ~r.c THEN WriteString('c error') END; IF ABS(r.b - 1.2345) > EPSILON THEN WriteString('b error') END; r.a := -1; r.b := -1.2345; r.c := FALSE; END TestRecord; BEGIN r.a := 1; r.b := 1.2345; r.c := TRUE; TestRecord(r); WriteBool((r.a = 1) & (ABS(r.b - 1.2345) < EPSILON) & r.c); WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}\n", output.ToString().NlFix()); }
public void TestWriterAgainstStringWriter() { const string source = @"MODULE ReadToRecord; VAR s: RECORD a : INTEGER END; BEGIN ReadInt(s.a); s.a := s.a + 1; WriteInt(s.a); WriteLn END ReadToRecord."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); using var sw = new StringWriter(); cg.WriteIntermediateCode(sw); Assert.Equal(code, sw.ToString()); }
public void TestProcedureInteger() { string source = @" MODULE ProcTest; VAR x, y: INTEGER; i, j: INTEGER; PROCEDURE Test(x: INTEGER; y: INTEGER; VAR a: INTEGER; b: INTEGER); BEGIN x := 0; y := -1; a := 10; b := 20; END Test; BEGIN x := 42; y := 1; i := 2; j := 0; Test(y, x, i, j); WriteBool(x = 42); WriteString(', '); WriteBool(y = 1); WriteString(', '); WriteBool(i = 10); WriteString(', '); WriteBool(j = 0); WriteLn END ProcTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal($"{true}, {true}, {true}, {true}\n", output.ToString().NlFix()); }
private void CheckCodeReal(string sourceCode, string expectedResults) { // ReSharper disable once StringLiteralTypo string source = $@"MODULE Test; CONST c = 1.2; i = 42; VAR r, s, t, x, y, z: REAL; BEGIN {sourceCode} IF isinfinity(z) THEN WriteString('Infinity') ELSE WriteReal(z) END; WriteLn END Test."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal(expectedResults, output.ToString().NlFix()); }
public void TestLessEqual() { const string source = @" MODULE TestLessEqual; VAR x, y: INTEGER; res: BOOLEAN; BEGIN ReadInt(x); ReadInt(y); IF (x <= y) THEN res := TRUE ELSE res := FALSE END; WriteBool(res); WriteLn END TestLessEqual. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader("5" + Environment.NewLine + "3")); Assert.Equal($"{false}\n", output.ToString().NlFix()); var sb = output.GetStringBuilder(); sb.Remove(0, sb.Length); Runner.Execute(assembly, output, new StringReader("5" + Environment.NewLine + "5")); Assert.Equal($"{true}\n", output.ToString().NlFix()); sb = output.GetStringBuilder(); sb.Remove(0, sb.Length); Runner.Execute(assembly, output, new StringReader("3" + Environment.NewLine + "5")); Assert.Equal($"{true}\n", output.ToString().NlFix()); }
public void SimpleArrayDefinitionLocal() { const string source = @"MODULE Array; VAR a: ARRAY 32 OF INTEGER; b: ARRAY 32 OF INTEGER; i: INTEGER; s: INTEGER; BEGIN i := 1; WHILE (i <= 32) DO a[i] := i; b[i] := 32-i; i := i+1 END; i := 1; WHILE (i <= 32) DO s := s + a[i] + b[i]; i := i+1 END; WriteInt(s); WriteLn END Array."; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal("1024\n", output.ToString().NlFix()); }
public void Divide(int a, int b, int res, int rem, bool div0) { const string source = @" MODULE DivisionTest; VAR x, y, q, r: INTEGER; PROCEDURE Divide(x: INTEGER; y: INTEGER; VAR q: INTEGER; VAR r: INTEGER); VAR w: INTEGER; negatex: BOOLEAN; negatey: BOOLEAN; BEGIN negatex := x < 0; negatey := y < 0; IF (negatex) THEN x := -x END; IF (negatey) THEN y := -y END; r := x; q := 0; w := y; WHILE w <= r DO w := 2*w END ; WHILE w > y DO q := 2*q; w := w DIV 2; IF w <= r THEN r := r - w; q := q + 1 END END; IF (negatex & ~negatey) OR (~negatex & negatey) THEN q := -q; r := -r END END Divide; BEGIN ReadInt(x); ReadInt(y); IF (y # 0) THEN Divide(x, y, q, r); WriteInt(q); WriteString('/'); WriteInt(r); ELSE WriteString('DIV0'); END; WriteLn END DivisionTest. "; var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.NotNull(assembly); using var output = new StringWriter(); Runner.Execute(assembly, output, new StringReader($"{a}{Environment.NewLine}{b}{Environment.NewLine}")); Assert.Equal(div0 ? "DIV0\n" : $"{res}/{rem}\n", output.ToString().NlFix()); }
public void FullExample1() { const string source = @"MODULE Samples; PROCEDURE Multiply(x: INTEGER; y: INTEGER); VAR z: INTEGER; BEGIN WriteString('Mul: '); WriteInt(x); WriteString('*'); WriteInt(y); WriteString('= '); z := 0; WHILE x > 0 DO IF x MOD 2 = 1 THEN z := z + y END ; y := 2*y; x := x DIV 2 END ; WriteInt(z); WriteLn END Multiply; PROCEDURE Divide(x: INTEGER; y: INTEGER); VAR r, q, w: INTEGER; BEGIN WriteString('Div: '); WriteInt(x); WriteString('/'); WriteInt(y); WriteString('= '); r := x; q := 0; w := y; WHILE w <= r DO w := 2*w END ; WHILE w > y DO q := 2*q; w := w DIV 2; IF w <= r THEN r := r - w; q := q + 1 END END ; WriteInt(q); WriteString(', rem '); WriteInt(r); WriteLn END Divide; BEGIN Multiply(5, 10); Multiply(-5, 10); Multiply(5, 0); Divide(100, 2); Divide(100, 3); Divide(3, 100) END Samples."; string expected = @"Mul: 5*10= 50 Mul: -5*10= 0 Mul: 5*0= 0 Div: 100/2= 50, rem 0 Div: 100/3= 33, rem 1 Div: 3/100= 0, rem 3 ".NlFix(); var cg = CompileHelper.CompileOberon0Code(source, out string code, _output); Assert.NotEmpty(code); var syntaxTree = CSharpSyntaxTree.ParseText(code); var assembly = syntaxTree.CompileAndLoadAssembly(cg, true); Assert.True(assembly != null); using var output = new StringWriter(); Runner.Execute(assembly, output); Assert.Equal(expected, output.ToString().NlFix()); }