Exemplo n.º 1
0
        public void Run()
        {
            ASTVisualizer visualizer = SheInfo.Visualize ? new ASTVisualizer() : null;

            using (var reader = new StreamReader(filePath))
            {
                Lexer             l   = new Lexer(reader);
                SheParser         sp  = new SheParser();
                NestedEnvironment env = new NestedEnvironment();
                Natives.AppendNatives(env);
                while (l.Peek(0) != Token.EOF)
                {
                    ASTree ast = sp.Parse(l);
                    // Console.WriteLine(ast);
                    Console.WriteLine(ast.Eval(env));
                    if (SheInfo.Visualize)
                    {
                        visualizer.Push(ast);
                    }
                }
            }
            if (SheInfo.Visualize)
            {
                visualizer.Visualize($"INTPRT_{SheInfo.StartTime:yyyy-MM-dd-HH-mm-ss}");
            }
        }
Exemplo n.º 2
0
        private void runBtn_Click(object sender, EventArgs e)
        {
            consoleText.Clear();
            Lexer       lexer   = new Lexer(new StringReader(codeText.Text));
            RavParser   parser  = new RavParser();
            EvalVisitor visitor = new EvalVisitor();

            visitor.DebugOption = false;
            IEnvironment env = new Natives().Enviroment(new NestedEnv());

            try {
                while (lexer.Peek(0) != Token.EOF)
                {
                    ASTree ast = parser.Parse(lexer);
                    //System.Console.WriteLine("  >>> " + ast.GetType().ToString() + " " + ast.ToString());
                    //consoleText.AppendText("> " + ast.ToString() + "\n");
                    ast.Accept(visitor, env);
                    consoleText.AppendText("> " + visitor.Result.ToString() + "\n");
                }
            }
            catch (ParseException ex) {
                consoleText.AppendText("> " + ex.Message + "\n");
            }

            /*
             * for (Token tok = lexer.Read(); tok != Token.EOF; tok = lexer.Read()) {
             *  consoleText.AppendText("> " + tok.Text + "\n");
             * }
             */
        }
Exemplo n.º 3
0
 public void Visit(ASTree t, IEnvironment env)
 {
     if (debug)
     {
         System.Console.WriteLine("ASTree: " + t.GetType().ToString());
     }
     result = new ErrorValue("cannot eval: [ASTree]");
     return;
 }
Exemplo n.º 4
0
 public Optimizer(Assembly assembly)
 {
     program = assembly.GetTree();
     OptimizeMemoryCalls();
     OptimizeRegistersTransactions();
     if (Program.verboseMode)
     {
         program.PrintCode();
     }
 }
Exemplo n.º 5
0
        private object ComputeAssign(BinaryExpr t, IEnvironment env, object rvalue)
        {
            ASTree l = t.Left;

            if (l is Name)
            {
                env.Put((l as Name).Text, rvalue);
                return(rvalue);
            }
            else if (l is PrimaryExpr)
            {
                PrimaryExpr p = l as PrimaryExpr;
                if (p.HasPostfix(0) && (p.Postfix(0) is Dot))
                {
                    p.Accept(this, env, 1);
                    object tp = result;
                    if (result is ErrorValue)
                    {
                        return(result);
                    }
                    if (tp is RavObject)
                    {
                        return(SetField(t, (tp as RavObject), (p.Postfix(0) as Dot), rvalue));
                    }
                }
                else if (p.HasPostfix(0) && (p.Postfix(0) is ArrayRef))
                {
                    p.Accept(this, env, 1);
                    object a = result;
                    if (result is ErrorValue)
                    {
                        return(result);
                    }
                    if (a is object[])
                    {
                        ArrayRef aref = p.Postfix(0) as ArrayRef;
                        aref.Index.Accept(this, env);
                        if (result is ErrorValue)
                        {
                            return(result);
                        }
                        object index = result;
                        if (index is int)
                        {
                            (a as object[])[(int)index] = rvalue;
                            return(rvalue);
                        }
                    }
                    result = new ErrorValue("bad array access", t);
                    return(result);
                }
            }
            return(new ErrorValue("bad assignment", t));
        }
Exemplo n.º 6
0
            public void Parse(Lexer lexer, List <ASTree> res)
            {
                ASTree     right = factor.Parse(lexer);
                Precedence prec;

                while ((prec = NextOperator(lexer)) != null)
                {
                    right = DoShift(lexer, right, prec.Value);
                }
                res.Add(right);
            }
Exemplo n.º 7
0
 public object Invoke(object[] args, ASTree tree)
 {
     if (method == null)
     {
         return(new EvalException("bad native function call: " + name, tree));
     }
     try {
         return(method.Invoke(null, args));
     }
     catch (Exception) {
         return(new EvalException("bad native function call: " + name, tree));
     }
 }
Exemplo n.º 8
0
            public void Parse(Lexer lexer, List <ASTree> res)
            {
                Token token = lexer.Read();

                if (Test(token))
                {
                    ASTree leaf = factory.Make(token);
                    res.Add(leaf);
                }
                else
                {
                    throw new ParseException(token);
                }
            }
Exemplo n.º 9
0
 public void Parse(Lexer lexer, List <ASTree> res)
 {
     while (parser.Match(lexer))
     {
         ASTree tree = parser.Parse(lexer);
         if (tree.GetType() != typeof(ASTList) || tree.NumChildren() > 0)
         {
             res.Add(tree);
         }
         if (onlyOnce)
         {
             break;
         }
     }
 }
