Skip to content

Commit

Permalink
Parse while expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
N00byEdge committed Oct 4, 2023
1 parent 5b8ee5f commit 9de1d17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions selfhost/parser.n
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const NodeType = enum(u8) {
// payload: loop body
@"loop",

// idx - 1: condition
// payload: while body
@"while",

// token is the keyword
// payload is optional value
return_expr,
Expand Down Expand Up @@ -297,6 +301,15 @@ fn parse_primary_expression(context: *ParserContext, require: bool, precedence:
node_payload.get(result).* = parse_block(context);
return result;
}
else if(p == .while_keyword) {
const token = context.advance();
context.expect("Expected '(' after 'while'".&, .@"(");
parse_expression(context);
const result = add_with_token(token, .@"while");
context.expect("Expected ')' after while condition".&, .@")");
node_payload.get(result).* = parse_expression_with_precedence(context, true, precedence);
return result;
}
else if(p == .@"{") {
const result = add_with_token(context.current_token, .block);
node_payload.get(result).* = parse_block(context);
Expand Down
10 changes: 10 additions & 0 deletions selfhost/tokenizer.n
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const TokenType = enum(u8) {
unreachable_keyword,
var_keyword,
//volatile_keyword,
while_keyword,
__keyword,

//bool_keyword,
Expand Down Expand Up @@ -312,6 +313,15 @@ fn init() void {
}
}.&;

token_handlers['w'] = fn(context: *TokenizationContext) void {
if(context.matches("while".&)) {
context.add_token_advance(.while_keyword, 5);
}
else {
ident_handler(context);
}
}.&;

{
var ch: u32 = 0;
loop(ch <= 0xFF) {
Expand Down

0 comments on commit 9de1d17

Please sign in to comment.