示例#1
0
    public void IntArrayIndexingError(string source)
    {
        var cflat = new CFlat();
        var v     = TestHelper.RunExpression <Int>(cflat, source, out var a);

        Assert.True(cflat.GetRuntimeError().isSome);
    }
        public static bool AddFunction <A0, A1, A2, A3, R>(this CFlat self, string functionName, System.Func <A0, A1, A2, A3, R> function)
            where A0 : struct, IMarshalable
            where A1 : struct, IMarshalable
            where A2 : struct, IMarshalable
            where A3 : struct, IMarshalable
            where R : struct, IMarshalable
        {
            var builder = new FunctionTypeBuilder(self.chunk);

            builder.WithParam(Marshal.TypeOf <A0>(self.chunk));
            builder.WithParam(Marshal.TypeOf <A1>(self.chunk));
            builder.WithParam(Marshal.TypeOf <A2>(self.chunk));
            builder.WithParam(Marshal.TypeOf <A3>(self.chunk));
            builder.returnType = Marshal.TypeOf <R>(self.chunk);
            return(FinishAddFunction(self, builder, functionName, (vm, top) =>
            {
                var functionInterface = new FunctionInterface(vm, top);
                FunctionInterface.Return(vm, function(
                                             functionInterface.Arg <A0>(),
                                             functionInterface.Arg <A1>(),
                                             functionInterface.Arg <A2>(),
                                             functionInterface.Arg <A3>()
                                             ));
            }));
        }
示例#3
0
    public void AddClassTwiceTest()
    {
        var cflat = new CFlat();

        cflat.AddClass <MyClass>();
        cflat.AddClass <MyClass>();
    }
示例#4
0
    public void TupleIoTest(string source, int n, bool b)
    {
        var cflat = new CFlat();

        cflat.AddFunction <Tuple <Int, Bool>, Tuple <Int, Bool> >(nameof(TupleTestFunction), TupleTestFunction);
        var t = TestHelper.RunExpression <Tuple <Int, Bool> >(cflat, source, out var a);

        a.AssertSuccessCall();
        Assert.Equal(n, t.e0.value);
        Assert.Equal(b, t.e1.value);
    }
        public static bool AddFunction <R>(this CFlat self, string functionName, System.Func <R> function)
            where R : struct, IMarshalable
        {
            var builder = new FunctionTypeBuilder(self.chunk);

            builder.returnType = Marshal.TypeOf <R>(self.chunk);
            return(FinishAddFunction(self, builder, functionName, (vm, top) =>
            {
                FunctionInterface.Return(vm, function());
            }));
        }
示例#6
0
    public static string CompileExpression(string source)
    {
        var cflat         = new CFlat();
        var compileErrors = cflat.CompileExpression(source, TestHelper.CompilerMode);

        if (compileErrors.count > 0)
        {
            return("COMPILE ERROR: " + cflat.GetFormattedCompileErrors());
        }
        return(null);
    }
示例#7
0
    public void StructIoTest(string source, int x, int y, int z)
    {
        var cflat = new CFlat();

        cflat.AddFunction <Struct <MyStruct>, Struct <MyStruct> >(nameof(StructTestFunction), StructTestFunction);
        var s = TestHelper.RunExpression <Struct <MyStruct> >(cflat, source, out var a).value;

        a.AssertSuccessCall();
        Assert.Equal(x, s.x);
        Assert.Equal(y, s.y);
        Assert.Equal(z, s.z);
    }
示例#8
0
    public void MarshalNestingStructTest(string source, int x, int y, int z, bool b)
    {
        var cflat = new CFlat();

        cflat.AddStruct <MyNestingStruct>();
        var s = TestHelper.RunExpression <Struct <MyNestingStruct> >(cflat, source, out var a).value;

        a.AssertSuccessCall();
        Assert.Equal(x, s.a.value.x);
        Assert.Equal(y, s.a.value.y);
        Assert.Equal(z, s.a.value.z);
        Assert.Equal(b, s.b);
    }
示例#9
0
    public void CompileMultipleSourcesErrors(string source0, string source1)
    {
        var sources = new Source[] {
            new Source(new Uri("source0"), source0),
            new Source(new Uri("source1"), source1)
        };
        var cflat          = new CFlat();
        var moduleResolver = new ModuleResolver(sources);

        var errors = cflat.CompileSource(sources[0], TestHelper.CompilerMode, moduleResolver);

        Assert.NotEqual(0, errors.count);
    }
