public override void EnterStructDeclaration(DecafParser.StructDeclarationContext context)
 {
     StructSymbol ss = new StructSymbol(context.Id().GetText(), currentScope);
     currentScope.define(ss);
     setNodeScope(context, ss);
     setNodeSymbol(context, ss);
     currentScope = ss;
 }
 public override void EnterSingle_location(DecafParser.Single_locationContext context)
 {
     theScopeManager.GetEnclosingScope();
     Symbol tempSymbol = theScopeManager.FindSymbol(context.Id().GetText(), symbolCategory.Cvariable);
     if (context.location() != null)
     {
         StructImpl typedStruct = tempSymbol as StructImpl;
         string structDecName = typedStruct.ParentStructName;
         StructDeclSymbol typedParent = theScopeManager.FindSymbol(structDecName, symbolCategory.CstructDecl) as StructDeclSymbol;
         theScopeManager.PushSymbolTable(typedParent.Members);
     }
 }
 public override void EnterArray_location(DecafParser.Array_locationContext context)
 {
     //Get the current symbol reference.
     Symbol tempSymbol = theScopeManager.FindSymbol(context.Id().GetText(), symbolCategory.Cvariable);
     if (context.location() != null)
     {
         StructArraySymbol typedSymbol = tempSymbol as StructArraySymbol;
         string structDecName = typedSymbol.ParentStructName;
         //Find the current struct declaration name.
         StructDeclSymbol typedParent = theScopeManager.FindSymbol(structDecName, symbolCategory.CstructDecl) as StructDeclSymbol;
         theScopeManager.PushSymbolTable(typedParent.Members);
     }
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            var input  = System.IO.File.ReadAllText("./examples/example.dc");
            var stream = CharStreams.fromString(input);
            var lexer  = new DecafLexer(stream);

            var tokens = new CommonTokenStream(lexer);

            tokens.Fill();
            foreach (var token in tokens.GetTokens())
            {
                var typeName = DecafLexer.DefaultVocabulary.GetDisplayName(token.Type);
                System.Console.WriteLine($"{token} type: {typeName}");
            }

            var parser = new DecafParser(tokens);

            parser.BuildParseTree = true;

            parser.AddParseListener(new FunctionLockCheckLisnter());

            IParseTree tree = parser.sourceFile();
        }
 public override void EnterInt_methodDeclaration(DecafParser.Int_methodDeclarationContext context)
 {
     parameterList = new List<SymbolType>();
     theScopeManager.AddSymbol(context.Id().GetText(), new MethodSymbol(SymbolType.Tint));
     theScopeManager.EnterScope();
 }
 public override void ExitVoid_varType(DecafParser.Void_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tvoid);
 }
 public override void ExitVar_location_expression(DecafParser.Var_location_expressionContext context)
 {
     setNodeType(context, getNodeType(context.location()));
 }
 public override void ExitStructDecl_varType(DecafParser.StructDecl_varTypeContext context)
 {
     setNodeType(context, SymbolType.TstructImpl);
     setNodeSymbol(context, new StructDeclSymbol(theScopeManager.ExitScope()));
 }
 public override void ExitNegative_expression(DecafParser.Negative_expressionContext context)
 {
     if (TypeHelper.isNumeric(getNodeType(context.expression())))
         setNodeType(context, SymbolType.Tint);
     else
         throw new Exception("Incompatible type. Numeric expected. " + getNodeType(context.expression()).ToString() + " found instead.");
 }
 public override void ExitMethodCall_expression(DecafParser.MethodCall_expressionContext context)
 {
     setNodeType(context, getNodeType(context.methodCall()));
 }
        public override void ExitMethodCall(DecafParser.MethodCallContext context)
        {
            string theId = context.Id().GetText();

            if (!theScopeManager.CheckScope(context.Id().GetText(), symbolCategory.Cmethod)) //checks if the method is already declared
            {
                int line = context.start.Line;
                throw new Exception("Undeclared Identifier at line " + line.ToString());
            }
            else
            {
                MethodSymbol theSymbol = theScopeManager.FindSymbol(context.Id().GetText(), symbolCategory.Cmethod) as MethodSymbol;
                List<SymbolType> MyparameterList = new List<SymbolType>();
                #region methodCall parameter handling

                for (int i = 0; i < context.arg().Length; i++)
                {
                    DecafParser.ArgContext argument = context.arg(i);
                    MyparameterList.Add(getNodeType(argument));
                }

                if (!theSymbol.checkParameterList(MyparameterList))

                    if (!TypeHelper.checkLists(MyparameterList, parameterList))
                    {
                        throw new Exception("Argument Mismatch Errror");
                    }

                #endregion
                setNodeType(context, theSymbol.Type);
            }
        }
 public override void ExitInt_varType(DecafParser.Int_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tint);
 }
 public override void ExitInt_literal_expression(DecafParser.Int_literal_expressionContext context)
 {
     setNodeValue(context, Convert.ToInt32(context.Num().GetText()));
     setNodeType(context, SymbolType.Tint);
 }
 public override void ExitExpression_statement(DecafParser.Expression_statementContext context)
 {
     if (context.expression() != null)
     {
         setNodeType(context, getNodeType(context.expression()));
     }
     else
         setNodeType(context, SymbolType.Tvoid);
 }
 public override void ExitNot_expression(DecafParser.Not_expressionContext context)
 {
     if (TypeHelper.isBoolean(getNodeType(context.expression())))
         setNodeType(context, SymbolType.Tboolean);
     else
         throw new Exception("Incompatible type. Boolean expected. " + getNodeType(context.expression()).ToString() + " found instead.");
 }
        public override void ExitStructDeclaration(DecafParser.StructDeclarationContext context)
        {
            SymbolTable theScope = theScopeManager.ExitScope();
            StructDeclSymbol theSymbolRepresentation = new StructDeclSymbol(theScope);

            theScopeManager.AddSymbol(context.Id().GetText(), theSymbolRepresentation);
        }
 public override void ExitParens_expression(DecafParser.Parens_expressionContext context)
 {
     setNodeType(context, getNodeType(context.expression()));
 }
 public override void ExitStructImpl_varType(DecafParser.StructImpl_varTypeContext context)
 {
     setNodeType(context, SymbolType.TstructImpl);
     setNodeSymbol(context, new StructImpl(context.Id().GetText()));
 }
 public override void ExitRel_op_expression(DecafParser.Rel_op_expressionContext context)
 {
     if (TypeHelper.isNumeric(getNodeType(context.expression(0)), getNodeType(context.expression(1))))
         setNodeType(context, SymbolType.Tboolean);
     else
         throw new Exception("The expressions have incompatible types.");
 }
        public override void ExitVoid_methodDeclaration(DecafParser.Void_methodDeclarationContext context)
        {
            parameterList = null;
            SymbolType theReturnType = SymbolType.Tvoid;
            List<SymbolType> theParameters = new List<SymbolType>();

            theScopeManager.GetAndExitScope();
            DecafParser.ParameterContext[] parameterGroup = context.parameter();

            foreach (DecafParser.ParameterContext item in parameterGroup)
                theParameters.Add(getNodeType(item));

            MethodSymbol theSymbolRepresentation = new MethodSymbol(theReturnType, theParameters);

            theScopeManager.AddSymbol(context.Id().GetText(), theSymbolRepresentation);

            if (theScopeManager.HasReturn)
            {
                if (!(TypeHelper.isEquivalentType(theScopeManager.ReturnType, SymbolType.Tvoid)))
                {
                    throw new Exception("Return Type Mismatch");
                }
            }

            theScopeManager.resetReturn();
        }
 public override void ExitReturn_statement(DecafParser.Return_statementContext context)
 {
     if (context.expression() != null)
     {
         setNodeType(context, getNodeType(context.expression()));
         if (!theScopeManager.HasReturn)
             theScopeManager.ReturnType = getNodeType(context.expression());
         else
             if (!TypeHelper.isEquivalentType(theScopeManager.ReturnType, getNodeType(context.expression())))
                 throw new Exception("Result type mismatch");
     }
 }
 public override void ExitWhile_statement(DecafParser.While_statementContext context)
 {
     if (!TypeHelper.isBoolean(getNodeType(context.expression())))
         throw new Exception("Boolean expression expected " + getNodeType(context.expression()).ToString() + " found instead.");
 }
 public override void EnterBlock(DecafParser.BlockContext context)
 {
     theScopeManager.EnterScope();
 }
 public override void EnterProgram(DecafParser.ProgramContext context)
 {
     theScopeManager.EnterScope();
 }
 public override void ExitSingle_location(DecafParser.Single_locationContext context)
 {
     string symbolName = context.Id().GetText();
     Symbol tempSymbol = theScopeManager.FindSymbol(context.Id().GetText(), symbolCategory.Cvariable);
     if (context.location() != null)
     {
         setNodeType(context, getNodeType(context.location()));
         theScopeManager.GetAndExitScope();
     }
     else
     {
         setNodeType(context, tempSymbol.Type);
     }
 }
 public override void EnterStructDeclaration(DecafParser.StructDeclarationContext context)
 {
     theScopeManager.EnterScope();
 }
 public override void ExitSingle_parameterDeclaration(DecafParser.Single_parameterDeclarationContext context)
 {
     SymbolType theParameterType = getNodeType(context.parameterType());
     Symbol theSymbolRepresentation = new Symbol(theParameterType);
     KeyValuePair<string, Symbol> theKeyValuePairRepresentaion = new KeyValuePair<string, Symbol>(context.Id().GetText(), theSymbolRepresentation);
     theScopeManager.AddSymbol(theKeyValuePairRepresentaion);
     parameterList.Add(theParameterType);
     setNodeSymbolTableEntry(context, theKeyValuePairRepresentaion);
     setNodeType(context, theParameterType);
 }
 public override void EnterProgram(DecafParser.ProgramContext context)
 {
     globals = new GlobalScope();
     currentScope = globals;
     setNodeScope(context, currentScope);
 }
        public override void ExitSingle_varDeclaration(DecafParser.Single_varDeclarationContext context)
        {
            SymbolType theType = getNodeType(context.varType());
            Symbol theSymbolRepresentation;

            if (theType != SymbolType.TstructImpl)
                theSymbolRepresentation = new Symbol(theType);
            else
                theSymbolRepresentation = getNodeSymbol(context.varType());

            theScopeManager.AddSymbol(context.Id().GetText(), theSymbolRepresentation);
        }
 public override void ExitStructDeclaration(DecafParser.StructDeclarationContext context)
 {
     currentScope = currentScope.getEnclosingScope();
 }
 public override void ExitChar_varType(DecafParser.Char_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tchar);
 }