Exemple #1
0
        static void Main(string[] args)
        {
            string fileName = "tests/string-test.rk";

            /* Lexer lexer = new Lexer(fileName);
             * Token next = lexer.Next();
             * while(next.Type != TokenType.EOF) {
             *  Console.WriteLine(next);
             *  next = lexer.Next();
             * } */
            Parser     parser = new Parser(fileName);
            List <AST> trees  = parser.Parse();

            Tree.Env.Environment env = Tree.Env.Environment.Scope();
            env.Set("Print", new Print(new List <string>(new string[] { "value" })));
            env.Set("Read", new Read(new List <string>(new string[] { "prompt" })));
            foreach (AST tree in trees)
            {
                tree.Evaluate(env);
            }

            /* while(token.Type != TokenType.EOF) {
             *  Console.WriteLine(token);
             *  token = lexer.Next();
             * } */

            /* string n = ".345";
             *
             * double result;
             * Console.WriteLine(n);
             * double.TryParse(n, out result);
             * Console.WriteLine(result); */
        }
Exemple #2
0
        public override dynamic Evaluate(Env.Environment env)
        {
            /**
             * Should create a new environment scope
             * Assign all arguments to function paramenter names in order
             * Store them in the new Environment scope
             * Evaluate the function from the environment passing the new scope
             */
            Env.Environment scope    = Env.Environment.Scope(env);
            Function        function = env.Get(this.name);

            if (this.arguments.Count != function.Parameters.Count)
            {
                this.Error("Expected " + function.Parameters.Count + " parameters to function " + this.name + " call. Found " + this.arguments.Count + " parameters.", 0, 0);
            }
            for (int i = 0; i < this.arguments.Count; i++)
            {
                scope.Set(function.Parameters[i], this.arguments[i].Evaluate(env));
            }
            return(function.Solve(scope));
        }
Exemple #3
0
        public override dynamic Evaluate(Env.Environment env)
        {
            if (this.right.Type == TreeType.LIST)
            {
                Tree.List  right = this.right as List;
                List <AST> items = new List <AST>();
                foreach (AST item in right.Items)
                {
                    items.Add(item.Evaluate(env));
                }
                this.right = new Tree.List(items);
            }
            AST final = this.right.Evaluate(env);

            if (final.Type == TreeType.LIST_ACCESS)
            {
                final = final.Evaluate(env);
            }
            env.Set(this.left, final);
            return(this.right);
        }