public override IVecValue VisitVardef([NotNull] VectorizeParser.VardefContext context) { var name = context.ID().GetText(); var type = ParseType(context.type()); bool isArray = context.array != null; var initialValue = context.initial?.Accept(this); var sizeV = context.size?.Accept(this); if (initialValue != null) { if (isArray) { if (initialValue.Type != VecValueType.Array) { throw new InvalidOperationException($"Variable '{name}': Initial value must be array! Was: {initialValue.Type}"); } var container = (IVecContainer)initialValue; if (container.ElementType != type) { throw new InvalidOperationException($"Variable '{name}': Initial value must be array of type {type}! Was: {container.ElementType}"); } } else { if (type != initialValue.Type) { throw new InvalidOperationException($"Type must match for initial value of variable!"); } } } else { if (isArray) { if (sizeV == null) { initialValue = ConstructDefault(type, 0); } else if (sizeV is VecInt size) { initialValue = ConstructDefault(type, size.Value); } else { throw new InvalidOperationException($"Size of array must be a int! Was: {sizeV.Type}"); } } else { initialValue = ConstructDefault(type); } } CurrentScope.DeclareVariable(name, initialValue); return(VecNull.Value); }
/// <summary> /// Exit a parse tree produced by <see cref="VectorizeParser.vardef"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitVardef([NotNull] VectorizeParser.VardefContext context) { }
/// <summary> /// Visit a parse tree produced by <see cref="VectorizeParser.vardef"/>. /// <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 VisitVardef([NotNull] VectorizeParser.VardefContext context) { return(VisitChildren(context)); }