Skip to content

Commit

Permalink
Merge branch 'thanm-new-spec-format' into anm-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
DankRank committed Aug 15, 2022
2 parents 1f134fa + 3adc7a9 commit 1b9a005
Show file tree
Hide file tree
Showing 21 changed files with 3,704 additions and 330 deletions.
11 changes: 8 additions & 3 deletions thanm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
include_directories(${PNG_INCLUDE_DIRS})
find_package(BISON REQUIRED)
find_package(FLEX REQUIRED)
bison_target(AnmParse anmparse.y ${CMAKE_CURRENT_BINARY_DIR}/anmparse.c COMPILE_FLAGS ${BISON_FLAGS})
flex_target(AnmScan anmscan.l ${CMAKE_CURRENT_BINARY_DIR}/anmscan.c)
include_directories(${PNG_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
add_executable(thanm
thanm.c image.c
thanm.h image.h
${BISON_AnmParse_OUTPUT_SOURCE} ${FLEX_AnmScan_OUTPUTS}
thanm.c image.c anmmap.c reg.c expr.c
thanm.h image.h anmmap.h reg.h expr.h
)
target_link_libraries(thanm util ${PNG_LIBRARY})
link_setargv(thanm)
Expand Down
181 changes: 181 additions & 0 deletions thanm/anmmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain this list
* of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce this
* list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include "anmmap.h"
#include "program.h"
#include "util.h"

anmmap_t*
anmmap_new() {
anmmap_t* map = (anmmap_t*)malloc(sizeof(anmmap_t));
if (map) {
map->ins_names = seqmap_new();
map->gvar_names = seqmap_new();
map->gvar_types = seqmap_new();
}
return map;
}

void
anmmap_free(
anmmap_t* map
) {
if (map) {
seqmap_free(map->ins_names);
seqmap_free(map->gvar_names);
seqmap_free(map->gvar_types);
free(map);
}
}

typedef struct state_t {
anmmap_t* amap;
seqmap_t* smap;
int ident;
const char* fn;
} state_t;

static int
control(
state_t* state,
int linenum,
const char* cline
) {
if (!strcmp(cline, "!ins_names")) {
state->smap = state->amap->ins_names;
state->ident = 1;
} else if (!strcmp(cline, "!gvar_names")) {
state->smap = state->amap->gvar_names;
state->ident = 1;
} else if (!strcmp(cline, "!gvar_types")) {
state->smap = state->amap->gvar_types;
state->ident = 0;
} else {
fprintf(stderr, "%s:%s:%u: unknown control line '%s'\n", argv0, state->fn, linenum, cline);
return 1;
}
return 0;
}

static int
is_keyword(
const char* value)
{
static const char* keywords[] = {
"script", "entry", "global", "int", "float",
"rand", "sin", "cos", "tan", "acos", "atan",
"timeof", "offsetof", "scriptof", "spriteof",
NULL
};
const char** kwp = keywords;
while (*kwp) {
if (!strcmp(value, *kwp++)) {
return 1;
}
}
return 0;
}

static int
validate_ident(
state_t* state,
int linenum,
const char* value)
{
const char* ptr = value;
if (ptr[0] >= '0' && ptr[0] <= '9') { /* first character must not be digit */
fprintf(stderr, "%s:%s:%u: '%s' isn't valid identifier\n", argv0, state->fn, linenum, value);
return 1;
}
while (*ptr) {
if (!(*ptr >= '0' && *ptr <= '9' || *ptr >= 'a' && *ptr <= 'z' || *ptr >= 'A' && *ptr <= 'Z' || *ptr == '_')) {
break;
}
ptr++;
}
if (*ptr) {
fprintf(stderr, "%s:%s:%u: '%s' isn't valid identifier\n", argv0, state->fn, linenum, value);
return 1;
}
if (!util_strcmp_ref(value, stringref("ins_"))) {
fprintf(stderr, "%s:%s:%u: mnemonic can't start with 'ins_'\n", argv0, state->fn, linenum);
return 1;
}
else if (is_keyword(value)) {
fprintf(stderr, "%s:%s:%u: '%s' is a keyword, ignoring\n", argv0, state->fn, linenum, value);
return 1;
}
return 0;
}

static int
validate_type(
state_t* state,
int linenum,
const char* ptr)
{
if (ptr[0] != '$' && ptr[0] != '%' || ptr[0] && ptr[1]) {
fprintf(stderr, "%s:%s:%u: unknown type '%s'\n", argv0, state->fn, linenum, ptr);
return 1;
}
return 0;
}

static int
set(
state_t* state,
int linenum,
const seqmap_entry_t* ent
) {
if (state->ident) {
if (validate_ident(state, linenum, ent->value))
return 1;
} else if (state->smap == state->amap->gvar_types) {
if (validate_type(state, linenum, ent->value))
return 1;
}
seqmap_set(state->smap, ent);
return 0;
}

void
anmmap_load(
anmmap_t* map,
FILE* file,
const char* fn
) {
state_t state;
state.amap = map;
state.fn = fn;
control(&state, 0, "!ins_names");
seqmap_load("!anmmap", &state, (seqmap_setfunc_t)set, (seqmap_controlfunc_t)control, file, fn);
}
46 changes: 46 additions & 0 deletions thanm/anmmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain this list
* of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce this
* list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef ANMMAP_H_
#define ANMMAP_H_

#include <config.h>
#include "seqmap.h"

typedef struct anmmap_t {
seqmap_t* ins_names;
seqmap_t* gvar_names;
seqmap_t* gvar_types;
} anmmap_t;

anmmap_t* anmmap_new();
void anmmap_free(anmmap_t* map);
void anmmap_load(anmmap_t* map, FILE* file, const char* fn);

#endif
Loading

0 comments on commit 1b9a005

Please sign in to comment.