A minimalistic programming language written in C89.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

117 rindas
3.3 KiB

#include <u.h>
#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);
}