示例#1
0
        public override void EnterDecl(LatteParser.DeclContext context)
        {
            if (context.type().Equals(new LatteParser.TVoidContext()))
            {
                StateUtils.InterruptWithMessage(
                    context.start.Line,
                    context.start.Column,
                    ErrorMessages.VoidDeclaration);
            }

            foreach (var decl in context.item())
            {
                var id = decl.ID().GetText();
                if (_environment.NameToVarDef.ContainsKey(id) &&
                    _environment.NameToVarDef[id].IsDefinedInCurrentBlock)
                {
                    _errorState.AddErrorMessage(new ErrorMessage(
                                                    decl.start.Line,
                                                    decl.start.Column,
                                                    ErrorMessages.VarAlreadyDefined(id)));
                }

                if (_skipNextDecl)
                {
                    _skipNextDecl = false;
                }
                else
                {
                    _environment.NameToVarDef[id] = new VarDef(context.type(), id);
                }

                if (decl.expr() == null)
                {
                    continue;
                }

                var exprType = new ExpressionTypeVisitor().Visit(decl.expr());
                if (!context.type().Equals(exprType) && !IsTypeParent(exprType, context.type()))
                {
                    StateUtils.InterruptWithMessage(
                        decl.start.Line,
                        decl.start.Column,
                        ErrorMessages.VarExprTypesMismatch(decl.ID().GetText()));
                }
            }
        }
示例#2
0
 public Decl(LatteParser.DeclContext context)
 {
     Type = context.type();
     context.item().ToList().ForEach(item => Items.Add(new Item(item)));
 }