Browse Source

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

gingerBill 4 năm trước cách đây
mục cha
commit
5d03bc61b8
2 tập tin đã thay đổi với 36 bổ sung11 xóa
  1. 25 8
      src/parser.cpp
  2. 11 3
      src/tokenizer.cpp

+ 25 - 8
src/parser.cpp

@@ -2724,6 +2724,16 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
 			}
 			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:
 			loop = false;
 			break;
@@ -2754,6 +2764,10 @@ Ast *parse_unary_expr(AstFile *f, bool lhs) {
 		return ast_auto_cast(f, token, expr);
 	}
 
+
+	case Token_Add:
+	case Token_Sub:
+	case Token_Xor:
 	case Token_And:
 	case Token_Not: {
 		Token token = advance_token(f);
@@ -2761,19 +2775,15 @@ Ast *parse_unary_expr(AstFile *f, bool lhs) {
 		return ast_unary_expr(f, token, expr);
 	}
 
-	case Token_Add:
-	case Token_Sub:
-	case Token_Xor: {
+	case Token_Increment:
+	case Token_Decrement: {
 		Token token = advance_token(f);
+		syntax_error(token, "Unary '%.*s' operator is not supported", LIT(token.string));
 		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);
 	}
 
+
 	case Token_Period: {
 		Token token = expect_token(f, Token_Period);
 		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);
 	}
 
+	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

+ 11 - 3
src/tokenizer.cpp

@@ -51,8 +51,10 @@ TOKEN_KIND(Token__AssignOpBegin, ""), \
 	TOKEN_KIND(Token_CmpAndEq, "&&="), \
 	TOKEN_KIND(Token_CmpOrEq,  "||="), \
 TOKEN_KIND(Token__AssignOpEnd, ""), \
-	TOKEN_KIND(Token_ArrowRight,       "->"), \
-	TOKEN_KIND(Token_Undef,            "---"), \
+	TOKEN_KIND(Token_Increment, "++"), \
+	TOKEN_KIND(Token_Decrement, "--"), \
+	TOKEN_KIND(Token_ArrowRight,"->"), \
+	TOKEN_KIND(Token_Undef,     "---"), \
 \
 TOKEN_KIND(Token__ComparisonBegin, ""), \
 	TOKEN_KIND(Token_CmpEq, "=="), \
@@ -1287,6 +1289,9 @@ void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) {
 			if (t->curr_rune == '=') {
 				advance_to_next_rune(t);
 				token->kind = Token_AddEq;
+			} else if (t->curr_rune == '+') {
+				advance_to_next_rune(t);
+				token->kind = Token_Increment;
 			}
 			break;
 		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);
 				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);
 				token->kind = Token_ArrowRight;
 			}