Esempio n. 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); */
        }
Esempio n. 2
0
        public virtual dynamic Solve(Env.Environment env)
        {
            dynamic result = new Tree.Null();

            foreach (AST line in this.bodyStatements)
            {
                result = line.Evaluate(env);
            }
            return(result);
        }
Esempio n. 3
0
        public override dynamic Solve(Env.Environment env)
        {
            Env.Environment environ = Env.Environment.Scope(env);
            dynamic         value   = environ.Get(this.Parameters[0]);
            string          input   = "";

            using (StreamWriter writer = new StreamWriter(Console.OpenStandardOutput())) {
                writer.Write(value.Value.ToString());
            }
            using (StreamReader reader = new StreamReader(Console.OpenStandardInput())) {
                input = reader.ReadLine();
                reader.Close();
            }
            return(new Tree.String(input));
        }
Esempio n. 4
0
        public override dynamic Evaluate(Env.Environment env)
        {
            this.env = env;
            dynamic      result = Solve(CreatePostFix());
            FunctionCall call   = result as FunctionCall;

            if (call != null)
            {
                this.Value = call.Evaluate(env);
            }
            else
            {
                this.Value = result;
            }
            return(this.Value);
        }
Esempio n. 5
0
 public override dynamic Evaluate(Env.Environment env)
 {
     this.print = "[";
     foreach (AST item in this.Items)
     {
         if (item.Type == TreeType.LIST)
         {
             this.print += item.Evaluate(env).ToString();
         }
         else
         {
             this.print += item.Evaluate(env).Value.ToString();
         }
         this.print += ",";
     }
     this.print  = this.print.TrimEnd(new char[] { ',' });
     this.print += "]";
     return(this);
 }
Esempio n. 6
0
        public override dynamic Evaluate(Env.Environment env)
        {
            if (this.right.Type == TreeType.LIST_MODIFY)
            {
                ListModify modifier = this.right as ListModify;
                env.ChangeList(left, modifier.Index.Evaluate(env).Value, modifier.Right.Evaluate(env));
                return(modifier.Right);
            }
            dynamic value = this.right.Evaluate(env);

            if (this.right.Type == TreeType.EXPRESSION)
            {
                env.Change(this.left, value);
            }
            else
            {
                env.Change(this.left, this.right);
            }
            return(value);
        }
Esempio n. 7
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));
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public override dynamic Solve(Env.Environment env)
        {
            Env.Environment environ = Env.Environment.Scope(env);
            AST             value   = environ.Get(this.Parameters[0]);
            dynamic         print   = "";

            if (value.Type == TreeType.LIST)
            {
                print = value.Evaluate(environ).ToString();
            }
            else if (value.Type == TreeType.LIST_ACCESS)
            {
                print = value.Evaluate(env).ToString();
            }
            else
            {
                print = value.Evaluate(environ).Value;
            }
            using (StreamWriter writer = new StreamWriter(Console.OpenStandardOutput())) {
                writer.Write(print.ToString() + "\n");
            }
            return(value);
        }
Esempio n. 10
0
 public override dynamic Evaluate(Env.Environment env)
 {
     return(this);
 }
Esempio n. 11
0
 public virtual dynamic Solve(Env.Environment env)
 {
     return(0);
 }
Esempio n. 12
0
 public override dynamic Evaluate(Env.Environment env)
 {
     //We only expect one argument
     return(this);
 }