xiranda/xiranda.c

213 lines
7.6 KiB
C
Raw Normal View History

2024-03-31 22:58:48 -04:00
#include <xolatile/xtandard.c>
enum {
2024-04-03 05:03:46 -04:00
coin_none,
coin_type, coin_loop, coin_if, coin_else, coin_case, coin_return, coin_import, coin_system,
coin_function, coin_variable, coin_constant, coin_data_type, coin_string, coin_number, coin_marker, coin_label,
coin_add, coin_subtract, coin_multiply, coin_divide, coin_modulus, coin_equal, coin_above, coin_below,
coin_open, coin_close, coin_next, coin_stop, coin_branch, coin_and, coin_or, coin_not,
coin_range, coin_enrange, coin_derange, coin_machine
2024-03-31 22:58:48 -04:00
};
2024-04-03 05:03:46 -04:00
static int coin_code = 0;
static int type_code = 0;
2024-04-03 04:31:51 -04:00
static int function_code = 0;
static int variable_code = 0;
static int constant_code = 0;
2024-03-31 22:58:48 -04:00
2024-04-03 05:03:46 -04:00
static int coin_type [240] = { 0 };
static int coin_data [240] = { 0 };
static char type_name [27] [40] = { "" };
static int type_size [27] = { 0 };
2024-04-03 04:31:51 -04:00
static char function_name [9] [40] = { "" };
static int function_type [9] = { 0 };
static char function_argument_name [9] [6] [40] = { { "" } };
static int function_argument_type [9] [6] = { { 0 } };
2024-03-31 22:58:48 -04:00
2024-04-03 04:31:51 -04:00
static char variable_name [27] [40] = { "" };
static int variable_type [27] = { 0 };
static int variable_data [27] = { 0 };
static char constant_name [30] [40] = { "" };
static int constant_type [30] = { 0 };
static int constant_data [30] = { 0 };
2024-03-31 22:58:48 -04:00
#include <stdio.h>
static char * word_list [] = {
"type", "loop", "if", "else", "case", "return", "import", "system"
2024-03-31 22:58:48 -04:00
};
2024-04-03 05:03:46 -04:00
static void add_coin (int type, int data) {
coin_type [coin_code] = type;
coin_data [coin_code] = data;
++coin_code;
}
static void add_type (char * name, int size) {
string_copy (type_name [type_code], name);
type_size [type_code] = size;
++type_code;
}
static void add_function (char * name, int type, char * * agrument_name, int * argument_type) {
string_copy (type_name [type_code], name);
type_size [type_code] = size;
++type_code;
2024-03-31 22:58:48 -04:00
}
static void kill (char * data) {
echo (data);
exit (log_failure);
}
int main (void) {
char * buffer = null;
int offset = 0;
int length = 0;
buffer = file_import ("./test.x");
for (offset = 0; buffer [offset] != '\0'; ++offset) {
2024-04-03 05:03:46 -04:00
if (buffer [offset] == '"') {
2024-03-31 22:58:48 -04:00
int size = 0;
char data [STRING_LIMIT] = "";
for (++offset; (buffer [offset] != '"') && (buffer [offset] != '\0'); ++offset) {
data [size++] = buffer [offset];
}
data [size] = '\0';
2024-04-03 05:03:46 -04:00
add_coin (core_string, string_code);
2024-03-31 22:58:48 -04:00
add_string (data, size);
} else if (character_is_digit (buffer [offset]) == true) {
int data = buffer [offset] - '0';
for (++offset; (character_is_digit (buffer [offset]) == true) && (buffer [offset] != '\0'); ++offset) {
data *= 10;
data += (buffer [offset] - '0');
}
2024-04-03 05:03:46 -04:00
add_coin (core_number, number_code);
2024-03-31 22:58:48 -04:00
add_number (data);
--offset;
} else if (character_is_alpha (buffer [offset]) == true) {
int size = 0;
char name [NAME_LIMIT] = "";
for (; ((character_is_alpha (buffer [offset]) == true) ||
(character_is_digit (buffer [offset]) == true) ||
(character_is_underscore (buffer [offset]) == true)) && (buffer [offset] != '\0'); ++offset) {
name [size++] = buffer [offset];
}
name [size] = '\0';
for (length = 0; length < (int) (sizeof (word_list) / sizeof (word_list [0])); ++length) {
2024-03-31 22:58:48 -04:00
if (string_compare_limit (name, word_list [length], string_length (word_list [length]) + 1) == true) {
2024-04-03 05:03:46 -04:00
add_coin (core_word, length);
2024-03-31 22:58:48 -04:00
goto here;
}
}
2024-04-03 05:03:46 -04:00
add_coin (core_marker, marker_code);
2024-03-31 22:58:48 -04:00
add_marker (name, token_type [token_count - 1]);
if ((token_type [token_count - 2] == core_word) && (token_data [token_count - 2] == word_type)) {
2024-04-01 07:23:49 -04:00
token_type [token_count - 1] = core_type;
add_type (name);
}
for (length = 0; length < type_code; ++length) {
if (string_compare_limit (name, type_name [length], string_length (type_name [length]) + 1) == true) {
2024-04-01 07:23:49 -04:00
token_type [token_count - 1] = core_type;
}
}
here:
2024-03-31 22:58:48 -04:00
--offset;
2024-04-01 07:23:49 -04:00
} else if (character_compare_array (buffer [offset], ",.;:=<>&|!+-*/%()[]") == true) {
2024-04-03 05:03:46 -04:00
add_coin (core_symbol, symbol_code);
2024-03-31 22:58:48 -04:00
add_symbol (buffer [offset]);
} else {
if (character_is_blank (buffer [offset]) == false) {
2024-04-01 07:23:49 -04:00
echo ("\033[1;31mCharacter set exception point: Segmentation\033[0m\n");
2024-03-31 22:58:48 -04:00
printf ("%c -- %i\n", buffer [offset], (int) buffer [offset]);
exit (log_failure);
}
}
}
2024-04-03 05:03:46 -04:00
end:
2024-03-31 22:58:48 -04:00
for (length = 0; length < token_count; ++length) {
switch (token_type [length]) {
case core_symbol: printf ("\033[1;34m%c\033[0m ", symbol_data [token_data [length]]); break;
case core_string: printf ("\033[1;31m%s\033[0m ", string_data [token_data [length]]); break;
case core_number: printf ("\033[1;32m%i\033[0m ", number_data [token_data [length]]); break;
2024-04-01 07:23:49 -04:00
case core_type: printf ("\033[1;36m%s\033[0m ", marker_name [token_data [length]]); break;
2024-03-31 22:58:48 -04:00
case core_marker: printf ("\033[1;33m%s\033[0m ", marker_name [token_data [length]]); break;
case core_word: printf ("\033[1;35m%s\033[0m ", word_list [token_data [length]]); break;
default: break;
}
}
printf ("\n");
for (length = 0; length < token_count; ++length) {
if ((token_type [length] == core_word) && (token_data [length] == word_return)) {
++length;
if ((token_type [length] == core_symbol) && (symbol_data [token_data [length]] == '(')) {
echo ("return (\n");
++length;
} else if ((token_type [length] == core_symbol) && (symbol_data [token_data [length]] == ';')) {
echo ("; return;\nxor rax, rax\nret\n");
2024-03-31 22:58:48 -04:00
++length;
2024-04-02 18:24:35 -04:00
} else if (token_type [length] == core_number && (token_type [length + 1] == core_symbol) && (symbol_data [token_data [length + 1]] == ';')) {
printf ("; return {number};\nmov rax, %i\nret\n", number_data [token_data [length]]);
++length;
2024-03-31 22:58:48 -04:00
} else {
kill ("return ?\n");
}
} else if ((token_type [length] == core_word) && (token_data [length] == word_if)) {
++length;
if ((token_type [length] == core_symbol) && (symbol_data [token_data [length]] == '(')) {
echo ("if (\n");
++length;
} else {
kill ("if ?\n");
}
} else if ((token_type [length] == core_word) && (token_data [length] == word_else)) {
++length;
if ((token_type [length] == core_symbol) && (symbol_data [token_data [length]] == ':')) {
echo ("else :\n");
++length;
} else if ((token_type [length] == core_word) && (token_data [length] == word_if)) {
echo ("else if\n");
++length;
} else {
2024-04-02 18:24:35 -04:00
echo ("else expression\n");
2024-03-31 22:58:48 -04:00
}
} else if ((token_type [length] == core_word) && (token_data [length] == word_type)) {
++length;
2024-04-01 07:23:49 -04:00
if (token_type [length] == core_type) {
2024-03-31 22:58:48 -04:00
echo ("type <name> -- ");
echo (marker_name [token_data [length]]);
echo ("\n");
++length;
} else {
kill ("type ?\n");
}
}
}
for (length = 0; length < string_code; ++length) { string_data [length] = deallocate (string_data [length]); }
for (length = 0; length < marker_code; ++length) { marker_name [length] = deallocate (marker_name [length]); }
2024-04-01 07:23:49 -04:00
for (length = 0; length < type_code; ++length) { type_name [length] = deallocate (type_name [length]); }
2024-03-31 22:58:48 -04:00
string_data = deallocate (string_data);
string_size = deallocate (string_size);
marker_name = deallocate (marker_name);
marker_type = deallocate (marker_type);
2024-04-01 07:23:49 -04:00
type_name = deallocate (type_name);
2024-03-31 22:58:48 -04:00
symbol_data = deallocate (symbol_data);
number_data = deallocate (number_data);
token_data = deallocate (token_data);
token_type = deallocate (token_type);
buffer = deallocate (buffer);
return (log_success);
}