linenoise repl...
This commit is contained in:
parent
d359c68c32
commit
54d12060ec
3 changed files with 12 additions and 39 deletions
|
|
@ -18,6 +18,7 @@ sources = [
|
|||
'src/print.c',
|
||||
'src/string.c',
|
||||
'src/vm.c',
|
||||
'src/vendor/linenoise.c',
|
||||
'src/vendor/mpc.c',
|
||||
'src/vendor/yar.c',
|
||||
]
|
||||
|
|
|
|||
29
src/main.c
29
src/main.c
|
|
@ -7,6 +7,7 @@
|
|||
#include "parser.h"
|
||||
#include "vm.h"
|
||||
|
||||
#include "vendor/linenoise.h"
|
||||
#include "vendor/mpc.h"
|
||||
|
||||
#define REPL_BUFFER_SIZE 4096
|
||||
|
|
@ -15,29 +16,11 @@ I repl(void) {
|
|||
Vm vm = {0};
|
||||
vm_init(&vm);
|
||||
|
||||
char input[REPL_BUFFER_SIZE];
|
||||
|
||||
for (;;) {
|
||||
printf("> ");
|
||||
fflush(stdout);
|
||||
if (fgets(input, REPL_BUFFER_SIZE, stdin) == NULL) {
|
||||
printf("\n");
|
||||
break;
|
||||
}
|
||||
I is_empty = 1;
|
||||
for (char *p = input; *p; p++) {
|
||||
if (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') {
|
||||
is_empty = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_empty)
|
||||
continue;
|
||||
if (strncmp(input, "bye", 3) == 0 || strncmp(input, "quit", 4) == 0)
|
||||
break;
|
||||
char *line;
|
||||
while ((line = linenoise("growl> ")) != NULL) {
|
||||
mpc_result_t res;
|
||||
if (!mpc_parse("<repl>", input, Program, &res)) {
|
||||
mpc_err_print(res.error);
|
||||
if (!mpc_parse("<repl>", line, Program, &res)) {
|
||||
mpc_err_print_to(res.error, stderr);
|
||||
mpc_err_delete(res.error);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -48,8 +31,10 @@ I repl(void) {
|
|||
if (chunk != NULL) {
|
||||
vm_run(&vm, chunk, 0);
|
||||
chunk_release(chunk);
|
||||
linenoiseHistoryAdd(line);
|
||||
}
|
||||
compiler_deinit(&cm);
|
||||
linenoiseFree(line);
|
||||
}
|
||||
vm_deinit(&vm);
|
||||
return 0;
|
||||
|
|
|
|||
21
test.grr
21
test.grr
|
|
@ -1,25 +1,12 @@
|
|||
def fib/aux {
|
||||
if: dig dup 0 =
|
||||
[drop drop]
|
||||
[bury [swap 1 - swap] dip dup [+] dip swap fib/aux];
|
||||
}
|
||||
def fib { 0 1 fib/aux }
|
||||
|
||||
"50 fib => " type
|
||||
50 fib .
|
||||
|
||||
def times {
|
||||
if: over 0 =
|
||||
[drop drop]
|
||||
[swap over >r >r call r> 1 - r> times];
|
||||
}
|
||||
|
||||
\ We can also calculate the Fibonnaci numbers using the `times' combinator
|
||||
\ we just implemented:
|
||||
|
||||
def fib-iter {
|
||||
0 1 dig times: [dup [+] dip swap]; drop
|
||||
def fib {
|
||||
0 1 dig [dup [+] dip swap] times drop
|
||||
}
|
||||
|
||||
"50 fib-iter => " type
|
||||
50 fib-iter .
|
||||
"50 fib => " type
|
||||
50 fib .
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue