Example #1
0
        public Node id_list_cont()
        {
            //Console.Write("id_list_cont");
            var id = new Id_();

            Expect(TokenCategory.COMA);
            var r = Expect(TokenCategory.IDENTIFIER);

            id.AnchorToken = r;
            return(id);
        }
Example #2
0
        public Node id_list()
        {
            //Console.WriteLine("id_list");
            var id_list = new Id_list_();
            var id      = new Id_();
            var r       = Expect(TokenCategory.IDENTIFIER);

            id_list.Add(id);
            id.AnchorToken = r;
            while (CurrentToken == TokenCategory.COMA)
            {
                id_list.Add(id_list_cont());
            }
            return(id_list);
        }
Example #3
0
        public Node stmt_()
        {
            //Console.WriteLine("stmt");
            Token r;

            switch (CurrentToken)
            {
            case TokenCategory.IF:
                //Console.WriteLine("stmt1");
                Node If = new If_();
                r = Expect(TokenCategory.IF);
                If.AnchorToken = r;
                Expect(TokenCategory.OPENP);
                If.Add(expr());
                Expect(TokenCategory.CLOSEP);
                Expect(TokenCategory.OPENB);
                If.Add(stmt_list());
                //Console.WriteLine("CLOSEB3");
                Expect(TokenCategory.CLOSEB);
                If.Add(else_if_list());
                If.Add(else_());
                return(If);

            case TokenCategory.SWITCH:
                //Console.WriteLine("stmt2");
                var Switch = new Switch_();
                r = Expect(TokenCategory.SWITCH);
                Switch.AnchorToken = r;
                Expect(TokenCategory.OPENP);
                Switch.Add(expr());
                Expect(TokenCategory.CLOSEP);
                Expect(TokenCategory.OPENB);
                Switch.Add(case_list());
                Switch.Add(default_());
                //Console.WriteLine("CLOSEB4");
                Expect(TokenCategory.CLOSEB);
                return(Switch);

            case TokenCategory.WHILE:
                //Console.WriteLine("stmt3");
                var While = new While_();
                r = Expect(TokenCategory.WHILE);
                While.AnchorToken = r;
                Expect(TokenCategory.OPENP);
                While.Add(expr());
                Expect(TokenCategory.CLOSEP);
                Expect(TokenCategory.OPENB);
                While.Add(stmt_list());
                //Console.WriteLine("CLOSEB11");
                Expect(TokenCategory.CLOSEB);
                return(While);

            /*       case TokenCategory.OPENP:
             *          Expect(TokenCategory.OPENP);
             *          expr();
             *          Expect(TokenCategory.CLOSEP);
             *          break;*/


            case TokenCategory.DO:     //OYE AQUI NO ES DO_WHILE???
                //Console.WriteLine("stmt4");
                var Do = new Do_();
                r = Expect(TokenCategory.DO);
                Do.AnchorToken = r;
                Expect(TokenCategory.OPENB);
                Do.Add(stmt_list());
                //Console.WriteLine("CLOSEB5");
                Expect(TokenCategory.CLOSEB);
                var While_ = new While_();
                var m      = Expect(TokenCategory.WHILE);
                While_.AnchorToken = m;
                Expect(TokenCategory.OPENP);
                While_.Add(expr());
                Do.Add(While_);
                Expect(TokenCategory.CLOSEP);
                //Console.WriteLine("semi4");
                Expect(TokenCategory.SEMICOLON);
                return(Do);

            case TokenCategory.FOR:
                //Console.WriteLine("stmt5");
                var For = new For_();
                r = Expect(TokenCategory.FOR);
                For.AnchorToken = r;
                Expect(TokenCategory.OPENP);
                var Id = new Id_();
                var n  = Expect(TokenCategory.IDENTIFIER);
                Id.AnchorToken = n;
                For.Add(Id);
                Expect(TokenCategory.IN);
                For.Add(expr());
                Expect(TokenCategory.CLOSEP);
                Expect(TokenCategory.OPENB);
                For.Add(stmt_list());
                //Console.WriteLine("CLOSEB6");
                Expect(TokenCategory.CLOSEB);
                return(For);

            case TokenCategory.BREAK:
                //Console.WriteLine("stmt6");
                var Break = new Break_();
                r = Expect(TokenCategory.BREAK);
                Break.AnchorToken = r;
                //Console.WriteLine("semi5");
                Expect(TokenCategory.SEMICOLON);
                return(Break);

            case TokenCategory.CONTINUE:
                //Console.WriteLine("stmt7");
                var Continue = new Continue_();
                r = Expect(TokenCategory.CONTINUE);
                Continue.AnchorToken = r;
                //Console.WriteLine("semi6");
                Expect(TokenCategory.SEMICOLON);
                return(Continue);

            case TokenCategory.RETURN:
                //Console.WriteLine("stmt8");
                var Return = new Return_();
                r = Expect(TokenCategory.RETURN);
                Return.AnchorToken = r;
                Return.Add(expr());
                //Console.WriteLine("semi7");
                Expect(TokenCategory.SEMICOLON);
                return(Return);

            case TokenCategory.SEMICOLON:
                //Console.WriteLine("stmt9");
                var empty = new Empty_();
                //Console.WriteLine("semi8");
                r = Expect(TokenCategory.SEMICOLON);
                empty.AnchorToken = r;
                return(empty);

            case TokenCategory.IDENTIFIER:
                //Console.WriteLine("stmt10");
                r = Expect(TokenCategory.IDENTIFIER);
                if (CurrentToken == TokenCategory.ASIGNATION)
                {
                    //Console.WriteLine("stmt11");
                    var id = new Id_();
                    id.AnchorToken = r;
                    Expect(TokenCategory.ASIGNATION);
                    id.Add(expr());
                    //Console.WriteLine("semi2");
                    Expect(TokenCategory.SEMICOLON);
                    return(id);
                }
                else if (CurrentToken == TokenCategory.OPENP)
                {
                    //Console.WriteLine("stmt12");
                    var Fun = new Fun_();
                    Fun.AnchorToken = r;
                    Expect(TokenCategory.OPENP);
                    if (CurrentToken != TokenCategory.CLOSEP)
                    {
                        //Console.WriteLine("stmt13");////////////MODIFICADO//////////////////////////////////////////
                        Fun.Add(expr_list());
                    }
                    //Console.WriteLine("stmt14");
                    Expect(TokenCategory.CLOSEP);
                    //Console.WriteLine("semi3");
                    Expect(TokenCategory.SEMICOLON);
                    return(Fun);
                }
                else
                {
                    throw new SyntaxError(TokenCategory.EQUAL, tokenStream.Current);
                }

            default:
                throw new SyntaxError(TokenCategory.IDENTIFIER, tokenStream.Current);
            }
        }
