implemented hexstrings, implemented MOVE, PUTCOLOR, GETCHAR, GETCOLOR, modified PUTCHAR, several fixes.
This commit is contained in:
89
parser.cpp
89
parser.cpp
@@ -29,9 +29,9 @@ struct t_external_function {
|
||||
};
|
||||
|
||||
struct t_string {
|
||||
char string[256];
|
||||
word references[256];
|
||||
int num_references = 1;
|
||||
unsigned char string[256];
|
||||
word references[256];
|
||||
int num_references = 1;
|
||||
};
|
||||
|
||||
struct t_constant {
|
||||
@@ -208,12 +208,35 @@ static void generate_anonymous_labelname(char* dest) {
|
||||
/****************************************************************************************/
|
||||
/* STRING MANAGEMENT */
|
||||
/****************************************************************************************/
|
||||
static const bool same_string(const unsigned char* a, const unsigned char*b) {
|
||||
for (int i = 0; i <= a[0]; i++) if (a[i] != b[i]) return false;
|
||||
return true;
|
||||
}
|
||||
static void copy_string(unsigned char* dest, const unsigned char* src) {
|
||||
memcpy(dest, src, src[0] + 1);
|
||||
//for (int i = 0; i <= src[0]; i++) dest[i] = src[i];
|
||||
}
|
||||
static void to_basic_string(unsigned char* dest, const char* src) {
|
||||
int len = strlen(src);
|
||||
*dest = len; dest++;
|
||||
memcpy(dest, src, len);
|
||||
}
|
||||
|
||||
static int register_array(const unsigned char* string) {
|
||||
for (int i = 0; i < num_strings; i++) {
|
||||
if (same_string(strings[i].string, string)) { strings[i].references[strings[i].num_references++] = get_current_address(); return 0; }
|
||||
}
|
||||
copy_string(strings[num_strings].string, string);
|
||||
strings[num_strings++].references[0] = get_current_address();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int register_string(const char* string) {
|
||||
unsigned char new_str[255]; to_basic_string(new_str, string);
|
||||
for (int i = 0; i < num_strings; i++) {
|
||||
if (strcmp(strings[i].string, string) == 0) { strings[i].references[strings[i].num_references++] = get_current_address(); return 0; }
|
||||
if (same_string(strings[i].string, new_str)) { strings[i].references[strings[i].num_references++] = get_current_address(); return 0; }
|
||||
}
|
||||
strcpy(strings[num_strings].string, string);
|
||||
copy_string(strings[num_strings].string, new_str);
|
||||
strings[num_strings++].references[0] = get_current_address();
|
||||
return 0;
|
||||
}
|
||||
@@ -224,9 +247,9 @@ static void append_strings() {
|
||||
emmit(num_strings);
|
||||
for (int i = 0; i < num_strings; i++) {
|
||||
for (int j = 0; j < strings[i].num_references; j++) { patch(strings[i].references[j], get_current_address()); }
|
||||
char len = strlen(strings[i].string);
|
||||
emmit(len);
|
||||
for (int j = 0; j < len; j++) { emmit(strings[i].string[j]); }
|
||||
//char len = strings[i].string[0];
|
||||
//emmit(len);
|
||||
for (int j = 0; j <= strings[i].string[0]; j++) { emmit(strings[i].string[j]); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,6 +267,8 @@ static void parse_chr();
|
||||
static void parse_strlen();
|
||||
static void parse_keypressed();
|
||||
static void parse_anykey();
|
||||
static void parse_getchar();
|
||||
static void parse_getcolor();
|
||||
|
||||
#define EXPECT(X, Y) if (tkn_get_token() != X) { parser_finished = true; error_raise(Y); return; }
|
||||
|
||||
@@ -256,6 +281,14 @@ static void parse_concat_atom() {
|
||||
tkn_next();
|
||||
return;
|
||||
}
|
||||
if (tkn_get_token() == TOKEN_HEXSTRING) {
|
||||
emmit(OP_SETX);
|
||||
emmit_w(register_array(tkn_get_array()));
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_string_load"));
|
||||
tkn_next();
|
||||
return;
|
||||
}
|
||||
if (tkn_get_token() == TOKEN_IDENTIFIER) {
|
||||
int ivar = get_variable_index(tkn_get_string());
|
||||
if (!is_variable_array()) { parser_finished = true; error_raise("Expected string constant or variable"); return; }
|
||||
@@ -334,6 +367,8 @@ static void parse_expr_atom() {
|
||||
if (tkn_get_token() == TOKEN_STRLEN) { tkn_next(); parse_strlen(); return; }
|
||||
if (tkn_get_token() == TOKEN_KEYPRESSED) { tkn_next(); parse_keypressed(); return; }
|
||||
if (tkn_get_token() == TOKEN_ANYKEY) { tkn_next(); parse_anykey(); return; }
|
||||
if (tkn_get_token() == TOKEN_GETCHAR) { tkn_next(); parse_getchar(); return; }
|
||||
if (tkn_get_token() == TOKEN_GETCOLOR) { tkn_next(); parse_getcolor(); return; }
|
||||
|
||||
parser_finished = true; error_raise("Syntax error");
|
||||
return;
|
||||
@@ -690,6 +725,13 @@ static void parse_locate() {
|
||||
emmit_w(get_label_address("_sys_vdp_locate"));
|
||||
}
|
||||
|
||||
static void parse_move() {
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_vdp_move"));
|
||||
}
|
||||
|
||||
static void parse_print() {
|
||||
parse_concatenation();
|
||||
emmit(OP_JSR);
|
||||
@@ -700,11 +742,32 @@ static void parse_putchar() {
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_vdp_putchar"));
|
||||
}
|
||||
|
||||
static void parse_putcolor() {
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_vdp_putcolor"));
|
||||
}
|
||||
|
||||
static void parse_getchar() {
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_vdp_getchar"));
|
||||
}
|
||||
|
||||
static void parse_getcolor() {
|
||||
parse_expression();
|
||||
parse_expression();
|
||||
emmit(OP_JSR);
|
||||
emmit_w(get_label_address("_sys_vdp_getcolor"));
|
||||
}
|
||||
|
||||
static void parse_setchar() {
|
||||
parse_expression();
|
||||
parse_concatenation();
|
||||
@@ -824,10 +887,18 @@ static void parse_statements() {
|
||||
tkn_next(); parse_putsprite(); break;
|
||||
case TOKEN_LOCATE:
|
||||
tkn_next(); parse_locate(); break;
|
||||
case TOKEN_MOVE:
|
||||
tkn_next(); parse_move(); break;
|
||||
case TOKEN_PRINT:
|
||||
tkn_next(); parse_print(); break;
|
||||
case TOKEN_PUTCHAR:
|
||||
tkn_next(); parse_putchar(); break;
|
||||
case TOKEN_PUTCOLOR:
|
||||
tkn_next(); parse_putcolor(); break;
|
||||
//case TOKEN_GETCHAR: <--- Parsed in parse_expr_atom
|
||||
// tkn_next(); parse_getchar(); break;
|
||||
//case TOKEN_GETCOLOR: <--- Parsed in parse_expr_atom
|
||||
// tkn_next(); parse_getcolor(); break;
|
||||
case TOKEN_SETCHAR:
|
||||
tkn_next(); parse_setchar(); break;
|
||||
//case TOKEN_KEYPRESSED: <--- Parsed in parse_expr_atom
|
||||
|
||||
Reference in New Issue
Block a user