diff --git a/sys/tga/mapgen/Makefile b/sys/tga/mapgen/Makefile index 4245b158..5d878c17 100644 --- a/sys/tga/mapgen/Makefile +++ b/sys/tga/mapgen/Makefile @@ -1,6 +1,10 @@ -CFLAGS = -g -Wall -I../../../csrc/. `sdl-config --cflags` -DU8G2_16BIT +CFLAGS = -g -Wall -I../../../csrc/. -I../../../tools/ugl/. -DU8G2_16BIT SRC = $(shell ls ../../../csrc/*.c) mapgen.c u8g2_d_tga.c +SRC += ../../../tools/ugl/ugl_arrays.c +SRC += ../../../tools/ugl/ugl_error.c +SRC += ../../../tools/ugl/ugl_parse.c +SRC += ../../../tools/ugl/ugl_bc.c OBJ = $(SRC:.c=.o) @@ -8,7 +12,7 @@ map.c: mapgen gm.map ./mapgen -o map.c gm.map mapgen: $(OBJ) - $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl-config --libs` -o mapgen + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o mapgen clean: -rm $(OBJ) mapgen map.c diff --git a/sys/tga/mapgen/gm.map b/sys/tga/mapgen/gm.map index 2126388f..be45de14 100644 --- a/sys/tga/mapgen/gm.map +++ b/sys/tga/mapgen/gm.map @@ -144,6 +144,12 @@ thing 'h $9c # chest thing 'H $92 # hut thing 'K $93 # kingdom +iteminit normal_door + print(add(1,2)) + print(add(3,4)) + print(add(1234,5678)) +endproc + map test 20 9 : K H : . . diff --git a/sys/tga/mapgen/mapgen.c b/sys/tga/mapgen/mapgen.c index 22762e37..18bd460a 100644 --- a/sys/tga/mapgen/mapgen.c +++ b/sys/tga/mapgen/mapgen.c @@ -26,6 +26,7 @@ #include #include "u8g2.h" +#include "ugl.h" extern void u8g2_SetupBuffer_TGA(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb); extern void tga_save(const char *name); @@ -662,9 +663,22 @@ int map_read_map_cmd(const char **s) return 1; } + +int is_inside_proc; +int is_inside_map; + int map_read_line(const char **s) { const char *id; + + if ( is_inside_proc != 0 ) + { + if ( uglReadLine(s) == 0 ) + is_inside_proc = 0; + return 1; + } + + skip_space(s); if ( **s == '#' ) /* comment (hmm handled by skip_space) */ @@ -688,9 +702,19 @@ int map_read_line(const char **s) { return map_read_tile(s, 1); } + else if ( strcmp(id, "iteminit") == 0 ) + { + const char *id; + uint16_t code_pos; + id = get_identifier(s); + code_pos = uglStartNamelessProc(0); + is_inside_proc = 1; + return 1; + } else if ( strcmp(id, "map") == 0 ) { - return map_read_map_cmd(s); + is_inside_map = 1; + return map_read_map_cmd(s); } else if ( strcmp(id, "endmap") == 0 ) { @@ -706,11 +730,12 @@ int map_read_line(const char **s) write_map_struct(); } } + is_inside_map = 0; return 1; } else { - printf("line %d: unkown command '%s'\n", map_curr_line, id); + printf("code line %d, map line %d: unkown command '%s'\n", ugl_current_input_line, map_curr_line, id); } return 1; @@ -719,20 +744,35 @@ int map_read_line(const char **s) int map_read_fp(void) { const char *s; + ugl_InitBytecode(); + if ( map_phase == PHASE_MAPDATA ) + ugl_is_suppress_log = 0; + if ( map_phase == PHASE_MAPSTRUCT ) + ugl_is_suppress_log = 1; + + ugl_current_input_line = 1; for(;;) { if ( fgets(map_line, MAP_LINE_MAX, map_fp) == NULL ) break; s = &(map_line[0]); if ( map_read_line(&s) == 0 ) + { + if ( is_inside_proc ) + printf("endproc missing\n"); + if ( is_inside_map ) + printf("endmap missing\n"); return 0; + } + ugl_current_input_line++; } + ugl_ResolveSymbols(); return 1; } int map_read_filename(const char *name, int phase) { - map_phase = phase; + map_phase = phase; map_fp = fopen(name, "r"); if ( map_fp == NULL ) return 0; @@ -792,6 +832,10 @@ int main(int argc, char **argv) fprintf(out_fp, "\n"); } map_read_filename(filename, PHASE_MAPDATA); + + ugl_WriteBytecodeCArray(out_fp, "map_code"); + + if ( out_fp != NULL ) { fprintf(out_fp, "map_t map_list[] = {\n"); diff --git a/tools/ugl/ugl.h b/tools/ugl/ugl.h index 203e2702..63c1a56c 100644 --- a/tools/ugl/ugl.h +++ b/tools/ugl/ugl.h @@ -10,12 +10,15 @@ #include #include +#include #define UGL_MAX_IDENTIFIER_LEN 63 /* ugl_error.c */ +extern int ugl_current_input_line; +extern int ugl_is_suppress_log; void ugl_err(const char *fmt, ...) __attribute__ ((noreturn)); void ugl_plog(const char *fmt, ...); void ugl_glog(const char *fmt, ...); @@ -29,6 +32,8 @@ void ugl_InitBytecode(void); void ugl_AddBytecode(uint8_t x); void ugl_ExecBytecode(void); void ugl_ResolveSymbols(void); +void ugl_WriteBytecodeCArray(FILE *fp, const char *name); + int ugl_GetLabel(const char *name); void ugl_SetLabelBytecodePos(const char *name, uint16_t bytecode_pos); @@ -36,9 +41,14 @@ uint16_t ugl_GetLabelBytecodePos(int idx); /* ugl_parse.c */ -extern int ugl_current_input_line; +uint16_t uglStartNamelessProc(int args); int ugl_read_line(const char **s); +int uglReadLine(const char **s); + + +/* ugl_main.c */ + int ugl_read_fp(void); int ugl_read_filename(const char *name); diff --git a/tools/ugl/ugl_arrays.c b/tools/ugl/ugl_arrays.c index 109ff385..f8abf9b9 100644 --- a/tools/ugl/ugl_arrays.c +++ b/tools/ugl/ugl_arrays.c @@ -3,6 +3,7 @@ #include "ugl.h" #include "ugl_bc.h" #include +#include /* arrays & variables */ @@ -32,7 +33,7 @@ void ugl_AddBytecode(uint8_t x) void ugl_ExecBytecode(void) { bc_t bc; - bc_exec(&bc, ugl_bytecode_array); + bc_exec(&bc, ugl_bytecode_array, 0); } @@ -219,3 +220,24 @@ uint16_t ugl_GetLabelBytecodePos(int idx) return ugl_label_bytecode_pos[idx]; } +void ugl_WriteBytecodeCArray(FILE *fp, const char *name) +{ + uint16_t i; + fprintf(fp, "unsigned char %s[] =\n \"", name); + + i = 0; + while ( i < ugl_bytecode_len ) + { + fprintf(fp, "\\x%02x", ugl_bytecode_array[i]); + if ( i+1 == ugl_bytecode_len ) + { + break; + } + if ( (i & 0x0f) == 0x0f ) + { + fprintf(fp, "\"\n \""); + } + i++; + } + fprintf(fp, "\";\n\n"); +} \ No newline at end of file diff --git a/tools/ugl/ugl_bc.c b/tools/ugl/ugl_bc.c index e97d31e2..8c28ad38 100644 --- a/tools/ugl/ugl_bc.c +++ b/tools/ugl/ugl_bc.c @@ -86,14 +86,14 @@ void bc_init(bc_t *bc) #define BC_DBG_OUT_NUM3(n) printf("%03d ", (int)(n)) #define BC_DBG_OUT_CR() printf("\n") -void bc_exec(bc_t *bc, uint8_t *code) +void bc_exec(bc_t *bc, uint8_t *code, uint16_t pos) { uint16_t val; uint8_t cmd; bc_init(bc); bc->code = code; - bc->code_pos = 0; + bc->code_pos = pos; for(;;) { diff --git a/tools/ugl/ugl_bc.h b/tools/ugl/ugl_bc.h index ffced8ba..23808eef 100644 --- a/tools/ugl/ugl_bc.h +++ b/tools/ugl/ugl_bc.h @@ -76,7 +76,7 @@ extern bc_buildin_fn bc_buildin_list[]; -void bc_exec(bc_t *bc, uint8_t *code); +void bc_exec(bc_t *bc, uint8_t *code, uint16_t pos); /*======================================================*/ diff --git a/tools/ugl/ugl_error.c b/tools/ugl/ugl_error.c index 48dd2358..ca4ecea9 100644 --- a/tools/ugl/ugl_error.c +++ b/tools/ugl/ugl_error.c @@ -4,6 +4,10 @@ #include #include +int ugl_current_input_line; +int ugl_is_suppress_log = 0; + + void ugl_err(const char *fmt, ...) { va_list va; @@ -17,6 +21,8 @@ void ugl_err(const char *fmt, ...) void ugl_plog(const char *fmt, ...) { va_list va; + if ( ugl_is_suppress_log != 0 ) + return; va_start(va, fmt); printf("%d: ", ugl_current_input_line); vprintf(fmt, va); @@ -28,6 +34,8 @@ void ugl_plog(const char *fmt, ...) void ugl_glog(const char *fmt, ...) { va_list va; + if ( ugl_is_suppress_log != 0 ) + return; va_start(va, fmt); vprintf(fmt, va); printf("\n"); diff --git a/tools/ugl/ugl_main.c b/tools/ugl/ugl_main.c index b88572b5..8d47a0a3 100644 --- a/tools/ugl/ugl_main.c +++ b/tools/ugl/ugl_main.c @@ -1,6 +1,40 @@ #include "ugl.h" +#include + +#define UGL_MAX_INPUT_LINE_LEN 1024 +FILE *ugl_input_fp; +char ugl_input_line[UGL_MAX_INPUT_LINE_LEN]; + + +int ugl_read_fp(void) +{ + const char *s; + ugl_current_input_line = 0; + for(;;) + { + if ( fgets(ugl_input_line, UGL_MAX_INPUT_LINE_LEN, ugl_input_fp) == NULL ) + break; + ugl_current_input_line++; + s = &(ugl_input_line[0]); + if ( ugl_read_line(&s) == 0 ) + return 0; + } + return 1; +} + +int ugl_read_filename(const char *name) +{ + ugl_input_fp = fopen(name, "r"); + if ( ugl_input_fp == NULL ) + return 0; + printf("file '%s'\n", name); + if ( ugl_read_fp() == 0 ) + return fclose(ugl_input_fp), 0; + fclose(ugl_input_fp); + return 1; +} int main(void) { @@ -8,5 +42,6 @@ int main(void) ugl_read_filename("test.ugl"); ugl_ResolveSymbols(); ugl_ExecBytecode(); - + ugl_WriteBytecodeCArray(stdout, "code"); + } \ No newline at end of file diff --git a/tools/ugl/ugl_parse.c b/tools/ugl/ugl_parse.c index f94d70ec..6416fa8d 100644 --- a/tools/ugl/ugl_parse.c +++ b/tools/ugl/ugl_parse.c @@ -28,12 +28,8 @@ #include "ugl_bc.h" -#define UGL_MAX_INPUT_LINE_LEN 1024 -FILE *ugl_input_fp; -int ugl_current_input_line; long ugl_current_local_variables = 0; long ugl_current_args = 0; -char ugl_input_line[UGL_MAX_INPUT_LINE_LEN]; #define UGL_MAX_INDENT 64 @@ -440,6 +436,16 @@ void ugl_parse_proc(const char **s, const char *id, int is_toplevel) } } +uint16_t uglStartNamelessProc(int args) +{ + ugl_current_local_variables = 0; + ugl_current_args = args; + if ( ugl_indent_level != 0 ) + ugl_err("nested procedures not allowed"); + ugl_IncIndent(UGL_INDENT_TYPE_PROC); + return ugl_bytecode_len; +} + int ugl_read_line(const char **s) { const char *id; @@ -455,15 +461,13 @@ int ugl_read_line(const char **s) { const char *name = get_identifier(s); long args = get_num(s); - ugl_current_local_variables = 0; - ugl_current_args = args; - ugl_plog("start procedure '%s' (args=%ld)", name, args); - if ( ugl_indent_level != 0 ) - ugl_err("nested procedures not allowed"); - ugl_GetLabel(name); /* create a label for the procedure name */ - ugl_SetLabelBytecodePos(name, ugl_bytecode_len); /* and set the label for it */ - ugl_IncIndent(UGL_INDENT_TYPE_PROC); + ugl_plog("start procedure '%s' (args=%ld)", name, args); + + ugl_GetLabel(name); /* create a label for the procedure name */ + ugl_SetLabelBytecodePos(name, uglStartNamelessProc(args)); /* and set the label for it */ + + } else if ( strcmp(id, "endproc") == 0 ) { @@ -512,30 +516,11 @@ int ugl_read_line(const char **s) return 1; } -int ugl_read_fp(void) +/* returns 0 if "endproc" is found */ +int uglReadLine(const char **s) { - const char *s; - ugl_current_input_line = 0; - for(;;) - { - if ( fgets(ugl_input_line, UGL_MAX_INPUT_LINE_LEN, ugl_input_fp) == NULL ) - break; - ugl_current_input_line++; - s = &(ugl_input_line[0]); - if ( ugl_read_line(&s) == 0 ) - return 0; - } - return 1; -} - -int ugl_read_filename(const char *name) -{ - ugl_input_fp = fopen(name, "r"); - if ( ugl_input_fp == NULL ) + ugl_read_line(s); + if ( ugl_indent_level == 0 ) return 0; - printf("file '%s'\n", name); - if ( ugl_read_fp() == 0 ) - return fclose(ugl_input_fp), 0; - fclose(ugl_input_fp); - return 1; + return 1; }