Skip to content

Commit 27ad464

Browse files
committed
allow for input from cli
1 parent 365dbd2 commit 27ad464

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cc -std=c11 -O2 -Isrc examples/simd.c src/ctx.c -o simd_example
3434
./simd_example
3535

3636
cc -std=c11 -O2 -Isrc examples/minilang.c src/ctx.c -o minilang
37-
./minilang
37+
./minilang "(def main (x) (sub (call inc x) 3)) (def inc (x) (add x 1))" 10
3838
```
3939

4040
alternatively build the library and then add `-lcj -Lbin/` instead of the c file.

examples/minilang.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,12 @@ static void emit_function(codegen *cg, function *fn) {
277277
cj_builder_scratch_release(&cg->scratch);
278278
}
279279

280-
static const char *program_source = "(def main (x) (sub (call inc x) 3))\n"
281-
"(def inc (x) (add x 1))\n";
282-
283-
int main(void) {
280+
int main(int argc, char** argv) {
284281
node_arena arena = {0};
285282
function functions[MAX_FUN];
286283
int function_count = 0;
287284
lexer lx;
288-
init_lexer(&lx, program_source);
285+
init_lexer(&lx, argv[1]);
289286

290287
while (lx.tok.kind != TOK_END) {
291288
if (function_count >= MAX_FUN) {
@@ -320,12 +317,10 @@ int main(void) {
320317
functions[i].fn = (int (*)(int))addr;
321318
}
322319

323-
printf("minilang demo:\n");
324320
function *main_fn = &functions[main_idx];
325-
for (int i = 0; i <= 5; i++) {
326-
int result = main_fn->fn(i);
327-
printf(" main(%d) = %d\n", i, result);
328-
}
321+
int input = strtol(argv[2], NULL, 10);
322+
int result = main_fn->fn(input);
323+
printf("main(%d) = %d\n", input, result);
329324

330325
destroy_cj_fn(cj, module);
331326
destroy_cj_ctx(cj);

0 commit comments

Comments
 (0)