RodrigoDornelles / 3bc-lang

Low-level language, tiny virtual machine, minimal runtime, intermediate representation, embeddable, easy for beginners. (Friendly Punched cards)
https://3bc-lang.org
GNU General Public License v3.0
232 stars 25 forks source link

better separate data from logic in source code #340

Closed RodrigoDornelles closed 1 year ago

RodrigoDornelles commented 1 year ago
#include <stdio.h>

union tbc_keyword_tbc_ut {
    char name[4];
    unsigned long compare;
};

struct tbc_keyword_opcode_st {
    union tbc_keyword_tbc_ut keyword;
    unsigned char value;
};

static const struct tbc_keyword_opcode_st tbc_keyword_opcode_list_c[] = {
    /** COMMON CPU MODES **/
    {.keyword.name = "nill", .value = 0},
    {.keyword.name = "mode", .value = 7},
    /** CPU MODES 1..5 **/
    {.keyword.name = "strb", .value = 1},
    {.keyword.name = "stro", .value = 2},
    {.keyword.name = "stri", .value = 3},
    {.keyword.name = "strx", .value = 4},
    {.keyword.name = "strc", .value = 5}
};

static const char tbc_keyword_opcode_list_size_c =
    sizeof(tbc_keyword_opcode_list_c)/sizeof(struct tbc_keyword_opcode_st);

int main()
{
    unsigned long teste = ('s' | ('t' << 1) | ('r' << 2) | (((unsigned long)'b') << 3));
    char i = 0;

    while(i < tbc_keyword_opcode_list_size_c){
        printf("\ncompare str [%4s] [%4s] ", tbc_keyword_opcode_list_c[i].keyword.name, (char*) &teste);
        printf("\ncompare nbm [%lu] [%lu] ", tbc_keyword_opcode_list_c[i].keyword.compare, teste);

        if (tbc_keyword_opcode_list_c[i].keyword.compare == teste) {
            printf("\nfound!");
        }

        i += 1;
    }

    return 0;
}