예제 #1
0
        public override bool VisitStmtVarDefFull([NotNull] GamaParser.StmtVarDefFullContext context)
        {
            var name = context.Symbol().GetText();
            var val  = Top.FindValue(name);

            if (val != null)
            {
                NamespaceContext.Context.AddError(new ErrorDuplicateVariable(context));
                return(false);
            }
            var ty = NamespaceContext.FindTypeRefGlobal(context.typeName());

            if (ty == null)
            {
                NamespaceContext.Context.AddError(new ErrorTypeNotFound(context.typeName()));
                return(false);
            }
            if (ty == InstanceTypes.Void)
            {
                NamespaceContext.Context.AddError(new ErrorVariableVoid(context.typeName()));
                return(false);
            }
            val = VisitExpression(ty, context.expr());
            if (val == null)
            {
                return(false);
            }
            // If expression compiler returns a function then it's impossible for a type mismatch to happen
            // Expression compiler checks for type mismatches when evaluating function pointers
            // Alsp no need to check if 'ty' is a GamaFunction either, expressiom compiler already did that
            if (val.Type is GamaFunction valtyfn)
            {
                var tyfn = new GamaPointer(ty as GamaFunction);
                /* LLVM */
                CurrentBlock.PositionBuilderAtEnd(Builder);
                var alloc = Builder.BuildAlloca(tyfn.UnderlyingType, name);
                Builder.BuildStore(val.Value, alloc);

                Top.AddValue(name, new GamaValueRef(ty, alloc, true));
                return(true);
            }

            // If not function, there might be a mismatch. Check it and error if it mismatches
            if (val.Type != ty)
            {
                NamespaceContext.Context.AddError(new ErrorTypeMismatch(context.expr()));
                return(false);
            }

            /* LLVM */
            // One block above to avoid dumb C# error message saying 'alloc' is already defined (SOMEHOW)
            {
                CurrentBlock.PositionBuilderAtEnd(Builder);
                var alloc = Builder.BuildAlloca(ty.UnderlyingType, name);
                Builder.BuildStore(val.Value, alloc);

                Top.AddValue(name, new GamaValueRef(ty, alloc, true));
            }

            return(true);
        }
예제 #2
0
 /// <summary>
 /// Visit a parse tree produced by the <c>StmtVarDefFull</c>
 /// labeled alternative in <see cref="GamaParser.stmtVarDef"/>.
 /// <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 VisitStmtVarDefFull([NotNull] GamaParser.StmtVarDefFullContext context)
 {
     return(VisitChildren(context));
 }
예제 #3
0
 /// <summary>
 /// Exit a parse tree produced by the <c>StmtVarDefFull</c>
 /// labeled alternative in <see cref="GamaParser.stmtVarDef"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitStmtVarDefFull([NotNull] GamaParser.StmtVarDefFullContext context)
 {
 }