Tokenize ++ and -- as tokens but disallow them in the parser, and give better error messages for they are used as operators/statements

This commit is contained in:
gingerBill
2021-05-15 19:34:46 +01:00
parent 5e31c04a01
commit 5d03bc61b8
2 changed files with 36 additions and 11 deletions
+25 -8
View File
@@ -2724,6 +2724,16 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
} }
break; break;
case Token_Increment:
case Token_Decrement:
if (!lhs) {
Token token = advance_token(f);
syntax_error(token, "Postfix '%.*s' operator is not supported", LIT(token.string));
} else {
loop = false;
}
break;
default: default:
loop = false; loop = false;
break; break;
@@ -2754,6 +2764,10 @@ Ast *parse_unary_expr(AstFile *f, bool lhs) {
return ast_auto_cast(f, token, expr); return ast_auto_cast(f, token, expr);
} }
case Token_Add:
case Token_Sub:
case Token_Xor:
case Token_And: case Token_And:
case Token_Not: { case Token_Not: {
Token token = advance_token(f); Token token = advance_token(f);
@@ -2761,19 +2775,15 @@ Ast *parse_unary_expr(AstFile *f, bool lhs) {
return ast_unary_expr(f, token, expr); return ast_unary_expr(f, token, expr);
} }
case Token_Add: case Token_Increment:
case Token_Sub: case Token_Decrement: {
case Token_Xor: {
Token token = advance_token(f); Token token = advance_token(f);
syntax_error(token, "Unary '%.*s' operator is not supported", LIT(token.string));
Ast *expr = parse_unary_expr(f, lhs); Ast *expr = parse_unary_expr(f, lhs);
if (expr != nullptr && expr->kind == Ast_UnaryExpr) {
if (expr->UnaryExpr.op.kind == token.kind) {
syntax_error(expr, "Duplicate unary operator '%.*s' will produce a redundant no-op", LIT(token.string));
}
}
return ast_unary_expr(f, token, expr); return ast_unary_expr(f, token, expr);
} }
case Token_Period: { case Token_Period: {
Token token = expect_token(f, Token_Period); Token token = expect_token(f, Token_Period);
Ast *ident = parse_ident(f); Ast *ident = parse_ident(f);
@@ -3163,6 +3173,13 @@ Ast *parse_simple_stmt(AstFile *f, u32 flags) {
return ast_bad_stmt(f, token, f->curr_token); return ast_bad_stmt(f, token, f->curr_token);
} }
switch (token.kind) {
case Token_Increment:
case Token_Decrement:
advance_token(f);
syntax_error(token, "Postfix '%.*s' statement is not supported", LIT(token.string));
break;
}
#if 0 #if 0
+11 -3
View File
@@ -51,8 +51,10 @@ TOKEN_KIND(Token__AssignOpBegin, ""), \
TOKEN_KIND(Token_CmpAndEq, "&&="), \ TOKEN_KIND(Token_CmpAndEq, "&&="), \
TOKEN_KIND(Token_CmpOrEq, "||="), \ TOKEN_KIND(Token_CmpOrEq, "||="), \
TOKEN_KIND(Token__AssignOpEnd, ""), \ TOKEN_KIND(Token__AssignOpEnd, ""), \
TOKEN_KIND(Token_ArrowRight, "->"), \ TOKEN_KIND(Token_Increment, "++"), \
TOKEN_KIND(Token_Undef, "---"), \ TOKEN_KIND(Token_Decrement, "--"), \
TOKEN_KIND(Token_ArrowRight,"->"), \
TOKEN_KIND(Token_Undef, "---"), \
\ \
TOKEN_KIND(Token__ComparisonBegin, ""), \ TOKEN_KIND(Token__ComparisonBegin, ""), \
TOKEN_KIND(Token_CmpEq, "=="), \ TOKEN_KIND(Token_CmpEq, "=="), \
@@ -1287,6 +1289,9 @@ void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) {
if (t->curr_rune == '=') { if (t->curr_rune == '=') {
advance_to_next_rune(t); advance_to_next_rune(t);
token->kind = Token_AddEq; token->kind = Token_AddEq;
} else if (t->curr_rune == '+') {
advance_to_next_rune(t);
token->kind = Token_Increment;
} }
break; break;
case '-': case '-':
@@ -1298,7 +1303,10 @@ void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) {
advance_to_next_rune(t); advance_to_next_rune(t);
advance_to_next_rune(t); advance_to_next_rune(t);
token->kind = Token_Undef; token->kind = Token_Undef;
} else if (t->curr_rune == '>') { } else if (t->curr_rune == '-') {
advance_to_next_rune(t);
token->kind = Token_Decrement;
}else if (t->curr_rune == '>') {
advance_to_next_rune(t); advance_to_next_rune(t);
token->kind = Token_ArrowRight; token->kind = Token_ArrowRight;
} }