Exemplo n.º 10
0
 public override void Parse(ILexer lexer, List <ASTree> res)
 {
     while (parser.Match(lexer))
     {
         ASTree t = parser.parse(lexer);
         if (t.GetType() != typeof(ASTList) || t.NumChildern > 0)
         {
             res.Add(t);
         }
         if (onlyOnce)
         {
             break;
         }
     }
 }
Exemplo n.º 11
0
        public ASTree GeneratePextCode(string[] arguments)
        {
            ASTree pextTree = new ASTree();

            //Preventing memory loss simply by storing B register content firstly.
            //To do it we have to move arguments which represents B on the first poistion.
            //We will use some simple sorting
            for (int i = 1; i < arguments.Length; i++)
            {
                for (int j = i; j < arguments.Length; j++)
                {
                    if (arguments[j] == "b")
                    {
                        dictLine cache = new dictLine();
                        //save
                        cache.argAddr = placeholders[i - 1];
                        cache.argName = arguments[i];
                        //mov
                        placeholders[i - 1] = placeholders[j - 1];
                        arguments[i]        = arguments[j];
                        //mov from cache
                        placeholders[j - 1] = cache.argAddr;
                        arguments[j]        = cache.argName;
                        break;
                    }
                }
            }
            pextTree.Add(new Swm(mountPoint));
            for (int i = 1; i < arguments.Length; i++)
            {
                if (arguments[i] == "a")
                {
                    pextTree.Add(new Mov("b", "a")); //may cause memory loss (but fixed earlier)
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
                else if (arguments[i] == "b")
                {
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
                else
                {
                    pextTree.Add(new Mov("b", arguments[i]));
                    pextTree.Add(new St(placeholders[i - 1].ToString()));
                }
            }
            pextTree.Add(new Ld(result.ToString()));
            return(pextTree);
        }
Exemplo n.º 12
0
            private ASTree DoShift(Lexer lexer, ASTree left, int prec)
            {
                List <ASTree> list = new List <ASTree>();

                list.Add(left);
                list.Add(new ASTLeaf(lexer.Read()));
                ASTree     right = factor.Parse(lexer);
                Precedence next;

                while ((next = NextOperator(lexer)) != null && RightIsExpr(prec, next))
                {
                    right = DoShift(lexer, right, next.Value);
                }
                list.Add(right);
                return(factory.Make(list));
            }
Exemplo n.º 13
0
        private static void Main(string[] args)
        {
            ASTree ast = new ASTree();
            string src = Console.ReadLine();

            try
            {
                ast.CreateTree(src);
                ast.PrintTree();
                ast.Evaluate();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
Exemplo n.º 14
0
 public void Run()
 {
     try {
         while (lexer.Peek(0) != Token.EOF)
         {
             ASTree ast = parser.Parse(lexer);
             ast.Accept(visitor, env);
             if (visitor.Result is ErrorValue)
             {
                 errStream.WriteLine("Error > " + visitor.Result.ToString());
             }
             else
             {
                 errStream.WriteLine("> " + visitor.Result.ToString());
             }
         }
     }
     catch (ParseException ex) {
         errStream.WriteLine("Error > " + ex.Message);
     }
 }
Exemplo n.º 15
0
        public void Start()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(startMessage);
            ASTVisualizer visualizer = SheInfo.Visualize ? new ASTVisualizer() : null;

            while (true)
            {
                WriteSystemMessage(prompt);
                string input = ReadSource();
                if (input == null)
                {
                    break;
                }
                Lexer lexer = new Lexer(new StringReader(input));

                /*while (lexer.Peek(0) != Token.EOF)
                 * {
                 *  Console.WriteLine($"{lexer.Peek(0).GetType()}:{lexer.Read()}");
                 * }*/

                SheParser parser = new SheParser();
                ASTree    ast    = parser.Parse(lexer);

                if (SheInfo.Visualize)
                {
                    visualizer.Push(ast);
                }
                //Console.WriteLine(ast);
                Console.WriteLine(ast.Eval(replEnvironment));
                Console.WriteLine();
            }
            if (SheInfo.Visualize)
            {
                visualizer.Visualize($"REPL_{SheInfo.StartTime:yyyy-MM-dd-HH-mm-ss}");
            }
        }
Exemplo n.º 16
0
 public EvalException(string s, ASTree t) : base(s + " " + t.Location())
 {
 }
Exemplo n.º 17
0
 public SheException(string m, ASTree t) : base(m + " " + t.Location())
 {
 }
Exemplo n.º 18
0
 public ErrorValue(string s, ASTree t) : this(s + " " + t.Location())
 {
 }
Exemplo n.º 19
0
 public StoneException(string message, ASTree tree) : this($"{message} {tree.Location()}")
 {
 }
Exemplo n.º 20
0
 public object Invoke(object[] args, ASTree tree)
 {
     try { return(NumParams == 0 ? method0() : method(args)); }
     catch (Exception) { throw new SheException("bad c#-native function call: " + Name, tree); }
 }
Exemplo n.º 21
0
 public StoneException(string m, ASTree t) : base(m + " " + t.location())
 {
 }