示例#10
0
    public void AddStructTwiceTest()
    {
        var source = "MyStruct{x=0,y=0,z=0}";
        var cflat  = new CFlat();

        cflat.AddStruct <MyStruct>();
        cflat.AddStruct <MyStruct>();
        var s = TestHelper.RunExpression <Struct <MyStruct> >(cflat, source, out var a).value;

        a.AssertSuccessCall();
        Assert.Equal(0, s.x);
        Assert.Equal(0, s.y);
        Assert.Equal(0, s.z);
    }
示例#11
0
    public void CompileMultipleSourcesTests(string source0, string source1)
    {
        var sources = new Source[] {
            new Source(new Uri("source0"), source0),
            new Source(new Uri("source1"), source1)
        };
        var cflat          = new CFlat();
        var moduleResolver = new ModuleResolver(sources);

        var errors = cflat.CompileSource(sources[0], TestHelper.CompilerMode, moduleResolver);

        if (errors.count > 0)
        {
            throw new CompileErrorException(cflat.GetFormattedCompileErrors());
        }
    }
        private static bool FinishAddFunction(CFlat self, FunctionTypeBuilder builder, string functionName, NativeFunction.Callback function)
        {
            var result = builder.Build(out var typeIndex);

            if (!self.compiler.io.CheckFunctionBuild(result, new Slice()))
            {
                return(false);
            }

            self.chunk.nativeFunctions.PushBack(new NativeFunction(
                                                    functionName,
                                                    typeIndex,
                                                    builder.returnType.GetSize(self.chunk),
                                                    function
                                                    ));
            return(true);
        }
示例#13
0
    public static R RunExpression <R>(CFlat cflat, string source, out CallAssertion assertion)
        where R : struct, IMarshalable
    {
        var compileErrors = cflat.CompileExpression(source, CompilerMode);

        if (compileErrors.count > 0)
        {
            throw new CompileErrorException(cflat.GetFormattedCompileErrors());
        }

        assertion = new CallAssertion(source, cflat);
        var function = cflat.GetFunction <Empty, R>(string.Empty);

        if (!function.isSome)
        {
            throw new FunctionNotFoundException();
        }

        return(function.value.Call(cflat, new Empty()));
    }
        public static Option <Function <A, R> > GetFunction <A, R>(this CFlat self, string functionName)
            where A : struct, ITuple
            where R : struct, IMarshalable
        {
            if (self.compiler.io.errors.count > 0)
            {
                return(Option.None);
            }

            var marshaler = new FunctionDefinitionMarshaler <A, R>(self.chunk);

            ValueType type;

            try
            {
                type = marshaler.GetDefinedType();
            }
            catch (Marshal.InvalidReflectionException)
            {
                return(Option.None);
            }

            for (var i = 0; i < self.chunk.functions.count; i++)
            {
                var function = self.chunk.functions.buffer[i];
                if (function.name == functionName && function.typeIndex == type.index)
                {
                    return(Option.Some(new Function <A, R>(
                                           function.codeIndex,
                                           (ushort)i,
                                           self.chunk.functionTypes.buffer[function.typeIndex].parametersSize
                                           )));
                }
            }

            return(Option.None);
        }
示例#15
0
    public void NestedTupleTest(string source, int[] expectedSlices, TypeKind[] expectedElementKinds)
    {
        var cflat = new CFlat();

        string error  = null;
        var    errors = cflat.CompileSource(new Source(new Uri("source"), source), TestHelper.CompilerMode, Option.None);

        if (errors.count > 0)
        {
            error = cflat.GetFormattedCompileErrors();
        }
        Assert.Null(error);

        var sliceCount = expectedSlices.Length / 2;

        Assert.Equal(sliceCount, cflat.chunk.tupleTypes.count);
        Assert.Equal(expectedElementKinds.Length, cflat.chunk.tupleElementTypes.count);

        var slices = new int[cflat.chunk.tupleTypes.count * 2];

        for (var i = 0; i < sliceCount; i++)
        {
            var tupleType = cflat.chunk.tupleTypes.buffer[i];
            slices[i * 2]     = tupleType.elements.index;
            slices[i * 2 + 1] = tupleType.elements.length;
        }
        Assert.Equal(expectedSlices, slices);

        var elementKinds = new TypeKind[cflat.chunk.tupleElementTypes.count];

        for (var i = 0; i < elementKinds.Length; i++)
        {
            var elementType = cflat.chunk.tupleElementTypes.buffer[i];
            elementKinds[i] = elementType.kind;
        }
        Assert.Equal(expectedElementKinds, elementKinds);
    }
