New syntax that's easier to parse, parsing doesn't need variable lookup
This commit is contained in:
55
parse_expr.c
55
parse_expr.c
@@ -1,5 +1,6 @@
|
||||
function Expr* parse_expr(Parser* p);
|
||||
function Expr* parse_list_expr(Parser* p);
|
||||
function Typespec *parse_type(Parser *p);
|
||||
|
||||
function Expr*
|
||||
parse_atom_expr(Parser* p){
|
||||
@@ -10,6 +11,21 @@ parse_atom_expr(Parser* p){
|
||||
token_is(p, TK_Int)){
|
||||
result = expr_atom(p, token_next(p));
|
||||
}
|
||||
|
||||
else if (token_is_keyword(p, keyword_sizeof)) {
|
||||
Token *token = token_next(p);
|
||||
token_expect(p, TK_OpenParen);
|
||||
Typespec *type = 0;
|
||||
Expr *expr = 0;
|
||||
if(token_match(p, TK_Colon)) {
|
||||
result = expr_sizeof_type(p, token, parse_type(p));
|
||||
}
|
||||
else {
|
||||
result = expr_sizeof_expr(p, token, parse_expr(p));
|
||||
}
|
||||
token_expect(p, TK_CloseParen);
|
||||
}
|
||||
|
||||
else if (token_match(p, TK_OpenParen)){
|
||||
result = parse_list_expr(p);
|
||||
token_expect(p, TK_CloseParen);
|
||||
@@ -24,17 +40,13 @@ function Expr*
|
||||
parse_postfix_expr(Parser* p){
|
||||
Expr* result = parse_atom_expr(p);
|
||||
while (token_is(p, TK_Dot)
|
||||
|| token_is(p, TK_Arrow)
|
||||
|| token_is(p, TK_DoubleColon)
|
||||
|| token_is(p, TK_OpenParen)
|
||||
|| token_is(p, TK_OpenBracket)
|
||||
|| token_is(p, TK_Decrement)
|
||||
|| token_is(p, TK_Increment)){
|
||||
Token *op = token_get(p);
|
||||
|
||||
if (token_match(p, TK_Arrow)
|
||||
|| token_match(p, TK_DoubleColon)
|
||||
|| token_match(p, TK_Dot)){
|
||||
if (token_match(p, TK_Dot)){
|
||||
Expr* r = parse_atom_expr(p);
|
||||
result = expr_binary(p, op, result, r);
|
||||
}
|
||||
@@ -85,35 +97,16 @@ Expr* parse_unary_expr(Parser* p) {
|
||||
result = parse_unary_expr(p);
|
||||
result = expr_unary(p, op, result);
|
||||
}
|
||||
else if (token_is_keyword(p, keyword_sizeof)) {
|
||||
|
||||
else if (token_is_keyword(p, keyword_cast)) {
|
||||
Token *token = token_next(p);
|
||||
token_expect(p, TK_OpenParen);
|
||||
Typespec *type = parse_type(p);
|
||||
token_expect(p, TK_CloseParen);
|
||||
result = parse_unary_expr(p);
|
||||
result = expr_unary(p, token, result);
|
||||
result = expr_cast(p, token, type, result);
|
||||
}
|
||||
/*
|
||||
else if (token_is(p, TK_OpenParen)) { // cast requires lookahead
|
||||
Token *token = token_peek(p, 1);
|
||||
if (token->kind == TK_Identifier) {
|
||||
|
||||
AST_Node *type = symbol_lookup_type(p, token->intern_val);
|
||||
if(type){
|
||||
token_next(p);
|
||||
token_next(p);
|
||||
// @Todo(Krzosa): Parse pointer types
|
||||
token_expect(p, TK_CloseParen);
|
||||
result = parse_unary_expr(p);
|
||||
result = expr_cast(p, token, type, result);
|
||||
}
|
||||
else {
|
||||
result = parse_postfix_expr(p);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
result = parse_postfix_expr(p);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
else {
|
||||
result = parse_postfix_expr(p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user