Beispiel #1
0
        public void TestForBreak()
        {
            string compileTest = @"
module Program
{
    function main(array : int[]) : int
    {
        var val : int = 0;
        for(i in array)
        {
            val = val + i;
            break;
        }
        return val;
    }
}
";

            var lexResults   = lexer.Lex(new Document(compileTest, "")).GetAwaiter().GetResult();
            var parseResults = parser.Parse(lexResults).GetAwaiter().GetResult();

            var compileUnit    = new CompileUnit(new [] { parseResults });
            var compileResults = compileUnit.Compile();

            System.Console.WriteLine();

            foreach (var err in compileResults.Errors)
            {
                System.Console.WriteLine($"ERROR: {err.Message}");
            }

            Assert.IsTrue(compileResults.Errors.Length == 0);

            byte[] image = compileResults.Output.Serialize();
            System.Console.WriteLine($"Compiled Cozi IL image - {image.Length} byte(s)");

            ILContext context = new ILContext(image);
            CoziVM    vm      = new CoziVM(context);

            foreach (var m in context.Modules)
            {
                System.Console.WriteLine(m.ToString());
            }

            var intType     = context.GlobalTypes.GetType("int");
            var intArrayRef = vm.NewArray(intType, 3);

            vm.Pin(intArrayRef.SrcPointer);

            vm.SetElement <int>(intArrayRef, 0, 24);
            vm.SetElement <int>(intArrayRef, 1, 49);
            vm.SetElement <int>(intArrayRef, 2, 7);

            Assert.IsTrue(vm.BeginInvoke("Program", "main", out var ctx));
            ctx.PushArg <VMSlice>(intArrayRef, 0);
            Assert.IsTrue(ctx.Invoke <int>() == 24);
        }
Beispiel #2
0
        public void BasicTest2()
        {
            string compileTest = @"
module Program
{
    function main(myArg : string[], myIdx : int) : string
    {
        return myArg[myIdx];
    }
}
";

            var lexResults   = lexer.Lex(new Document(compileTest, "")).GetAwaiter().GetResult();
            var parseResults = parser.Parse(lexResults).GetAwaiter().GetResult();

            var compileUnit    = new CompileUnit(new [] { parseResults });
            var compileResults = compileUnit.Compile();

            System.Console.WriteLine();

            foreach (var err in compileResults.Errors)
            {
                System.Console.WriteLine($"ERROR: {err.Message}");
            }

            Assert.IsTrue(compileResults.Errors.Length == 0);

            byte[] image = compileResults.Output.Serialize();
            System.Console.WriteLine($"Compiled Cozi IL image - {image.Length} byte(s)");

            ILContext context = new ILContext(image);
            CoziVM    vm      = new CoziVM(context);

            foreach (var m in context.Modules)
            {
                System.Console.WriteLine(m.ToString());
            }

            var stringType     = context.GlobalTypes.GetType("string");
            var stringArrayRef = vm.NewArray(stringType, 1);

            vm.Pin(stringArrayRef.SrcPointer);

            vm.SetElement <VMSlice>(stringArrayRef, 0, vm.NewString("Hello, Cozi!"));

            Assert.IsTrue(vm.BeginInvoke("Program", "main", out var ctx));
            ctx.PushArg <int>(0, 1);
            ctx.PushArg <VMSlice>(stringArrayRef, 0);
            var returnVal = vm.AsString(ctx.Invoke <VMSlice>());

            Assert.IsTrue(returnVal == "Hello, Cozi!");
        }