Skip to content

Commit 10d467f

Browse files
gh-194: Make EXIST use STR.
Closes #194.
1 parent d85535c commit 10d467f

20 files changed

Lines changed: 41 additions & 40 deletions

docs/SPECIFICATION.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@
701701

702702
- `ANY ASSIGN(target, ANY value)` = MUST evaluate `value`, assign it to `target`, and return the assigned value. `target` MAY be a plain identifier, a tensor indexed target, or a map indexed target. If a plain identifier has not yet been declared, the typed form `ASSIGN(TYPE name, value)` MUST be used. The typed form MUST NOT be used with indexed targets.
703703

704-
- `BOOL EXIST(SYMBOL name)` = MUST return `TRUE` if `name` resolves to a visible readable binding in the current lexical environment chain and `FALSE` otherwise.
704+
- `BOOL EXIST(STR name)` = MUST evaluate `name`, require the result to be a `STR`, and return `TRUE` if that string's contents resolve to a visible readable binding in the current lexical environment chain and `FALSE` otherwise.
705705

706706
- `BOOL ISBOOL/ISINT/ISFLT/ISSTR/ISTNS/ISMAP/ISFUNC/ISTHR(ANY value)` = MUST return `TRUE` if the runtime type of `value` is respectively `BOOL`, `INT`, `FLT`, `STR`, `TNS`, `MAP`, `FUNC`, or `THR`, and `FALSE` otherwise.
707707

src/builtins.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8670,16 +8670,14 @@ static Value builtin_permafrozen(Interpreter *interp, Value *args, int argc, Exp
86708670
}
86718671

86728672
static Value builtin_exist(Interpreter *interp, Value *args, int argc, Expr **arg_nodes, Env *env, int line, int col) {
8673-
(void)args;
8674-
(void)interp;
8675-
(void)line;
8676-
(void)col;
8673+
(void)arg_nodes;
86778674

8678-
if (argc != 1 || arg_nodes[0]->type != EXPR_IDENT) {
8679-
return value_bool(false);
8675+
if (argc != 1) {
8676+
RUNTIME_ERROR(interp, "EXIST expects 1 argument", line, col);
86808677
}
8678+
EXPECT_STR(args[0], "EXIST", interp, line, col);
86818679

8682-
const char *name = arg_nodes[0]->as.ident;
8680+
const char *name = args[0].as.s ? args[0].as.s : "";
86838681
return value_bool(env_exists(env, name));
86848682
}
86858683

src/interpreter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ Value eval_expr(Interpreter *interp, Expr *expr, Env *env) {
14061406
deferred_async_arg0 = true;
14071407
continue;
14081408
}
1409-
if ((strcmp(func_name, "EXIST") == 0 || strcmp(func_name, "ASSIGN") == 0) && i == 0) {
1409+
if (strcmp(func_name, "ASSIGN") == 0 && i == 0) {
14101410
// leave as null placeholder for identifier-like targets
14111411
continue;
14121412
}
@@ -1493,7 +1493,7 @@ Value eval_expr(Interpreter *interp, Expr *expr, Env *env) {
14931493

14941494
// effective_argc should count the original positional arguments
14951495
// and extend if any keyword maps beyond them. Do NOT trim placeholder
1496-
// NULLs for intentionally-unevaluated positional args (e.g. EXIST or ASSIGN).
1496+
// NULLs for intentionally-unevaluated positional args (e.g. ASSIGN).
14971497
int effective_argc = pos_argc;
14981498
if (max_slot > effective_argc) {
14991499
effective_argc = max_slot;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BOOL x = TRUE
2+
EXIST(x)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
TRY{
22
ASSERT(FALSE)
33
} CATCH(SYMBOL: err) {
4-
ASSERT(EXIST(err))
4+
ASSERT(EXIST("err"))
55
}

tests/cases/passing/assign.pre

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
ASSIGN(BOOL x, TRUE)
2-
ASSERT(EXIST(x))
2+
ASSERT(EXIST("x"))

tests/cases/passing/break-nested.pre

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ FOR(i, 0d3){
1111
}
1212
ASSERT(EQ(outer, 0d1))
1313
ASSERT(EQ(inner, 0d1))
14-
REFUTE(EXIST(i))
15-
REFUTE(EXIST(j))
14+
REFUTE(EXIST("i"))
15+
REFUTE(EXIST("j"))

tests/cases/passing/del-frozen.pre

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ TRY{
88
frozen_delete_failed = TRUE
99
}
1010
ASSERT(frozen_delete_failed)
11-
ASSERT(EXIST(frozen_symbol))
11+
ASSERT(EXIST("frozen_symbol"))
1212

1313
REFUTE(THAW(frozen_symbol))
1414
REFUTE(DEL("frozen_symbol"))
15-
REFUTE(EXIST(frozen_symbol))
15+
REFUTE(EXIST("frozen_symbol"))
1616

1717
BOOL permafrozen_symbol = TRUE
1818
REFUTE(PERMAFREEZE(permafrozen_symbol))
@@ -24,4 +24,4 @@ TRY{
2424
permafrozen_delete_failed = TRUE
2525
}
2626
ASSERT(permafrozen_delete_failed)
27-
ASSERT(EXIST(permafrozen_symbol))
27+
ASSERT(EXIST("permafrozen_symbol"))

tests/cases/passing/del-symbol.pre

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BOOL symbol = TRUE
2-
ASSERT(EXIST(symbol))
2+
ASSERT(EXIST("symbol"))
33
REFUTE(DEL("symbol"))
4-
REFUTE(EXIST(symbol))
4+
REFUTE(EXIST("symbol"))
55

66
BOOL deleted_read_failed = FALSE
77
TRY{
@@ -15,4 +15,4 @@ INT count = 0d1
1515
REFUTE(DEL("count"))
1616
count = 0d2
1717
ASSERT(EQ(count, 0d2))
18-
ASSERT(EXIST(count))
18+
ASSERT(EXIST("count"))

tests/cases/passing/exist.pre

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
BOOL outer = TRUE
22
BOOL declared_only
3+
STR outer_name = "outer"
34

45
FUNC BOOL outer_visible(){
5-
RETURN(EXIST(outer))
6+
RETURN(EXIST(outer_name))
67
}
78

89
FUNC BOOL inner_visible(){
910
BOOL inner = TRUE
10-
RETURN(EXIST(inner))
11+
RETURN(EXIST("inner"))
1112
}
1213

1314
FUNC BOOL missing_visible(){
14-
RETURN(EXIST(missing))
15+
RETURN(EXIST("missing"))
1516
}
1617

17-
ASSERT(EXIST(outer))
18-
REFUTE(EXIST(declared_only))
18+
ASSERT(EXIST(outer_name))
19+
REFUTE(EXIST("declared_only"))
1920
ASSERT(outer_visible())
2021
ASSERT(inner_visible())
2122
REFUTE(missing_visible())
2223

2324
DEL("outer")
24-
REFUTE(EXIST(outer))
25+
REFUTE(EXIST(outer_name))
2526
REFUTE(outer_visible())

0 commit comments

Comments
 (0)