mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-30 23:23:07 +00:00 
			
		
		
		
	Make compilation simpler.
This commit is contained in:
		| @@ -83,14 +83,10 @@ static char *gst_getline() { | ||||
|  | ||||
| /* Compile and run an ast */ | ||||
| static int debug_compile_and_run(Gst *vm, GstValue ast, int64_t flags) { | ||||
|     GstCompiler c; | ||||
|     GstValue func; | ||||
|     /* Try to compile generated AST */ | ||||
|     gst_compiler(&c, vm); | ||||
|     func = gst_wrap_function(gst_compiler_compile(&c, ast)); | ||||
|     GstValue func = gst_compile(vm, vm->env, ast); | ||||
|     /* Check for compilation errors */ | ||||
|     if (c.error.type != GST_NIL) { | ||||
|         printf_flags(flags, "31", "compiler error: %s\n", (const char *)gst_to_string(vm, c.error)); | ||||
|     if (func.type != GST_FUNCTION) { | ||||
|         printf_flags(flags, "31", "compiler error: %s\n", (const char *)gst_to_string(vm, func)); | ||||
|         return 1; | ||||
|     } | ||||
|     /* Execute function */ | ||||
|   | ||||
| @@ -24,9 +24,25 @@ | ||||
|  | ||||
| #define GST_LOCAL_FLAG_MUTABLE 1 | ||||
|  | ||||
| /* Compiler typedefs */ | ||||
| typedef struct GstCompiler GstCompiler; | ||||
| typedef struct FormOptions FormOptions; | ||||
| typedef struct SlotTracker SlotTracker; | ||||
| typedef struct GstScope GstScope; | ||||
|  | ||||
| /* Compilation state */ | ||||
| struct GstCompiler { | ||||
|     Gst *vm; | ||||
|     GstValue error; | ||||
|     jmp_buf onError; | ||||
|     GstScope *tail; | ||||
|     GstBuffer *buffer; | ||||
|     GstTable *env; | ||||
|     int recursionGuard; | ||||
| }; | ||||
|  | ||||
| /* During compilation, FormOptions are passed to ASTs | ||||
|  * as configuration options to allow for some optimizations. */ | ||||
| typedef struct FormOptions FormOptions; | ||||
| struct FormOptions { | ||||
|     /* The location the returned Slot must be in. Can be ignored | ||||
|      * if either canDrop or canChoose is true */ | ||||
| @@ -68,7 +84,6 @@ struct Slot { | ||||
|  | ||||
| /* A SlotTracker provides a handy way to keep track of | ||||
|  * Slots on the stack and free them in bulk. */ | ||||
| typedef struct SlotTracker SlotTracker; | ||||
| struct SlotTracker { | ||||
|     Slot *slots; | ||||
|     uint32_t count; | ||||
| @@ -1293,43 +1308,36 @@ static Slot compile_value(GstCompiler *c, FormOptions opts, GstValue x) { | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| /* Initialize a GstCompiler struct */ | ||||
| void gst_compiler(GstCompiler *c, Gst *vm) { | ||||
|     c->vm = vm; | ||||
|     c->buffer = gst_buffer(vm, 128); | ||||
|     c->tail = NULL; | ||||
|     c->error.type = GST_NIL; | ||||
|     c->env = vm->env; | ||||
|     c->recursionGuard = GST_RECURSION_GUARD; | ||||
|     compiler_push_scope(c, 0); | ||||
| } | ||||
|  | ||||
| /* Compile interface. Returns a function that evaluates the | ||||
|  * given AST. Returns NULL if there was an error during compilation. */ | ||||
| GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) { | ||||
| GstValue gst_compile(Gst *vm, GstTable *env, GstValue form) { | ||||
|     GstCompiler c; | ||||
|     FormOptions opts = form_options_default(); | ||||
|     c->recursionGuard = GST_RECURSION_GUARD; | ||||
|     GstFuncDef *def; | ||||
|     if (setjmp(c->onError)) { | ||||
|         /* Clear all but root scope */ | ||||
|         if (c->tail) | ||||
|             c->tail->parent = NULL; | ||||
|         if (c->error.type == GST_NIL) | ||||
|             c->error = gst_string_cv(c->vm, "unknown error"); | ||||
|         return NULL; | ||||
|     if (setjmp(c.onError)) { | ||||
|         if (c.error.type == GST_NIL) | ||||
|             return gst_string_cv(vm, "unknown error"); | ||||
|         return c.error; | ||||
|     } | ||||
|     c.error.type = GST_NIL; | ||||
|     c.env = env; | ||||
|     c.vm = vm; | ||||
|     c.tail = NULL; | ||||
|     c.buffer = gst_buffer(vm, 24); | ||||
|     c.recursionGuard = GST_RECURSION_GUARD; | ||||
|     compiler_push_scope(&c, 0); | ||||
|     opts.isTail = 1; | ||||
|     compiler_return(c, compile_value(c, opts, form)); | ||||
|     def = compiler_gen_funcdef(c, c->buffer->count, 0, 0); | ||||
|     compiler_return(&c, compile_value(&c, opts, form)); | ||||
|     def = compiler_gen_funcdef(&c, c.buffer->count, 0, 0); | ||||
|     { | ||||
|         GstFuncEnv *env = gst_alloc(c->vm, sizeof(GstFuncEnv)); | ||||
|         GstFunction *func = gst_alloc(c->vm, sizeof(GstFunction)); | ||||
|         GstFuncEnv *env = gst_alloc(vm, sizeof(GstFuncEnv)); | ||||
|         GstFunction *func = gst_alloc(vm, sizeof(GstFunction)); | ||||
|         env->values = NULL; | ||||
|         env->stackOffset = 0; | ||||
|         env->thread = NULL; | ||||
|         func->parent = NULL; | ||||
|         func->def = def; | ||||
|         func->env = env; | ||||
|         return func; | ||||
|         return gst_wrap_function(func); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								core/stl.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								core/stl.c
									
									
									
									
									
								
							| @@ -994,16 +994,11 @@ static int gst_stl_parse(Gst *vm) { | ||||
|  | ||||
| /* Compile a value */ | ||||
| static int gst_stl_compile(Gst *vm) { | ||||
|     GstFunction *ret; | ||||
|     GstCompiler c; | ||||
|     gst_compiler(&c, vm); | ||||
|     GstTable *env = vm->env; | ||||
|     if (gst_arg(vm, 1).type == GST_TABLE) { | ||||
|         c.env = gst_arg(vm, 1).data.table; | ||||
|         env = gst_arg(vm, 1).data.table; | ||||
|     } | ||||
|     ret = gst_compiler_compile(&c, gst_arg(vm, 0)); | ||||
|     if (!ret) | ||||
|         gst_c_throw(vm, c.error); | ||||
|     gst_c_return(vm, gst_wrap_function(ret)); | ||||
|     gst_c_return(vm, gst_compile(vm, env, gst_arg(vm, 0))); | ||||
| } | ||||
|  | ||||
| /****/ | ||||
|   | ||||
| @@ -169,8 +169,6 @@ typedef struct GstModuleItem GstModuleItem; | ||||
| typedef struct GstUserType GstUserType; | ||||
| typedef struct GstParser GstParser; | ||||
| typedef struct GstParseState GstParseState; | ||||
| typedef struct GstCompiler GstCompiler; | ||||
| typedef struct GstScope GstScope; | ||||
|  | ||||
| /* C Api data types */ | ||||
| struct GstModuleItem { | ||||
| @@ -374,17 +372,6 @@ struct GstParser { | ||||
|     } comment; | ||||
| }; | ||||
|  | ||||
| /* Compilation state */ | ||||
| struct GstCompiler { | ||||
|     Gst *vm; | ||||
|     GstValue error; | ||||
|     jmp_buf onError; | ||||
|     GstScope *tail; | ||||
|     GstBuffer *buffer; | ||||
|     GstTable *env; | ||||
|     int recursionGuard; | ||||
| }; | ||||
|  | ||||
| /* Bytecode */ | ||||
| enum GstOpCode { | ||||
|     GST_OP_FLS,     /* Load false */ | ||||
| @@ -548,10 +535,7 @@ GstValue gst_parse_consume(GstParser *p); | ||||
| /* Compilation */ | ||||
| /***/ | ||||
|  | ||||
| void gst_compiler(GstCompiler *c, Gst *vm); | ||||
| void gst_compiler_mergeenv(GstCompiler *c, GstValue env); | ||||
| void gst_compiler_global(GstCompiler *c, const char *name, GstValue x); | ||||
| GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form); | ||||
| GstValue gst_compile(Gst *vm, GstTable *env, GstValue form); | ||||
|  | ||||
| /****/ | ||||
| /* GC */ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 bakpakin
					bakpakin