Пример #1
0
        /// <summary>
        /// Store variables in data segment and initialize
        /// </summary>
        public void VariableDeclarationProcedure(SymbolProcedure symbolProcedure)
        {
            symbolProcedure.Arguments = new List <ProcedureArgument>();
            while (scanner.CurrentToken.TokenTypeDefinition == TokenTypeDefinition.TK_ID && scanner.ErrorLog.Count == 0)
            {
                //begin reading variable names
                List <SymbolVariable> variableTokens = new List <SymbolVariable>();
                while (scanner.CurrentToken.TokenTypeDefinition == TokenTypeDefinition.TK_ID && scanner.ErrorLog.Count == 0)
                {
                    variableTokens.Add(new SymbolVariable
                    {
                        Address            = 0,
                        Name               = scanner.CurrentToken.Value,
                        FlagTypeDefinition = FlagTypeDefinition.Register,
                        ChildSymbolTable   = new HashSet <SymbolBase>()
                    });

                    if (SymbolTable.Any(p => p.Name == scanner.CurrentToken.Value))
                    {
                        scanner.LogErrorToken(new Token(TokenCategory.Identifier, TokenTypeDefinition.TK_MULTIPLE_DECLARATIONS, ""));
                    }

                    scanner.Match(TokenTypeDefinition.TK_ID);
                    if (scanner.CurrentToken.TokenTypeDefinition == TokenTypeDefinition.TK_COMMA)
                    {
                        scanner.Match(TokenTypeDefinition.TK_COMMA);
                    }
                }

                scanner.Match(TokenTypeDefinition.TK_COLON);

                int size = 0;
                DataTypeDefinition currentDataType = DataTypeDefinition.TYPE_VOID;

                switch (scanner.CurrentToken.TokenTypeDefinition)
                {
                case TokenTypeDefinition.TK_INT:
                {
                    size            = 4;
                    currentDataType = DataTypeDefinition.TYPE_INT;
                    scanner.Match(TokenTypeDefinition.TK_INT);
                    break;
                }

                case TokenTypeDefinition.TK_FLOAT:
                {
                    size            = 4;
                    currentDataType = DataTypeDefinition.TYPE_FLOAT;
                    scanner.Match(TokenTypeDefinition.TK_FLOAT);
                    break;
                }

                case TokenTypeDefinition.TK_CHAR:
                {
                    size            = 1;
                    currentDataType = DataTypeDefinition.TYPE_CHAR;
                    scanner.Match(TokenTypeDefinition.TK_CHAR);
                    break;
                }

                case TokenTypeDefinition.TK_BOOL:
                {
                    size            = 1;
                    currentDataType = DataTypeDefinition.TYPE_BOOL;
                    scanner.Match(TokenTypeDefinition.TK_BOOL);
                    break;
                }

                default:
                {
                    scanner.LogErrorToken(scanner.CurrentToken);
                    break;
                }
                }

                //compute addresses amd imitialize from stack
                foreach (SymbolVariable t in variableTokens)
                {
                    t.Size               = size;
                    t.Address            = dp;
                    t.DataTypeDefinition = currentDataType;
                    dp = dp + size;
                    SymbolTable.Add(t);

                    switch (currentDataType)
                    {
                    case DataTypeDefinition.TYPE_CHAR:
                        GenerateOperation(OperationTypeDefinition.op_store);
                        gen4(t.Address);
                        break;

                    case DataTypeDefinition.TYPE_INT:
                        GenerateOperation(OperationTypeDefinition.op_storei);
                        gen4(t.Address);
                        break;

                    case DataTypeDefinition.TYPE_FLOAT:
                        GenerateOperation(OperationTypeDefinition.op_storef);
                        gen4(t.Address);
                        break;
                    }

                    symbolProcedure.Arguments.Add(new ProcedureArgument
                    {
                        DataType = currentDataType,
                        Name     = t.Name
                    });
                }
            }
        }
