Пример #1
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()));
    }
Пример #2
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();
    }