Skip to content

Commit

Permalink
chore: improves in util_dsl_keyword #421
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Dec 9, 2023
1 parent 892b7a9 commit 5e8903c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
47 changes: 26 additions & 21 deletions src/lang/lang_3bc_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@ union ___u8_u16_t {
* representation of the menomonics for registers
* with their respective opcodes.
*
* @note must be ordered.
*
* @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 },
{ "mili", 4 }, { "mode", 7 }, { "moff", 3 }, { "muse", 4 }, { "nb02", 1 },
{ "nb08", 2 }, { "nb10", 3 }, { "nb16", 4 }, { "ncal", 5 }, { "ngto", 5 },
{ "nill", 0 }, { "nret", 5 }, { "pcal", 4 }, { "pgto", 4 }, { "pret", 4 },
{ "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);
* @pre must be ordered.
*/
const char opcodes_key[] = "aloc"
"back" "call" "fake" "fcal" "fgto"
"free" "fret" "goto" "math" "micr"
"mili" "mode" "moff" "muse" "nb02"
"nb08" "nb10" "nb16" "ncal" "ngto"
"nill" "nret" "pcal" "pgto" "pret"
"pull" "push" "real" "seco" "spin"
"strb" "strc" "stri" "stro" "strx"
"zcal" "zgto" "zret";

const tbc_u8_t opcodes_val[] = { 2,
1, 1, 2, 2, 2,
1, 2, 1, 1, 3,
4, 7, 3, 4, 1,
2, 3, 4, 5, 5,
0, 5, 4, 4, 4,
3, 5, 1, 5, 4,
1, 5, 3, 2, 4,
3, 3, 3
};

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,16 +152,16 @@ 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_key, sizeof(opcodes_key));
if (key >= 0) {
if (column_size[i] > 8) {
*cpu_r[i].u16 = opcodes_arr[key].value;
*cpu_r[i].u16 = opcodes_val[key];
} else {
*cpu_r[i].u8 = opcodes_arr[key].value;
*cpu_r[i].u8 = opcodes_val[key];
}
} else {
self->cache.l1.error = ERROR_INVALID_MNEMONIC;
}*/
}
}
else {
self->cache.l1.error = column_errors[i];
Expand Down
26 changes: 12 additions & 14 deletions src/util/util_dsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,39 +220,37 @@ tbc_i8_t util_dsl_line(char **beg, char **mid, char **end, char *src, tbc_u8_t s
/**
* @short keyword index from string
* @brief find opcode value given a list of keywords
* @details binary search optimized for 4-letter keywords in an array
* that contains a sorted key and a 2-byte (16bit) value.
* @todo use string instead struct
* @details binary search optimized for 4-letter keywords in string.
* @note this function is case unsensitive.
* @note this function has operation complexity @f$O(log (n))@f$.
* @note this function has endless optimization.
* @note this function has old processors optimization.
* @param[in] src string source
* @param[in] kl keyword list @b (array) of @c tbc_keyword_st
* @param[in] kn keyword size @b (bytes)
* @pre @c kl must be ordered by key
* @pre @c kl must be lowcase keywords
* @param[in] keys keyword list @b (char*)
* @param[in] kn keyword list size @b (bytes)
* @pre @c keys must be ordered
* @pre @c keys must be lowcase
* @pre @c kn must be @c keys*4 also size of string.
* @return index of keyword
* @retval -1 when not found
* @retval -2 wrong paramters
* @retval 0..32_768 when found
* @retval 0..16_383 when found
*
*/
tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_i16_t kn)
tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_u16_t kn)
{
tbc_i16_t res = -2;
tbc_i16_t low = 0;
tbc_i16_t high = (kn - 1);
tbc_i16_t high = ((kn - 1) >> 2);
tbc_i16_t mid;
tbc_i32_t sum;
tbc_keyword_ut key;
tbc_keyword_ut *kl = (tbc_keyword_ut*) keys;

do {
if (src == NULL) {
break;
}
if (kl == NULL) {
if (keys == NULL) {
break;
}
if (kn <= 0) {
Expand Down Expand Up @@ -282,8 +280,8 @@ tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_i1

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

#if defined(TBC_CPU_BYTE_SEXBE)
/* native cpu sub */
Expand Down
2 changes: 1 addition & 1 deletion src/util/util_dsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ union ___tbc_keyword_key_u {

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 char *const keys, tbc_i16_t kn);
tbc_i16_t util_dsl_keyword(const char *const src, const char *const keys, tbc_u16_t kn);

#endif
7 changes: 1 addition & 6 deletions tests/unit/error_util_dsl_keyword.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "util/util_dsl.h"

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

int main()
{
Expand All @@ -21,11 +21,6 @@ int main()
int index = util_dsl_keyword(key, keywords, 0);
assert(index == -2);
}
{
char key[] = "foot";
int index = util_dsl_keyword(key, keywords, -1);
assert(index == -2);
}
{
char key[] = "ball";
int index = util_dsl_keyword(key, keywords, kn);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/tests_util_dsl_keyword.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "util/util_dsl.h"

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

int main()
{
Expand Down

0 comments on commit 5e8903c

Please sign in to comment.