Skip to content

Commit

Permalink
feat: add djb2 to compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Oct 23, 2023
1 parent f09190b commit 66f596b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/lang/lang_3bc_compile.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "3bc_types.h"
#include "bus/bus_mem_0000.h"
#include "util/util_djb2.h"
#include "util/util_stoi.h"
#include "util/util_ascii.h"
#include "util/util_asm.h"
Expand Down Expand Up @@ -120,6 +121,9 @@ void lang_3bc_compile(tbc_app_st *const self)
if (cast != NULL) {
self->cache_l1.error = cast(cpu_r[i].ptr, tokens[i], column_size[i], tokens_idk[i]);
}
else if (tokens[i][0] == ':') {
self->cache_l1.error = util_djb2(cpu_r[i].ptr, tokens[i], column_size[i], tokens_idk[i]);
}
else if (tokens_idk[i] == 4) {
tbc_i16_t key = util_keyword(tokens[i], opcodes_arr, opcodes_size);
if (key >= 0) {
Expand All @@ -133,7 +137,7 @@ void lang_3bc_compile(tbc_app_st *const self)
}
}
else if (tokens[i][0] == '\'') {
negative = util_ascii(tokens[i], tokens_idk);
negative = util_ascii(tokens[i], tokens_idk[i]);
if (negative == 0x15) {
self->cache_l1.error = ERROR_CHAR_SCAPE;
break;
Expand Down
36 changes: 20 additions & 16 deletions src/util/util_djb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @param[in] src string source
* @param[in] dn integer destination length @b (bits)
* @param[in] sn string source length @b (bytes)
* @pre @c sn must be 8, 16 or 32.
* @pre @c sn @b cannot be @b 64 bits.
* @pre @c dn must be greater than @b 3 bits.
* @pre @c dn @b cannot be greater @b 32 bits.
* @note the character @c '\n' is a terminator of hash.
* @return status of digest
* @retval ERROR_UNKNOWN when success
Expand All @@ -19,51 +19,55 @@ tbc_error_et util_djb2(void *const dest, char *const src, const tbc_u8_t dn, tbc
tbc_u8_t index = 0;
tbc_error_et res = ERROR_UNKNOWN;

switch (dn) {
case 8:
{
do {
if (dn <= 8) {
tbc_u8_t mask = 0xFF >> (8 - dn);
tbc_u8_t hash = 5;

if (dn <= 3) {
res = ERROR_NUMBER_OVERFLOW;
break;
}
while (index < sn && src[index] != '\n' && src[index] != '\0') {
hash = ((hash << 5) + hash) + src[index];
++index;
}
if (index > 0) {
*((tbc_u8_t*)dest) = hash;
*((tbc_u8_t*)dest) = mask & hash;
}
break;
}
case 16:
{
if (dn <= 16) {
tbc_u16_t mask = 0xFFFF >> (16 - dn);
tbc_u16_t hash = 5381;
while (index < sn && src[index] != '\n' && src[index] != '\0') {
hash = ((hash << 5) + hash) + src[index];
++index;
}
if (index > 0) {
*((tbc_u16_t*)dest) = hash;
*((tbc_u16_t*)dest) = mask & hash;
}
break;
}
case 32:
{
if (dn <= 32) {
tbc_u32_t mask = 0xFFFFFFFF >> (32 - dn);
tbc_u32_t hash = 5381;
while (index < sn && src[index] != '\n' && src[index] != '\0') {
hash = ((hash << 5) + hash) + src[index];
++index;
}
if (index > 0) {
*((tbc_u32_t*)dest) = hash;
*((tbc_u32_t*)dest) = mask & hash;
}
break;
}
default:
res = ERROR_NUMBER_INVALID_BASE;
break;
res = ERROR_NUMBER_SIZE_TOO_LONG;
}
while (0);

if (index == 0 && res == ERROR_UNKNOWN) {
res = ERROR_NUMBER_NO_DIGITS;
}

return res;
}
}

0 comments on commit 66f596b

Please sign in to comment.