public override string VisitConstantDeclaration(AstConstantDeclaration decl, int data = 0) { StringBuilder sb = new StringBuilder(); if (decl.GetFlag(StmtFlags.IsLocal)) { sb.Append("local "); } sb.Append(decl.Pattern.Accept(this)); switch (decl.Type) { case CheezTypeType _: case FunctionType _: case GenericType _: sb.Append($" :"); break; default: sb.Append($" : {decl.Type} "); break; } sb.Append(": "); sb.Append(decl.Initializer.Accept(this)); if (decl.Type == CheezType.Type && !(decl.Initializer is AstStructTypeExpr || decl.Initializer is AstEnumTypeExpr || decl.Initializer is AstTraitTypeExpr)) { sb.Append(" = "); sb.Append(decl.Initializer.Value); } return(sb.ToString()); }
private AstStatement AnalyseConstantDeclaration(AstConstantDeclaration c) { if (c.HasDirective("local")) { c.SetFlag(StmtFlags.IsLocal, true); } if (c.TypeExpr != null) { c.TypeExpr.AttachTo(c); c.TypeExpr.SetFlag(ExprFlags.ValueRequired, true); c.TypeExpr = ResolveTypeNow(c.TypeExpr, out var t); c.Type = t; } c.Initializer.AttachTo(c); c.Initializer.SetFlag(ExprFlags.ValueRequired, true); c.Initializer = InferType(c.Initializer, c.Type); if (c.Type == null) { c.Type = c.Initializer.Type; } else { c.Initializer = CheckType(c.Initializer, c.Type); } if (!c.Initializer.IsCompTimeValue) { ReportError(c.Initializer, $"Value of constant declaration must be constant"); return(c); } c.Value = c.Initializer.Value; CheckValueRangeForType(c.Type, c.Value, c.Initializer); var(ok, other) = c.GetFlag(StmtFlags.IsLocal) ? c.Scope.DefineLocalSymbol(c) : c.Scope.DefineSymbol(c); if (!ok) { ReportError(c, $"A symbol with name '{c.Name.Name}' already exists in this scope", ("Other declaration here:", other)); } return(c); }
public override string VisitConstantDeclaration(AstConstantDeclaration decl, int data = 0) { StringBuilder sb = new StringBuilder(); sb.Append(decl.Pattern.Accept(this)); if (decl.TypeExpr != null) { sb.Append($" : {decl.TypeExpr.Accept(this)} "); } else { sb.Append(" :"); } sb.Append(": "); sb.Append(decl.Initializer.Accept(this)); return(sb.ToString()); }
private IEnumerable <AstConstantDeclaration> SplitConstantDeclaration(AstConstantDeclaration v) { v.Pattern.SetFlag(ExprFlags.IsDeclarationPattern, true); switch (v.Pattern) { case AstCompCallExpr cc when cc.Name.Name == "id": cc.AttachTo(v); v.Name = InferType(cc, null) as AstIdExpr; break; case AstIdExpr name: // ok, do nothing v.Name = name; break; case AstTupleExpr t: v.Name = new AstIdExpr(GetUniqueName(t.ToString()), false, t); // create new declarations for sub patterns var index = 0; foreach (var subPattern in t.Values) { var init = new AstArrayAccessExpr( new AstConstantRef(v, v.Initializer), new AstNumberExpr(NumberData.FromBigInt(index), Location: v.Initializer)); var sub = new AstConstantDeclaration(subPattern, null, init, v.Documentation, null, Location: v); sub.Scope = v.Scope; sub.SetFlags(v.GetFlags()); yield return(sub); index += 1; } v.Pattern = v.Name; break; default: ReportError(v.Pattern, $"Invalid pattern in variable declaration"); break; } }
// declarations public virtual ReturnType VisitConstantDeclaration(AstConstantDeclaration decl, DataType data = default) => default;
public AstConstantRef(AstConstantDeclaration let, ILocation Location = null) : base(Location) { Declaration = let; Type = let.Type; }