コード例 #1
0
        public void EmptyApplication()
        {
            var m = Oberon0Compiler.CompileString("MODULE Test; END Test.");

            Assert.Equal("Test", m.Name);
            Assert.Equal(3, m.Block.Declarations.Count);
        }
コード例 #2
0
        public void FullExample1()
        {
            string source = @"
MODULE Samples;
VAR 
  n: INTEGER;

PROCEDURE Multiply;
VAR 
  x, y, z: INTEGER;
BEGIN 
    ReadInt(x); ReadInt(y); 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(x); WriteInt(y); WriteInt(z); WriteLn
END Multiply;

PROCEDURE Divide;
    VAR x, y, r, q, w: INTEGER;

BEGIN 
    ReadInt(x); ReadInt(y); 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(x); WriteInt(y); WriteInt(q); WriteInt(r); WriteLn
END Divide;

PROCEDURE BinSearch;
VAR i, j, k, n, x: INTEGER;
    a: ARRAY 32 OF INTEGER;
BEGIN 
    ReadInt(x); 
    k := 0;
    WHILE ~eot() DO 
        ReadInt(a[k]); 
        k := k + 1 
    END;
    i := 0; 
    j := n;
    WHILE i < j DO
        k := (i+j) DIV 2;
        IF x < a[k] THEN j := k ELSE i := k+1 END
    END;
    WriteInt(i); 
    WriteInt(j); 
    WriteInt(a[j]); 
    WriteLn
END BinSearch;
BEGIN ReadInt(n);
 IF n = 0 THEN Multiply ELSIF n = 1 THEN Divide ELSE BinSearch END
END Samples.
";

            Oberon0Compiler.CompileString(source);
        }
コード例 #3
0
        private static int StartCompile(BindingContext context, FileSystemInfo inputFile, DirectoryInfo outputPath, string projectName, bool clean, bool verbose)
        {
            var m = Oberon0Compiler.CompileString(File.ReadAllText(inputFile.FullName));

            if (m.CompilerInstance.HasError)
            {
                return(1);
            }

            ICodeGenerator cg = new MsilBinGenerator {
                Module = m
            };

            cg.GenerateIntermediateCode();

            return(cg.GenerateBinary(new CreateBinaryOptions()
            {
                OutputPath = outputPath?.FullName ?? Path.GetDirectoryName(inputFile.FullName),
                CleanSolution = clean,
                OutputDataRetrieved = OutputDataRetrieved,
                ErrorDataRetrieved = ErrorDataRetrieved,
                ModuleName = projectName ?? m.Name,
                Verbose = verbose,
            }) ? 0 : 2);
        }
コード例 #4
0
        public void ConstSimpleExpr()
        {
            var m = Oberon0Compiler.CompileString(
                @"MODULE Test;
CONST
  Test = 1+1;

 END Test.");

            var c = m.Block.LookupVar("Test");

            Assert.NotNull(c);
            Assert.IsType <ConstDeclaration>(c);
            var cp = (ConstDeclaration)c;

            Assert.Equal("Test", cp.Name);
            Assert.Equal(m.Block.LookupType("INTEGER"), cp.Type);
            Assert.Equal(2, cp.Value.ToInt32());
        }
コード例 #5
0
        internal static ICodeGenerator CompileOberon0Code(string source, out string code,
                                                          ITestOutputHelper outputHelper = null)
        {
            var m = Oberon0Compiler.CompileString(source);

            if (m.CompilerInstance.HasError)
            {
                throw new ArgumentException("Source code contains errors", nameof(source));
            }

            ICodeGenerator cg = new MsilBinGenerator {
                Module = m
            };

            cg.GenerateIntermediateCode();

            code = cg.IntermediateCode();
            outputHelper?.WriteLine(code);
            return(cg);
        }
コード例 #6
0
 public static Module CompileString(string source, List <CompilerError> errors)
 {
     CompilerErrors.Clear();
     return(Oberon0Compiler.CompileString(
                source,
                new Oberon0CompilerOptions
     {
         InitParser = parser =>
         {
             parser.RemoveErrorListeners();
             parser.AddErrorListener(TestErrorListener.Instance);
         },
         InitLexer = lexer =>
         {
             lexer.RemoveErrorListeners();
             lexer.AddErrorListener(TestErrorListener.Instance);
         },
         AfterCompile = compiler => { errors.AddRange(CompilerErrors); }
     }));
 }