Пример #2
0
        public TokenTypeValue F()
        {
            TokenTypeValue tokenFound = new TokenTypeValue(DataTypeDefinition.TYPE_VOID, null);

            switch (scanner.CurrentToken.TokenTypeDefinition)
            {
            case TokenTypeDefinition.TK_ID:
            {
                SymbolBase searchedSymVar = SymbolTable.FirstOrDefault(p => p.Name == scanner.CurrentToken.Value);
                if (searchedSymVar != null)
                {
                    //get the type of variable
                    if (searchedSymVar.GetType() == typeof(SymbolVariable))
                    {
                        SymbolVariable symVar = (SymbolVariable)searchedSymVar;
                        tokenFound           = new TokenTypeValue(symVar.DataTypeDefinition, symVar.Address);
                        tokenFound.isAddress = true;
                        switch (symVar.DataTypeDefinition)
                        {
                        case DataTypeDefinition.TYPE_BOOL:
                        case DataTypeDefinition.TYPE_CHAR:
                        {
                            GenerateOperation(OperationTypeDefinition.op_fetch);             //does a push of the value onto the stack
                            gen4(symVar.Address);
                            break;
                        }

                        case DataTypeDefinition.TYPE_INT:
                        {
                            GenerateOperation(OperationTypeDefinition.op_fetchi);
                            gen4(symVar.Address);
                            break;
                        }

                        case DataTypeDefinition.TYPE_FLOAT:
                        {
                            GenerateOperation(OperationTypeDefinition.op_fetchf);
                            gen4(symVar.Address);
                            break;
                        }
                        }
                    }
                    else if (searchedSymVar.GetType() == typeof(SymbolProcedure))
                    {
                        //store where to return to in the stack before the parameters
                        GenerateOperation(OperationTypeDefinition.op_pushi);
                        int hole = ip;
                        gen4(0);

                        int             count  = 0;
                        SymbolProcedure symVar = (SymbolProcedure)searchedSymVar;
                        scanner.Match(TokenTypeDefinition.TK_ID);
                        scanner.Match(TokenTypeDefinition.TK_LBRACE);

                        while (scanner.CurrentToken.TokenTypeDefinition != TokenTypeDefinition.TK_RBRACE && scanner.ErrorLog.Count == 0)
                        {
                            count++;
                            switch (scanner.CurrentToken.TokenTypeDefinition)
                            {
                            case TokenTypeDefinition.TK_BOOLLIT:
                            case TokenTypeDefinition.TK_CHARLIT:
                            {
                                GenerateOperation(OperationTypeDefinition.op_push);             //does a push of the value onto the stack
                                gen1(scanner.CurrentToken.Value[0]);
                                break;
                            }

                            case TokenTypeDefinition.TK_INTLIT:
                            {
                                GenerateOperation(OperationTypeDefinition.op_pushi);
                                gen4(Convert.ToInt32(scanner.CurrentToken.Value));
                                break;
                            }

                            case TokenTypeDefinition.TK_REALLIT:
                            {
                                GenerateOperation(OperationTypeDefinition.op_pushf);
                                gen8(Convert.ToSingle(scanner.CurrentToken.Value));
                                break;
                            }
                            }
                            scanner.Match(scanner.CurrentToken.TokenTypeDefinition);
                            if (scanner.CurrentToken.TokenTypeDefinition == TokenTypeDefinition.TK_COMMA)
                            {
                                scanner.Match(scanner.CurrentToken.TokenTypeDefinition);
                            }
                        }
                        gen_Address(ip, hole);         //push where to return to the bottom of the parameters
                        scanner.Match(TokenTypeDefinition.TK_SEMI);

                        if (count != symVar.Arguments.Count)
                        {
                            scanner.LogErrorToken(new Token(TokenCategory.Literal, TokenTypeDefinition.TK_INVALID_NUMBER_OF_ARGUMENTS, ""));
                        }
                    }
                }
                break;
            }

            case TokenTypeDefinition.TK_INTLIT:
            {
                GenerateOperation(OperationTypeDefinition.op_pushi);
                gen4(Convert.ToInt32(scanner.CurrentToken.Value));
                tokenFound = new TokenTypeValue(DataTypeDefinition.TYPE_INT, Convert.ToInt32(scanner.CurrentToken.Value));

                break;
            }

            case TokenTypeDefinition.TK_REALLIT:
            {
                GenerateOperation(OperationTypeDefinition.op_pushf);
                gen8(Convert.ToInt32(scanner.CurrentToken.Value));
                tokenFound = new TokenTypeValue(DataTypeDefinition.TYPE_FLOAT, Convert.ToSingle(scanner.CurrentToken.Value));
                break;
            }

            case TokenTypeDefinition.TK_BOOLLIT:
            {
                GenerateOperation(OperationTypeDefinition.op_push);
                gen1(Convert.ToBoolean(scanner.CurrentToken.Value) ? '1' : '0');
                tokenFound = new TokenTypeValue(DataTypeDefinition.TYPE_BOOL, Convert.ToBoolean(scanner.CurrentToken.Value));
                break;
            }

            case TokenTypeDefinition.TK_CHARLIT:
            {
                GenerateOperation(OperationTypeDefinition.op_push);
                gen1(scanner.CurrentToken.Value[0]);
                tokenFound = new TokenTypeValue(DataTypeDefinition.TYPE_CHAR, scanner.CurrentToken.Value);
                break;
            }
            }
            scanner.Advance();
            return(tokenFound);
        }
Пример #3
0
 public static BasicPolyadic CreateProcedureCall(ILanguageType exprType, SymbolProcedure langOp)
 {
     return(new BasicPolyadic(exprType, langOp));
 }