|
|
|
@ -141,19 +141,40 @@ static void print_releasever(struct context* ctx) { |
|
|
|
ctx->putchar(ctx, '\n'); |
|
|
|
} |
|
|
|
|
|
|
|
static void read_stdin(struct context* ctx) { |
|
|
|
struct elem e; |
|
|
|
e.type = INK_INTEGER; |
|
|
|
e.value = getchar(); |
|
|
|
ink_push(ctx, e); |
|
|
|
} |
|
|
|
|
|
|
|
static void static_EOF(struct context* ctx) { |
|
|
|
struct elem e; |
|
|
|
e.type = INK_INTEGER; |
|
|
|
e.value = EOF; |
|
|
|
ink_push(ctx, e); |
|
|
|
} |
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
char read_buffer[INK_SH_READ_BUFF]; |
|
|
|
struct context* ctx; |
|
|
|
char** end_argv; |
|
|
|
int interactive = 1; |
|
|
|
ctx = ink_make_default_context(); |
|
|
|
ink_add_native(ctx, "words?", list_words); |
|
|
|
ink_add_native(ctx, "words!", resolve_word); |
|
|
|
ink_add_native(ctx, "words.call", call_word); |
|
|
|
ink_add_native(ctx, "version", print_releasever); |
|
|
|
ink_add_native(ctx, "read-stdin", read_stdin); |
|
|
|
ink_add_native(ctx, "EOF", static_EOF); |
|
|
|
end_argv = argv + argc; |
|
|
|
size_t cnt; |
|
|
|
int no_exit = 1; |
|
|
|
for(argv+=1; argv != end_argv; argv++) { |
|
|
|
if (argv+1 == end_argv && strcmp(*argv, "--") == 0) { |
|
|
|
interactive = 0; |
|
|
|
continue; |
|
|
|
} |
|
|
|
FILE* file; |
|
|
|
file = fopen(*argv, "r"); |
|
|
|
cnt = fread(read_buffer, 1, INK_SH_READ_BUFF - 1, file); |
|
|
|
@ -174,81 +195,87 @@ int main(int argc, char** argv) { |
|
|
|
fclose(file); |
|
|
|
} |
|
|
|
|
|
|
|
char* it = read_buffer; |
|
|
|
do { |
|
|
|
int line_on = 0; |
|
|
|
int routine; |
|
|
|
struct ink_routine* rt; |
|
|
|
// cnt = fread(read_buffer, 1, INK_SH_READ_BUFF - 1, stdin); |
|
|
|
fputc('%', stdout); |
|
|
|
fputc(' ', stdout); |
|
|
|
cnt = 0; |
|
|
|
|
|
|
|
while(!line_on) { |
|
|
|
int c = fgetc(stdin); |
|
|
|
if (c == EOF) { |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
line_on = 1; |
|
|
|
no_exit = 0; |
|
|
|
} else if(c == '\n'){ |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
line_on = 1; |
|
|
|
} else { |
|
|
|
read_buffer[cnt] = c; |
|
|
|
++cnt; |
|
|
|
} |
|
|
|
} |
|
|
|
if(cnt > 0) { |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
routine = ink_compile(ctx, read_buffer); |
|
|
|
if (interactive) { |
|
|
|
char* it = read_buffer; |
|
|
|
do { |
|
|
|
int line_on = 0; |
|
|
|
int routine; |
|
|
|
struct ink_routine* rt; |
|
|
|
// cnt = fread(read_buffer, 1, INK_SH_READ_BUFF - 1, stdin); |
|
|
|
fputc('%', stdout); |
|
|
|
fputc(' ', stdout); |
|
|
|
cnt = 0; |
|
|
|
|
|
|
|
if (ctx->panic) { |
|
|
|
fprintf(stderr, "Panicked ctx !! -> %d\n", ctx->panic); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].panic) { |
|
|
|
fprintf(stderr, "Panicked routine !! -> %d\n", ctx->routines[routine].panic); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].parse_error.is_set) { |
|
|
|
fprintf(stderr, "Panicked !! -> %s\n", ctx->routines[routine].parse_error.error_message); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].runtime_error.is_set) { |
|
|
|
fprintf(stderr, "Panicked !! -> %s\n", ctx->routines[routine].runtime_error.error_message); |
|
|
|
while(!line_on) { |
|
|
|
int c = fgetc(stdin); |
|
|
|
if (c == EOF) { |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
line_on = 1; |
|
|
|
no_exit = 0; |
|
|
|
} else if(c == '\n'){ |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
line_on = 1; |
|
|
|
} else { |
|
|
|
read_buffer[cnt] = c; |
|
|
|
++cnt; |
|
|
|
} |
|
|
|
} |
|
|
|
if(cnt > 0) { |
|
|
|
read_buffer[cnt] = 0; |
|
|
|
routine = ink_compile(ctx, read_buffer); |
|
|
|
|
|
|
|
while (ink_can_run(ctx)) { |
|
|
|
ink_step_everyone(ctx); |
|
|
|
} |
|
|
|
rt = ctx->routines + routine; |
|
|
|
if(rt->top) { |
|
|
|
if(rt->stack[rt->top - 1].type == INK_INTEGER) { |
|
|
|
fprintf(stdout, "%u\n", rt->stack[rt->top - 1].value); |
|
|
|
if (ctx->panic) { |
|
|
|
fprintf(stderr, "Panicked ctx !! -> %d\n", ctx->panic); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].panic) { |
|
|
|
fprintf(stderr, "Panicked routine !! -> %d\n", ctx->routines[routine].panic); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].parse_error.is_set) { |
|
|
|
fprintf(stderr, "Panicked !! -> %s\n", ctx->routines[routine].parse_error.error_message); |
|
|
|
} |
|
|
|
if (ctx->routines[routine].runtime_error.is_set) { |
|
|
|
fprintf(stderr, "Panicked !! -> %s\n", ctx->routines[routine].runtime_error.error_message); |
|
|
|
} |
|
|
|
if(rt->stack[rt->top - 1].type == get_type_by_name(ctx, "array")) { |
|
|
|
struct elem* ary_it; |
|
|
|
int print_as_string = 1; |
|
|
|
struct ink_array* ary = ink_get_value(ctx, rt->stack[rt->top - 1]); |
|
|
|
|
|
|
|
|
|
|
|
for(ary_it = ary->elements; ary_it != ary->elements + ary->top; ++ary_it) { |
|
|
|
print_as_string = print_as_string && (isprint(ary_it->value) || isspace(ary_it->value)) && (ary_it->type == INK_INTEGER); |
|
|
|
while (ink_can_run(ctx)) { |
|
|
|
ink_step_everyone(ctx); |
|
|
|
} |
|
|
|
rt = ctx->routines + routine; |
|
|
|
if(rt->top) { |
|
|
|
if(rt->stack[rt->top - 1].type == INK_INTEGER) { |
|
|
|
fprintf(stdout, "%u\n", rt->stack[rt->top - 1].value); |
|
|
|
} |
|
|
|
if(rt->stack[rt->top - 1].type == get_type_by_name(ctx, "array")) { |
|
|
|
struct elem* ary_it; |
|
|
|
int print_as_string = 1; |
|
|
|
struct ink_array* ary = ink_get_value(ctx, rt->stack[rt->top - 1]); |
|
|
|
|
|
|
|
if(print_as_string) { |
|
|
|
print_string(ctx, rt->stack[rt->top - 1]); |
|
|
|
fputc('\n', stdout); |
|
|
|
} else { |
|
|
|
fputc('[', stdout); |
|
|
|
for (ary_it = ary->elements; ary_it != ary->elements + ary->top; ++ary_it) { |
|
|
|
fprintf(stdout, "%u", ary_it->value); |
|
|
|
if (ary_it + 1 != ary->elements + ary->top) fputc(',', stdout); |
|
|
|
|
|
|
|
for(ary_it = ary->elements; ary_it != ary->elements + ary->top; ++ary_it) { |
|
|
|
print_as_string = print_as_string && (isprint(ary_it->value) || isspace(ary_it->value)) && (ary_it->type == INK_INTEGER); |
|
|
|
} |
|
|
|
|
|
|
|
if(print_as_string) { |
|
|
|
print_string(ctx, rt->stack[rt->top - 1]); |
|
|
|
fputc('\n', stdout); |
|
|
|
} else { |
|
|
|
fputc('[', stdout); |
|
|
|
for (ary_it = ary->elements; ary_it != ary->elements + ary->top; ++ary_it) { |
|
|
|
fprintf(stdout, "%u", ary_it->value); |
|
|
|
if (ary_it + 1 != ary->elements + ary->top) fputc(',', stdout); |
|
|
|
} |
|
|
|
fputc(']', stdout); |
|
|
|
fputc('\n', stdout); |
|
|
|
} |
|
|
|
fputc(']', stdout); |
|
|
|
fputc('\n', stdout); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} while(no_exit); |
|
|
|
fputc('\n', stdout); |
|
|
|
} else { |
|
|
|
while (ink_can_run(ctx)) { |
|
|
|
ink_step_everyone(ctx); |
|
|
|
} |
|
|
|
} while(no_exit); |
|
|
|
fputc('\n', stdout); |
|
|
|
} |
|
|
|
return ink_destroy(ctx); |
|
|
|
} |