From c24d807e24b4ea7dafe5872db618163fc683cdca Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 15 Mar 2020 15:05:39 -0400 Subject: removed where --- copy.c | 4 ---- parse.c | 16 ++-------------- tests/printf.toc | 37 +++++++++++++++++++++++++++++++++++++ tests/printf_expected | 3 +++ tests/test.sh | 4 ++-- tests/where.toc | 30 ------------------------------ tests/where_expected | 1 - types.c | 27 --------------------------- types.h | 4 +--- 9 files changed, 45 insertions(+), 81 deletions(-) create mode 100644 tests/printf.toc create mode 100644 tests/printf_expected delete mode 100644 tests/where.toc delete mode 100644 tests/where_expected diff --git a/copy.c b/copy.c index d03782c..9ff5128 100644 --- a/copy.c +++ b/copy.c @@ -226,10 +226,6 @@ static void copy_fn_expr(Copier *c, FnExpr *fout, FnExpr *fin, U8 flags) { copy_decl(c, fout->ret_decls + i, fin->ret_decls + i); } copy_type(c, &fout->ret_type, &fin->ret_type); - - if (fin->condition) { - fout->condition = copy_expr_(c, fin->condition); - } } c->block = prev; if (copy_body) { diff --git a/parse.c b/parse.c index 7a1e59e..9266cfe 100644 --- a/parse.c +++ b/parse.c @@ -396,10 +396,6 @@ static Token *expr_find_end(Parser *p, ExprEndFlags flags) { if (all_levels_0 && (flags & EXPR_CAN_END_WITH_EQ)) return token; break; - case KW_WHERE: - if (all_levels_0 && (flags & EXPR_CAN_END_WITH_WHERE)) - return token; - break; case KW_COLON: if ((flags & EXPR_CAN_END_WITH_COLON) && all_levels_0) return token; @@ -807,7 +803,7 @@ static bool parser_is_definitely_type(Parser *p, Token **end) { --paren_level; if (paren_level == 0) { ++t->token; - if (token_is_kw(t->token, KW_LBRACE) || token_is_kw(t->token, KW_WHERE)) goto end; /* void fn expr */ + if (token_is_kw(t->token, KW_LBRACE)) goto end; /* void fn expr */ if (is_decl(t)) /* has return declaration */ goto end; @@ -959,7 +955,6 @@ static Status parse_fn_expr(Parser *p, FnExpr *f) { f->instance_id = 0; f->ret_decls = NULL; f->instances = NULL; - f->condition = NULL; /* only called when token is fn */ assert(token_is_kw(t->token, KW_FN)); ++t->token; @@ -990,7 +985,7 @@ static Status parse_fn_expr(Parser *p, FnExpr *f) { success = false; goto ret; } - if (token_is_kw(t->token, KW_LBRACE) || token_is_kw(t->token, KW_WHERE)) { + if (token_is_kw(t->token, KW_LBRACE)) { /* void function */ f->ret_type.kind = TYPE_VOID; f->ret_type.flags = 0; @@ -1017,13 +1012,6 @@ static Status parse_fn_expr(Parser *p, FnExpr *f) { goto ret; } } - if (token_is_kw(t->token, KW_WHERE)) { - ++t->token; - f->condition = parser_new_expr(p); - if (!parse_expr(p, f->condition, expr_find_end(p, EXPR_CAN_END_WITH_LBRACE))) { - return false; - } - } p->block = prev_block; /* be nice to parse_block */ if (!parse_block(p, &f->body, PARSE_BLOCK_DONT_CREATE_IDENTS)) success = false; diff --git a/tests/printf.toc b/tests/printf.toc new file mode 100644 index 0000000..e030af4 --- /dev/null +++ b/tests/printf.toc @@ -0,0 +1,37 @@ +printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; + + +// NOTE: this doesn't work (e.g. "%%%") +tprintf_valid ::= fn(fmt :: []char, nargs: int) bool { + if fmt[fmt.len-1] != '\0' { + return false; + } + count := 0; + for x, i := fmt { + if x == '%' { + if i == fmt.len-1 { + count += 1; + } elif fmt[i+1] != '%' { + count += 1; + } else { + count -= 1; + } + } + } + count == nargs +}; + + +tprintf ::= fn(fmt :: []char, args: ..) { + #if !tprintf_valid(fmt, args.len) { + #error "Invalid printf format"; + } + f := fmt; + printf(&f[0], args); +}; + +main ::= fn() { + tprintf("%d %d%%\n\0", 3, 4); + tprintf("%d %d %d%%\n\0", 3, 4, 5); + tprintf("Hello!\n\0"); +}; diff --git a/tests/printf_expected b/tests/printf_expected new file mode 100644 index 0000000..a5e51ec --- /dev/null +++ b/tests/printf_expected @@ -0,0 +1,3 @@ +3 4% +3 4 5% +Hello! diff --git a/tests/test.sh b/tests/test.sh index 31ccb6b..d30de7c 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -8,7 +8,7 @@ foreign params nms varargs -where +printf misc' STARTPWD=$(pwd) @@ -22,7 +22,7 @@ compile_c() { if [ "$CC" = "gcc -O0 -g" ]; then EXTRA_FLAGS="-Wno-builtin-declaration-mismatch" elif [ "$CC" = "clang -O3 -s" ]; then - EXTRA_FLAGS="-Wno-builtin-requires-header" + EXTRA_FLAGS="-Wno-builtin-requires-header -Wno-format-security" elif [ "$CC" = "tcc" ]; then EXTRA_FLAGS="-w" fi diff --git a/tests/where.toc b/tests/where.toc deleted file mode 100644 index 4223656..0000000 --- a/tests/where.toc +++ /dev/null @@ -1,30 +0,0 @@ -printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; - -tprintf_valid ::= fn(fmt :: []char, nargs: int) bool { - if fmt[fmt.len-1] != '\0' { - return false; - } - count := 0; - for x, i := fmt { - if x == '%' { - if i == fmt.len-1 { - count += 1; - } elif fmt[i+1] != '%' { - count += 1; - } else { - count -= 1; - } - } - } - count == nargs -}; - - -tprintf ::= fn(fmt :: []char, args: ..) where tprintf_valid(fmt, args.len) { - f := fmt; - printf(&f[0], args); -}; - -main ::= fn() { - tprintf("%d %d%%\n\0", 3, 4); -}; diff --git a/tests/where_expected b/tests/where_expected deleted file mode 100644 index 622c000..0000000 --- a/tests/where_expected +++ /dev/null @@ -1 +0,0 @@ -3 4% diff --git a/types.c b/types.c index ce40a88..9d36e78 100644 --- a/types.c +++ b/types.c @@ -2361,33 +2361,6 @@ static Status types_expr(Typer *tr, Expression *e) { } } - if (fn_copy->condition) { - typer_block_enter(tr, &fn_copy->body); - /* check where condition */ - if (!types_expr(tr, fn_copy->condition)) { - typer_block_exit(tr); - return false; - } - typer_block_exit(tr); - - Type *condition_type = &fn_copy->condition->type; - if (!type_is_builtin(condition_type, BUILTIN_BOOL)) { - char *s = type_to_str(condition_type); - err_print(fn_copy->condition->where, "where conditions must be of type bool, but this is of type %s.", s); - free(s); - return false; - } - Value val; - if (!eval_expr(tr->evalr, fn_copy->condition, &val)) { - return false; - } - if (!val.boolv) { - err_print(fn_copy->condition->where, "Function where condition not satisfied. You are probably calling this function incorrectly."); - info_print(e->where, "Offending call is here."); - return false; - } - } - ret_type = f->type.fn.types; param_types = ret_type + 1; } diff --git a/types.h b/types.h index 885d737..6b6df59 100644 --- a/types.h +++ b/types.h @@ -321,7 +321,6 @@ typedef enum { KW_FALSE, KW_NMS, KW_TYPEOF, - KW_WHERE, KW_COUNT } Keyword; @@ -336,7 +335,7 @@ static const char *const keywords[KW_COUNT] = "int", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "float", "f32", "f64", "Type", "Namespace", - "char", "bool", "true", "false", "nms", "typeof", "where"}; + "char", "bool", "true", "false", "nms", "typeof"}; typedef enum { NUM_LITERAL_INT, @@ -694,7 +693,6 @@ typedef struct FnExpr { U64 instance_id; Type ret_type; Block body; - struct Expression *condition; /* fn(...) ... where ... */ }; struct { Type type; /* type of this function */ -- cgit v1.2.3