Example #4
0
        public Node expr_primary()
        {
            //Console.WriteLine("expr_primary");
            if (CurrentToken == TokenCategory.IDENTIFIER)
            {
                //Console.WriteLine("expr_primary1");
                var id = new Id_();
                var r  = Expect(TokenCategory.IDENTIFIER);
                id.AnchorToken = r;
                if (CurrentToken == TokenCategory.OPENP)
                {
                    //Console.WriteLine("expr_primary2");
                    Expect(TokenCategory.OPENP);
                    if (CurrentToken != TokenCategory.CLOSEP) /////////////////////////////MODIFICADO///////////////////////////
                    {
                        id.Add(expr_list());
                    }
                    Expect(TokenCategory.CLOSEP);
                    //Console.WriteLine("semi9");
                    //Expect(TokenCategory.SEMICOLON);
                }
                return(id);
            }
            else if (CurrentToken == TokenCategory.FALSE)
            {
                //Console.WriteLine("expr_primary3");
                var False = new False_();
                var r     = Expect(TokenCategory.FALSE);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.TRUE)
            {
                //Console.WriteLine("expr_primary4");
                var False = new True_();
                var r     = Expect(TokenCategory.TRUE);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.CHARACTER)
            {
                //Console.WriteLine("expr_primary5");
                var False = new Character_();
                var r     = Expect(TokenCategory.CHARACTER);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.INTEGER)
            {
                //Console.WriteLine("expr_primary6");
                var False = new Integer_();
                var r     = Expect(TokenCategory.INTEGER);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.BINARYINT)
            {
                //Console.WriteLine("expr_primary7");
                var False = new Integer_();
                var r     = Expect(TokenCategory.BINARYINT);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.OCTALINT)
            {
                //Console.WriteLine("expr_primary8");
                var False = new Integer_();
                var r     = Expect(TokenCategory.OCTALINT);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.HEXAINT)
            {
                //Console.WriteLine("expr_primary9");
                var False = new Integer_();
                var r     = Expect(TokenCategory.HEXAINT);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.STRING)
            {
                //Console.WriteLine("expr_primary10");
                var False = new String_();
                var r     = Expect(TokenCategory.STRING);
                False.AnchorToken = r;
                return(False);
            }
            else if (CurrentToken == TokenCategory.OPENP)
            {
                //Console.WriteLine("expr_primary11");
                var openp = new Openp_();
                var r     = Expect(TokenCategory.OPENP);
                openp.AnchorToken = r;
                //Console.WriteLine("CURRRRRRRREEEEENT" + CurrentToken);
                if (CurrentToken != TokenCategory.CLOSEP)
                {
                    openp.Add(expr());
                }

                Expect(TokenCategory.CLOSEP);
                return(openp);
            }
            else if (CurrentToken == TokenCategory.OPENB)
            {
                //Console.WriteLine("expr_primary12");
                var r = array_list();
                return(r);
            }
            else if (CurrentToken == TokenCategory.EXCLAMATION)
            {
                //Console.WriteLine("expr_primary13");
                var exclamation = new Exclamation_();
                var r           = Expect(TokenCategory.EXCLAMATION);
                exclamation.AnchorToken = r;
                if (CurrentToken == TokenCategory.IDENTIFIER)
                {
                    var id = new Id_();
                    var m  = Expect(TokenCategory.IDENTIFIER);
                    id.AnchorToken = m;
                    exclamation.Add(id);
                }
                return(exclamation);
            }
            else
            {
                throw new SyntaxError(firstOfSimpleExpression, tokenStream.Current);
            }
        }
