while; range is now for; remove ++ and --

This commit is contained in:
Ginger Bill
2017-01-03 19:11:12 +00:00
parent a86896e4d3
commit 70d4ca00df
20 changed files with 280 additions and 436 deletions
+2 -2
View File
@@ -132,7 +132,7 @@ typedef enum BuiltinProcId {
BuiltinProc_panic,
BuiltinProc_copy,
BuiltinProc_append,
// BuiltinProc_append,
BuiltinProc_swizzle,
@@ -175,7 +175,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT("panic"), 1, false, Expr_Stmt},
{STR_LIT("copy"), 2, false, Expr_Expr},
{STR_LIT("append"), 2, false, Expr_Expr},
// {STR_LIT("append"), 2, false, Expr_Expr},
{STR_LIT("swizzle"), 1, true, Expr_Expr},
+1
View File
@@ -57,6 +57,7 @@ struct Entity {
struct {
i32 field_index;
i32 field_src_index;
bool is_immutable;
} Variable;
i32 TypeName;
struct {
+7 -12
View File
@@ -965,9 +965,9 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
}
#else
o->mode = Addressing_Variable;
// if (e->Variable.is_let) {
// o->mode = Addressing_Value;
// }
if (e->Variable.is_immutable) {
o->mode = Addressing_Value;
}
#endif
break;
@@ -2967,6 +2967,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
operand->mode = Addressing_Value;
} break;
#if 0
case BuiltinProc_append: {
// append :: proc(x : ^[]Type, y : Type) -> bool
Type *x_type = NULL, *y_type = NULL;
@@ -3003,6 +3004,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
operand->type = t_bool; // Returns if it was successful
operand->mode = Addressing_Value;
} break;
#endif
case BuiltinProc_swizzle: {
// swizzle :: proc(v: {N}T, T...) -> {M}T
@@ -4342,9 +4344,6 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
if (o->mode == Addressing_Constant) {
max_count = o->value.value_string.len;
}
if (se->max != NULL) {
error_node(se->max, "Max (3rd) index not needed in substring expression");
}
o->type = t_string;
}
break;
@@ -4375,8 +4374,8 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
o->mode = Addressing_Value;
i64 indices[3] = {0};
AstNode *nodes[3] = {se->low, se->high, se->max};
i64 indices[2] = {0};
AstNode *nodes[2] = {se->low, se->high};
for (isize i = 0; i < gb_count_of(nodes); i++) {
i64 index = max_count;
if (nodes[i] != NULL) {
@@ -4680,10 +4679,6 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
str = write_expr_to_string(str, se->low);
str = gb_string_appendc(str, ":");
str = write_expr_to_string(str, se->high);
if (se->triple_indexed) {
str = gb_string_appendc(str, ":");
str = write_expr_to_string(str, se->max);
}
str = gb_string_appendc(str, "]");
case_end;
+17 -61
View File
@@ -127,13 +127,13 @@ bool check_is_terminating(AstNode *node) {
}
case_end;
case_ast_node(fs, ForStmt, node);
if (fs->cond == NULL && !check_has_break(fs->body, true)) {
case_ast_node(ws, WhileStmt, node);
if (ws->cond == NULL && !check_has_break(ws->body, true)) {
return true;
}
case_end;
case_ast_node(rs, RangeStmt, node);
case_ast_node(rs, ForStmt, node);
if (!check_has_break(rs->body, true)) {
return true;
}
@@ -375,48 +375,6 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
check_stmt(c, ts->stmt, flags);
case_end;
case_ast_node(ids, IncDecStmt, node);
Token op = ids->op;
switch (ids->op.kind) {
case Token_Increment:
op.kind = Token_Add;
op.string.len = 1;
break;
case Token_Decrement:
op.kind = Token_Sub;
op.string.len = 1;
break;
default:
error(ids->op, "Unknown inc/dec operation %.*s", LIT(ids->op.string));
return;
}
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, ids->expr);
if (operand.mode == Addressing_Invalid) {
return;
}
if (!is_type_numeric(operand.type) && !is_type_pointer(operand.type)) {
gbString type_str = type_to_string(operand.type);
error(ids->op, "Non numeric type `%s`", type_str);
gb_string_free(type_str);
return;
}
AstNode basic_lit = {AstNode_BasicLit};
ast_node(bl, BasicLit, &basic_lit);
*bl = ids->op;
bl->kind = Token_Integer;
bl->string = str_lit("1");
AstNode binary_expr = {AstNode_BinaryExpr};
ast_node(be, BinaryExpr, &binary_expr);
be->op = op;
be->left = ids->expr;
be->right = &basic_lit;
check_binary_expr(c, &operand, &binary_expr);
case_end;
case_ast_node(as, AssignStmt, node);
switch (as->op.kind) {
case Token_Eq: {
@@ -567,30 +525,27 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
}
case_end;
case_ast_node(fs, ForStmt, node);
case_ast_node(ws, WhileStmt, node);
u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
check_open_scope(c, node);
if (fs->init != NULL) {
check_stmt(c, fs->init, 0);
if (ws->init != NULL) {
check_stmt(c, ws->init, 0);
}
if (fs->cond) {
if (ws->cond) {
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, fs->cond);
check_expr(c, &operand, ws->cond);
if (operand.mode != Addressing_Invalid &&
!is_type_boolean(operand.type)) {
error_node(fs->cond, "Non-boolean condition in `for` statement");
error_node(ws->cond, "Non-boolean condition in `while` statement");
}
}
if (fs->post != NULL) {
check_stmt(c, fs->post, 0);
}
check_stmt(c, fs->body, new_flags);
check_stmt(c, ws->body, new_flags);
check_close_scope(c);
case_end;
case_ast_node(rs, RangeStmt, node);
case_ast_node(rs, ForStmt, node);
u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
check_open_scope(c, node);
@@ -646,8 +601,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
goto skip_expr;
}
if (!is_type_integer(x.type) && !is_type_float(x.type)) {
error(ie->op, "Only numerical types are allowed within interval expressions");
if (!is_type_integer(x.type) && !is_type_float(x.type) && !is_type_pointer(x.type)) {
error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
goto skip_expr;
}
@@ -661,7 +616,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
bool ok = compare_exact_values(Token_Lt, a, b);
if (!ok) {
// TODO(bill): Better error message
error(ie->op, "Invalid interval expression");
error(ie->op, "Invalid interval range");
goto skip_expr;
}
}
@@ -723,6 +678,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, type);
entity->Variable.is_immutable = true;
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;
@@ -1051,12 +1007,12 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
switch (token.kind) {
case Token_break:
if ((flags & Stmt_BreakAllowed) == 0) {
error(token, "`break` only allowed in `for` or `match` statements");
error(token, "`break` only allowed in loops or `match` statements");
}
break;
case Token_continue:
if ((flags & Stmt_ContinueAllowed) == 0) {
error(token, "`continue` only allowed in `for` statements");
error(token, "`continue` only allowed in loops");
}
break;
case Token_fallthrough:
+4 -11
View File
@@ -116,6 +116,8 @@ typedef struct TypeRecord {
ProcCallingConvention calling_convention; \
})
typedef enum TypeKind {
Type_Invalid,
#define TYPE_KIND(k, ...) GB_JOIN2(Type_, k),
@@ -987,7 +989,6 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
} else if (type->kind == Type_Slice) {
String data_str = str_lit("data");
String count_str = str_lit("count");
String capacity_str = str_lit("capacity");
if (str_eq(field_name, data_str)) {
selection_add_index(&sel, 0);
@@ -1002,14 +1003,6 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
sel.entity = entity__slice_count;
return sel;
} else if (str_eq(field_name, capacity_str)) {
selection_add_index(&sel, 2);
if (entity__slice_capacity == NULL) {
entity__slice_capacity = make_entity_field(a, NULL, make_token_ident(capacity_str), t_int, false, 2);
}
sel.entity = entity__slice_capacity;
return sel;
}
}
@@ -1393,8 +1386,8 @@ i64 type_size_of_internal(BaseTypeSizes s, gbAllocator allocator, Type *t, TypeP
} break;
case Type_Slice: // ptr + len + cap
return 3 * s.word_size;
case Type_Slice: // ptr + count
return 2 * s.word_size;
case Type_Maybe: { // value + bool
i64 align, size;