Example #1
0
        public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken)
        {
            Variable      variable = new Variable();
            List <string> errors   = new List <string>();

            while (semanticRecordTable.Peek().recordType == RecordTypes.IdName ||
                   semanticRecordTable.Peek().recordType == RecordTypes.TypeName ||
                   semanticRecordTable.Peek().recordType == RecordTypes.Size)
            {
                SemanticRecord topRecord = semanticRecordTable.Pop();
                switch (topRecord.recordType)
                {
                case RecordTypes.Size:
                    variable.AddDimension(int.Parse(topRecord.getValue()));
                    break;

                case RecordTypes.IdName:
                    variable.SetName(topRecord.getValue());
                    break;

                case RecordTypes.TypeName:
                    variable.SetType(topRecord.getType());
                    SemanticRecord variableRecord = new SemanticRecord(variable);
                    semanticRecordTable.Push(variableRecord);
                    break;

                default:
                    // This should only occur if the grammar is not valid
                    errors.Add("Grammar error, parsed rule that placed unexpected character on semantic stack");
                    break;
                }
            }

            return(errors);
        }
        public override List <string> ExecuteSemanticAction(Stack <SemanticRecord> semanticRecordTable, Stack <SymbolTable> symbolTable, IToken lastToken, MoonCodeResult moonCode)
        {
            SymbolTable currentTable = symbolTable.Peek();

            LinkedList <Variable> foundParameters = new LinkedList <Variable>();

            bool          entryCreated = false;
            string        funcName     = string.Empty;
            List <string> errors       = new List <string>();

            // Iterate over the semantic stack and consume records relevant to this function
            while (!entryCreated)
            {
                SemanticRecord topRecord = semanticRecordTable.Pop();

                // Handle the record depending on its type
                switch (topRecord.recordType)
                {
                case RecordTypes.Variable:
                    // Encountered paramenters
                    foundParameters.AddFirst(topRecord.getVariable());
                    break;

                case RecordTypes.IdName:
                    // The name of this function
                    funcName = topRecord.getValue();
                    break;

                case RecordTypes.TypeName:
                    // If we encounter a type we are done collecting and can create the entry
                    FunctionEntry funcEntry = new FunctionEntry(currentTable, funcName, topRecord.getType());
                    funcEntry.AddParameters(foundParameters);

                    // Push the function's scope to the stack of symbol tables
                    symbolTable.Push(funcEntry.getChild());

                    entryCreated = true;

                    break;

                default:
                    // This should only fail if there is an error in the grammar.
                    errors.Add("Grammar error, parsed rule that placed unexpected character on semantic stack");
                    break;
                }
            }

            return(errors);
        }