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); }
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()); } }
public void AssertSuccessCall() { string errorMessage = null; var error = cflat.GetRuntimeError(); if (error.isSome) { errorMessage = cflat.GetFormattedCompileErrors(); } Assert.Null(errorMessage); if (CompilerMode == Mode.Release) { cflat.vm.debugData.stackTypes.PushBack(new ValueType(TypeKind.Unit)); } Assert.Single(cflat.vm.debugData.stackTypes.ToArray()); }
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 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); }
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); }
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(); }