Exemple #1
0
        private void ParseExpressionRaw(Statement s)
        {
            while (tokens[currentToken].Value != ";")
            {
                if (tokens[currentToken].Group == TokenCodes.NUMBER)
                {
                    if (s.Current.Details.Group == TokenCodes.KEYWORD)                    //initial number
                    {
                        s.PushChild(tokens[currentToken]);
                        s.Follow();
                        eat();
                    }
                    else
                    {
                        s.PushChild(tokens[currentToken]);
                        s.Follow();
                        eat();
                    }
                }

                if (tokens[currentToken].Group == TokenCodes.EXPRESSION)
                {
                    if (s.Current.Parent.Details.Group != TokenCodes.EXPRESSION)   //only 1 number in tree
                    {
                        s.PushChild(tokens[currentToken]);                         //now update to binaryExpression
                        s.Current.RotateLeft();
                        eat();
                    }
                    else if (Tokeniser.OperatorPrecedence(tokens[currentToken], s.Current.Parent.Details))
                    {
                        s.PushChild(tokens[currentToken]);                         //right side precedence
                        s.Current.RotateLeft();
                        eat();
                    }
                    else                         //left side precedence
                    {
                        s.StepUp();
                        s.InsertParent(tokens[currentToken]);
                        s.StepUp();
                        //s.PushChild(); //right side precedence
                        //s.Current.RotateLeft();
                        eat();
                    }
                }
                //Console.WriteLine();
                //eat(); //parameter
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            string   filename = "imlang.wasm";
            Compiler c        = new Compiler("", filename);

            //c.BuildWASM();

            List <Token> tokens = Tokeniser.getTokenArray(@"123 12.3");

            foreach (Token t in tokens)
            {
                if (t.Group != TokenCodes.WHITESPACE)
                {
                    Console.WriteLine("Group: {0}, {2}, Value: |{1}|", t.GroupName, t.Value, t.Group);
                    //Console.Write(t.Value);
                }
            }
            Console.WriteLine("\nprint123;end(hello);foobar123;varasdf;1+1=3");


            Console.WriteLine("Compiling...");
            Console.Write("Built: " + filename);
            Console.ReadKey(true);
        }
Exemple #3
0
        public void BuildWASM()
        {
            // Binary class will automatically sort functions based on index to ensure the export definition index matches the function code index
            // This means functions can be added in whatever order desired

            string[] operators = { "+", "+", "/", "*" };
            Random   r         = new Random();
            string   field     = "";

            for (int i = 0; i < 10; i++)
            {
                field += (Math.Floor(r.NextDouble() * 100)).ToString();
                field += operators[(int)Math.Floor(r.NextDouble() * 4)];
            }
            field += (Math.Floor(r.NextDouble() * 100)).ToString();

            Console.WriteLine(field);
            List <Token> tokens = Tokeniser.getTokenArray(@"return " + field + ";");

            /*
             * ((((1*2) + 4) * 1) + ((2 + 3) + (4*(1+2))))
             */
            Parser p = new Parser(tokens);

            p.Parse();
            p.Dump();
            List <Statement> statements = p.getStatements();

            binary = new Binary();
            Func f = new Func("temp");

            f.setInputParameters();
            f.setOutputParameters(Types.i32);
            f.setLocalCounts();
            //*
            Console.WriteLine("Printing pretty statements");
            foreach (Statement s in statements)
            {
                s.Tree.PrintPretty("", true);
                f.pushCode(EncodeStatement(s));
            }

            List <byte> temp = EncodeStatement(statements[0]);

            foreach (byte b in temp)
            {
                if (b == 0x6a)
                {
                    Console.Write("+");
                }
                else if (b == 0x6b)
                {
                    Console.Write("-");
                }
                else if (b == 0x6c)
                {
                    Console.Write("*");
                }
                else if (b == 0x6d)
                {
                    Console.Write("/");
                }
            }
            //*/


            binary.addFunction(f);

            byte[] bits = binary.getBinary();

            File.Delete(outFilename);
            FileStream fs = File.OpenWrite(outFilename);

            fs.Write(bits, 0, bits.Length);
            fs.Close();
        }