Skip to content

Commit

Permalink
Parse container expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
N00byEdge committed Oct 8, 2023
1 parent 718ce3a commit d88fca4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
16 changes: 14 additions & 2 deletions selfhost/parser.n
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ fn init() inline void {
}

fn add_with_token(tok: u32, tag: NodeType) u32 {
const retval = node_type.size();
node_type.append_assume_capacity(tag);
const retval = node_type.add();
node_type.get(retval).* = tag;
node_token.append_assume_capacity(tok);
node_payload.add();
node_next.add();
Expand Down Expand Up @@ -286,6 +286,18 @@ fn parse_primary_expression(context: *ParserContext, require: bool, precedence:
else if(p == .unreachable_keyword) {
return context.add_advance(.@"unreachable");
}
else if(p == .struct_keyword || p == .union_keyword || p == .enum_keyword) {
const retval = context.add_advance(.container_expression);
context.expect("Expected '{' before container body".&, .@"{");
var is_enum = false;
// TODO: Make == a proper value so that we don't have to do this shit
if(p == .enum_keyword) {
is_enum = true;
}
node_payload.get(retval).* = parse_container_body(context, is_enum);
context.expect("Expected '}' before container body".&, .@"}");
return retval;
}
else if(p == .if_keyword) {
const token = context.advance();
context.expect("Expected '(' after 'if'".&, .@"(");
Expand Down
21 changes: 18 additions & 3 deletions selfhost/tokenizer.n
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const TokenType = enum(u8) {
continue_keyword,
else_keyword,
//endcase_keyword,
//enum_keyword,
enum_keyword,
//false_keyword,
fn_keyword,
if_keyword,
inline_keyword,
loop_keyword,
return_keyword,
//struct_keyword,
struct_keyword,
//switch_keyword,
//true_keyword,
undefined_keyword,
//union_keyword,
union_keyword,
unreachable_keyword,
var_keyword,
//volatile_keyword,
Expand Down Expand Up @@ -276,6 +276,9 @@ fn init() void {
if(context.match_kw("else".&)) {
context.add_token_advance(.else_keyword, 4);
}
if(context.match_kw("enum".&)) {
context.add_token_advance(.enum_keyword, 4);
}
else {
ident_handler(context);
}
Expand Down Expand Up @@ -341,6 +344,15 @@ fn init() void {
}
}.&;

token_handlers['s'] = fn(context: *TokenizationContext) void {
if(context.match_kw("struct".&)) {
context.add_token_advance(.struct_keyword, 6);
}
else {
ident_handler(context);
}
}.&;

token_handlers['t'] = fn(context: *TokenizationContext) void {
if(context.match_kw("type".&)) {
context.add_token_advance(.type_keyword, 4);
Expand Down Expand Up @@ -369,6 +381,9 @@ fn init() void {
else if(context.match_kw("u64".&)) {
context.add_token_advance(.@"u64", 3);
}
else if(context.match_kw("union".&)) {
context.add_token_advance(.union_keyword, 5);
}
else {
ident_handler(context);
}
Expand Down

0 comments on commit d88fca4

Please sign in to comment.