Esempio n. 1
0
 public override void ExitStruct_definition(CSharpParser.Struct_definitionContext context)
 {
     Console.WriteLine("Exiting struct_definition context.");
     // Exit struct scope in the symbol table
     symbolTable.ExitScope();
 }
Esempio n. 2
0
 public override object VisitStruct_definition([NotNull] CSharpParser.Struct_definitionContext context)
 {
     StructIdentity = context.identifier();
     return(null);
 }
Esempio n. 3
0
        public override void EnterStruct_definition(CSharpParser.Struct_definitionContext context)
        {
            Console.WriteLine("Entering struct_definition context.");

            // Getting the current scope node:
            Node currentScopeNode = ast.GetNode(symbolTable.CurrentScopeNode);

            // Add class symbol to symbol table: parent class, owner class and other informations and add to node data:

            // Getting the parent classes:
            List <IToken> interfaceTokens = new List <IToken>(); // Will hold the base class tokens

            // Getting the interfaces' names identifiers:
            CSharpParser.Struct_interfacesContext interfaces = context.struct_interfaces();
            if (interfaces != null)
            {
                CSharpParser.Interface_type_listContext typeListCtx = interfaces.interface_type_list();
                if (typeListCtx != null)
                {
                    CSharpParser.Namespace_or_type_nameContext[] types = typeListCtx.namespace_or_type_name();
                    foreach (CSharpParser.Namespace_or_type_nameContext name in types)
                    {
                        CSharpParser.IdentifierContext[] ids = name.identifier();
                        foreach (CSharpParser.IdentifierContext id in ids)
                        {
                            interfaceTokens.Add(id.Start);
                        }
                    }
                }
            }

            // Getting the base classes' symbols:
            Symbol[]           interfaceSymbols      = symbolTable.FindSymbols(interfaceTokens.ToArray(), ast);
            List <ClassSymbol> interfaceClassSymbols = new List <ClassSymbol>();

            foreach (Symbol bs in interfaceSymbols)
            {
                interfaceClassSymbols.Add((ClassSymbol)bs);
            }

            // Getting the class' modifiers:
            Symbol.ModifierFlag modFlags = TreatModTokens();
            modifiersTokens.Clear();

            // Creating the class symbol:
            ClassSymbol structSymbol = new ClassSymbol(modFlags, interfaceClassSymbols.ToArray());

            // Adding the class node as a child to the current scope's AST node:
            Type type            = new Type(context.STRUCT().Symbol);
            Node structNode      = new Node(context.STRUCT().Symbol, Node.Kind.ClassDefinition, type);
            int  structNodeIndex = ast.NodeIndex(structNode);

            currentScopeNode.AddChildIndex(structNodeIndex);

            // Adding the struct node:
            ast.AddNode(structNode);

            // Adding an identifier node as a struct node child:
            CSharpParser.IdentifierContext idCtx = context.identifier();
            IToken idToken = idCtx.Start;

            // Adding the struct body node as a class node child:
            CSharpParser.Struct_bodyContext bodyCtx = context.struct_body();
            ClassType structType = new ClassType(idToken, ClassTag.Struct, structSymbol);

            symbolTable.AddType(structType);
            Node bodyNode = new Node(idToken, Node.Kind.ClassBody, structType);

            ast.AddNode(bodyNode);
            int bodyNodeIndex = ast.NodeIndex(bodyNode);

            structNode.AddChildIndex(bodyNodeIndex);

            // Enter scope in the symbol table
            symbolTable.EnterScope(bodyNodeIndex);

            symbolTable.AddSymbol(idToken, structSymbol);
        }