Example #1
0
        public EarleCompiler(EarleRuntime runtime)
        {
            if (runtime == null) throw new ArgumentNullException(nameof(runtime));
            _runtime = runtime;

            InitializeGrammarProcessor();
        }
Example #2
0
 public EarleStackFrame(EarleRuntime runtime, EarleFunction function, IEarleStackFrameExecutor executor,
     int callerIp,
     EarleStackFrame superFrame, EarleThread thread)
 {
     if (runtime == null)
         throw new ArgumentNullException(nameof(runtime));
     if (thread == null)
         throw new ArgumentNullException(nameof(thread));
     Runtime = runtime;
     ParentFrame = superFrame;
     Executor = executor;
     CallerIP = callerIp;
     Function = function;
     Thread = thread;
 }
Example #3
0
        private static void Main(string[] args)
        {
            //Console.WriteLine(4 << 1 + 1);//16
            //Console.WriteLine(4 | 1 << 1 + 1);//4
            //Console.WriteLine(4 * 3 == 12);//true
            //Console.WriteLine(3 & 1 | 2 << 2);//9
            //Console.WriteLine(1 < 3 == 3 < 1);//false

            var runtime = new EarleRuntime();
            runtime.GlobalVariables["level"] = new EarleSimpleEventableStructure().ToEarleValue();

            // Load code
            var swc = Stopwatch.StartNew();
            var codeDir = Path.Combine(Directory.GetCurrentDirectory(), "code");
            foreach (var file in Directory.GetFiles(codeDir, "*.earle", SearchOption.AllDirectories))
            {
                var rel = GetRelativePath(file, codeDir).Replace('/', '\\');
                rel = '\\' + rel.Substring(0, rel.Length - 6);
                runtime.CompileFile(rel, File.ReadAllText(file));
            }
            swc.Stop();
            Console.WriteLine($"Compiling took: {swc.Elapsed}");

            // Add localization
            foreach (var file in Directory.GetFiles(codeDir, "*.estr", SearchOption.AllDirectories))
            {
                var rel = GetRelativePath(file, codeDir).Replace('/', '\\');
                rel = rel.Substring(0, rel.Length - 5);
                runtime.Localizer.LoadFromFile(rel, File.ReadAllText(file), Path.GetFileNameWithoutExtension(file).ToUpper() + "_");
            }
            runtime.Localizer.Key = "LANG_ENGLISH";

            foreach (var l in runtime.GetDebugInformation())
                Console.WriteLine(l);

            runtime.Natives.Register(EarleNativeFunction.Create("printstacktrace", (EarleStackFrame frame) =>
            {
                var trace = frame.GetStackTrace();
                Console.WriteLine(trace);
            }));

            var test = runtime["\\main"]["test"];

            if (test != null)
            {
                var count = runtime["\\main"]["gettestcount"]?
                    .Invoke(null, EarleValue.Undefined)?
                    .CastTo<int>() ?? 10000;

                var sw = Stopwatch.StartNew();
                for (var i = 0; i < count; i++)
                {
                    test.Invoke(null, EarleValue.Undefined);
                }
                sw.Stop();

                Console.WriteLine($"Running {count} times took {sw.Elapsed}");
            }
            else
            {
                // Invoke main::init
                runtime["\\main"]["init"].Invoke(result =>
                {
                    Console.WriteLine();
                    Console.WriteLine("Code execution completed!");
                    Console.WriteLine("Result: " + result);

                    if (result.Is<EarleStructure>())
                    {
                        var struc = result.As<EarleStructure>();
                        foreach (var kv in struc)
                            Console.WriteLine($"> {kv.Key} = {kv.Value}");
                    }
                    else if (result.Is<EarleArray>())
                    {
                        var arr = result.As<EarleArray>();
                        foreach (var v in arr)
                            Console.WriteLine(v);
                    }
                }, EarleValue.Undefined);

                do
                {
                    Thread.Sleep(1000/30);
                } while (!runtime.Tick());
            }

            Console.WriteLine("Done!");
        }