示例#16
0
    public void NestedFunctionExpressionTest(string source, int[] expectedSlices, TypeKind[] expectedParamsKinds)
    {
        var cflat = new CFlat();

        string error  = null;
        var    errors = cflat.CompileSource(new Source(new Uri("source"), source), TestHelper.CompilerMode, Option.None);

        if (errors.count > 0)
        {
            error = cflat.GetFormattedCompileErrors();
        }
        Assert.Null(error);

        var sliceCount = expectedSlices.Length / 2;

        Assert.Equal(sliceCount, cflat.chunk.functionTypes.count);
        Assert.Equal(expectedParamsKinds.Length, cflat.chunk.functionParamTypes.count);

        var slices = new int[cflat.chunk.functionTypes.count * 2];

        for (var i = 0; i < sliceCount; i++)
        {
            var functionType = cflat.chunk.functionTypes.buffer[i];
            slices[i * 2]     = functionType.parameters.index;
            slices[i * 2 + 1] = functionType.parameters.length;
        }
        Assert.Equal(expectedSlices, slices);

        var fieldKinds = new TypeKind[cflat.chunk.functionParamTypes.count];

        for (var i = 0; i < fieldKinds.Length; i++)
        {
            var functionParamType = cflat.chunk.functionParamTypes.buffer[i];
            fieldKinds[i] = functionParamType.kind;
        }
        Assert.Equal(expectedParamsKinds, fieldKinds);
    }
示例#17
0
 public CallAssertion(string source, CFlat cflat)
 {
     this.source = source;
     this.cflat  = cflat;
 }
示例#18
0
    public static void RunSource(string sourcePath, string sourceContent, bool printDisassembled)
    {
        var filename = Path.GetFileNameWithoutExtension(sourcePath);
        var source   = new Source(new Uri(filename), sourceContent);

        /*
         * var debugger = new EmbeddedDebugger((s, v) =>
         * {
         *      EmbeddedDebugger.Break();
         * });
         * /*/
        var debugger = new DebugServer(DebugServer.DefaultPort);

        debugger.StartPaused();
        //*/

        var cflat = new CFlat();

        //cflat.SetDebugger(debugger);

        cflat.AddFunction <Class <Stopwatch> >(nameof(StartStopwatch), StartStopwatch);
        cflat.AddFunction <Class <Stopwatch>, Float>(nameof(StopStopwatch), StopStopwatch);

        var compileErrors = cflat.CompileSource(source, Mode.Debug, Option.None);

        if (compileErrors.count > 0)
        {
            var errorMessage = cflat.GetFormattedCompileErrors();
            ConsoleHelper.Error("COMPILER ERROR\n");
            ConsoleHelper.Error(errorMessage);
            ConsoleHelper.LineBreak();

            System.Environment.ExitCode = 65;
            return;
        }

        if (printDisassembled)
        {
            ConsoleHelper.Write(cflat.Disassemble());
            ConsoleHelper.LineBreak();
        }

        var main = cflat.GetFunction <Empty, Unit>("main");

        if (main.isSome)
        {
            System.Console.WriteLine("RESULT: {0}", main.value.Call(cflat, new Empty()));
        }
        else
        {
            System.Console.WriteLine("NOT FOUNDED");
        }

        var runtimeError = cflat.GetRuntimeError();

        if (runtimeError.isSome)
        {
            var errorMessage = cflat.GetFormattedRuntimeError();
            ConsoleHelper.Error("RUNTIME ERROR\n");
            ConsoleHelper.Error(errorMessage);
            ConsoleHelper.LineBreak();
            ConsoleHelper.Error(cflat.TraceCallStack());

            System.Environment.ExitCode = 70;
        }
        else
        {
            System.Environment.ExitCode = 0;
        }

        ConsoleHelper.LineBreak();
    }
 public static void AddClass <T>(this CFlat self) where T : class
 {
     Marshal.TypeOf <Class <T> >(self.chunk);
 }
 public static void AddStruct <T>(this CFlat self) where T : struct, IStruct
 {
     Marshal.TypeOf <Struct <T> >(self.chunk);
 }
示例#21
0
 public R Call(CFlat cflat, A arguments)
 {
     return(Call(cflat.vm, arguments));
 }