Esempio n. 1
0
        public static void ReplMode()
        {
            var environment = new ReplEnvironment(new OperationCodeFactory(), new ValueFactory());
            var interpreter = new ReplInterpreter();

            environment.AddClassesDerivedFromClassInAssembly(typeof(Eilang));
            environment.AddExportedFunctionsFromAssembly(typeof(Eilang));
            environment.AddExportedModulesFromAssembly(typeof(Eilang));

            while (true)
            {
                Console.Write(">");
                var code = Console.ReadLine() ?? "";
                if (string.IsNullOrWhiteSpace(code))
                {
                    continue;
                }
                try
                {
                    code = Simplify(code);
                    var reader = new ScriptReader(code, "repl");
                    var lexer  = new ScriptLexer(reader, new CommonLexer(reader));
                    var parser = new Parser(lexer);
                    var ast    = parser.Parse();

                    Compiler.Compile(environment, ast);

                    var eval = interpreter.Interpret(environment);
                    if (eval.Type != EilangType.Void)
                    {
                        ExportedFunctions.PrintLine(new State(environment, null, null, null, null, new ValueFactory()), Arguments.Create(eval, "eval"));
                    }
                }
                catch (ExitException e)
                {
                    if (!string.IsNullOrWhiteSpace(e.Message))
                    {
                        var oldColor = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(e.Message);
                        Console.ForegroundColor = oldColor;
                    }
                    Environment.Exit(e.ExitCode);
                }
                catch (ErrorMessageException e)
                {
                    Program.LogLine(ConsoleColor.Red, e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Esempio n. 2
0
        public void AddExportedFunctionsFromClass(Type type)
        {
            var functions = type.GetMethods()
                            .Where(m => m.GetCustomAttributes <ExportFunctionAttribute>().Any());

            foreach (var func in functions)
            {
                var names = func.GetCustomAttributes <ExportFunctionAttribute>();
                foreach (var name in names)
                {
                    ExportedFunctions.Add(name.FunctionName,
                                          (ExportedFunction)func.CreateDelegate(typeof(ExportedFunction)));
                }
            }
        }
Esempio n. 3
0
        public void AddExportedModule(Type type)
        {
            var moduleName = type.GetCustomAttribute <ExportModuleAttribute>().ModuleName;
            var functions  = type.GetMethods()
                             .Where(m => m.CustomAttributes.Any(a =>
                                                                ReferenceEquals(a.AttributeType, typeof(ExportFunctionAttribute))));

            foreach (var func in functions)
            {
                var names = func.GetCustomAttributes <ExportFunctionAttribute>();
                foreach (var name in names)
                {
                    var nameWithModule = $"{moduleName}::{name.FunctionName}";
                    ExportedFunctions.Add(nameWithModule,
                                          (ExportedFunction)func.CreateDelegate(typeof(ExportedFunction)));
                }
            }
        }