예제 #1
0
        public override void EnterVarDecl([NotNull] DaedalusParser.VarDeclContext context)
        {
            if (context.Parent.Parent is DaedalusParser.DaedalusFileContext || _assemblyBuilder.IsContextInsideExecBlock())
            {
                var typeName = context.typeReference().GetText();
                var type     = DatSymbolTypeFromString(typeName);
                if (type == DatSymbolType.Class)
                {
                    type = DatSymbolType.Instance;
                }

                for (int i = 0; i < context.ChildCount; i++)
                {
                    var varContext = context.GetChild(i);

                    if (varContext is TerminalNodeImpl)
                    {
                        continue; // skips ','
                    }
                    if (varContext is DaedalusParser.VarValueDeclContext varValueContext)
                    {
                        var name = varValueContext.nameNode().GetText();
                        if (_assemblyBuilder.IsContextInsideExecBlock())
                        {
                            // TODO consider making assemblyBuilder.active public and using it here
                            BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last();
                            string execBlockName = baseExecBlock.GetSymbol().Name;
                            name = $"{execBlockName}.{name}";
                        }

                        var location = GetLocation(context);

                        int           parentIndex       = DatSymbol.NULL_INDEX;
                        string        parameterTypeName = context.typeReference().GetText();
                        DatSymbolType?parameterType     = DatSymbolTypeFromString(parameterTypeName);
                        if (parameterType is DatSymbolType.Class)
                        {
                            var parentSymbol = _assemblyBuilder.ResolveSymbol(parameterTypeName);
                            parentIndex = parentSymbol.Index;
                        }

                        var symbol = SymbolBuilder.BuildVariable(name, type, location, parentIndex); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);
                    }

                    if (varContext is DaedalusParser.VarArrayDeclContext varArrayContext)
                    {
                        var name     = varArrayContext.nameNode().GetText();
                        var location = GetLocation(context);
                        var size     = EvaluatorHelper.EvaluteArraySize(varArrayContext.arraySize(), _assemblyBuilder);

                        var symbol =
                            SymbolBuilder.BuildArrOfVariables(name, type, (uint)size,
                                                              location); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);
                    }
                }
            }
        }
예제 #2
0
        public override void EnterVarDecl([NotNull] DaedalusParser.VarDeclContext context)
        {
            var varDecls = context.varDecl();

            if (varDecls != null && varDecls.Length > 0)
            {
                var error = new RecognitionException(parser, parser.InputStream, context);
                error.Data.Add(SyntaxError.DataKey_ErrorCode, SyntaxErrorCodes.D0002_Split_Multiple_Var_Decl);
                parser.NotifyErrorListeners(varDecls[0].Start, error.Message, error);
            }
            base.EnterVarDecl(context);
        }
예제 #3
0
        private VarDeclarationsTemporaryNode GetVarDeclarationsTemporaryNode(DaedalusParser.VarDeclContext varDeclContext)
        {
            NameNode typeNameNode = new NameNode(GetLocation(varDeclContext.dataType()), varDeclContext.dataType().GetText());

            List <DeclarationNode> varDeclarationNodes = new List <DeclarationNode>();

            foreach (IParseTree childContext in varDeclContext.children)
            {
                if (childContext is DaedalusParser.VarValueDeclContext varValueDeclContext)
                {
                    DaedalusParser.NameNodeContext nameNodeContext = varValueDeclContext.nameNode();
                    NameNode nameNode = new NameNode(GetLocation(nameNodeContext), nameNodeContext.GetText());

                    ExpressionNode rightSideNode = null;
                    if (varValueDeclContext.varValueAssignment() != null)
                    {
                        rightSideNode = (ExpressionNode)Visit(varValueDeclContext.varValueAssignment().expression());                          // TODO check if null
                    }

                    varDeclarationNodes.Add(new VarDeclarationNode(GetLocation(varValueDeclContext), typeNameNode, nameNode, rightSideNode));
                }
                else if (childContext is DaedalusParser.VarArrayDeclContext varArrayDeclContext)
                {
                    DaedalusParser.NameNodeContext nameNodeContext = varArrayDeclContext.nameNode();
                    NameNode       nameNode      = new NameNode(GetLocation(nameNodeContext), nameNodeContext.GetText());
                    ExpressionNode arraySizeNode = (ExpressionNode)VisitArraySize(varArrayDeclContext.arraySize());


                    List <ExpressionNode> elementNodes = null;
                    if (varArrayDeclContext.varArrayAssignment() != null)
                    {
                        elementNodes = new List <ExpressionNode>();
                        foreach (DaedalusParser.ExpressionContext expressionContext in varArrayDeclContext.varArrayAssignment().expression())
                        {
                            elementNodes.Add((ExpressionNode)Visit(expressionContext));
                        }
                    }

                    VarArrayDeclarationNode varArrayDeclarationNode =
                        new VarArrayDeclarationNode(GetLocation(nameNodeContext), typeNameNode, nameNode, arraySizeNode, elementNodes);                         // TODO check if null
                    _arrayDeclarationNodes.Add(varArrayDeclarationNode);
                    varDeclarationNodes.Add(varArrayDeclarationNode);
                }
            }
            return(new VarDeclarationsTemporaryNode(GetLocation(varDeclContext), varDeclarationNodes));
        }
예제 #4
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="DaedalusParser.varDecl"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitVarDecl([NotNull] DaedalusParser.VarDeclContext context)
 {
     return(VisitChildren(context));
 }
예제 #5
0
 public override ASTNode VisitVarDecl([NotNull] DaedalusParser.VarDeclContext context)
 {
     return(GetVarDeclarationsTemporaryNode(context));
 }
 /// <summary>
 /// Exit a parse tree produced by <see cref="DaedalusParser.varDecl"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitVarDecl([NotNull] DaedalusParser.VarDeclContext context)
 {
 }