mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +00:00 
			
		
		
		
	More work on compiler. Add compiler unit test that currently
segfaults alot. Added dst_disasm to reconstruct dsts assembly from a funcdef.
This commit is contained in:
		| @@ -7,9 +7,7 @@ int main() { | ||||
|     DstAssembleResult ares; | ||||
|     DstFunction *func; | ||||
|  | ||||
|     printf("sizeof(DstValue) = %lu\n", sizeof(DstValue)); | ||||
|  | ||||
|     FILE *f = fopen("./dsts/minimal.dsts", "rb"); | ||||
|     FILE *f = fopen("./dsttest/minimal.dsts", "rb"); | ||||
|     fseek(f, 0, SEEK_END); | ||||
|     long fsize = ftell(f); | ||||
|     fseek(f, 0, SEEK_SET);  //same as rewind(f); | ||||
| @@ -26,25 +24,27 @@ int main() { | ||||
|     free(string); | ||||
|  | ||||
|     if (pres.status == DST_PARSE_ERROR) { | ||||
|         dst_puts(dst_formatc("parse error at %d: %S\n", pres.bytes_read, pres.result.error)); | ||||
|         dst_puts(dst_formatc("parse error at %d: %S\n", pres.bytes_read, pres.error)); | ||||
|         return 1; | ||||
|     } | ||||
|     assert(pres.status == DST_PARSE_OK); | ||||
|     dst_puts(dst_formatc("\nparse result: %v\n\n", pres.result.value)); | ||||
|     dst_puts(dst_formatc("\nparse result: %v\n\n", pres.value)); | ||||
|  | ||||
|     opts.flags = 0; | ||||
|     opts.source = pres.result.value; | ||||
|     opts.source = pres.value; | ||||
|     opts.sourcemap = pres.map; | ||||
|  | ||||
|     ares = dst_asm(opts); | ||||
|     if (ares.status == DST_ASSEMBLE_ERROR) { | ||||
|         dst_puts(dst_formatc("assembly error: %S\n", ares.result.error)); | ||||
|         dst_puts(dst_formatc("assembly error: %S\n", ares.error)); | ||||
|         dst_puts(dst_formatc("error location: %d, %d\n", ares.error_start, ares.error_end)); | ||||
|         return 1; | ||||
|     } | ||||
|     assert(ares.status == DST_ASSEMBLE_OK); | ||||
|  | ||||
|     func = dst_asm_func(ares); | ||||
|  | ||||
|     dst_puts(dst_formatc("\nfuncdef: %v\n\n", dst_disasm(ares.funcdef))); | ||||
|      | ||||
|     dst_run(dst_wrap_function(func)); | ||||
|     dst_puts(dst_formatc("result: %v\n", dst_vm_fiber->ret)); | ||||
|   | ||||
							
								
								
									
										55
									
								
								unittests/compile_test.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								unittests/compile_test.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| #include "unit.h" | ||||
| #include <dst/dst.h> | ||||
|  | ||||
| int main() { | ||||
|     DstParseResult pres; | ||||
|     DstCompileOptions opts; | ||||
|     DstCompileResults cres; | ||||
|     DstFunction *func; | ||||
|  | ||||
|     FILE *f = fopen("./dsttest/basic.dst", "rb"); | ||||
|     fseek(f, 0, SEEK_END); | ||||
|     long fsize = ftell(f); | ||||
|     fseek(f, 0, SEEK_SET);  //same as rewind(f); | ||||
|  | ||||
|     char *string = malloc(fsize + 1); | ||||
|     fread(string, fsize, 1, f); | ||||
|     fclose(f); | ||||
|  | ||||
|     string[fsize] = 0; | ||||
|  | ||||
|     dst_init(); | ||||
|  | ||||
|     pres = dst_parsec(string); | ||||
|     free(string); | ||||
|  | ||||
|     if (pres.status == DST_PARSE_ERROR) { | ||||
|         dst_puts(dst_formatc("parse error at %d: %S\n", pres.bytes_read, pres.error)); | ||||
|         return 1; | ||||
|     } | ||||
|     assert(pres.status == DST_PARSE_OK); | ||||
|     dst_puts(dst_formatc("\nparse result: %v\n\n", pres.value)); | ||||
|  | ||||
|     opts.flags = 0; | ||||
|     opts.source = pres.value; | ||||
|     opts.sourcemap = pres.map; | ||||
|  | ||||
|     cres = dst_compile(opts); | ||||
|     if (cres.status == DST_COMPILE_ERROR) { | ||||
|         dst_puts(dst_formatc("compilation error: %S\n", cres.error)); | ||||
|         dst_puts(dst_formatc("error location: %d, %d\n", cres.error_start, cres.error_end)); | ||||
|         return 1; | ||||
|     } | ||||
|     assert(cres.status == DST_COMPILE_OK); | ||||
|  | ||||
|     dst_puts(dst_formatc("\nfuncdef: %v\n\n", dst_disasm(cres.funcdef))); | ||||
|      | ||||
|     func = dst_compile_func(cres); | ||||
|      | ||||
|     dst_run(dst_wrap_function(func)); | ||||
|     dst_puts(dst_formatc("result: %v\n", dst_vm_fiber->ret)); | ||||
|  | ||||
|     dst_deinit(); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| @@ -10,9 +10,9 @@ int main() { | ||||
|     pres = dst_parsec("'(+ 1 () [] 3 5 :hello \"hi\\h41\")"); | ||||
|  | ||||
|     assert(pres.status == DST_PARSE_OK); | ||||
|     assert(dst_checktype(pres.result.value, DST_TUPLE)); | ||||
|     assert(dst_checktype(pres.value, DST_TUPLE)); | ||||
|  | ||||
|     str = dst_to_string(pres.result.value); | ||||
|     str = dst_to_string(pres.value); | ||||
|     printf("%.*s\n", dst_string_length(str), (const char *) str); | ||||
|  | ||||
|     return 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 bakpakin
					bakpakin