Example #5
0
        public string Visit(Id_ node, int modo)
        {
            Console.WriteLine("aqui5");
            var str = "";

            if (modo == 0)
            {
                Console.WriteLine(node.AnchorToken.lexeme);
                str += String.Format("'{0}'", node.AnchorToken.lexeme);
            }
            if (modo == 1)
            {
                Console.WriteLine("aqui555");
                str = String.Format("'{0}'", node.AnchorToken.lexeme);
            }
            if (modo == 2)
            {
                Console.WriteLine("aqui5555");
                // si se hace asi esto aqui tengo que checar
                //var a = Visit((dynamic) node[0], modo);
                // str = str +" ldc.i8 "+ a +"\n";
                Console.WriteLine(currentFunc + "en otro");
                var argument = "'" + (node.AnchorToken.lexeme) + "'";
                Console.WriteLine(argument + "verdadero??");
                //Console.WriteLine(args[currentFunc][0]+"nnlk0");
                //Console.WriteLine(args[currentFunc][1]+"nnlk1");
                var boolean = false;
                if (args.ContainsKey(currentFunc))
                {
                    if (args[currentFunc].Contains(argument))
                    {
                        boolean = true;
                    }
                }
                if (boolean)
                {
                    Console.WriteLine("aqui5558");
                    str += String.Format(Visit((dynamic)node[0], 2));
                    str += String.Format("\t\tstarg '{0}'\n", node.AnchorToken.lexeme);
                }
                else if (global_vars.Contains(node.AnchorToken.lexeme))
                {
                    Console.WriteLine("aqui5557");
                    try{
                        str  = str + Visit((dynamic)node[0], 2) + "\n";
                        str += String.Format("\t\tstsfld int64 'program'::'{0}'\n", node.AnchorToken.lexeme);
                    }
                    catch (System.ArgumentOutOfRangeException m) {}
                    //str += String.Format("\t\tstsfld int64 'program'::'{0}'\n", node.AnchorToken.lexeme);
                }
                else
                {
                    Console.WriteLine(node.AnchorToken.lexeme);
                    //if(1==1){
                    str = str + Visit((dynamic)node[0], 2) + "\n";
                    str = str + "\t\tstloc '" + node.AnchorToken.lexeme + "'\n";
                    //aqui tengo que arreglarlo para los argumentos
                }
            }
            if (modo == 4)
            {
                Console.WriteLine("aqui   " + node.AnchorToken.lexeme);
                var argument = "'" + (node.AnchorToken.lexeme) + "'";
                var boolean  = false;
                if (args.ContainsKey(currentFunc))
                {
                    if (args[currentFunc].Contains(argument))
                    {
                        boolean = true;
                    }
                }

                if (boolean)
                {
                    Console.WriteLine("aqui5558");
                    //str = str + node.AnchorToken.lexeme+"\n";
                    str = str + "\t\tldarg '" + node.AnchorToken.lexeme + "'\n";
                }
                else if (localvars.ContainsKey(node.AnchorToken.lexeme))
                {
                    var str2 = "";
                    var i    = 0;
                    Console.WriteLine(node.AnchorToken.lexeme + "   klkk");
                    while (i < funcs[node.AnchorToken.lexeme])
                    {
                        str2 = str2 + "int64";
                        if (funcs[node.AnchorToken.lexeme] > 1 && funcs[node.AnchorToken.lexeme] != i + 1)
                        {
                            str2 = str2 + ",";
                        }
                        i = i + 1;
                    }

                    foreach (var n in node)
                    {
                        str = str + Visit((dynamic)n, modo);
                    }
                    if (apifuncs.Contains(node.AnchorToken.lexeme))
                    {
                        str = str + "\t\tcall int64 class ['int64lib']'Int64'.'Utils'::'" + node.AnchorToken.lexeme + "'(" + str2 + ")\n";
                    }
                    else
                    {
                        str = str + "\t\tcall int64 class 'program'::'" + node.AnchorToken.lexeme + "'(" + str2 + ")\n";
                    }
                    return(str);
                }
                else if (global_vars.Contains(node.AnchorToken.lexeme))
                {
                    Console.WriteLine("aqui5557");
                    str += String.Format("\t\tldsfld int64 'program'::'{0}'\n", node.AnchorToken.lexeme);
                }
                else if (localvars[currentFunc].Contains(node.AnchorToken.lexeme))
                {
                    Console.WriteLine(node.AnchorToken.lexeme + "   jnrjnrnnr");
                    Console.WriteLine(node.AnchorToken.lexeme + "rarararararar");
                    str = str + "\t\tldloc '" + node.AnchorToken.lexeme + "'\n";
                }
            }
            return(str);
        }
