Ejemplo n.º 1
0
        public Parser(Runtime runtime, Scanner scanner)
        {
            this.runtime = runtime;
            this.scanner = scanner;

            scopeManager = new ScopeManager(runtime);
        }
Ejemplo n.º 2
0
        public Runtime()
        {
            unaryOps = new Dictionary<ExpressionType, UnaryOperationBinder>();
            binaryOps = new Dictionary<ExpressionType, BinaryOperationBinder>();
            InvokeCallSite = new InvokeBinder();
            ConvertCallSite = new ConvertBinder();
            GetIndexCallSite = new GetIndexBinder();
            SetIndexCallSite = new SetIndexBinder();

            BuiltIns = new Dictionary<string, Expression>();

            RegisterClass("Foo", null);
            RegisterFunction("range", args =>
                {
                    return Iter.Range(((Number)args[0]).Value, ((Number)args[1]).Value);
                });
            RegisterFunction("using", args =>
                {
                    var filename = args[0].ToString();
                    var scanner = new Scanner(File.ReadAllText(filename));
                    var parser = new Parser(this, scanner);
                    var expr = parser.Parse();

                    var func = expr.Compile();
                    return func();
                });
            RegisterFunction("print", args => Console.WriteLine(string.Join(" ", args)));

            RegisterModule("maths", new Dictionary<string, Any>
                {
                    {"PI", new Number(Math.PI)},
                    {"cos", new Function(args => new Number(Math.Cos(((Number) args[0]).Value)))},
                    {"sin", new Function(args => new Number(Math.Sin(((Number) args[0]).Value)))},

                });
        }