Exemplo n.º 1
0
//-----------------------------------------------------------
        public void Visit(NAssign node)
        {
            Console.WriteLine($"+++++++++++++++ NAssign ++++++++++++++++");
            var variableName = node.AnchorToken.Lexeme;

            if (pasones == 0)
            {
                if (Table.Contains(variableName))
                {
                    Visit((dynamic)node[0]);
                }
                else
                {
                    throw new SemanticError(
                              "Undeclared variable: " + variableName,
                              node.AnchorToken);
                }
            }

            else if (pasones == 2)
            {
                //Console.WriteLine($"\n\n\n\n\t\t\t\t\t\tNAssign 3er pason Funcion  {nombreFuncion}  {node.AnchorToken.Lexeme}");
                if (!Table[nombreFuncion].locTable.ContainsKey(node.AnchorToken.Lexeme))
                {
                    if (!globVars.Contains(node.AnchorToken.Lexeme))
                    {
                        throw new SemanticError("variable [" + node.AnchorToken.Lexeme + "] has not been declared ",
                                                node.AnchorToken);
                    }
                }


                Visit((dynamic)node[0]);
            }
        }
Exemplo n.º 2
0
        public string Visit(NAssign nAssign)
        {
            string lexeme = nAssign.AnchorToken.Lexeme;
            Node   nExpr  = nAssign[0];

            return(Visit((dynamic)nExpr) + storeVariable(lexeme));
        }
        public void Visit(NAssign nAssign)
        {
            string lexeme = nAssign.AnchorToken.Lexeme;

            if (currentFunction.GetLocalVariableSymbolByLexeme(lexeme) != null || semanticAnalyzer.GetGlobalVariableSymbolByLexeme(lexeme) != null)
            {
                Visit((dynamic)nAssign[0]);
            }
            else
            {
                throw new SemanticError("Variable not in scope", nAssign.AnchorToken);
            }
        }
Exemplo n.º 4
0
        // Appends NStmt to nStmtList
        public void Stmt(NStmtList nStmtList)
        {
            switch (CurrentToken)
            {
            case TokenCategory.IDENTIFIER: {
                Token tokenForAnchor = Expect(TokenCategory.IDENTIFIER);
                if (CurrentToken == TokenCategory.ASSIGN)
                {
                    NAssign nAssign = new NAssign()
                    {
                        AnchorToken = tokenForAnchor
                    };
                    nAssign.Add(Assign());
                    nStmtList.Add(nAssign);
                }
                else if (CurrentToken == TokenCategory.PARENTHESIS_LEFT)
                {
                    NFunCall nFunCall = new NFunCall()
                    {
                        AnchorToken = tokenForAnchor
                    };
                    nFunCall.Add(FunCall());
                    nStmtList.Add(nFunCall);
                }
                else
                {
                    throw new SyntaxError(new HashSet <TokenCategory>()
                        {
                            TokenCategory.ASSIGN,
                            TokenCategory.PARENTHESIS_LEFT
                        }
                                          , tokenStream.Current);
                }
                break;
            }

            case TokenCategory.IF: {
                Expect(TokenCategory.IF);
                nStmtList.Add(IfStmt());
                break;
            }

            case TokenCategory.SWITCH: {
                nStmtList.Add(SwitchStmt());
                break;
            }

            case TokenCategory.WHILE: {
                nStmtList.Add(WhileStmt());
                break;
            }

            case TokenCategory.DO: {
                nStmtList.Add(DoWhileStmt());
                break;
            }

            case TokenCategory.FOR: {
                nStmtList.Add(ForStmt());
                break;
            }

            case TokenCategory.BREAK: {
                nStmtList.Add(new NBreak()
                    {
                        AnchorToken = Expect(TokenCategory.BREAK)
                    });
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.CONTINUE: {
                nStmtList.Add(new NContinue()
                    {
                        AnchorToken = Expect(TokenCategory.CONTINUE)
                    });
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.RETURN: {
                Expect(TokenCategory.RETURN);
                NReturn nReturn          = new NReturn();
                Node    returnExpression = Expr();
                if (returnExpression != null)
                {
                    nReturn.Add(returnExpression);
                }
                nStmtList.Add(nReturn);
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            case TokenCategory.SEMICOLON: {
                Expect(TokenCategory.SEMICOLON);
                break;
            }

            default: {
                throw new SyntaxError(firstOfStatement, tokenStream.Current);
            }
            }
        }
Exemplo n.º 5
0
//-----------------------------------------------------------
        public void Visit(NAssign node)
        {
            Console.WriteLine($"+++++++++++++++ NAssign ++++++++++++++++");
            var variableName = node.AnchorToken.Lexeme;

            if (pasones == 0)
            {
                if (Table.Contains(variableName))
                {
                    Visit((dynamic)node[0]);
                }
                else
                {
                    throw new SemanticError(
                              "Undeclared variable: " + variableName,
                              node.AnchorToken);
                }
            }

            else if (pasones == 2)
            {
                //Console.WriteLine($"\n\n\n\n\t\t\t\t\t\tNAssign 3er pason Funcion  {nombreFuncion}  {node.AnchorToken.Lexeme}");
                if (!Table[nombreFuncion].locTable.ContainsKey(node.AnchorToken.Lexeme))
                {
                    if (!globVars.Contains(node.AnchorToken.Lexeme))
                    {
                        throw new SemanticError("variable [" + node.AnchorToken.Lexeme + "] has not been declared ",
                                                node.AnchorToken);
                    }
                }


                Visit((dynamic)node[0]);
            }
            else if (pasones == 3)
            {
                Visit((dynamic)node[0]);
                var cuentaExtraLocal = 0;
                //  if(Table[nombreFuncion].locTable.ContainsKey(node.AnchorToken.Lexeme))
                if (globVars.Contains(variableName))
                {
                    File.AppendAllText(lePatheo,
                                       $@"stsfld int32 Test::{variableName}
");
                }

                else
                {
                    foreach (var XXX in Table[nombreFuncion].locTable)
                    {
                        Console.WriteLine($"\n\n\n\n\t\t\t\t%%%%%%%%%%%%%%%%%%Estoy en el foreach con valor {cuentaExtraLocal} buscando {variableName}");
                        if (XXX.Value.name == variableName && XXX.Value.param == false)
                        {
                            File.AppendAllText(lePatheo,
                                               $@"stloc.{cuentaExtraLocal}
        ");
                            break;
                        }
                        else if (XXX.Value.name == variableName && XXX.Value.param == true)
                        {
                            File.AppendAllText(lePatheo,
                                               $@"starg.s {XXX.Value.pos}
        ");
                            break;
                        }
                        if (XXX.Value.param == false)
                        {
                            cuentaExtraLocal++;
                        }
                    }
                }
            }
        }