diff --git a/.gitignore b/.gitignore index 1dd414f..3551c36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /pmake* *.o /src/compiler/*.c +/src/js/*.c diff --git a/GNUmakefile b/GNUmakefile index c2b60c6..925b30c 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -27,7 +27,7 @@ endif CFLAGS += -Iexternal/duktape -Iexternal/stb ### Rules -OBJS = src/main.o +OBJS = src/main.o src/js.o OBJS += external/duktape/duktape.o external/stb/stb_ds.o .PHONY: all clean mk maint @@ -38,8 +38,8 @@ all: pmake$(EXE) include mk/compiler.mk mk/compiler.mk: - find src/compiler -name "*.js" | while read js; do \ - name="compiler_`echo $$js | rev | cut -d/ -f1 | rev | cut -d. -f1`" ; \ + find src/js src/compiler -name "*.js" | while read js; do \ + name="`echo $$js | cut -d/ -f2`_`echo $$js | rev | cut -d/ -f1 | rev | cut -d. -f1`" ; \ c="`echo $$js | sed 's/\.js$$/.c/'`" ; \ o="`echo $$js | sed 's/\.js$$/.o/'`" ; \ echo "OBJS += $$o" ; \ diff --git a/mk/compiler.mk b/mk/compiler.mk index 0d834df..85d3ed2 100644 --- a/mk/compiler.mk +++ b/mk/compiler.mk @@ -1,3 +1,11 @@ +OBJS += src/js/core.o +src/js/core.c: src/js/core.js + xxd -njs_core -i src/js/core.js > src/js/core.c + +OBJS += src/js/cmdline.o +src/js/cmdline.c: src/js/cmdline.js + xxd -njs_cmdline -i src/js/cmdline.js > src/js/cmdline.c + OBJS += src/compiler/watcom.o src/compiler/watcom.c: src/compiler/watcom.js xxd -ncompiler_watcom -i src/compiler/watcom.js > src/compiler/watcom.c diff --git a/src/compiler/gcc.js b/src/compiler/gcc.js index bc5ac1d..0fa93d6 100644 --- a/src/compiler/gcc.js +++ b/src/compiler/gcc.js @@ -1,2 +1,5 @@ -class GCC extends Compiler { -}; +const GCC = pmake.inherit(Compiler); + +GCC.description = "GCC/Clang"; + +pmake.register(GCC); diff --git a/src/compiler/watcom.js b/src/compiler/watcom.js index 8469359..6b29ca4 100644 --- a/src/compiler/watcom.js +++ b/src/compiler/watcom.js @@ -1,2 +1,5 @@ -class Watcom extends Compiler { -}; +const Watcom = pmake.inherit(Compiler); + +Watcom.description = "Open Watcom"; + +pmake.register(Watcom); diff --git a/src/js.c b/src/js.c new file mode 100644 index 0000000..45a2f67 --- /dev/null +++ b/src/js.c @@ -0,0 +1,106 @@ +#include "pmake.h" + +duk_context* js_ctx; + +LOAD(js_core); +LOAD(js_cmdline); + +static duk_ret_t js_console_log(duk_context* self){ + printf("%s\n", duk_to_string(self, 0)); + + return 0; +} + +static void js_argv_init(int argc, char** argv){ + int arr; + int i; + + duk_push_global_object(js_ctx); + arr = duk_push_array(js_ctx); + + for(i = 0; i < argc; i++){ + duk_push_string(js_ctx, argv[i]); + duk_put_prop_index(js_ctx, arr, i); + } + + duk_put_prop_string(js_ctx, 0, "ARGV"); + + duk_pop(js_ctx); +} + +static void js_console_init(void){ + int obj; + + duk_push_global_object(js_ctx); + obj = duk_push_object(js_ctx); + + duk_push_c_function(js_ctx, js_console_log, 1); + duk_put_prop_string(js_ctx, obj, "log"); + + duk_put_prop_string(js_ctx, 0, "console"); + + duk_pop(js_ctx); +} + +static duk_ret_t js_process_exit(duk_context* self){ + duk_idx_t argc = duk_get_top(self); + int ex; + + if(argc > 1){ + return duk_error(self, DUK_ERR_TYPE_ERROR, "needs 0 or 1 argument(s)"); + } + + if(argc == 1){ + ex = duk_to_number(self, 0); + }else{ + ex = 0; + } + + exit(ex); +} + +static void js_process_init(void){ + int obj; + + duk_push_global_object(js_ctx); + obj = duk_push_object(js_ctx); + + duk_push_c_function(js_ctx, js_process_exit, DUK_VARARGS); + duk_put_prop_string(js_ctx, obj, "exit"); + + duk_put_prop_string(js_ctx, 0, "process"); + + duk_pop(js_ctx); +} + +int js_init(int argc, char** argv){ + js_ctx = duk_create_heap_default(); + + js_argv_init(argc, argv); + js_console_init(); + js_process_init(); + + if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0){ + printf("error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1)); + return 1; + } + +#define X(name) \ + if(duk_peval_lstring(js_ctx, compiler_ ## name, compiler_ ## name ## _len) != 0){ \ + printf("error: %s: %s\n", "compiler/" #name ".js", duk_safe_to_string(js_ctx, -1)); \ + return 1; \ + } + COMPILERS; +#undef X + + if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0){ + printf("error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1)); + return 1; + } + + return 0; +} + +void js_uninit(void){ + duk_destroy_heap(js_ctx); +} diff --git a/src/js/cmdline.js b/src/js/cmdline.js new file mode 100644 index 0000000..5a4aa04 --- /dev/null +++ b/src/js/cmdline.js @@ -0,0 +1,13 @@ +if(ARGV.length == 1){ + console.log("Type '" + ARGV[0] + " --help' for help"); + process.exit(1); +} + +for(var i = 1; i < ARGV.length; i++){ + if(ARGV[i] == "--help"){ + console.log("PMake " + pmake.VERSION + " - Pyrite's build script generator"); + console.log(""); + console.log("Usage: " + ARGV[0] + " [options] action [arguments]"); + process.exit(0); + } +} diff --git a/src/js/core.js b/src/js/core.js new file mode 100644 index 0000000..4d31e83 --- /dev/null +++ b/src/js/core.js @@ -0,0 +1,31 @@ +const pmake = { + VERSION: "0.0" +}; + +pmake.inherit = function(base){ + const func = function(){}; + + Object.setPrototypeOf(func.prototype, base.prototype); + + return func; +}; + +pmake.inherited = function(base, parent){ + var proto = base.prototype; + + while(proto){ + if(proto == parent.prototype) return true; + + proto = Object.getPrototypeOf(proto); + } + + return false; +} + +pmake.register = function(base){ + if(pmake.inherited(base, Compiler)){ + } +} + +function Compiler(){ +} diff --git a/src/main.c b/src/main.c index dd7dd44..a50818c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,9 @@ #include "pmake.h" int main(int argc, char** argv){ + int st; + + if((st = js_init(argc, argv)) != 0) return st; + + return 0; } diff --git a/src/pmake.h b/src/pmake.h index 4759edd..78e5347 100644 --- a/src/pmake.h +++ b/src/pmake.h @@ -1,6 +1,8 @@ #ifndef __PMAKE_H__ #define __PMAKE_H__ +#include "config.h" + #include #include #include @@ -8,4 +10,17 @@ #include #include +#define LOAD(name) \ + extern unsigned char name[]; \ + extern unsigned int name ## _len; + +#define X(name) LOAD(compiler_ ## name) +COMPILERS; +#undef X + +/* js.c */ +extern duk_context* js_context; +int js_init(int argc, char** argv); +void js_uninit(void); + #endif