Example #6
0
        public String Visit(Id_ node, int modo)
        {
            if (modo == 1)
            {
                if (!global_vars.Contains(node.AnchorToken.lexeme))
                {
                    global_vars.Add(node.AnchorToken.lexeme);
                }
                else
                {
                    throw new SemanticError("repeated variable " + "in row " + node.AnchorToken.row, node.AnchorToken);
                }
                VisitChildren(node, modo);
                return(node.AnchorToken.lexeme);
            }
            else if (modo == 2 || modo == 4)
            {
                //Console.WriteLine(node.AnchorToken.lexeme+"    "+modo);
                if (!localvars[this.currentFunc].Contains(node.AnchorToken.lexeme))
                {
                    localvars[this.currentFunc].Add(node.AnchorToken.lexeme);
                }
                else
                {
                    //Console.WriteLine("aquihola");
                    //var result = String.Join(", ", localvars[this.currentFunc].ToArray());
                    //Console.WriteLine(result);
                    if (modo == 2)
                    {
                        throw new SemanticError("repeated variable " + node.AnchorToken.lexeme, node.AnchorToken);
                    }
                }
                VisitChildren(node, modo);
                return(node.AnchorToken.lexeme);
            }
            else if (modo == 5)
            {
                if (!global_vars.Contains(node.AnchorToken.lexeme) && !localvars[currentFunc].Contains(node.AnchorToken.lexeme))
                {
                    if (!funcs.Contains(node.AnchorToken.lexeme))
                    {
                        throw new SemanticError("variable not found  " + node.AnchorToken.lexeme, node.AnchorToken);
                    }
                    else if (funcs.Contains(node.AnchorToken.lexeme))
                    {
                        int n1 = 0;
                        ////Console.WriteLine(node.AnchorToken.lexeme);
                        foreach (var n in node[0])
                        {
                            n1 = n1 + 1;
                            ////Console.WriteLine(node.AnchorToken.lexeme);
                        }
                        ////Console.WriteLine(n1 + "  " +node.AnchorToken.lexeme);
                        if (funcs[node.AnchorToken.lexeme] != n1)
                        {
                            throw new SemanticError("wrong number of arguments", node.AnchorToken);
                        }
                        //VisitChildren(node,modo);
                    }
                    else
                    {
                        throw new SemanticError("function not found", node.AnchorToken);
                    }
                }
            }
            try{
                VisitChildren(node, modo);
            }
            catch (Exception g) {
                //this.funcs[node.AnchorToken.lexeme] =  n;

                if (g is Microsoft.CSharp.RuntimeBinder.RuntimeBinderException || g is System.ArgumentOutOfRangeException)
                {
                    //WebId = Guid.Empty;
                    return(node.AnchorToken.lexeme);
                }

                throw;
            }
            //Console.WriteLine("aqui");
            return(node.AnchorToken.lexeme);
        }