Remove need for type keyword

This commit is contained in:
Ginger Bill
2017-02-10 16:12:14 +00:00
parent f18ae89931
commit 73d6a55f5c
11 changed files with 194 additions and 219 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
#foreign_system_library ws2 "Ws2_32.lib" when ODIN_OS == "windows"; #foreign_system_library ws2 "Ws2_32.lib" when ODIN_OS == "windows";
SOCKET :: type uint; SOCKET :: #type uint;
INVALID_SOCKET :: ~(cast(SOCKET)0); INVALID_SOCKET :: ~(cast(SOCKET)0);
AF :: enum i32 { AF :: enum i32 {
+6 -6
View File
@@ -109,7 +109,7 @@ type_info_base :: proc(info: ^Type_Info) -> ^Type_Info {
return nil; return nil;
} }
base := info; base := info;
match type i in base { match i in base {
case Type_Info.Named: case Type_Info.Named:
base = i.base; base = i.base;
} }
@@ -122,7 +122,7 @@ type_info_base_without_enum :: proc(info: ^Type_Info) -> ^Type_Info {
return nil; return nil;
} }
base := info; base := info;
match type i in base { match i in base {
case Type_Info.Named: case Type_Info.Named:
base = i.base; base = i.base;
case Type_Info.Enum: case Type_Info.Enum:
@@ -152,9 +152,9 @@ Allocator_Mode :: enum u8 {
FREE_ALL, FREE_ALL,
RESIZE, RESIZE,
} }
Allocator_Proc :: type proc(allocator_data: rawptr, mode: Allocator_Mode, Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr; old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
Allocator :: struct #ordered { Allocator :: struct #ordered {
procedure: Allocator_Proc, procedure: Allocator_Proc,
data: rawptr, data: rawptr,
@@ -657,7 +657,7 @@ __dynamic_map_erase :: proc(using h: __Map_Header, fr: __Map_Find_Result) {
__print_ti_ptr :: proc(ti: ^Type_Info) { __print_ti_ptr :: proc(ti: ^Type_Info) {
fmt.println(ti); fmt.println(ti);
match type e in ti { match e in ti {
case Type_Info.Enum: case Type_Info.Enum:
fmt.println(e.names); fmt.println(e.names);
} }
+8 -8
View File
@@ -107,7 +107,7 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) {
} }
using Type_Info; using Type_Info;
match type info in ti { match info in ti {
case Named: case Named:
buffer_write_string(buf, info.name); buffer_write_string(buf, info.name);
case Integer: case Integer:
@@ -355,7 +355,7 @@ int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) {
if arg_index < args.count { if arg_index < args.count {
arg := args[arg_index]; arg := args[arg_index];
arg.type_info = type_info_base(arg.type_info); arg.type_info = type_info_base(arg.type_info);
match type i in arg { match i in arg {
case int: num = i; case int: num = i;
case i8: num = cast(int)i; case i8: num = cast(int)i;
case i16: num = cast(int)i; case i16: num = cast(int)i;
@@ -645,7 +645,7 @@ fmt_enum :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
} }
using Type_Info; using Type_Info;
match type e in v.type_info { match e in v.type_info {
default: default:
fmt_bad_verb(fi, verb); fmt_bad_verb(fi, verb);
return; return;
@@ -658,7 +658,7 @@ fmt_enum :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
f: f64; f: f64;
ok := false; ok := false;
a := any{type_info_base(e.base), v.data}; a := any{type_info_base(e.base), v.data};
match type v in a { match v in a {
case i8: i = cast(i64)v; case i8: i = cast(i64)v;
case i16: i = cast(i64)v; case i16: i = cast(i64)v;
case i32: i = cast(i64)v; case i32: i = cast(i64)v;
@@ -709,9 +709,9 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
} }
using Type_Info; using Type_Info;
match type info in v.type_info { match info in v.type_info {
case Named: case Named:
match type b in info.base { match b in info.base {
case Struct: case Struct:
if verb != 'v' { if verb != 'v' {
fmt_bad_verb(fi, verb); fmt_bad_verb(fi, verb);
@@ -882,7 +882,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
if verb == 'T' { if verb == 'T' {
ti := arg.type_info; ti := arg.type_info;
match type a in arg { match a in arg {
case ^Type_Info: ti = a; case ^Type_Info: ti = a;
} }
buffer_write_type(fi.buf, ti); buffer_write_type(fi.buf, ti);
@@ -892,7 +892,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
base_arg := arg; base_arg := arg;
base_arg.type_info = type_info_base(base_arg.type_info); base_arg.type_info = type_info_base(base_arg.type_info);
match type a in base_arg { match a in base_arg {
case bool: fmt_bool(fi, a, verb); case bool: fmt_bool(fi, a, verb);
case f32: fmt_float(fi, cast(f64)a, 32, verb); case f32: fmt_float(fi, cast(f64)a, 32, verb);
case f64: fmt_float(fi, a, 64, verb); case f64: fmt_float(fi, a, 64, verb);
+2 -2
View File
@@ -217,7 +217,7 @@ align_of_type_info :: proc(type_info: ^Type_Info) -> int {
WORD_SIZE :: size_of(int); WORD_SIZE :: size_of(int);
MAX_ALIGN :: size_of([vector 64]f64); // TODO(bill): Should these constants be builtin constants? MAX_ALIGN :: size_of([vector 64]f64); // TODO(bill): Should these constants be builtin constants?
using Type_Info; using Type_Info;
match type info in type_info { match info in type_info {
case Named: case Named:
return align_of_type_info(info.base); return align_of_type_info(info.base);
case Integer: case Integer:
@@ -270,7 +270,7 @@ align_formula :: proc(size, align: int) -> int {
size_of_type_info :: proc(type_info: ^Type_Info) -> int { size_of_type_info :: proc(type_info: ^Type_Info) -> int {
WORD_SIZE :: size_of(int); WORD_SIZE :: size_of(int);
using Type_Info; using Type_Info;
match type info in type_info { match info in type_info {
case Named: case Named:
return size_of_type_info(info.base); return size_of_type_info(info.base);
case Integer: case Integer:
+3 -3
View File
@@ -20,7 +20,7 @@ LPARAM :: int;
LRESULT :: int; LRESULT :: int;
ATOM :: i16; ATOM :: i16;
BOOL :: i32; BOOL :: i32;
WNDPROC :: type proc(HWND, u32, WPARAM, LPARAM) -> LRESULT #cc_c; WNDPROC :: #type proc(HWND, u32, WPARAM, LPARAM) -> LRESULT #cc_c;
INVALID_HANDLE_VALUE :: cast(HANDLE)(~cast(int)0); INVALID_HANDLE_VALUE :: cast(HANDLE)(~cast(int)0);
@@ -359,8 +359,8 @@ PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000;
PFD_STEREO_DONTCARE :: 0x80000000; PFD_STEREO_DONTCARE :: 0x80000000;
HGLRC :: HANDLE; HGLRC :: HANDLE;
PROC :: type proc() #cc_c; PROC :: #type proc() #cc_c;
wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC; wglCreateContextAttribsARBType :: #type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC;
PIXELFORMATDESCRIPTOR :: struct #ordered { PIXELFORMATDESCRIPTOR :: struct #ordered {
+17 -17
View File
@@ -11,7 +11,7 @@ is_signed :: proc(info: ^Type_Info) -> bool {
is_integer :: proc(info: ^Type_Info) -> bool { is_integer :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Integer: return true; case Type_Info.Integer: return true;
} }
return false; return false;
@@ -19,7 +19,7 @@ is_integer :: proc(info: ^Type_Info) -> bool {
is_float :: proc(info: ^Type_Info) -> bool { is_float :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Float: return true; case Type_Info.Float: return true;
} }
return false; return false;
@@ -27,7 +27,7 @@ is_float :: proc(info: ^Type_Info) -> bool {
is_any :: proc(info: ^Type_Info) -> bool { is_any :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Any: return true; case Type_Info.Any: return true;
} }
return false; return false;
@@ -35,7 +35,7 @@ is_any :: proc(info: ^Type_Info) -> bool {
is_string :: proc(info: ^Type_Info) -> bool { is_string :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.String: return true; case Type_Info.String: return true;
} }
return false; return false;
@@ -43,7 +43,7 @@ is_string :: proc(info: ^Type_Info) -> bool {
is_boolean :: proc(info: ^Type_Info) -> bool { is_boolean :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Boolean: return true; case Type_Info.Boolean: return true;
} }
return false; return false;
@@ -51,7 +51,7 @@ is_boolean :: proc(info: ^Type_Info) -> bool {
is_pointer :: proc(info: ^Type_Info) -> bool { is_pointer :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Pointer: return true; case Type_Info.Pointer: return true;
} }
return false; return false;
@@ -59,7 +59,7 @@ is_pointer :: proc(info: ^Type_Info) -> bool {
is_procedure :: proc(info: ^Type_Info) -> bool { is_procedure :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Procedure: return true; case Type_Info.Procedure: return true;
} }
return false; return false;
@@ -67,7 +67,7 @@ is_procedure :: proc(info: ^Type_Info) -> bool {
is_array :: proc(info: ^Type_Info) -> bool { is_array :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Array: return true; case Type_Info.Array: return true;
} }
return false; return false;
@@ -75,7 +75,7 @@ is_array :: proc(info: ^Type_Info) -> bool {
is_dynamic_array :: proc(info: ^Type_Info) -> bool { is_dynamic_array :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Dynamic_Array: return true; case Type_Info.Dynamic_Array: return true;
} }
return false; return false;
@@ -83,7 +83,7 @@ is_dynamic_array :: proc(info: ^Type_Info) -> bool {
is_dynamic_map :: proc(info: ^Type_Info) -> bool { is_dynamic_map :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Map: return i.count == 0; case Type_Info.Map: return i.count == 0;
} }
return false; return false;
@@ -91,7 +91,7 @@ is_dynamic_map :: proc(info: ^Type_Info) -> bool {
is_slice :: proc(info: ^Type_Info) -> bool { is_slice :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Slice: return true; case Type_Info.Slice: return true;
} }
return false; return false;
@@ -99,7 +99,7 @@ is_slice :: proc(info: ^Type_Info) -> bool {
is_vector :: proc(info: ^Type_Info) -> bool { is_vector :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Vector: return true; case Type_Info.Vector: return true;
} }
return false; return false;
@@ -107,7 +107,7 @@ is_vector :: proc(info: ^Type_Info) -> bool {
is_tuple :: proc(info: ^Type_Info) -> bool { is_tuple :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Tuple: return true; case Type_Info.Tuple: return true;
} }
return false; return false;
@@ -115,7 +115,7 @@ is_tuple :: proc(info: ^Type_Info) -> bool {
is_struct :: proc(info: ^Type_Info) -> bool { is_struct :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Struct: return true; case Type_Info.Struct: return true;
} }
return false; return false;
@@ -123,7 +123,7 @@ is_struct :: proc(info: ^Type_Info) -> bool {
is_union :: proc(info: ^Type_Info) -> bool { is_union :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Union: return true; case Type_Info.Union: return true;
} }
return false; return false;
@@ -131,7 +131,7 @@ is_union :: proc(info: ^Type_Info) -> bool {
is_raw_union :: proc(info: ^Type_Info) -> bool { is_raw_union :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Raw_Union: return true; case Type_Info.Raw_Union: return true;
} }
return false; return false;
@@ -139,7 +139,7 @@ is_raw_union :: proc(info: ^Type_Info) -> bool {
is_enum :: proc(info: ^Type_Info) -> bool { is_enum :: proc(info: ^Type_Info) -> bool {
if info == nil { return false; } if info == nil { return false; }
match type i in type_info_base(info) { match i in type_info_base(info) {
case Type_Info.Enum: return true; case Type_Info.Enum: return true;
} }
return false; return false;
+5 -4
View File
@@ -4766,11 +4766,12 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
o->expr = node; o->expr = node;
case_end; case_end;
case_ast_node(te, TagExpr, node); case_ast_node(te, TagExpr, node);
// TODO(bill): Tag expressions String name = te->name.string;
error_node(node, "Tag expressions are not supported yet"); error_node(node, "Unknown tag expression, #%.*s", LIT(name));
kind = check_expr_base(c, o, te->expr, type_hint); if (te->expr) {
kind = check_expr_base(c, o, te->expr, type_hint);
}
o->expr = node; o->expr = node;
case_end; case_end;
+25 -5
View File
@@ -942,7 +942,25 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
bool is_union_ptr = false; bool is_union_ptr = false;
bool is_any = false; bool is_any = false;
check_expr(c, &x, ms->tag); if (ms->tag->kind != AstNode_AssignStmt) {
error_node(ms->tag, "Expected an `in` assignment for this type match statement");
break;
}
ast_node(as, AssignStmt, ms->tag);
Token as_token = ast_node_token(ms->tag);
if (as->lhs.count != 1) {
syntax_error(as_token, "Expected 1 name before `in`");
break;
}
if (as->rhs.count != 1) {
syntax_error(as_token, "Expected 1 expression after `in`");
break;
}
AstNode *lhs = as->lhs.e[0];
AstNode *rhs = as->rhs.e[0];
check_expr(c, &x, rhs);
check_assignment(c, &x, NULL, str_lit("type match expression")); check_assignment(c, &x, NULL, str_lit("type match expression"));
if (!check_valid_type_match_type(x.type, &is_union_ptr, &is_any)) { if (!check_valid_type_match_type(x.type, &is_union_ptr, &is_any)) {
gbString str = type_to_string(x.type); gbString str = type_to_string(x.type);
@@ -980,7 +998,9 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
} }
} }
if (ms->var->kind != AstNode_Ident) {
if (unparen_expr(lhs)->kind != AstNode_Ident) {
error_node(rhs, "Expected an identifier, got `%.*s`", LIT(ast_node_strings[rhs->kind]));
break; break;
} }
@@ -1056,10 +1076,10 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
tt = make_type_pointer(c->allocator, case_type); tt = make_type_pointer(c->allocator, case_type);
add_type_info_type(c, tt); add_type_info_type(c, tt);
} }
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt, true); Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, lhs->Ident, tt, true);
tag_var->flags |= EntityFlag_Used; tag_var->flags |= EntityFlag_Used;
add_entity(c, c->context.scope, ms->var, tag_var); add_entity(c, c->context.scope, lhs, tag_var);
add_entity_use(c, ms->var, tag_var); add_entity_use(c, lhs, tag_var);
} }
check_stmt_list(c, cc->stmts, mod_flags); check_stmt_list(c, cc->stmts, mod_flags);
check_close_scope(c); check_close_scope(c);
+8 -2
View File
@@ -5255,7 +5255,13 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
ir_emit_comment(proc, str_lit("TypeMatchStmt")); ir_emit_comment(proc, str_lit("TypeMatchStmt"));
gbAllocator allocator = proc->module->allocator; gbAllocator allocator = proc->module->allocator;
irValue *parent = ir_build_expr(proc, ms->tag); ast_node(as, AssignStmt, ms->tag);
GB_ASSERT(as->lhs.count == 1);
GB_ASSERT(as->rhs.count == 1);
AstNode *lhs = as->lhs.e[0];
AstNode *rhs = as->rhs.e[0];
irValue *parent = ir_build_expr(proc, rhs);
bool is_union_ptr = false; bool is_union_ptr = false;
bool is_any = false; bool is_any = false;
GB_ASSERT(check_valid_type_match_type(ir_type(parent), &is_union_ptr, &is_any)); GB_ASSERT(check_valid_type_match_type(ir_type(parent), &is_union_ptr, &is_any));
@@ -5276,7 +5282,7 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
ast_node(body, BlockStmt, ms->body); ast_node(body, BlockStmt, ms->body);
String tag_var_name = ms->var->Ident.string; String tag_var_name = lhs->Ident.string;
AstNodeArray default_stmts = {0}; AstNodeArray default_stmts = {0};
+38 -49
View File
@@ -250,7 +250,6 @@ AST_NODE_KIND(_ComplexStmtBegin, "", i32) \
AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \ AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \
Token token; \ Token token; \
AstNode *tag; \ AstNode *tag; \
AstNode *var; \
AstNode *body; \ AstNode *body; \
}) \ }) \
AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \ AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
@@ -893,11 +892,10 @@ AstNode *ast_match_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, As
} }
AstNode *ast_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *var, AstNode *body) { AstNode *ast_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *body) {
AstNode *result = make_ast_node(f, AstNode_TypeMatchStmt); AstNode *result = make_ast_node(f, AstNode_TypeMatchStmt);
result->TypeMatchStmt.token = token; result->TypeMatchStmt.token = token;
result->TypeMatchStmt.tag = tag; result->TypeMatchStmt.tag = tag;
result->TypeMatchStmt.var = var;
result->TypeMatchStmt.body = body; result->TypeMatchStmt.body = body;
return result; return result;
} }
@@ -1756,6 +1754,7 @@ AstNode *parse_operand(AstFile *f, bool lhs) {
} else if (str_eq(name.string, str_lit("file"))) { return ast_basic_directive(f, token, name.string); } else if (str_eq(name.string, str_lit("file"))) { return ast_basic_directive(f, token, name.string);
} else if (str_eq(name.string, str_lit("line"))) { return ast_basic_directive(f, token, name.string); } else if (str_eq(name.string, str_lit("line"))) { return ast_basic_directive(f, token, name.string);
} else if (str_eq(name.string, str_lit("procedure"))) { return ast_basic_directive(f, token, name.string); } else if (str_eq(name.string, str_lit("procedure"))) { return ast_basic_directive(f, token, name.string);
} else if (str_eq(name.string, str_lit("type"))) { return ast_helper_type(f, token, parse_type(f));
} else { } else {
operand = ast_tag_expr(f, token, name, parse_expr(f, false)); operand = ast_tag_expr(f, token, name, parse_expr(f, false));
} }
@@ -2180,9 +2179,7 @@ AstNode *parse_value_decl(AstFile *f, AstNodeArray lhs) {
bool is_mutable = true; bool is_mutable = true;
if (allow_token(f, Token_Colon)) { if (allow_token(f, Token_Colon)) {
if (!allow_token(f, Token_type)) { type = parse_type_attempt(f);
type = parse_type_attempt(f);
}
} else if (f->curr_token.kind != Token_Eq && } else if (f->curr_token.kind != Token_Eq &&
f->curr_token.kind != Token_Semicolon) { f->curr_token.kind != Token_Semicolon) {
syntax_error(f->curr_token, "Expected a type separator `:` or `=`"); syntax_error(f->curr_token, "Expected a type separator `:` or `=`");
@@ -2528,10 +2525,16 @@ AstNode *parse_type_or_ident(AstFile *f) {
return e; return e;
} }
case Token_type: { case Token_Hash: {
Token token = expect_token(f, Token_type); Token hash_token = expect_token(f, Token_Hash);
AstNode *type = parse_type(f); Token name = expect_token(f, Token_Ident);
return ast_helper_type(f, token, type); String tag = name.string;
if (str_eq(tag, str_lit("type"))) {
AstNode *type = parse_type(f);
return ast_helper_type(f, hash_token, type);
}
syntax_error(name, "Expected `type` after #");
return ast_bad_expr(f, hash_token, f->curr_token);
} }
case Token_Pointer: { case Token_Pointer: {
@@ -2941,8 +2944,7 @@ AstNode *parse_for_stmt(AstFile *f) {
f->expr_level = -1; f->expr_level = -1;
if (f->curr_token.kind != Token_Semicolon) { if (f->curr_token.kind != Token_Semicolon) {
cond = parse_simple_stmt(f, true); cond = parse_simple_stmt(f, true);
if (cond->kind == AstNode_AssignStmt && if (cond->kind == AstNode_AssignStmt && cond->AssignStmt.op.kind == Token_in) {
cond->AssignStmt.op.kind == Token_in) {
is_range = true; is_range = true;
} }
} }
@@ -3034,6 +3036,7 @@ AstNode *parse_for_stmt(AstFile *f) {
#endif #endif
} }
AstNode *parse_case_clause(AstFile *f) { AstNode *parse_case_clause(AstFile *f) {
Token token = f->curr_token; Token token = f->curr_token;
AstNodeArray list = make_ast_node_array(f); AstNodeArray list = make_ast_node_array(f);
@@ -3066,6 +3069,7 @@ AstNode *parse_type_case_clause(AstFile *f) {
} }
AstNode *parse_match_stmt(AstFile *f) { AstNode *parse_match_stmt(AstFile *f) {
if (f->curr_proc == NULL) { if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a match statement in the file scope"); syntax_error(f->curr_token, "You cannot use a match statement in the file scope");
@@ -3077,37 +3081,16 @@ AstNode *parse_match_stmt(AstFile *f) {
AstNode *tag = NULL; AstNode *tag = NULL;
AstNode *body = NULL; AstNode *body = NULL;
Token open, close; Token open, close;
bool is_type_match = false;
if (allow_token(f, Token_type)) { if (f->curr_token.kind != Token_OpenBrace) {
isize prev_level = f->expr_level; isize prev_level = f->expr_level;
f->expr_level = -1; f->expr_level = -1;
AstNode *var = parse_ident(f); tag = parse_simple_stmt(f, true);
expect_token_after(f, Token_in, "match type name"); if (tag->kind == AstNode_AssignStmt && tag->AssignStmt.op.kind == Token_in) {
tag = parse_simple_stmt(f, false); is_type_match = true;
} else {
f->expr_level = prev_level;
open = expect_token(f, Token_OpenBrace);
AstNodeArray list = make_ast_node_array(f);
while (f->curr_token.kind == Token_case ||
f->curr_token.kind == Token_default) {
array_add(&list, parse_type_case_clause(f));
}
close = expect_token(f, Token_CloseBrace);
body = ast_block_stmt(f, list, open, close);
tag = convert_stmt_to_expr(f, tag, str_lit("type match expression"));
return ast_type_match_stmt(f, token, tag, var, body);
} else {
if (f->curr_token.kind != Token_OpenBrace) {
isize prev_level = f->expr_level;
f->expr_level = -1;
if (f->curr_token.kind != Token_Semicolon) {
tag = parse_simple_stmt(f, false);
}
if (allow_token(f, Token_Semicolon)) { if (allow_token(f, Token_Semicolon)) {
init = tag; init = tag;
tag = NULL; tag = NULL;
@@ -3115,28 +3098,33 @@ AstNode *parse_match_stmt(AstFile *f) {
tag = parse_simple_stmt(f, false); tag = parse_simple_stmt(f, false);
} }
} }
f->expr_level = prev_level;
} }
f->expr_level = prev_level;
}
open = expect_token(f, Token_OpenBrace);
AstNodeArray list = make_ast_node_array(f);
open = expect_token(f, Token_OpenBrace); while (f->curr_token.kind == Token_case ||
AstNodeArray list = make_ast_node_array(f); f->curr_token.kind == Token_default) {
if (is_type_match) {
while (f->curr_token.kind == Token_case || array_add(&list, parse_type_case_clause(f));
f->curr_token.kind == Token_default) { } else {
array_add(&list, parse_case_clause(f)); array_add(&list, parse_case_clause(f));
} }
}
close = expect_token(f, Token_CloseBrace); close = expect_token(f, Token_CloseBrace);
body = ast_block_stmt(f, list, open, close); body = ast_block_stmt(f, list, open, close);
if (!is_type_match) {
tag = convert_stmt_to_expr(f, tag, str_lit("match expression")); tag = convert_stmt_to_expr(f, tag, str_lit("match expression"));
return ast_match_stmt(f, token, init, tag, body); return ast_match_stmt(f, token, init, tag, body);
} else {
return ast_type_match_stmt(f, token, tag, body);
} }
} }
AstNode *parse_defer_stmt(AstFile *f) { AstNode *parse_defer_stmt(AstFile *f) {
if (f->curr_proc == NULL) { if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a defer statement in the file scope"); syntax_error(f->curr_token, "You cannot use a defer statement in the file scope");
@@ -3328,6 +3316,7 @@ AstNode *parse_stmt(AstFile *f) {
Token hash_token = expect_token(f, Token_Hash); Token hash_token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Ident); Token name = expect_token(f, Token_Ident);
String tag = name.string; String tag = name.string;
if (str_eq(tag, str_lit("import"))) { if (str_eq(tag, str_lit("import"))) {
AstNode *cond = NULL; AstNode *cond = NULL;
Token import_name = {0}; Token import_name = {0};
+81 -122
View File
@@ -12,23 +12,24 @@ TOKEN_KIND(Token__LiteralBegin, "_LiteralBegin"), \
TOKEN_KIND(Token__LiteralEnd, "_LiteralEnd"), \ TOKEN_KIND(Token__LiteralEnd, "_LiteralEnd"), \
\ \
TOKEN_KIND(Token__OperatorBegin, "_OperatorBegin"), \ TOKEN_KIND(Token__OperatorBegin, "_OperatorBegin"), \
TOKEN_KIND(Token_Eq, "="), \ TOKEN_KIND(Token_Eq, "="), \
TOKEN_KIND(Token_Not, "!"), \ TOKEN_KIND(Token_Not, "!"), \
TOKEN_KIND(Token_Hash, "#"), \ TOKEN_KIND(Token_Hash, "#"), \
TOKEN_KIND(Token_At, "@"), \ TOKEN_KIND(Token_At, "@"), \
TOKEN_KIND(Token_Pointer, "^"), \ TOKEN_KIND(Token_Dollar, "$"), \
/* TOKEN_KIND(Token_Maybe, "?"), */ \ TOKEN_KIND(Token_Pointer, "^"), \
TOKEN_KIND(Token_Add, "+"), \ TOKEN_KIND(Token_Question, "?"), \
TOKEN_KIND(Token_Sub, "-"), \ TOKEN_KIND(Token_Add, "+"), \
TOKEN_KIND(Token_Mul, "*"), \ TOKEN_KIND(Token_Sub, "-"), \
TOKEN_KIND(Token_Quo, "/"), \ TOKEN_KIND(Token_Mul, "*"), \
TOKEN_KIND(Token_Mod, "%"), \ TOKEN_KIND(Token_Quo, "/"), \
TOKEN_KIND(Token_And, "&"), \ TOKEN_KIND(Token_Mod, "%"), \
TOKEN_KIND(Token_Or, "|"), \ TOKEN_KIND(Token_And, "&"), \
TOKEN_KIND(Token_Xor, "~"), \ TOKEN_KIND(Token_Or, "|"), \
TOKEN_KIND(Token_AndNot, "&~"), \ TOKEN_KIND(Token_Xor, "~"), \
TOKEN_KIND(Token_Shl, "<<"), \ TOKEN_KIND(Token_AndNot, "&~"), \
TOKEN_KIND(Token_Shr, ">>"), \ TOKEN_KIND(Token_Shl, "<<"), \
TOKEN_KIND(Token_Shr, ">>"), \
\ \
/*TOKEN_KIND(Token_as, "as"), */\ /*TOKEN_KIND(Token_as, "as"), */\
/*TOKEN_KIND(Token_transmute, "transmute"), */\ /*TOKEN_KIND(Token_transmute, "transmute"), */\
@@ -83,44 +84,44 @@ TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
\ \
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
/* TODO(bill): Of these keywords are not used but "reserved", why not remove them? */ \ /* TODO(bill): Of these keywords are not used but "reserved", why not remove them? */ \
TOKEN_KIND(Token_when, "when"), \ TOKEN_KIND(Token_when, "when"), \
TOKEN_KIND(Token_if, "if"), \ TOKEN_KIND(Token_if, "if"), \
TOKEN_KIND(Token_else, "else"), \ TOKEN_KIND(Token_else, "else"), \
TOKEN_KIND(Token_for, "for"), \ TOKEN_KIND(Token_for, "for"), \
TOKEN_KIND(Token_in, "in"), \ TOKEN_KIND(Token_in, "in"), \
TOKEN_KIND(Token_break, "break"), \ TOKEN_KIND(Token_break, "break"), \
TOKEN_KIND(Token_continue, "continue"), \ TOKEN_KIND(Token_continue, "continue"), \
TOKEN_KIND(Token_fallthrough, "fallthrough"), \ TOKEN_KIND(Token_fallthrough, "fallthrough"), \
TOKEN_KIND(Token_match, "match"), \ TOKEN_KIND(Token_match, "match"), \
TOKEN_KIND(Token_type, "type"), \ /* TOKEN_KIND(Token_type, "type"), */ \
TOKEN_KIND(Token_default, "default"), \ TOKEN_KIND(Token_default, "default"), \
TOKEN_KIND(Token_case, "case"), \ TOKEN_KIND(Token_case, "case"), \
TOKEN_KIND(Token_defer, "defer"), \ TOKEN_KIND(Token_defer, "defer"), \
TOKEN_KIND(Token_return, "return"), \ TOKEN_KIND(Token_return, "return"), \
TOKEN_KIND(Token_give, "give"), \ TOKEN_KIND(Token_give, "give"), \
TOKEN_KIND(Token_proc, "proc"), \ TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_macro, "macro"), \ TOKEN_KIND(Token_macro, "macro"), \
TOKEN_KIND(Token_struct, "struct"), \ TOKEN_KIND(Token_struct, "struct"), \
TOKEN_KIND(Token_union, "union"), \ TOKEN_KIND(Token_union, "union"), \
TOKEN_KIND(Token_raw_union, "raw_union"), \ TOKEN_KIND(Token_raw_union, "raw_union"), \
TOKEN_KIND(Token_enum, "enum"), \ TOKEN_KIND(Token_enum, "enum"), \
TOKEN_KIND(Token_vector, "vector"), \ TOKEN_KIND(Token_vector, "vector"), \
TOKEN_KIND(Token_map, "map"), \ TOKEN_KIND(Token_map, "map"), \
TOKEN_KIND(Token_static, "static"), \ TOKEN_KIND(Token_static, "static"), \
TOKEN_KIND(Token_dynamic, "dynamic"), \ TOKEN_KIND(Token_dynamic, "dynamic"), \
TOKEN_KIND(Token_using, "using"), \ TOKEN_KIND(Token_using, "using"), \
TOKEN_KIND(Token_no_alias, "no_alias"), \ TOKEN_KIND(Token_no_alias, "no_alias"), \
/* TOKEN_KIND(Token_mutable, "mutable"), */\ /* TOKEN_KIND(Token_mutable, "mutable"), */ \
/* TOKEN_KIND(Token_immutable, "immutable"), */\ /* TOKEN_KIND(Token_immutable, "immutable"), */\
TOKEN_KIND(Token_thread_local, "thread_local"), \ TOKEN_KIND(Token_thread_local, "thread_local"), \
TOKEN_KIND(Token_cast, "cast"), \ TOKEN_KIND(Token_cast, "cast"), \
TOKEN_KIND(Token_transmute, "transmute"), \ TOKEN_KIND(Token_transmute, "transmute"), \
TOKEN_KIND(Token_down_cast, "down_cast"), \ TOKEN_KIND(Token_down_cast, "down_cast"), \
TOKEN_KIND(Token_union_cast, "union_cast"), \ TOKEN_KIND(Token_union_cast, "union_cast"), \
TOKEN_KIND(Token_context, "context"), \ TOKEN_KIND(Token_context, "context"), \
TOKEN_KIND(Token_push_context, "push_context"), \ TOKEN_KIND(Token_push_context, "push_context"), \
TOKEN_KIND(Token_push_allocator, "push_allocator"), \ TOKEN_KIND(Token_push_allocator, "push_allocator"), \
TOKEN_KIND(Token_asm, "asm"), \ TOKEN_KIND(Token_asm, "asm"), \
TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \ TOKEN_KIND(Token__KeywordEnd, "_KeywordEnd"), \
TOKEN_KIND(Token_Count, "") TOKEN_KIND(Token_Count, "")
@@ -480,7 +481,6 @@ gb_inline void scan_mantissa(Tokenizer *t, i32 base, bool allow_underscore) {
} }
} }
Token scan_number_to_token(Tokenizer *t, bool seen_decimal_point) { Token scan_number_to_token(Tokenizer *t, bool seen_decimal_point) {
Token token = {0}; Token token = {0};
token.kind = Token_Integer; token.kind = Token_Integer;
@@ -736,20 +736,10 @@ Token tokenizer_get_token(Tokenizer *t) {
// NOTE(bill): All keywords are > 1 // NOTE(bill): All keywords are > 1
if (token.string.len > 1) { if (token.string.len > 1) {
/* if (str_eq(token.string, token_strings[Token_as])) { for (i32 k = Token__KeywordBegin+1; k < Token__KeywordEnd; k++) {
token.kind = Token_as; if (str_eq(token.string, token_strings[k])) {
} else if (str_eq(token.string, token_strings[Token_transmute])) { token.kind = cast(TokenKind)k;
token.kind = Token_transmute; break;
} else if (str_eq(token.string, token_strings[Token_down_cast])) {
token.kind = Token_down_cast;
} else if (str_eq(token.string, token_strings[Token_union_cast])) {
token.kind = Token_union_cast;
} else */{
for (i32 k = Token__KeywordBegin+1; k < Token__KeywordEnd; k++) {
if (str_eq(token.string, token_strings[k])) {
token.kind = cast(TokenKind)k;
break;
}
} }
} }
} }
@@ -863,57 +853,28 @@ Token tokenizer_get_token(Tokenizer *t) {
} }
break; break;
case '#': case '#': token.kind = Token_Hash; break;
token.kind = Token_Hash; case '@': token.kind = Token_At; break;
break; case '$': token.kind = Token_Dollar; break;
case '@': case '?': token.kind = Token_Question; break;
token.kind = Token_At; case '^': token.kind = Token_Pointer; break;
break; case ';': token.kind = Token_Semicolon; break;
case '^': case ',': token.kind = Token_Comma; break;
token.kind = Token_Pointer; case ':': token.kind = Token_Colon; break;
break; case '(': token.kind = Token_OpenParen; break;
// case '?': case ')': token.kind = Token_CloseParen; break;
// token.kind = Token_Maybe; case '[': token.kind = Token_OpenBracket; break;
// break; case ']': token.kind = Token_CloseBracket; break;
case ';': case '{': token.kind = Token_OpenBrace; break;
token.kind = Token_Semicolon; case '}': token.kind = Token_CloseBrace; break;
break;
case ',':
token.kind = Token_Comma;
break;
case ':':
token.kind = Token_Colon;
break;
case '(':
token.kind = Token_OpenParen;
break;
case ')':
token.kind = Token_CloseParen;
break;
case '[':
token.kind = Token_OpenBracket;
break;
case ']':
token.kind = Token_CloseBracket;
break;
case '{':
token.kind = Token_OpenBrace;
break;
case '}':
token.kind = Token_CloseBrace;
break;
case '*': token.kind = token_kind_variant2(t, Token_Mul, Token_MulEq); break; case '*': token.kind = token_kind_variant2(t, Token_Mul, Token_MulEq); break;
case '%': token.kind = token_kind_variant2(t, Token_Mod, Token_ModEq); break; case '%': token.kind = token_kind_variant2(t, Token_Mod, Token_ModEq); break;
case '=': token.kind = token_kind_variant2(t, Token_Eq, Token_CmpEq); break; case '=': token.kind = token_kind_variant2(t, Token_Eq, Token_CmpEq); break;
case '~': token.kind = token_kind_variant2(t, Token_Xor, Token_XorEq); break; case '~': token.kind = token_kind_variant2(t, Token_Xor, Token_XorEq); break;
case '!': token.kind = token_kind_variant2(t, Token_Not, Token_NotEq); break; case '!': token.kind = token_kind_variant2(t, Token_Not, Token_NotEq); break;
case '+': case '+': token.kind = token_kind_variant3(t, Token_Add, Token_AddEq, '+', Token_Increment); break;
token.kind = token_kind_variant3(t, Token_Add, Token_AddEq, '+', Token_Increment); case '-': token.kind = token_kind_variant4(t, Token_Sub, Token_SubEq, '-', Token_Decrement, '>', Token_ArrowRight); break;
break;
case '-':
token.kind = token_kind_variant4(t, Token_Sub, Token_SubEq, '-', Token_Decrement, '>', Token_ArrowRight);
break;
case '/': { case '/': {
if (t->curr_rune == '/') { if (t->curr_rune == '/') {
while (t->curr_rune != '\n' && t->curr_rune != GB_RUNE_EOF) { while (t->curr_rune != '\n' && t->curr_rune != GB_RUNE_EOF) {
@@ -953,9 +914,7 @@ Token tokenizer_get_token(Tokenizer *t) {
token.kind = token_kind_dub_eq(t, '<', Token_Lt, Token_LtEq, Token_Shl, Token_ShlEq); token.kind = token_kind_dub_eq(t, '<', Token_Lt, Token_LtEq, Token_Shl, Token_ShlEq);
} }
break; break;
case '>': case '>': token.kind = token_kind_dub_eq(t, '>', Token_Gt, Token_GtEq, Token_Shr, Token_ShrEq); break;
token.kind = token_kind_dub_eq(t, '>', Token_Gt, Token_GtEq, Token_Shr, Token_ShrEq);
break;
case '&': case '&':
token.kind = Token_And; token.kind = Token_And;