コード例 #1
0
 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;
 }
コード例 #2
0
 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);
     }
 }
コード例 #3
0
 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);
     }
 }
コード例 #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();
        }
コード例 #5
0
 public override void EnterInt_methodDeclaration(DecafParser.Int_methodDeclarationContext context)
 {
     parameterList = new List<SymbolType>();
     theScopeManager.AddSymbol(context.Id().GetText(), new MethodSymbol(SymbolType.Tint));
     theScopeManager.EnterScope();
 }
コード例 #6
0
 public override void ExitVoid_varType(DecafParser.Void_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tvoid);
 }
コード例 #7
0
 public override void ExitVar_location_expression(DecafParser.Var_location_expressionContext context)
 {
     setNodeType(context, getNodeType(context.location()));
 }
コード例 #8
0
 public override void ExitStructDecl_varType(DecafParser.StructDecl_varTypeContext context)
 {
     setNodeType(context, SymbolType.TstructImpl);
     setNodeSymbol(context, new StructDeclSymbol(theScopeManager.ExitScope()));
 }
コード例 #9
0
 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.");
 }
コード例 #10
0
 public override void ExitMethodCall_expression(DecafParser.MethodCall_expressionContext context)
 {
     setNodeType(context, getNodeType(context.methodCall()));
 }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
 public override void ExitInt_varType(DecafParser.Int_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tint);
 }
コード例 #13
0
 public override void ExitInt_literal_expression(DecafParser.Int_literal_expressionContext context)
 {
     setNodeValue(context, Convert.ToInt32(context.Num().GetText()));
     setNodeType(context, SymbolType.Tint);
 }
コード例 #14
0
 public override void ExitExpression_statement(DecafParser.Expression_statementContext context)
 {
     if (context.expression() != null)
     {
         setNodeType(context, getNodeType(context.expression()));
     }
     else
         setNodeType(context, SymbolType.Tvoid);
 }
コード例 #15
0
 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.");
 }
コード例 #16
0
        public override void ExitStructDeclaration(DecafParser.StructDeclarationContext context)
        {
            SymbolTable theScope = theScopeManager.ExitScope();
            StructDeclSymbol theSymbolRepresentation = new StructDeclSymbol(theScope);

            theScopeManager.AddSymbol(context.Id().GetText(), theSymbolRepresentation);
        }
コード例 #17
0
 public override void ExitParens_expression(DecafParser.Parens_expressionContext context)
 {
     setNodeType(context, getNodeType(context.expression()));
 }
コード例 #18
0
 public override void ExitStructImpl_varType(DecafParser.StructImpl_varTypeContext context)
 {
     setNodeType(context, SymbolType.TstructImpl);
     setNodeSymbol(context, new StructImpl(context.Id().GetText()));
 }
コード例 #19
0
 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.");
 }
コード例 #20
0
        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();
        }
コード例 #21
0
 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");
     }
 }
コード例 #22
0
 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.");
 }
コード例 #23
0
 public override void EnterBlock(DecafParser.BlockContext context)
 {
     theScopeManager.EnterScope();
 }
コード例 #24
0
 public override void EnterProgram(DecafParser.ProgramContext context)
 {
     theScopeManager.EnterScope();
 }
コード例 #25
0
 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);
     }
 }
コード例 #26
0
 public override void EnterStructDeclaration(DecafParser.StructDeclarationContext context)
 {
     theScopeManager.EnterScope();
 }
コード例 #27
0
 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);
 }
コード例 #28
0
 public override void EnterProgram(DecafParser.ProgramContext context)
 {
     globals = new GlobalScope();
     currentScope = globals;
     setNodeScope(context, currentScope);
 }
コード例 #29
0
        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);
        }
コード例 #30
0
 public override void ExitStructDeclaration(DecafParser.StructDeclarationContext context)
 {
     currentScope = currentScope.getEnclosingScope();
 }
コード例 #31
0
 public override void ExitChar_varType(DecafParser.Char_varTypeContext context)
 {
     setNodeType(context, SymbolType.Tchar);
 }