working on the parser module
This commit is contained in:
82
parser.cpp
82
parser.cpp
@@ -1,6 +1,7 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "tokenizer.h"
|
#include "tokenizer.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -13,6 +14,7 @@
|
|||||||
#define MAX_CONSTANTS 256
|
#define MAX_CONSTANTS 256
|
||||||
#define MAX_STRUCT_MEMBERS 10
|
#define MAX_STRUCT_MEMBERS 10
|
||||||
|
|
||||||
|
/*
|
||||||
struct t_variable {
|
struct t_variable {
|
||||||
char name[MAX_IDENTIFIER_LENGTH];
|
char name[MAX_IDENTIFIER_LENGTH];
|
||||||
int type;
|
int type;
|
||||||
@@ -29,6 +31,7 @@ struct t_struct {
|
|||||||
struct t_scope {
|
struct t_scope {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
struct t_label {
|
struct t_label {
|
||||||
char name[MAX_IDENTIFIER_LENGTH];
|
char name[MAX_IDENTIFIER_LENGTH];
|
||||||
@@ -59,9 +62,9 @@ struct t_constant {
|
|||||||
};*/
|
};*/
|
||||||
|
|
||||||
static byte* code; // [MAX_CODE_SIZE];
|
static byte* code; // [MAX_CODE_SIZE];
|
||||||
static word codepos = 0;
|
static uint32_t codepos = 0;
|
||||||
static word lines[32768];
|
static uint32_t lines[32768];
|
||||||
static word current_line;
|
static uint32_t current_line;
|
||||||
|
|
||||||
//static t_label known_labels[MAX_LABELS];
|
//static t_label known_labels[MAX_LABELS];
|
||||||
//static int num_known_labels = 0;
|
//static int num_known_labels = 0;
|
||||||
@@ -72,6 +75,7 @@ static int num_anonymous_labels = 0;
|
|||||||
static char* breaklabel = nullptr;
|
static char* breaklabel = nullptr;
|
||||||
static char* contlabel = nullptr;
|
static char* contlabel = nullptr;
|
||||||
|
|
||||||
|
/*
|
||||||
static t_variable* variables = nullptr;
|
static t_variable* variables = nullptr;
|
||||||
static int num_variables = 0;
|
static int num_variables = 0;
|
||||||
static bool variable_is_array = false;
|
static bool variable_is_array = false;
|
||||||
@@ -82,10 +86,11 @@ static int num_external_functions = 0;
|
|||||||
static t_string strings[256];
|
static t_string strings[256];
|
||||||
static int num_strings = 0;
|
static int num_strings = 0;
|
||||||
|
|
||||||
static bool parser_finished = false;
|
|
||||||
|
|
||||||
static t_constant constants[MAX_CONSTANTS];
|
static t_constant constants[MAX_CONSTANTS];
|
||||||
static int num_constants;
|
static int num_constants;
|
||||||
|
*/
|
||||||
|
|
||||||
|
static bool parser_finished = false;
|
||||||
|
|
||||||
/****************************************************************************************/
|
/****************************************************************************************/
|
||||||
/* GENERIC FUNCTIONS */
|
/* GENERIC FUNCTIONS */
|
||||||
@@ -118,6 +123,14 @@ static void emmit_w(const word value) {
|
|||||||
code[codepos++] = value >> 8;
|
code[codepos++] = value >> 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void emmit_dw(const uint32_t value) {
|
||||||
|
byte *v = (byte*)&value;
|
||||||
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
|
}
|
||||||
|
|
||||||
static void emmit_f(const float value) {
|
static void emmit_f(const float value) {
|
||||||
byte *v = (byte*)&value;
|
byte *v = (byte*)&value;
|
||||||
lines[codepos] = current_line; code[codepos++] = *(v++);
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
@@ -126,62 +139,23 @@ static void emmit_f(const float value) {
|
|||||||
lines[codepos] = current_line; code[codepos++] = *(v++);
|
lines[codepos] = current_line; code[codepos++] = *(v++);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void patch(const word address, const word dest) {
|
static void patch(const uint32_t address, const word dest) {
|
||||||
code[address] = dest & 255;
|
code[address] = dest & 255;
|
||||||
code[address + 1] = dest >> 8;
|
code[address + 1] = dest >> 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const word get_current_address() {
|
static void patch_dw(const uint32_t address, const uint32_t dest) {
|
||||||
|
byte *v = (byte*)&dest;
|
||||||
|
code[address] = v[0];
|
||||||
|
code[address+1] = v[1];
|
||||||
|
code[address+2] = v[2];
|
||||||
|
code[address+3] = v[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
static const uint32_t get_current_address() {
|
||||||
return codepos;
|
return codepos;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************************/
|
|
||||||
/* VARIABLE MANAGEMENT */
|
|
||||||
/****************************************************************************************/
|
|
||||||
|
|
||||||
static int get_variable_index(const char* string, const int array_size = 1) {
|
|
||||||
t_variable* variable = variables;
|
|
||||||
t_variable* last = variables;
|
|
||||||
while (variable != nullptr) {
|
|
||||||
if (strcmp(variable->name, string) == 0) { variable_is_array = (variable->size > 1); return variable->index + 0xC000; }
|
|
||||||
last = variable;
|
|
||||||
variable = variable->next;
|
|
||||||
}
|
|
||||||
t_variable* newvar = (t_variable*)malloc(sizeof(t_variable));
|
|
||||||
//newvar->name = (char*)malloc(strlen(string) + 1);
|
|
||||||
strcpy(newvar->name, string);
|
|
||||||
newvar->index = num_variables;
|
|
||||||
num_variables += array_size;
|
|
||||||
newvar->size = array_size;
|
|
||||||
variable_is_array = (newvar->size > 1);
|
|
||||||
newvar->next = nullptr;
|
|
||||||
if (last != nullptr) {
|
|
||||||
last->next = newvar;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
variables = newvar;
|
|
||||||
}
|
|
||||||
return newvar->index + 0xC000;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_variable_array() { return variable_is_array; }
|
|
||||||
|
|
||||||
/****************************************************************************************/
|
|
||||||
/* CONSTANT MANAGEMENT */
|
|
||||||
/****************************************************************************************/
|
|
||||||
void parser_register_constant(const char* name, const unsigned char value) {
|
|
||||||
strcpy(constants[num_constants].name, name);
|
|
||||||
constants[num_constants++].value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const int get_constant(const char* name) {
|
|
||||||
for (int i = 0; i < num_constants; i++) {
|
|
||||||
if (strcmp(constants[i].name, name) == 0)
|
|
||||||
return constants[i].value;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************************/
|
/****************************************************************************************/
|
||||||
/* LABEL MANAGEMENT */
|
/* LABEL MANAGEMENT */
|
||||||
/****************************************************************************************/
|
/****************************************************************************************/
|
||||||
|
|||||||
10
parser.h
10
parser.h
@@ -41,10 +41,10 @@ enum OPS {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void parser_parse(const char* buffer, unsigned char* mem);
|
void parser_parse(const char* buffer, unsigned char* mem);
|
||||||
const int parser_get_codesize();
|
//const int parser_get_codesize();
|
||||||
const int parser_get_memory_usage();
|
//const int parser_get_memory_usage();
|
||||||
unsigned short* parser_get_lines();
|
//unsigned short* parser_get_lines();
|
||||||
|
|
||||||
void parser_register_external_function(const char* name, const char* parameters, void (*fun)(void));
|
//void parser_register_external_function(const char* name, const char* parameters, void (*fun)(void));
|
||||||
|
|
||||||
void parser_register_constant(const char* name, const unsigned char value);
|
//void parser_register_constant(const char* name, const unsigned char value);
|
||||||
Reference in New Issue
Block a user