mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-30 19:30:06 +00:00
Reduce number of range and slice operators #239
Replace .. and ... with : and ..
This commit is contained in:
+1
-3
@@ -6110,8 +6110,6 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
|
||||
// It is okay to continue as it will assume the 1st index is zero
|
||||
}
|
||||
|
||||
TokenKind interval_kind = se->interval.kind;
|
||||
|
||||
i64 indices[2] = {};
|
||||
Ast *nodes[2] = {se->low, se->high};
|
||||
for (isize i = 0; i < gb_count_of(nodes); i++) {
|
||||
@@ -6122,7 +6120,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
|
||||
capacity = max_count;
|
||||
}
|
||||
i64 j = 0;
|
||||
if (check_index_value(c, interval_kind == Token_Ellipsis, nodes[i], capacity, &j)) {
|
||||
if (check_index_value(c, false, nodes[i], capacity, &j)) {
|
||||
index = j;
|
||||
}
|
||||
} else if (i == 0) {
|
||||
|
||||
+3
-6
@@ -715,8 +715,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
|
||||
continue;
|
||||
}
|
||||
switch (ie->op.kind) {
|
||||
case Token_Ellipsis: op = Token_GtEq; break;
|
||||
case Token_HalfClosed: op = Token_Gt; break;
|
||||
case Token_Ellipsis: op = Token_GtEq; break;
|
||||
default: error(ie->op, "Invalid interval operator"); continue;
|
||||
}
|
||||
|
||||
@@ -726,8 +725,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
|
||||
}
|
||||
|
||||
switch (ie->op.kind) {
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
case Token_HalfClosed: op = Token_Lt; break;
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
default: error(ie->op, "Invalid interval operator"); continue;
|
||||
}
|
||||
|
||||
@@ -1362,8 +1360,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
|
||||
|
||||
TokenKind op = Token_Lt;
|
||||
switch (ie->op.kind) {
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
case Token_HalfClosed: op = Token_Lt; break;
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
default: error(ie->op, "Invalid range operator"); break;
|
||||
}
|
||||
bool ok = compare_exact_values(op, a, b);
|
||||
|
||||
+1
-1
@@ -1548,7 +1548,7 @@ i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
|
||||
return 0;
|
||||
}
|
||||
if (e->kind == Ast_UnaryExpr &&
|
||||
e->UnaryExpr.op.kind == Token_Ellipsis) {
|
||||
e->UnaryExpr.op.kind == Token_Question) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
+2
-8
@@ -5820,10 +5820,6 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
|
||||
if (se->low != nullptr) low = ir_build_expr(proc, se->low);
|
||||
if (se->high != nullptr) high = ir_build_expr(proc, se->high);
|
||||
|
||||
if (high != nullptr && se->interval.kind == Token_Ellipsis) {
|
||||
high = ir_emit_arith(proc, Token_Add, high, v_one, t_int);
|
||||
}
|
||||
|
||||
irValue *addr = ir_build_addr_ptr(proc, se->expr);
|
||||
irValue *base = ir_emit_load(proc, addr);
|
||||
Type *type = base_type(ir_type(base));
|
||||
@@ -6625,8 +6621,7 @@ void ir_build_range_interval(irProcedure *proc, AstBinaryExpr *node, Type *val_t
|
||||
|
||||
TokenKind op = Token_Lt;
|
||||
switch (node->op.kind) {
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
case Token_HalfClosed: op = Token_Lt; break;
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
default: GB_PANIC("Invalid interval operator"); break;
|
||||
}
|
||||
|
||||
@@ -7224,8 +7219,7 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
|
||||
ast_node(ie, BinaryExpr, expr);
|
||||
TokenKind op = Token_Invalid;
|
||||
switch (ie->op.kind) {
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
case Token_HalfClosed: op = Token_Lt; break;
|
||||
case Token_Ellipsis: op = Token_LtEq; break;
|
||||
default: GB_PANIC("Invalid interval operator"); break;
|
||||
}
|
||||
irValue *lhs = ir_build_expr(proc, ie->left);
|
||||
|
||||
+6
-12
@@ -1142,7 +1142,7 @@ Token expect_operator(AstFile *f) {
|
||||
if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
|
||||
syntax_error(f->curr_token, "Expected an operator, got '%.*s'",
|
||||
LIT(token_strings[prev.kind]));
|
||||
} else if (!f->allow_range && (prev.kind == Token_Ellipsis || prev.kind == Token_HalfClosed)) {
|
||||
} else if (!f->allow_range && (prev.kind == Token_Ellipsis)) {
|
||||
syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'",
|
||||
LIT(token_strings[prev.kind]));
|
||||
}
|
||||
@@ -2041,7 +2041,7 @@ Ast *parse_call_expr(AstFile *f, Ast *operand) {
|
||||
Token eq = expect_token(f, Token_Eq);
|
||||
|
||||
if (prefix_ellipsis) {
|
||||
syntax_error(ellipsis, "'...' must be applied to value rather than the field name");
|
||||
syntax_error(ellipsis, "'..' must be applied to value rather than the field name");
|
||||
}
|
||||
|
||||
Ast *value = parse_value(f);
|
||||
@@ -2118,18 +2118,14 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
|
||||
f->expr_level++;
|
||||
open = expect_token(f, Token_OpenBracket);
|
||||
|
||||
if (f->curr_token.kind != Token_Ellipsis &&
|
||||
f->curr_token.kind != Token_HalfClosed) {
|
||||
if (f->curr_token.kind != Token_Colon) {
|
||||
indices[0] = parse_expr(f, false);
|
||||
}
|
||||
|
||||
if ((f->curr_token.kind == Token_Ellipsis ||
|
||||
f->curr_token.kind == Token_HalfClosed)) {
|
||||
if (f->curr_token.kind == Token_Colon) {
|
||||
ellipsis = advance_token(f);
|
||||
is_ellipsis = true;
|
||||
if (f->curr_token.kind != Token_Ellipsis &&
|
||||
f->curr_token.kind != Token_HalfClosed &&
|
||||
f->curr_token.kind != Token_CloseBracket &&
|
||||
if (f->curr_token.kind != Token_CloseBracket &&
|
||||
f->curr_token.kind != Token_EOF) {
|
||||
indices[1] = parse_expr(f, false);
|
||||
}
|
||||
@@ -2214,7 +2210,6 @@ bool is_ast_range(Ast *expr) {
|
||||
TokenKind op = expr->BinaryExpr.op.kind;
|
||||
switch (op) {
|
||||
case Token_Ellipsis:
|
||||
case Token_HalfClosed:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -2226,7 +2221,6 @@ i32 token_precedence(AstFile *f, TokenKind t) {
|
||||
case Token_Question:
|
||||
return 1;
|
||||
case Token_Ellipsis:
|
||||
case Token_HalfClosed:
|
||||
if (!f->allow_range) {
|
||||
return 0;
|
||||
}
|
||||
@@ -2690,7 +2684,7 @@ Ast *parse_var_type(AstFile *f, bool allow_ellipsis, bool allow_type_token) {
|
||||
Token tok = advance_token(f);
|
||||
Ast *type = parse_type_or_ident(f);
|
||||
if (type == nullptr) {
|
||||
syntax_error(tok, "variadic field missing type after '...'");
|
||||
syntax_error(tok, "variadic field missing type after '..'");
|
||||
type = ast_bad_expr(f, tok, f->curr_token);
|
||||
}
|
||||
return ast_ellipsis(f, tok, type);
|
||||
|
||||
+11
-7
@@ -76,8 +76,7 @@ TOKEN_KIND(Token__ComparisonEnd, ""), \
|
||||
TOKEN_KIND(Token_Semicolon, ";"), \
|
||||
TOKEN_KIND(Token_Period, "."), \
|
||||
TOKEN_KIND(Token_Comma, ","), \
|
||||
TOKEN_KIND(Token_Ellipsis, "..."), \
|
||||
TOKEN_KIND(Token_HalfClosed, ".."), \
|
||||
TOKEN_KIND(Token_Ellipsis, ".."), \
|
||||
TOKEN_KIND(Token_BackSlash, "\\"), \
|
||||
TOKEN_KIND(Token__OperatorEnd, ""), \
|
||||
\
|
||||
@@ -226,6 +225,9 @@ void error_va(Token token, char *fmt, va_list va) {
|
||||
gb_bprintf_va(fmt, va));
|
||||
}
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
if (global_error_collector.count > 20) {
|
||||
gb_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void error_no_newline_va(Token token, char *fmt, va_list va) {
|
||||
@@ -241,6 +243,9 @@ void error_no_newline_va(Token token, char *fmt, va_list va) {
|
||||
gb_bprintf_va(fmt, va));
|
||||
}
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
if (global_error_collector.count > 20) {
|
||||
gb_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -258,6 +263,9 @@ void syntax_error_va(Token token, char *fmt, va_list va) {
|
||||
}
|
||||
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
if (global_error_collector.count > 20) {
|
||||
gb_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void syntax_warning_va(Token token, char *fmt, va_list va) {
|
||||
@@ -936,11 +944,7 @@ Token tokenizer_get_token(Tokenizer *t) {
|
||||
case '.':
|
||||
if (t->curr_rune == '.') { // Could be an ellipsis
|
||||
advance_to_next_rune(t);
|
||||
token.kind = Token_HalfClosed;
|
||||
if (t->curr_rune == '.') {
|
||||
advance_to_next_rune(t);
|
||||
token.kind = Token_Ellipsis;
|
||||
}
|
||||
token.kind = Token_Ellipsis;
|
||||
} else if ('0' <= t->curr_rune && t->curr_rune <= '9') {
|
||||
token = scan_number_to_token(t, true);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user