From b74b2821b84ac4681b6b85c1c74f975e44bac997 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Fri, 3 Oct 2025 16:06:51 +0200 Subject: [PATCH] better shell, starting to port shell to 9front --- 9build | 4 ++ 9sh.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ lib.c | 130 +++++++++++++++++++++++++++-------------------------- sh.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 319 insertions(+), 72 deletions(-) create mode 100644 9build create mode 100644 9sh.c diff --git a/9build b/9build new file mode 100644 index 0000000..ec5493c --- /dev/null +++ b/9build @@ -0,0 +1,4 @@ + + +cc -Iinclude lib.c 9sh.c +ld lib.$SUFFIX 9sh.$SUFFIX -o inksh diff --git a/9sh.c b/9sh.c new file mode 100644 index 0000000..6fc28b2 --- /dev/null +++ b/9sh.c @@ -0,0 +1,117 @@ + +#include +#include "ink.h" +#include "libc.h" + +#ifndef INK_SH_READ_BUFF +#define INK_SH_READ_BUFF 32768 +#endif // INK_SH_READ_BUFF + +int get_type_by_name(struct context* ctx, const char* name); + +void print_str(struct context* ctx, const char* str) { + while(*str != 0) { + ctx->putchar(ctx, *str); + ++str; + } +} + +static void list_words(struct context* ctx) { + struct native_fn* nit; + struct fn* dit; + for(nit = ctx->native_words; nit != ctx->native_words + ctx->native_words_top; ++nit) { + print_str(ctx, nit->name); + ctx->putchar(ctx, '\n'); + } + for(dit = ctx->words; dit != ctx->words + ctx->words_top; ++dit) { + print_str(ctx, dit->name); + ctx->putchar(ctx, '\n'); + } +} + +int main(int argc, char** argv) { + char read_buffer[INK_SH_READ_BUFF]; + struct context *ctx; + char **end_argv; + ctx = ink_make_default_context(); + ink_add_native(ctx, "words?", list_words); + end_argv = argv + argc; + size_t cnt; + int no_exit = 1; + for(argv+=1; argv != end_argv; argv++) { + int file; + file = p9open(*argv, "r"); + cnt = fread(read_buffer, 1, INK_SH_READ_BUFF - 1, file); + if(cnt == 0) { + fprintf(stderr, "Can't read file !! -> %s\n", *argv); + } + read_buffer[cnt] = 0; + ink_compile(ctx, read_buffer); + + if(ctx->panic) { + fprintf(stderr, "Panicked !! -> %d\n", ctx->panic); + } + + while (ink_can_run(ctx)) { + ink_step_everyone(ctx); + } + + 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; + } 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 (ctx->panic) { + fprintf(stderr, "Panicked !! -> %d\n", ctx->panic); + } + + 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* it; + struct ink_array* ary = ink_get_value(ctx, rt->stack[rt->top - 1]); + fputc('[', stdout); + for(it = ary->elements; it != ary->elements + ary->top; ++it) { + fprintf(stdout, "%u", it->value); + if(it + 1 != ary->elements + ary->top) fputc(',', stdout); + } + fputc(']', stdout); + fputc('\n', stdout); + } + } + } + } while(no_exit); + + return ink_destroy(ctx); +} \ No newline at end of file diff --git a/lib.c b/lib.c index acaafee..5168e98 100644 --- a/lib.c +++ b/lib.c @@ -482,14 +482,14 @@ static int ink_consume_one(int* end, struct context* pContext, char* r, int is_s int it = 0; new_protected_array(pContext); if(routine->top < 1) { - pContext->panic = -1; + pContext->panic = -35482; return -8746; } value = routine->stack[routine->top - 1]; ary = ink_get_value(pContext, value); #ifndef NOEXTRACHECKS if(ary == NULL) { - pContext->panic = -1; + pContext->panic = -13546; return -8747; } #endif @@ -587,7 +587,7 @@ static int ink_consume_one(int* end, struct context* pContext, char* r, int is_s if (!done) { i = ink_add_lex_string(pContext, r); if(i < 0) { - pContext->panic = 1; + pContext->panic = -3247; return -7; } value.value = i; @@ -628,7 +628,9 @@ static int ink_lex(struct context *pContext, const char* buffer) { if(*(buffer+1) == 0 || isspace(*(buffer+1))) { err = ink_consume_one(&end, pContext, r, 1); if(err < 0) { - pContext->panic = 1; + if(!pContext->panic) { + pContext->panic = -324; + } return -995; } parses_string = 0; @@ -664,7 +666,7 @@ static int ink_lex(struct context *pContext, const char* buffer) { /* Send the token off to the wizard (ink_parse) */ #ifndef NOEXTRACHECKS if(err < 0) { - pContext->panic = 1; + pContext->panic = -121; return -8; } #endif @@ -683,7 +685,7 @@ static int ink_lex(struct context *pContext, const char* buffer) { err = ink_consume_one(&end, pContext, r, 0); #ifndef NOEXTRACHECKS if(err < 0) { - pContext->panic = 1; + pContext->panic = -3458; return -9; } #endif @@ -1117,7 +1119,9 @@ int ink_compile(struct context *pContext, const char* buffer) { currentRoutine->parse_error.offset = -1; } #endif - pContext->panic = 1; + if(!pContext->panic) { + pContext->panic = -2824; + } return -1; } executable_buffer_top = 0; @@ -1130,7 +1134,7 @@ int ink_compile(struct context *pContext, const char* buffer) { currentRoutine->parse_error.offset = -1; } #endif - pContext->panic = 1; + pContext->panic = -2188; return -1; } if(executable_buffer_top != 0) { @@ -1149,7 +1153,7 @@ int ink_compile(struct context *pContext, const char* buffer) { currentRoutine->parse_error.offset = -1; } #endif - pContext->panic = 1; + pContext->panic = -2114; return -1; } frame.executing.type = INK_FUNCTION; @@ -1163,7 +1167,7 @@ int ink_compile(struct context *pContext, const char* buffer) { currentRoutine->parse_error.error_message = "Could not push any executable frame: push failed"; currentRoutine->parse_error.offset = -1; } - pContext->panic = 1; + pContext->panic = -1752; return -1; } #endif @@ -1552,7 +1556,7 @@ static void add_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -312; return; } #endif @@ -1560,7 +1564,7 @@ static void add_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - ctx->panic = 1; + ctx->panic = -684; return; } #endif @@ -1575,7 +1579,7 @@ static void sub_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -2581; return; } #endif @@ -1583,7 +1587,7 @@ static void sub_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -821; return; } #endif @@ -1598,7 +1602,7 @@ static void mult_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -8533; return; } #endif @@ -1606,7 +1610,7 @@ static void mult_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -543; return; } #endif @@ -1621,7 +1625,7 @@ static void div_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -84; return; } #endif @@ -1629,7 +1633,7 @@ static void div_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -1328; return; } #endif @@ -1645,7 +1649,7 @@ static void is_equal(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -13587; return; } #endif @@ -1666,7 +1670,7 @@ static void is_different(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -3873; return; } #endif @@ -1688,7 +1692,7 @@ static void rem_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -218; return; } #endif @@ -1696,7 +1700,7 @@ static void rem_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -8321; return; } #endif @@ -1711,7 +1715,7 @@ static void xor_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -184; return; } #endif @@ -1719,7 +1723,7 @@ static void xor_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -183; return; } #endif @@ -1734,7 +1738,7 @@ static void gt_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -132; return; } #endif @@ -1742,7 +1746,7 @@ static void gt_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -1185; return; } #endif @@ -1757,7 +1761,7 @@ static void gte_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -138; return; } #endif @@ -1765,7 +1769,7 @@ static void gte_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -172; return; } #endif @@ -1780,7 +1784,7 @@ static void lte_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -116; return; } #endif @@ -1788,7 +1792,7 @@ static void lte_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -171; return; } #endif @@ -1805,7 +1809,7 @@ static void lt_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -135; return; } #endif @@ -1813,7 +1817,7 @@ static void lt_int(struct context* ctx) { b = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(!(a.type == INK_INTEGER && b.type == INK_INTEGER)) { - currentRoutine->panic = -1; + currentRoutine->panic = -1187; return; } #endif @@ -1832,14 +1836,14 @@ static void dupe_elem(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1) { - ctx->panic = 1; + ctx->panic = -2488; return; } #endif a = currentRoutine->stack[currentRoutine->top-1]; err = ink_push(ctx, a); #ifndef NOEXTRACHECKS - if(err < 0) ctx->panic = 1; + if(err < 0) ctx->panic = -348; #endif } @@ -1848,7 +1852,7 @@ static void drop_elem(struct context* ctx) { struct ink_routine* currentRoutine; currentRoutine = ctx->routines + ctx->routine_current; if(currentRoutine->top < 1) { - ctx->panic = 1; + ctx->panic = -1835; return; } #endif @@ -1867,28 +1871,28 @@ static void pluck_elem(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1) { - currentRoutine->panic = -1; + currentRoutine->panic = -1454; return; } #endif a = currentRoutine->stack[currentRoutine->top-1]; #ifndef NOEXTRACHECKS if(a.type != INK_INTEGER) { - ctx->panic = 1; + ctx->panic = -175; return; } #endif position = currentRoutine->top - (a.value + 1); #ifndef NOEXTRACHECKS if(position >= currentRoutine->top || position < 0) { - ctx->panic = 1; + ctx->panic = -721; return; } #endif ink_pop(ctx); err = ink_push(ctx, currentRoutine->stack[position]); #ifndef NOEXTRACHECKS - if(err < 0) ctx->panic = 1; + if(err < 0) ctx->panic = -7965; #endif } @@ -1899,7 +1903,7 @@ static void swap_elem(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - currentRoutine->panic = -1; + currentRoutine->panic = -11354; return; } #endif @@ -1915,14 +1919,14 @@ static void return_if(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1) { - ctx->panic = -1; + ctx->panic = -348; return; } #endif a = currentRoutine->stack[currentRoutine->top-1]; #ifndef NOEXTRACHECKS if(a.type != INK_INTEGER) { - ctx->panic = 1; + ctx->panic = -124; return; } #endif @@ -1941,7 +1945,7 @@ static void jump_if(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2) { - ctx->panic = -1; + ctx->panic = -751; return; } #endif @@ -1949,7 +1953,7 @@ static void jump_if(struct context* ctx) { condition = currentRoutine->stack[currentRoutine->top-2]; #ifndef NOEXTRACHECKS if(label.type != INK_INTEGER || condition.type != INK_INTEGER) { - ctx->panic = -1; + ctx->panic = -126; return; } #endif @@ -1971,7 +1975,7 @@ static void print_int(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) { - currentRoutine->panic = -1; + currentRoutine->panic = -13544; return; } #endif @@ -1998,7 +2002,7 @@ static void print_as_utf8(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top-1].type != INK_INTEGER) { - ctx->panic = -1; + ctx->panic = -1185; return; } #endif @@ -2018,7 +2022,7 @@ static void print_as_utf8(struct context* ctx) { ctx->putchar(ctx, ((a.value & 0xFC0) >> 6) | 128); ctx->putchar(ctx, (a.value & 0x3F) | 128); } else { - ctx->panic = -1; + ctx->panic = -9472; return; } ink_pop(ctx); @@ -2188,7 +2192,7 @@ int array_push_s(struct context* ctx, struct ink_array* ary, struct elem value) void array_push(struct context* ctx, struct ink_routine* currentRoutine, struct ink_array* ary, struct elem value) { if(array_push_s(ctx, ary, value)) { - currentRoutine->panic = -1; + currentRoutine->panic = -156; } } @@ -2203,15 +2207,15 @@ static void push_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top-1].type != tid) { - currentRoutine->panic = -1; + currentRoutine->panic = -197; return; } #endif a = currentRoutine->stack[currentRoutine->top-1]; - ary= ink_get_value(ctx, a); + ary = ink_get_value(ctx, a); #ifndef NOEXTRACHECKS if(ary == NULL) { - currentRoutine->panic = -1; + currentRoutine->panic = -137; return; } #endif @@ -2229,7 +2233,7 @@ static void push_delimited_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if(currentRoutine->top < 1) { - currentRoutine->panic = -1; + currentRoutine->panic = -1456; return; } #endif @@ -2286,7 +2290,7 @@ static void index_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if (currentRoutine->top < 2 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) { - currentRoutine->panic = -1; + currentRoutine->panic = -173; return; } #endif @@ -2294,7 +2298,7 @@ static void index_array(struct context* ctx) { ary = ink_get_value(ctx, a); #ifndef NOEXTRACHECKS if (ary == NULL) { - currentRoutine->panic = -1; + currentRoutine->panic = -1787; return; } #endif @@ -2305,7 +2309,7 @@ static void index_array(struct context* ctx) { #ifndef NOEXTRACHECKS if(ary->top <= idx.value) { - currentRoutine->panic = -1; + currentRoutine->panic = -1375; return; } #endif @@ -2327,7 +2331,7 @@ static void set_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if (currentRoutine->top < 3 || currentRoutine->stack[currentRoutine->top - 1].type != tid || currentRoutine->stack[currentRoutine->top - 2].type != INK_INTEGER) { - currentRoutine->panic = -1; + currentRoutine->panic = -1787; return; } #endif @@ -2335,7 +2339,7 @@ static void set_array(struct context* ctx) { ary = ink_get_value(ctx, a); #ifndef NOEXTRACHECKS if (ary == NULL) { - currentRoutine->panic = -1; + currentRoutine->panic = -1732; return; } #endif @@ -2345,7 +2349,7 @@ static void set_array(struct context* ctx) { #ifndef NOEXTRACHECKS if(ary->top <= idx.value) { - currentRoutine->panic = -1; + currentRoutine->panic = -1232; return; } #endif @@ -2370,7 +2374,7 @@ static void get_size_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) { - currentRoutine->panic = -1; + currentRoutine->panic = -17657; return; } #endif @@ -2378,7 +2382,7 @@ static void get_size_array(struct context* ctx) { ary = ink_get_value(ctx, a); #ifndef NOEXTRACHECKS if (ary == NULL) { - currentRoutine->panic = -1; + currentRoutine->panic = -13783; return; } #endif @@ -2399,7 +2403,7 @@ static void is_array(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if (currentRoutine->top < 1) { - currentRoutine->panic = -1; + currentRoutine->panic = -1783; return; } #endif @@ -2423,7 +2427,7 @@ static void print_array_of_codepoints(struct context* ctx) { currentRoutine = ctx->routines + ctx->routine_current; #ifndef NOEXTRACHECKS if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) { - currentRoutine->panic = -1; + currentRoutine->panic = -1378; return; } #endif @@ -2433,7 +2437,7 @@ static void print_array_of_codepoints(struct context* ctx) { #ifndef NOEXTRACHECKS for(i = 0; i < ary->top; ++i) { if(ary->elements[i].type != INK_INTEGER) { - currentRoutine->panic = -1; + currentRoutine->panic = -11234; return; } } diff --git a/sh.c b/sh.c index 5b0bcae..c206365 100644 --- a/sh.c +++ b/sh.c @@ -1,6 +1,8 @@ #include "ink.h" #include #include +#include +#include #ifndef INK_SH_READ_BUFF #define INK_SH_READ_BUFF 32768 @@ -19,12 +21,111 @@ static void list_words(struct context* ctx) { } } +static void resolve_word(struct context* ctx) { + struct native_fn* nit; + struct fn* dit; + int i; + struct ink_routine *currentRoutine; + struct elem a; + struct ink_array *ary; + unsigned char* name; + int it; + int found = 0; + +#ifndef NOEXTRACHECKS + int tid; + tid = get_type_by_name(ctx, "array"); +#endif + currentRoutine = ctx->routines + ctx->routine_current; +#ifndef NOEXTRACHECKS + if (currentRoutine->top < 1 || currentRoutine->stack[currentRoutine->top - 1].type != tid) { + currentRoutine->panic = -1; + return; + } +#endif + a = currentRoutine->stack[currentRoutine->top - 1]; + ary = ink_get_value(ctx, a); + +#ifndef NOEXTRACHECKS + for(i = 0; i < ary->top; ++i) { + if(ary->elements[i].type != INK_INTEGER) { + currentRoutine->panic = -1; + return; + } + } +#endif + ink_pop(ctx); + it = 0; + name = ctx->malloc(ctx, ary->top*4); + for(i = 0; i < ary->top; ++i) { + a = ary->elements[i]; + if(a.value <= 0x7F) { + name[it++] = a.value; + } else if(a.value <= 0x7FF) { + name[it++] = ((a.value & 0xFC0) >> 6) | 192; + name[it++] =(a.value & 0x3F) | 128; + } else if(a.value <= 0xFFFF) { + name[it++] = ((a.value & 0x3F000) >> 12) | 224; + name[it++] = ((a.value & 0xFC0) >> 6) | 128; + name[it++] = (a.value & 0x3F) | 128; + } else if(a.value <= 0x10FFFF) { + name[it++] = ((a.value & 0x3C0000) >> 18) | 240; + name[it++] = ((a.value & 0x3F000) >> 12) | 128; + name[it++] = ((a.value & 0xFC0) >> 6) | 128; + name[it++] = (a.value & 0x3F) | 128; + } + } + name[it] = 0; + + for(nit = ctx->native_words; (!found) && nit != ctx->native_words + ctx->native_words_top; ++nit) { + if(strcmp(name, nit->name) == 0) { + struct elem fn; + fn.type = INK_NATIVE_FUNCTION; + fn.value = nit - ctx->native_words; + ink_push(ctx, fn); + found = 1; + } + } + for(dit = ctx->words; (!found) && dit != ctx->words + ctx->words_top; ++dit) { + if(strcmp(name, dit->name) == 0) { + struct elem fn; + fn.type = INK_FUNCTION; + fn.value = dit - ctx->words; + ink_push(ctx, fn); + found = 1; + } + } + ctx->free(ctx, name); +} + +static void call_word(struct context* ctx) { + struct ink_routine *currentRoutine; + unsigned char* name; + int it; + int found = 0; + struct stack_frame frame; + + currentRoutine = ctx->routines + ctx->routine_current; +#ifndef NOEXTRACHECKS + if (currentRoutine->top < 1 || (currentRoutine->stack[currentRoutine->top - 1].type != INK_FUNCTION && currentRoutine->stack[currentRoutine->top - 1].type != INK_NATIVE_FUNCTION)) { + currentRoutine->panic = -1; + return; + } +#endif + frame.executing = currentRoutine->stack[currentRoutine->top - 1]; + frame.index = 0; + ink_pop(ctx); + ink_push_fn(ctx, frame); +} + int main(int argc, char** argv) { char read_buffer[INK_SH_READ_BUFF]; struct context* ctx; char** end_argv; 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); end_argv = argv + argc; size_t cnt; int no_exit = 1; @@ -64,6 +165,7 @@ int main(int argc, char** argv) { if (c == EOF) { read_buffer[cnt] = 0; line_on = 1; + no_exit = 0; } else if(c == '\n'){ read_buffer[cnt] = 0; line_on = 1; @@ -77,7 +179,13 @@ int main(int argc, char** argv) { routine = ink_compile(ctx, read_buffer); if (ctx->panic) { - fprintf(stderr, "Panicked !! -> %d\n", 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].runtime_error.is_set) { + fprintf(stderr, "Panicked !! -> %s\n", ctx->routines[routine].runtime_error.error_message); } while (ink_can_run(ctx)) { @@ -89,19 +197,33 @@ int main(int argc, char** argv) { 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* it; + struct elem* ary_it; + int print_as_string = 1; struct ink_array* ary = ink_get_value(ctx, rt->stack[rt->top - 1]); - fputc('[', stdout); - for(it = ary->elements; it != ary->elements + ary->top; ++it) { - fprintf(stdout, "%u", it->value); - if(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) { + for (ary_it = ary->elements; ary_it != ary->elements + ary->top; ++ary_it) { + fputc(ary_it->value, stdout); + } + 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); return ink_destroy(ctx); } \ No newline at end of file