|
| 1 | +#include "../inc/interpreter.h" |
| 2 | + |
| 3 | +API_t API_tree[NUM_OF_API_FUNCS]; |
| 4 | +uint32_t API_cntr; |
| 5 | + |
| 6 | +//macro to add new instruction to the instructions binary tree |
| 7 | +#define create_instruction_data(name, desc) \ |
| 8 | + const char *name##_API_NAME = (const char *)#name; \ |
| 9 | + const char *name##_API_DESC = (const char *)desc; |
| 10 | + |
| 11 | +// ***** Create instruction data for the API ***** |
| 12 | +create_instruction_data(cica, "cica fuggveny leirasa, jeeee :)"); |
| 13 | +create_instruction_data(kutya, "kutya fuggveny leirasa, joooo :)"); |
| 14 | +create_instruction_data(tehen, "tehen fuggveny leirasa, jii :)"); |
| 15 | +create_instruction_data(majom, "majom fuggveny leirasa, jaaaa :)"); |
| 16 | +create_instruction_data(eger, "eger fuggveny leirasa, gyoooo :)"); |
| 17 | +create_instruction_data(okor, "eger fuggveny leirasa, gyaaa :)"); |
| 18 | +create_instruction_data(csiga, "eger fuggveny leirasa, dikk :)"); |
| 19 | + |
| 20 | +#define add_instruction(name, func) add_interpreter_instruction(&name##_API_NAME, &name##_API_DESC, func); |
| 21 | + |
| 22 | +void cica_func(char *args, char *response) |
| 23 | +{ |
| 24 | + printf("Cica!\r\n"); |
| 25 | + printf("Args: %s\r\n", args); |
| 26 | +} |
| 27 | + |
| 28 | +void kutya_func(char *args, char *response) |
| 29 | +{ |
| 30 | + printf("Kutya!\r\n"); |
| 31 | + printf("Args: %s\r\n", args); |
| 32 | +} |
| 33 | + |
| 34 | +void init_interpreter(void) |
| 35 | +{ |
| 36 | + // Initialize the 'API_cntr' variable as zero before adding new items |
| 37 | + API_cntr = 0; |
| 38 | + |
| 39 | + // ***** Create the binary tree of the instructions ***** |
| 40 | + add_instruction(majom, kutya_func); |
| 41 | + add_instruction(tehen, kutya_func); |
| 42 | + add_instruction(cica, cica_func); |
| 43 | + add_instruction(kutya, kutya_func); |
| 44 | + add_instruction(eger, kutya_func); |
| 45 | + add_instruction(okor, kutya_func); |
| 46 | + add_instruction(csiga, kutya_func); |
| 47 | + |
| 48 | + print_apis_in_order( &API_tree[0] ); |
| 49 | + |
| 50 | + if( API_cntr != NUM_OF_API_FUNCS ){ |
| 51 | + printf("**ERROR**\tAPI function number mismatch!!!\r\n"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void add_interpreter_instruction(const char **name, const char **desc, void (*func)(char *, char *)) |
| 56 | +{ |
| 57 | + API_t *next; |
| 58 | + API_t *prev; |
| 59 | + int32_t comp_res; |
| 60 | + |
| 61 | + // if the API_cntr value is greater or equal than the defined maximum |
| 62 | + // API command number, there is a problem in the code! |
| 63 | + if (API_cntr >= NUM_OF_API_FUNCS) |
| 64 | + { |
| 65 | + |
| 66 | + printf("**ERROR**\tToo many instruction, memory is full!\r\n"); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // print out the new command name |
| 71 | + //printf("%s\r\n", (char *)*name); |
| 72 | + |
| 73 | + // if it is the first command we hacve to create the root of the |
| 74 | + // binary tree |
| 75 | + if (API_cntr == 0) |
| 76 | + { |
| 77 | + API_tree[0].name = (const char**)name; // address of the name string( char** type ) |
| 78 | + API_tree[0].desc = (const char**)desc; // address of the description string( char** type ) |
| 79 | + API_tree[0].func = func; // function pointer to the actual function |
| 80 | + |
| 81 | + API_tree[0].left = NULL; // because it is the first element of the tree, |
| 82 | + API_tree[0].right = NULL; // left and right branches has to be NULL |
| 83 | + } |
| 84 | + |
| 85 | + // if it is ot the first command we have to find it's place in the tree |
| 86 | + else |
| 87 | + { |
| 88 | + prev = &API_tree[0]; // get the address of the root element |
| 89 | + comp_res = strcmp( (char*)*(prev->name), (char*)*name ); // compare the names and save the result to 'comp_res' |
| 90 | + |
| 91 | + // compare( ABC order ) the root element name and the new element name |
| 92 | + (comp_res > 0) ? (next = (prev->left)) : ( next = (prev->right)); |
| 93 | + |
| 94 | + // find the place in the tree |
| 95 | + while (next != 0) |
| 96 | + { |
| 97 | + prev = next; |
| 98 | + comp_res = strcmp( *(prev->name), *name); |
| 99 | + (comp_res > 0) ? (next = (prev->left)) : (next = (prev->right)); |
| 100 | + } |
| 101 | + |
| 102 | + // link the new item on the previous branch |
| 103 | + ( comp_res > 0 ) ? ( ( prev->left ) = &API_tree[API_cntr] ) : ( ( prev->right ) = &API_tree[API_cntr] ); |
| 104 | + |
| 105 | + API_tree[API_cntr].name = (const char**)name; // address of the name string( char** type ) |
| 106 | + API_tree[API_cntr].desc = (const char**)desc; // address of the description string( char** type ) |
| 107 | + API_tree[API_cntr].func = func; // function pointer to the actual function |
| 108 | + |
| 109 | + API_tree[API_cntr].left = NULL; // close the branch |
| 110 | + API_tree[API_cntr].right = NULL; |
| 111 | + } |
| 112 | + |
| 113 | + API_cntr++; |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +void print_apis_in_order(API_t *head){ |
| 118 | + |
| 119 | + if( head==0 ){ |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + print_apis_in_order( head -> left ); |
| 124 | + printf( "%s\r\n", *(head->name) ); |
| 125 | + print_apis_in_order( head -> right ); |
| 126 | +} |
0 commit comments