aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bootstrap/test.cff3
-rw-r--r--src/cffc.hff1
-rw-r--r--src/parse.cff18
3 files changed, 21 insertions, 1 deletions
diff --git a/bootstrap/test.cff b/bootstrap/test.cff
index 31dfd6b..4475b0b 100644
--- a/bootstrap/test.cff
+++ b/bootstrap/test.cff
@@ -92,6 +92,9 @@ extern fn main (argc int, argv **u8) int {
Vec2f:zero(&x);
printf("mag = %g\n", (&x)->mag());
+ let x = Number:none;
+ let x = Number:int{};
+
let is []int = { [4] = 1, 2, [1 - 1] = 3 };
isort(is, is.#len);
each(i, x, is,
diff --git a/src/cffc.hff b/src/cffc.hff
index eff95e4..686de57 100644
--- a/src/cffc.hff
+++ b/src/cffc.hff
@@ -168,6 +168,7 @@ struct Expr {
Call struct { lhs *Expr, args [#]Expr },
ZeroIni,
EnumIni struct { name *const u8, val i64 },
+ EUnionIni struct { var *const AggField, ex *Expr },
AggIni struct { flds [#]*const AggField, exs [#]Expr },
ArrIni struct { idxs [#]u32, exs [#]Expr, maxn i64 },
Stmt [#]Stmt,
diff --git a/src/parse.cff b/src/parse.cff
index 84df71a..0aa9fb9 100644
--- a/src/parse.cff
+++ b/src/parse.cff
@@ -1226,6 +1226,7 @@ fn pexprimary(P *Parser) Expr {
fatal(P, tok.loc, "cannot infer type for variant");
}
let ty = P.targty;
+ P.targty = #null;
if ty->is(:Enum) {
let name = (tok = lexexpects(P, :ident, "variant name")).u.ident;
let i i64 #?;
@@ -1240,7 +1241,22 @@ fn pexprimary(P *Parser) Expr {
} while #f;
ex = { tok.loc, ty, :EnumIni { name, i }};
} else if ty->is(:Agg) and ty.u.Agg.kind == :EUnion {
- assert(#f, "NYI");
+ let name = (tok = lexexpects(P, :ident, "variant name")).u.ident;
+ let fld = findaggfield(ty, name);
+ if fld == #null {
+ fatal(P, tok.loc, "%t has no such variant %qT", ty, tok);
+ }
+ let iex *Expr = #null;
+ if lexpeek(P).t == '(' or lexpeek(P).t == '{' {
+ P.targty = fld.ty;
+ iex = exprdup(P.alloc, parseexpr(P));
+ if fld.ty == #null {
+ fatal(P, iex.loc, "%t variant %qT is empty", ty, tok);
+ }
+ } else if fld.ty {
+ fatal(P, P.tokloc, "%t variant %qT must be initialized", ty, tok);
+ }
+ ex = { tok.loc, ty, :EUnionIni { fld, iex }};
} else {
fatal(P, tok.loc, "cannot infer type for variant");
}