From 622fdef4f48a02402e115d7cf90f0b2403e70b9c Mon Sep 17 00:00:00 2001 From: Hannes Bredberg Date: Thu, 5 Oct 2023 00:10:26 +0200 Subject: [PATCH] Selfhost: Parse block expressions with and without identifiers --- selfhost/parser.n | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/selfhost/parser.n b/selfhost/parser.n index fdcd75f..de9ef20 100644 --- a/selfhost/parser.n +++ b/selfhost/parser.n @@ -155,11 +155,6 @@ fn parse_statement(context: *ParserContext) u32 { context.expect("Expected ';' after expression statement".&, .@";"); return result; } - // if(p == .@"{") { - // const result = add_with_token(context.current_token, .block); - // stmt_payload[0].get(result).* = parse_block(context); - // return result; - // } // else if(p == .break_keyword) { // context.report_error("TODO: Break statement".&); // } @@ -254,6 +249,12 @@ fn parse_primary_expression(context: *ParserContext) u32 { const p = context.peek(); if(p == .identifier) { + if(tokenizer.token_type.get(context.current_token + 1).* == .@":") { + context.current_token += 2; + const result = add_with_token(context.current_token, .block); + node_payload.get(result).* = parse_block(context); + return result; + } return context.add_advance(.identifier); } else if(p == .int_literal) { @@ -266,6 +267,11 @@ fn parse_primary_expression(context: *ParserContext) u32 { context.advance(); return parse_function_expression(context); } + else if(p == .@"{") { + const result = add_with_token(context.current_token, .block); + node_payload.get(result).* = parse_block(context); + return result; + } else if(p == .@"+") { const plus_tok = context.advance(); _ = parse_primary_expression(context);