1
0
mirror of https://github.com/janet-lang/janet synced 2024-07-08 04:44:20 +00:00
janet/main.c

115 lines
3.0 KiB
C
Raw Normal View History

2017-02-09 20:02:59 +00:00
#include <stdlib.h>
#include <stdio.h>
#include "gst.h"
2017-02-09 20:02:59 +00:00
2017-03-08 15:54:50 +00:00
/* Simple printer for gst strings */
void string_put(FILE *out, uint8_t * string) {
uint32_t i;
uint32_t len = gst_string_length(string);
for (i = 0; i < len; ++i)
fputc(string[i], out);
}
2017-02-12 20:53:52 +00:00
/* Test c function */
int print(Gst *vm) {
uint32_t j, count;
count = gst_count_args(vm);
2017-02-13 04:45:52 +00:00
for (j = 0; j < count; ++j) {
string_put(stdout, gst_to_string(vm, gst_arg(vm, j)));
2017-02-13 04:45:52 +00:00
fputc('\n', stdout);
}
return GST_RETURN_OK;
}
2017-02-09 20:02:59 +00:00
/* A simple repl for debugging */
void debug_repl(FILE *in, FILE *out) {
char buffer[1024] = {0};
2017-02-13 04:45:52 +00:00
const char * reader = buffer;
GstValue func;
Gst vm;
GstParser p;
GstCompiler c;
2017-02-09 20:02:59 +00:00
gst_init(&vm);
2017-02-09 20:02:59 +00:00
2017-02-13 04:45:52 +00:00
for (;;) {
2017-02-09 20:02:59 +00:00
2017-02-13 04:45:52 +00:00
/* Reset state */
gst_parser(&p, &vm);
2017-02-09 20:02:59 +00:00
2017-02-13 04:45:52 +00:00
/* Get and parse input until we have a full form */
while (p.status == GST_PARSER_PENDING) {
2017-02-13 04:45:52 +00:00
/* Get some input if we are done */
if (*reader == '\0') {
if (out)
fprintf(out, ">> ");
if (!fgets(buffer, sizeof(buffer), in)) {
2017-02-13 04:45:52 +00:00
return;
}
p.index = 0;
reader = buffer;
}
reader += gst_parse_cstring(&p, reader);
2017-02-09 20:02:59 +00:00
}
2017-02-13 04:45:52 +00:00
/* Check for parsing errors */
if (p.error) {
unsigned i;
if (out) {
fprintf(out, "\n");
fprintf(out, "%s\n", buffer);
for (i = 0; i < p.index; ++i) {
fprintf(out, " ");
}
fprintf(out, "^\n");
fprintf(out, "\nParse error: %s\n", p.error);
2017-02-13 04:45:52 +00:00
}
reader = buffer; /* Flush the input buffer */
buffer[0] = '\0';
continue;
}
2017-02-09 20:02:59 +00:00
2017-02-13 04:45:52 +00:00
/* Try to compile generated AST */
gst_compiler(&c, &vm);
gst_compiler_add_global_cfunction(&c, "print", print);
func.type = GST_FUNCTION;
func.data.function = gst_compiler_compile(&c, p.value);
2017-02-09 20:02:59 +00:00
2017-02-13 04:45:52 +00:00
/* Check for compilation errors */
if (c.error) {
if (out) {
fprintf(out, "Compiler error: %s\n", c.error);
}
2017-02-13 04:45:52 +00:00
reader = buffer;
buffer[0] = 0;
continue;
}
2017-02-13 04:45:52 +00:00
/* Execute function */
2017-03-08 15:54:50 +00:00
if (gst_start(&vm, func)) {
if (out) {
if (vm.crash) {
fprintf(out, "VM crash: %s\n", vm.crash);
} else {
fprintf(out, "VM error: ");
string_put(out, gst_to_string(&vm, vm.ret));
printf("\n");
}
}
2017-02-13 04:45:52 +00:00
reader = buffer;
buffer[0] = 0;
continue;
} else if (out) {
string_put(out, gst_to_string(&vm, vm.ret));
fprintf(out, "\n");
2017-02-13 04:45:52 +00:00
}
2017-02-09 20:02:59 +00:00
}
}
int main() {
2017-02-13 04:45:52 +00:00
printf("Super cool interpreter v0.0\n");
debug_repl(stdin, stdout);
2017-02-13 04:45:52 +00:00
return 0;
2017-02-09 20:02:59 +00:00
}