Correct #sparse usage and error messages

This commit is contained in:
gingerBill
2022-02-05 13:09:16 +00:00
parent 2bcc7b0064
commit 6418ec3b21
4 changed files with 25 additions and 8 deletions
+1 -1
View File
@@ -172,7 +172,7 @@ Error :: enum int {
Unimplemented = 127,
}
Error_String :: #partial [Error]string{
Error_String :: #sparse[Error]string{
.Okay = "Okay",
.Out_Of_Memory = "Out of memory",
.Invalid_Pointer = "Invalid pointer",
+7 -7
View File
@@ -1921,14 +1921,14 @@ constant_literal_expressions :: proc() {
fmt.println("-------")
Partial_Baz :: enum{A=5, B, C, D=16}
#assert(len(Partial_Baz) < len(#partial [Partial_Baz]int))
PARTIAL_ENUM_ARRAY_CONST :: #partial [Partial_Baz]int{.A ..= .C = 1, .D = 16}
Sparse_Baz :: enum{A=5, B, C, D=16}
#assert(len(Sparse_Baz) < len(#sparse[Sparse_Baz]int))
SPARSE_ENUM_ARRAY_CONST :: #sparse[Sparse_Baz]int{.A ..= .C = 1, .D = 16}
fmt.println(PARTIAL_ENUM_ARRAY_CONST[.A])
fmt.println(PARTIAL_ENUM_ARRAY_CONST[.B])
fmt.println(PARTIAL_ENUM_ARRAY_CONST[.C])
fmt.println(PARTIAL_ENUM_ARRAY_CONST[.D])
fmt.println(SPARSE_ENUM_ARRAY_CONST[.A])
fmt.println(SPARSE_ENUM_ARRAY_CONST[.B])
fmt.println(SPARSE_ENUM_ARRAY_CONST[.C])
fmt.println(SPARSE_ENUM_ARRAY_CONST[.D])
fmt.println("-------")
+16
View File
@@ -2134,6 +2134,22 @@ Ast *parse_operand(AstFile *f, bool lhs) {
break;
}
return original_type;
} else if (name.string == "partial") {
Ast *tag = ast_basic_directive(f, token, name);
Ast *original_expr = parse_expr(f, lhs);
Ast *expr = unparen_expr(original_expr);
switch (expr->kind) {
case Ast_ArrayType:
syntax_error(expr, "#partial has been replaced with #sparse for non-contiguous enumerated array types");
break;
case Ast_CompoundLit:
expr->CompoundLit.tag = tag;
break;
default:
syntax_error(expr, "Expected a compound literal after #%.*s, got %.*s", LIT(name.string), LIT(ast_strings[expr->kind]));
break;
}
return original_expr;
} else if (name.string == "sparse") {
Ast *tag = ast_basic_directive(f, token, name);
Ast *original_type = parse_type(f);
+1
View File
@@ -350,6 +350,7 @@ char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = {
Slice<Ast *> elems; \
Token open, close; \
i64 max_count; \
Ast *tag; \
}) \
AST_KIND(_ExprBegin, "", bool) \
AST_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \