mapgen/ugl

This commit is contained in:
olikraus 2017-07-09 17:37:16 +02:00
parent a47835a895
commit f883f4306d
10 changed files with 161 additions and 47 deletions

View File

@ -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 = $(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) OBJ = $(SRC:.c=.o)
@ -8,7 +12,7 @@ map.c: mapgen gm.map
./mapgen -o map.c gm.map ./mapgen -o map.c gm.map
mapgen: $(OBJ) mapgen: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) `sdl-config --libs` -o mapgen $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o mapgen
clean: clean:
-rm $(OBJ) mapgen map.c -rm $(OBJ) mapgen map.c

View File

@ -144,6 +144,12 @@ thing 'h $9c # chest
thing 'H $92 # hut thing 'H $92 # hut
thing 'K $93 # kingdom thing 'K $93 # kingdom
iteminit normal_door
print(add(1,2))
print(add(3,4))
print(add(1234,5678))
endproc
map test 20 9 map test 20 9
: K H : K H
: . . : . .

View File

@ -26,6 +26,7 @@
#include <unistd.h> #include <unistd.h>
#include "u8g2.h" #include "u8g2.h"
#include "ugl.h"
extern void u8g2_SetupBuffer_TGA(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb); extern void u8g2_SetupBuffer_TGA(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb);
extern void tga_save(const char *name); extern void tga_save(const char *name);
@ -662,9 +663,22 @@ int map_read_map_cmd(const char **s)
return 1; return 1;
} }
int is_inside_proc;
int is_inside_map;
int map_read_line(const char **s) int map_read_line(const char **s)
{ {
const char *id; const char *id;
if ( is_inside_proc != 0 )
{
if ( uglReadLine(s) == 0 )
is_inside_proc = 0;
return 1;
}
skip_space(s); skip_space(s);
if ( **s == '#' ) /* comment (hmm handled by skip_space) */ 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); 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 ) 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 ) else if ( strcmp(id, "endmap") == 0 )
{ {
@ -706,11 +730,12 @@ int map_read_line(const char **s)
write_map_struct(); write_map_struct();
} }
} }
is_inside_map = 0;
return 1; return 1;
} }
else 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; return 1;
@ -719,20 +744,35 @@ int map_read_line(const char **s)
int map_read_fp(void) int map_read_fp(void)
{ {
const char *s; 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(;;) for(;;)
{ {
if ( fgets(map_line, MAP_LINE_MAX, map_fp) == NULL ) if ( fgets(map_line, MAP_LINE_MAX, map_fp) == NULL )
break; break;
s = &(map_line[0]); s = &(map_line[0]);
if ( map_read_line(&s) == 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; return 0;
}
ugl_current_input_line++;
} }
ugl_ResolveSymbols();
return 1; return 1;
} }
int map_read_filename(const char *name, int phase) int map_read_filename(const char *name, int phase)
{ {
map_phase = phase; map_phase = phase;
map_fp = fopen(name, "r"); map_fp = fopen(name, "r");
if ( map_fp == NULL ) if ( map_fp == NULL )
return 0; return 0;
@ -792,6 +832,10 @@ int main(int argc, char **argv)
fprintf(out_fp, "\n"); fprintf(out_fp, "\n");
} }
map_read_filename(filename, PHASE_MAPDATA); map_read_filename(filename, PHASE_MAPDATA);
ugl_WriteBytecodeCArray(out_fp, "map_code");
if ( out_fp != NULL ) if ( out_fp != NULL )
{ {
fprintf(out_fp, "map_t map_list[] = {\n"); fprintf(out_fp, "map_t map_list[] = {\n");

View File

@ -10,12 +10,15 @@
#include <stdint.h> #include <stdint.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#define UGL_MAX_IDENTIFIER_LEN 63 #define UGL_MAX_IDENTIFIER_LEN 63
/* ugl_error.c */ /* 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_err(const char *fmt, ...) __attribute__ ((noreturn));
void ugl_plog(const char *fmt, ...); void ugl_plog(const char *fmt, ...);
void ugl_glog(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_AddBytecode(uint8_t x);
void ugl_ExecBytecode(void); void ugl_ExecBytecode(void);
void ugl_ResolveSymbols(void); void ugl_ResolveSymbols(void);
void ugl_WriteBytecodeCArray(FILE *fp, const char *name);
int ugl_GetLabel(const char *name); int ugl_GetLabel(const char *name);
void ugl_SetLabelBytecodePos(const char *name, uint16_t bytecode_pos); void ugl_SetLabelBytecodePos(const char *name, uint16_t bytecode_pos);
@ -36,9 +41,14 @@ uint16_t ugl_GetLabelBytecodePos(int idx);
/* ugl_parse.c */ /* ugl_parse.c */
extern int ugl_current_input_line;
uint16_t uglStartNamelessProc(int args);
int ugl_read_line(const char **s); int ugl_read_line(const char **s);
int uglReadLine(const char **s);
/* ugl_main.c */
int ugl_read_fp(void); int ugl_read_fp(void);
int ugl_read_filename(const char *name); int ugl_read_filename(const char *name);

View File

@ -3,6 +3,7 @@
#include "ugl.h" #include "ugl.h"
#include "ugl_bc.h" #include "ugl_bc.h"
#include <string.h> #include <string.h>
#include <stdio.h>
/* arrays & variables */ /* arrays & variables */
@ -32,7 +33,7 @@ void ugl_AddBytecode(uint8_t x)
void ugl_ExecBytecode(void) void ugl_ExecBytecode(void)
{ {
bc_t bc; 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]; 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");
}

View File

@ -86,14 +86,14 @@ void bc_init(bc_t *bc)
#define BC_DBG_OUT_NUM3(n) printf("%03d ", (int)(n)) #define BC_DBG_OUT_NUM3(n) printf("%03d ", (int)(n))
#define BC_DBG_OUT_CR() printf("\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; uint16_t val;
uint8_t cmd; uint8_t cmd;
bc_init(bc); bc_init(bc);
bc->code = code; bc->code = code;
bc->code_pos = 0; bc->code_pos = pos;
for(;;) for(;;)
{ {

View File

@ -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);
/*======================================================*/ /*======================================================*/

View File

@ -4,6 +4,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int ugl_current_input_line;
int ugl_is_suppress_log = 0;
void ugl_err(const char *fmt, ...) void ugl_err(const char *fmt, ...)
{ {
va_list va; va_list va;
@ -17,6 +21,8 @@ void ugl_err(const char *fmt, ...)
void ugl_plog(const char *fmt, ...) void ugl_plog(const char *fmt, ...)
{ {
va_list va; va_list va;
if ( ugl_is_suppress_log != 0 )
return;
va_start(va, fmt); va_start(va, fmt);
printf("%d: ", ugl_current_input_line); printf("%d: ", ugl_current_input_line);
vprintf(fmt, va); vprintf(fmt, va);
@ -28,6 +34,8 @@ void ugl_plog(const char *fmt, ...)
void ugl_glog(const char *fmt, ...) void ugl_glog(const char *fmt, ...)
{ {
va_list va; va_list va;
if ( ugl_is_suppress_log != 0 )
return;
va_start(va, fmt); va_start(va, fmt);
vprintf(fmt, va); vprintf(fmt, va);
printf("\n"); printf("\n");

View File

@ -1,6 +1,40 @@
#include "ugl.h" #include "ugl.h"
#include <stdio.h>
#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) int main(void)
{ {
@ -8,5 +42,6 @@ int main(void)
ugl_read_filename("test.ugl"); ugl_read_filename("test.ugl");
ugl_ResolveSymbols(); ugl_ResolveSymbols();
ugl_ExecBytecode(); ugl_ExecBytecode();
ugl_WriteBytecodeCArray(stdout, "code");
} }

View File

@ -28,12 +28,8 @@
#include "ugl_bc.h" #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_local_variables = 0;
long ugl_current_args = 0; long ugl_current_args = 0;
char ugl_input_line[UGL_MAX_INPUT_LINE_LEN];
#define UGL_MAX_INDENT 64 #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) int ugl_read_line(const char **s)
{ {
const char *id; const char *id;
@ -455,15 +461,13 @@ int ugl_read_line(const char **s)
{ {
const char *name = get_identifier(s); const char *name = get_identifier(s);
long args = get_num(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 ) else if ( strcmp(id, "endproc") == 0 )
{ {
@ -512,30 +516,11 @@ int ugl_read_line(const char **s)
return 1; return 1;
} }
int ugl_read_fp(void) /* returns 0 if "endproc" is found */
int uglReadLine(const char **s)
{ {
const char *s; ugl_read_line(s);
ugl_current_input_line = 0; if ( ugl_indent_level == 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; return 0;
printf("file '%s'\n", name); return 1;
if ( ugl_read_fp() == 0 )
return fclose(ugl_input_fp), 0;
fclose(ugl_input_fp);
return 1;
} }