Skip to content

Commit ce53bbf

Browse files
gh-58: Add operator WARN.
1 parent 04bcaa5 commit ce53bbf

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

docs/SPECIFICATION.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@
900900
901901
- `INT: PRINT(INT|STR: a1, INT|STR: a2, ..., INT|STR: aN)` ; prints `INT`/`STR` arguments (side-effect), tensors are rejected; returns `INT` 0
902902
903+
- `INT: WARN(INT|STR: a1, INT|STR: a2, ..., INT|STR: aN)` ; if the interpreter was started with the `-verbose` flag, prints `WARNING: ` followed by the arguments side-by-side using the same rendering rules as `PRINT`. Returns `INT` 1 when the warning was printed, otherwise `INT` 0. Tensors are rejected.
904+
903905
- `INT: CL(STR: command)` - Executes the provided `command` string using the host shell and returns the subprocess exit code as an `INT`. The `command` argument MUST be a `STR`. On a failure to start the subprocess (for example if the system cannot invoke a shell), the interpreter raises a runtime error (rewrite: CL). The call records a `CL` event in the execution I/O log containing the command text and returned exit code, enabling deterministic logging and replay.
904906
905907
- `INT: SHUSH()` - Suppresses forwarding of console output to the configured output sink. While shushed, calls that would normally print to the console (for example, `PRINT` and output captured from `CL`) do not forward their text to the interpreter's output sink, though the interpreter continues to record the corresponding I/O events in the execution log for deterministic replay. `INPUT` prompts are still forwarded even when shushed. The `RUN` operator temporarily disables shushing for the duration of the executed source, so any output produced by code run via `RUN` is forwarded regardless of the current shush state. `SHUSH` takes no arguments and returns `INT` 0 on success.

src/builtins.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,6 +4894,45 @@ static Value builtin_print(Interpreter* interp, Value* args, int argc, Expr** ar
48944894
return value_int(0);
48954895
}
48964896

4897+
static Value builtin_warn(Interpreter* interp, Value* args, int argc, Expr** arg_nodes, Env* env, int line, int col) {
4898+
(void)arg_nodes; (void)env; (void)line; (void)col;
4899+
if (!interp) return value_int(0);
4900+
4901+
int forward = (interp->verbose && !interp->shushed);
4902+
4903+
if (forward) {
4904+
printf("WARNING: ");
4905+
for (int i = 0; i < argc; i++) {
4906+
if (i > 0) printf(" ");
4907+
switch (args[i].type) {
4908+
case VAL_INT: {
4909+
char* s = int_to_base_prefixed_str(args[i].as.i, numeric_base_of(args[i]));
4910+
printf("%s", s);
4911+
free(s);
4912+
break;
4913+
}
4914+
case VAL_FLT: {
4915+
char* s = flt_to_base_prefixed_str(args[i].as.f, numeric_base_of(args[i]), args[i].num_base_nan);
4916+
printf("%s", s);
4917+
free(s);
4918+
break;
4919+
}
4920+
case VAL_STR:
4921+
printf("%s", args[i].as.s);
4922+
break;
4923+
case VAL_FUNC:
4924+
printf("<func %p>", (void*)args[i].as.func);
4925+
break;
4926+
default:
4927+
printf("<null>");
4928+
break;
4929+
}
4930+
}
4931+
printf("\n");
4932+
}
4933+
return value_int(forward ? 1 : 0);
4934+
}
4935+
48974936
static Value builtin_input(Interpreter* interp, Value* args, int argc, Expr** arg_nodes, Env* env, int line, int col) {
48984937
(void)arg_nodes; (void)env; (void)interp; (void)line; (void)col;
48994938

@@ -7250,6 +7289,7 @@ static BuiltinFunction builtins_table[] = {
72507289

72517290
// I/O
72527291
{"PRINT", 0, -1, builtin_print},
7292+
{"WARN", 0, -1, builtin_warn},
72537293
{"INPUT", 0, 1, builtin_input},
72547294
{"SHUSH", 0, 0, builtin_shush},
72557295
{"UNSHUSH", 0, 0, builtin_unshush},

0 commit comments

Comments
 (0)