Skip to content

Commit

Permalink
chore: use only string in util_dsl_keyword #421
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Dec 8, 2023
1 parent 52b074b commit 892b7a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/lang/lang_3bc_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ union ___u8_u16_t {
* @par JOKE
* why you need for CPP if C language is already 'complete and total'?
* { .keyword.name = "mode", .opcode = 7 } <-- not allowed in C++
*/
* @todo refact
const tbc_keyword_st opcodes_arr[] = { { "aloc", 2 },
{ "back", 1 }, { "call", 1 }, { "fake", 2 }, { "fcal", 2 }, { "fgto", 2 },
{ "free", 1 }, { "fret", 2 }, { "goto", 1 }, { "math", 1 }, { "micr", 3 },
Expand All @@ -39,8 +39,8 @@ const tbc_keyword_st opcodes_arr[] = { { "aloc", 2 },
{ "pull", 3 }, { "push", 5 }, { "real", 1 }, { "seco", 5 }, { "spin", 4 },
{ "strb", 1 }, { "strc", 5 }, { "stri", 3 }, { "stro", 2 }, { "strx", 4 },
{ "zcal", 3 }, { "zgto", 3 }, { "zret", 3 } };

const tbc_i8_t opcodes_size = sizeof(opcodes_arr) / sizeof(*opcodes_arr);
*/
//const tbc_i8_t opcodes_size = sizeof(opcodes_arr) / sizeof(*opcodes_arr);

static const tbc_u8_t column_size[] = {3, 9 ,12};
static const tbc_u8_t column_errors[] = {ERROR_INVALID_REGISTER,
Expand Down Expand Up @@ -147,7 +147,7 @@ void lang_3bc_compile(tbc_app_st *const self)
}
}
else if (tokens_idk[i] == 4) {
tbc_i16_t key = util_dsl_keyword(tokens[i], opcodes_arr, opcodes_size);
/*tbc_i16_t key = util_dsl_keyword(tokens[i], opcodes_arr, opcodes_size);
if (key >= 0) {
if (column_size[i] > 8) {
*cpu_r[i].u16 = opcodes_arr[key].value;
Expand All @@ -156,7 +156,7 @@ void lang_3bc_compile(tbc_app_st *const self)
}
} else {
self->cache.l1.error = ERROR_INVALID_MNEMONIC;
}
}*/
}
else {
self->cache.l1.error = column_errors[i];
Expand Down
46 changes: 31 additions & 15 deletions src/util/util_dsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdbool.h>
#include "util_dsl.h"
#include "detect/detect_cpu.h"
#include "types/types_null.h"

/**
Expand Down Expand Up @@ -237,14 +238,15 @@ tbc_i8_t util_dsl_line(char **beg, char **mid, char **end, char *src, tbc_u8_t s
* @retval 0..32_768 when found
*
*/
tbc_i16_t util_dsl_keyword(const char *const src, const tbc_keyword_st *const kl, tbc_i16_t kn)
tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_i16_t kn)
{
tbc_i16_t res = -2;
tbc_i16_t low = 0;
tbc_i16_t high = (kn - 1);
tbc_i16_t mid;
tbc_i32_t sum;
tbc_keyword_st my;
tbc_keyword_ut key;
tbc_keyword_ut *kl = (tbc_keyword_ut*) keys;

do {
if (src == NULL) {
Expand All @@ -260,27 +262,41 @@ tbc_i16_t util_dsl_keyword(const char *const src, const tbc_keyword_st *const kl
/* correct params */
res = -1;

#if defined(TBC_CPU_BYTE_SEXBE)
/* copy normal */
key.name[0] = src[0];
key.name[1] = src[1];
key.name[2] = src[2];
key.name[3] = src[3];
#elif defined(TBC_CPU_BYTE_SEXLE)
/* copy reversed */
my.key.name[0] = src[3];
my.key.name[1] = src[2];
my.key.name[2] = src[1];
my.key.name[3] = src[0];

key.name[0] = src[3];
key.name[1] = src[2];
key.name[2] = src[1];
key.name[3] = src[0];
#else
#error "[3BC] CPU sex not found."
#endif
/* force to lowercase */
my.key.compare |= 0x20202020;
key.compare |= 0x20202020;

/* binary search */
do {
/* find middle */
mid = (low + high) / 2;

/* force litle endian sub */
#if defined(TBC_CPU_BYTE_SEXBE)
/* native cpu sub */
sum = ((tbc_keyword_ut*)keys)[mid].compare - key.compare;
#else
/* force big endian sub */
sum = 0;
sum |= kl[mid].key.name[3];
sum |= kl[mid].key.name[2] << 8;
sum |= kl[mid].key.name[1] << 16;
sum |= kl[mid].key.name[0] << 24;
sum -= *(tbc_i32_t*)&my.key.compare;
sum |= ((tbc_keyword_ut*)keys)[mid].name[3];
sum |= ((tbc_keyword_ut*)keys)[mid].name[2] << 8;
sum |= ((tbc_keyword_ut*)keys)[mid].name[1] << 16;
sum |= ((tbc_keyword_ut*)keys)[mid].name[0] << 24;
sum -= key.compare;
#endif

/* found */
if (sum == 0) {
Expand All @@ -298,6 +314,6 @@ tbc_i16_t util_dsl_keyword(const char *const src, const tbc_keyword_st *const kl
} while (low <= high);
}
while(0);

return res;
}
11 changes: 3 additions & 8 deletions src/util/util_dsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@

#include "types/types_primitive.h"

typedef struct ___tbc_keyword_s tbc_keyword_st;
typedef union ___tbc_keyword_key_u tbc_keyword_ut;

union ___tbc_keyword_key_u {
char name[5];
char name[4];
tbc_i32_t compare;
};

struct ___tbc_keyword_s {
union ___tbc_keyword_key_u key;
tbc_i16_t value;
};

tbc_i8_t util_dsl_split(char** dest, tbc_u8_t* destn, char* src, tbc_u8_t dn, tbc_u8_t sn);
tbc_i8_t util_dsl_line(char **beg, char **mid, char **end, char *src, tbc_u8_t sn);
tbc_i16_t util_dsl_keyword(const char *const src, const tbc_keyword_st *const kl, tbc_i16_t kn);
tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_i16_t kn);

#endif
6 changes: 3 additions & 3 deletions tests/unit/error_driver_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main()
unsigned char stack[16] = {0};
driver_stack_init(&app, &stack, 0);
assert(app.rc == TBC_RET_THROW_ERROR);
assert(app.cache_l1.error == ERROR_MEM_STACK_CFG_MIS);
assert(app.cache.l1.error == ERROR_MEM_STACK_CFG_MIS);
}
/** Test case 2: Stack configuration with insufficient stack size */
{
Expand All @@ -24,7 +24,7 @@ int main()
unsigned char stack[4] = {0};
driver_stack_init(&app, &stack, sizeof(stack));
assert(app.rc == TBC_RET_THROW_ERROR);
assert(app.cache_l1.error == ERROR_MEM_STACK_CFG_MIN);
assert(app.cache.l1.error == ERROR_MEM_STACK_CFG_MIN);
}
/** Test case 3: Stack configuration with insufficient memory to configure all components */
{
Expand All @@ -33,6 +33,6 @@ int main()
unsigned char stack[16] = {0};
driver_stack_init(&app, &stack, sizeof(stack));
assert(app.rc == TBC_RET_THROW_ERROR);
assert(app.cache_l1.error == ERROR_MEM_STACK_CFG_OUT);
assert(app.cache.l1.error == ERROR_MEM_STACK_CFG_OUT);
}
}
7 changes: 2 additions & 5 deletions tests/unit/error_util_dsl_keyword.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
#include "types/types_null.h"
#include "util/util_dsl.h"

static tbc_keyword_st keywords[] = {
{"aldo", 2}, {"erdb", 3}, {"rona", 1}, {"socc", 2}
};

static const int kn = sizeof(keywords)/sizeof(tbc_keyword_st);
static const char keywords[] = "aldo" "erdb" "rona" "socc";
static const int kn = sizeof(keywords)/4;

int main()
{
Expand Down
14 changes: 2 additions & 12 deletions tests/unit/tests_util_dsl_keyword.c
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
#include <assert.h>
#include "util/util_dsl.h"

static tbc_keyword_st keywords[] = {
{"aldo", 2}, {"erdb", 3}, {"rona", 1}, {"socc", 2}
};
static const int kn = sizeof(keywords)/sizeof(tbc_keyword_st);
static const char keywords[] = "aldo" "erdb" "rona" "socc";
static const int kn = sizeof(keywords)/4;

int main()
{
{
char key[] = "aldo";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 0);
assert(keywords[index].value == 2);
}
{
char key[] = "erdb";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 1);
assert(keywords[index].value == 3);
}
{
char key[] = "rona";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 2);
assert(keywords[index].value == 1);
}
{
char key[] = "socc";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 3);
assert(keywords[index].value == 2);
}
{
char key[] = "ALDO";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 0);
assert(keywords[index].value == 2);
}
{
char key[] = "ERDB";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 1);
assert(keywords[index].value == 3);
}
{
char key[] = "RONA";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 2);
assert(keywords[index].value == 1);
}
{
char key[] = "SOCC";
int index = util_dsl_keyword(key, keywords, kn);
assert(index == 3);
assert(keywords[index].value == 2);
}
return 0;
}

0 comments on commit 892b7a9

Please sign in to comment.