Exemplo n.º 1
0
 static void Main(string[] args)
 {
     var interpreter = new AphidInterpreter();
     interpreter.Interpret("add = @(x, y) x + y;");
     var x = interpreter.CallFunction("add", 3, 7).Value;
     Console.WriteLine(x);
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                DisplayDirections();
            }
            else if (!File.Exists(args[0]))
            {
                Console.WriteLine("Could not find {0}", args[0]);
                Environment.Exit(1);
            }

            var code = File.ReadAllText(args[0]);

            EnvironmentLibrary.SetEnvArgs(true);

            var interpreter = new AphidInterpreter();

            try
            {
                interpreter.Interpret(code);
            }
            catch (AphidParserException exception)
            {
                Console.WriteLine(ParserErrorMessage.Create(code, exception));
            }
            catch (AphidRuntimeException exception)
            {
                Console.WriteLine("Unexpected runtime exception\r\n\r\n{0}\r\n", exception.Message);
            }
        }
        private AphidObject Execute(string script)
        {
            script = PrefixScript(script);

            var interpreter = new AphidInterpreter();
            interpreter.Loader.SearchPaths.Add(Path.Combine(Environment.CurrentDirectory, "Library"));
            interpreter.Interpret(script);
            return interpreter.GetReturnValue();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var interpreter = new AphidInterpreter();

            interpreter.Interpret(@"
                #'Std';
                print('Hello, world');
            ");
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var interpreter = new AphidInterpreter();
            interpreter.CurrentScope.Add("x", new AphidObject("foo"));

            interpreter.Interpret(@"
                #'Std';
                print(x);
            ");
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var interpreter = new AphidInterpreter();

            interpreter.Interpret(@"
                #'Std';
                ret {
                    name: 'My Widget',
                    location: { x: 10, y: 20 }
                };
            ");

            var widget = interpreter.GetReturnValue().ConvertTo<Widget>();
            Console.WriteLine(widget);
            widget.Location.X = 40;
            var aphidWidget = AphidObject.ConvertFrom(widget);
            interpreter.CurrentScope.Add("w", aphidWidget);
            interpreter.Interpret(@"printf('New X value: {0}', w.location.x);");
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var interpreter = new AphidInterpreter();

            interpreter.Interpret(@"
                #'Std';
                add = @(x, y) x + y;
                print(add(3, 7));
            ");
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            var interpreter = new AphidInterpreter();

            interpreter.Interpret(@"
                #'Std';
                call = @(func) func();
                foo = @() print('foo() called');
                call(foo);
            ");
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var interprer = new AphidInterpreter();
            interprer.Loader.LoadModule(Assembly.GetExecutingAssembly());

            interprer.Interpret(@"
                #'Std';
                ##'InteropFunctionSample.AphidMath';
                print(math.add(3, 7));
            ");
        }
Exemplo n.º 10
0
        public void LoadScript(string scriptFile)
        {
            var f = FindScriptFile(scriptFile);

            if (f != null)
            {
                _interpreter.Interpret(File.ReadAllText(f));
            }
            else
            {
                throw new AphidRuntimeException("Cannot find script {0}", scriptFile);
            }
        }
Exemplo n.º 11
0
        public AphidObject Deserialize(string obj)
        {
            var lexer = new AphidObjectLexer(obj);
            var tokens = lexer.GetTokens();
            tokens.Add(new AphidToken(AphidTokenType.EndOfStatement, null, 0));
            var ast = new AphidParser(tokens).Parse();

            if (ast.Count != 1)
            {
                throw new AphidRuntimeException("Invalid Aphid object string: {0}", obj);
            }

            ast[0] = new UnaryOperatorExpression(AphidTokenType.retKeyword, ast[0]);
            var objInterpreter = new AphidInterpreter();
            objInterpreter.Interpret(ast);
            return objInterpreter.GetReturnValue();
        }
Exemplo n.º 12
0
        public void LoadScript(string scriptFile)
        {
            var f = FindScriptFile(scriptFile);

            if (f != null)
            {
                if (f.Contains(Path.DirectorySeparatorChar))
                {
                    var dir = Path.GetFullPath(Path.GetDirectoryName(f));

                    if (!_searchPaths.Contains(dir))
                    {
                        _searchPaths.Add(dir);
                    }
                }

                _interpreter.Interpret(File.ReadAllText(f));
            }
            else
            {
                throw new AphidRuntimeException("Cannot find script {0}", scriptFile);
            }
        }
Exemplo n.º 13
0
 static void Main(string[] args)
 {
     var interpreter = new AphidInterpreter();
     interpreter.Interpret("x = 'foo';");
     Console.WriteLine(interpreter.CurrentScope["x"].Value);
 }
Exemplo n.º 14
0
 private static object Eval(AphidInterpreter interpreter, string code)
 {
     interpreter.EnterChildScope();
     interpreter.Interpret(code);
     var retVal = interpreter.GetReturnValue();
     interpreter.LeaveChildScope();
     return retVal;
 }
Exemplo n.º 15
0
        static void RunTest(PerfTest test, int iterations)
        {
            var interpreter = new AphidInterpreter();

            if (test.Prologue != null)
            {
                interpreter.Interpret(test.Prologue);
            }

            var ast = new AphidParser(new AphidLexer(test.Body).GetTokens()).Parse();

            for (int i = 0; i < iterations; i++)
            {
                interpreter.Interpret(ast);
            }
        }