added prelude noja script that's embedded into the executable

This commit is contained in:
Francesco Cozzuto
2023-01-11 00:31:01 +01:00
parent c0f6ddda50
commit 8de2ef719d
7 changed files with 159 additions and 39 deletions
+5 -1
View File
@@ -11,4 +11,8 @@ vgcore.*
*.gcov *.gcov
lib*.a lib*.a
./*.noja ./*.noja
embedder
*_n.c
+11 -1
View File
@@ -112,9 +112,13 @@ LIB_OBJDIR = $(OBJDIR)/$(LIB_SUBDIR)
CLI_OBJDIR = $(OBJDIR)/$(CLI_SUBDIR) CLI_OBJDIR = $(OBJDIR)/$(CLI_SUBDIR)
TST_OBJDIR = $(OBJDIR)/$(TST_SUBDIR) TST_OBJDIR = $(OBJDIR)/$(TST_SUBDIR)
LIB_NFILES = $(call rwildcard, $(LIB_SRCDIR), *.noja)
LIB_CFILES = $(call rwildcard, $(LIB_SRCDIR), *.c) LIB_CFILES = $(call rwildcard, $(LIB_SRCDIR), *.c)
LIB_HFILES = $(call rwildcard, $(LIB_SRCDIR), *.h) LIB_HFILES = $(call rwildcard, $(LIB_SRCDIR), *.h)
LIB_OFILES = $(patsubst $(LIB_SRCDIR)/%.c, $(LIB_OBJDIR)/%.o, $(LIB_CFILES)) LIB_OFILES = $(patsubst $(LIB_SRCDIR)/%.c, $(LIB_OBJDIR)/%.o, $(LIB_CFILES)) \
$(patsubst $(LIB_SRCDIR)/%.noja, $(LIB_OBJDIR)/%_n.o, $(LIB_NFILES))
$(info $(LIB_OFILES))
CLI_CFILES = $(call rwildcard, $(CLI_SRCDIR), *.c) CLI_CFILES = $(call rwildcard, $(CLI_SRCDIR), *.c)
CLI_HFILES = $(call rwildcard, $(CLI_SRCDIR), *.h) CLI_HFILES = $(call rwildcard, $(CLI_SRCDIR), *.h)
@@ -139,6 +143,12 @@ TST = $(OUTDIR)/$(TST_FNAME)
all: $(LIB) $(CLI) $(TST) all: $(LIB) $(CLI) $(TST)
embedder: misc/embedder.c
gcc $< -o $@ -Wall -Wextra
%_n.c: %.noja embedder
./embedder $< start_noja $@
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR)/%.o: $(SRCDIR)/%.c
@ mkdir -p $(@D) @ mkdir -p $(@D)
$(CC) $(CFLAGS) -c $^ -o $@ $(CC) $(CFLAGS) -c $^ -o $@
-6
View File
@@ -1,6 +0,0 @@
a = [];
i = 0;
while(i<100000000):{
a[i]= "ciao mondo mmododooddododododododododododdodododododd";
i = i+1;
}
+66
View File
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
// gcc embedder.c -o embedder -Wall -Wextra
int main(int argc, char **argv)
{
const char *variable = "__variable_name__";
const char *input = NULL;
const char *output = "output.c";
if (argc < 3) {
fprintf(stderr, "Usage: %s <input> <variable-name> [<output>]\n",
argv[0]);
return -1;
}
input = argv[1];
variable = argv[2];
if (argc > 3)
output = argv[3];
FILE *in_stream = fopen(input, "rb");
if (in_stream == NULL) {
fprintf(stderr, "Error: Failed to open \"%s\"\n", input);
return -1;
}
FILE *out_stream = fopen(output, "wb");
if (out_stream == NULL) {
fprintf(stderr, "Error: Failed to open \"%s\"\n", output);
fclose(in_stream);
return -1;
}
fprintf(out_stream, "const unsigned char %s[] = {\n\t", variable);
size_t w = 0;
bool done = false;
while (!done) {
uint8_t buffer[1024];
size_t num = fread(buffer, 1, sizeof(buffer), in_stream);
if (num < sizeof(buffer)) {
if (ferror(in_stream)) {
abort();
} else {
assert(feof(in_stream));
done = true;
}
}
for (size_t i = 0; i < num; i++, w++) {
fprintf(out_stream, "%3d, ", buffer[i]);
if ((w+1) % 16 == 0)
fprintf(out_stream, "\n\t");
}
}
fprintf(out_stream, "\n};\n");
fclose(in_stream);
fclose(out_stream);
return 0;
}
+1
View File
@@ -0,0 +1 @@
name = "Francesco";
+66 -10
View File
@@ -56,18 +56,74 @@ static _Bool interpret(Executable *exe)
RuntimeError error; RuntimeError error;
RuntimeError_Init(&error, runt); // Here we specify the runtime to snapshot in case of failure. RuntimeError_Init(&error, runt); // Here we specify the runtime to snapshot in case of failure.
Object *bins = Object_NewStaticMap(bins_basic, bins_basic_init, runt, (Error*) &error);
if(bins == NULL)
{ {
assert(error.base.occurred == 1); Object *native_bins = Object_NewStaticMap(bins_basic, bins_basic_init, runt, (Error*) &error);
print_error(NULL, (Error*) &error); if(native_bins == NULL)
RuntimeError_Free(&error); {
Runtime_Free(runt); assert(error.base.occurred == 1);
return 0; print_error(NULL, (Error*) &error);
} RuntimeError_Free(&error);
Runtime_Free(runt);
return 0;
}
Runtime_SetBuiltins(runt, bins); // Just to execute the prelude
Runtime_SetBuiltins(runt, native_bins);
extern char start_noja[];
Source *prelude = Source_FromString("<prelude>", start_noja, -1, (Error*) &error);
if (prelude == NULL) {
assert(error.base.occurred == 1);
print_error(NULL, (Error*) &error);
RuntimeError_Free(&error);
Runtime_Free(runt);
return 0;
}
CompilationErrorType errtyp;
Executable *prelude_exe = compile(prelude, (Error*) &error, &errtyp);
if(exe == NULL) {
const char *errname;
switch(errtyp) {
default:
case CompilationErrorType_INTERNAL: errname = NULL; break;
case CompilationErrorType_SYNTAX: errname = "Syntax"; break;
case CompilationErrorType_SEMANTIC: errname = "Semantic"; break;
}
print_error(errname, (Error*) &error);
RuntimeError_Free(&error);
Runtime_Free(runt);
Source_Free(prelude);
return 0;
}
Object *rets[8];
int retc = run(runt, (Error*) &error, prelude_exe, 0, NULL, NULL, 0, rets);
if(retc < 0) {
print_error("Runtime", (Error*) &error);
RuntimeError_Free(&error);
Runtime_Free(runt);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
}
Object *noja_bins = rets[0];
Object *all_bins = Object_NewClosure(native_bins, noja_bins, Runtime_GetHeap(runt), (Error*) &error);
if (all_bins == NULL) {
print_error(NULL, (Error*) &error);
RuntimeError_Free(&error);
Runtime_Free(runt);
Source_Free(prelude);
Executable_Free(prelude_exe);
return 0;
}
Runtime_SetBuiltins(runt, all_bins);
Source_Free(prelude);
Executable_Free(prelude_exe);
}
Object *rets[8]; Object *rets[8];
int retc = run(runt, (Error*) &error, exe, 0, NULL, NULL, 0, rets); int retc = run(runt, (Error*) &error, exe, 0, NULL, NULL, 0, rets);
+10 -21
View File
@@ -34,9 +34,9 @@
typedef struct ClosureObject ClosureObject; typedef struct ClosureObject ClosureObject;
struct ClosureObject { struct ClosureObject {
Object base; Object base;
ClosureObject *prev; Object *prev;
Object *vars; Object *vars;
}; };
static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp); static void walk(Object *self, void (*callback)(Object **referer, void *userp), void *userp);
@@ -57,13 +57,7 @@ Object *Object_NewClosure(Object *parent, Object *new_map, Heap *heap, Error *er
if(obj == NULL) if(obj == NULL)
return NULL; return NULL;
if(parent != NULL && parent->type != &t_closure) obj->prev = parent;
{
Error_Report(error, 0, "Object is not a Closure");
return NULL;
}
obj->prev = (ClosureObject*) parent;
obj->vars = new_map; obj->vars = new_map;
return (Object*) obj; return (Object*) obj;
@@ -74,18 +68,13 @@ static Object *select_(Object *self, Object *key,
{ {
ClosureObject *closure = (ClosureObject*) self; ClosureObject *closure = (ClosureObject*) self;
Object *selected = NULL; Object *selected = Object_Select(closure->vars, key, heap, err);
if(err->occurred)
while(closure != NULL && selected == NULL) return NULL;
{
selected = Object_Select(closure->vars, key, heap, err);
if(err->occurred)
return NULL;
closure = closure->prev;
}
if (selected == NULL)
selected = Object_Select(closure->prev, key, heap, err);
return selected; return selected;
} }