Code editor WIP
This commit is contained in:
@@ -215,6 +215,7 @@ int main(int argc,char*argv[]) {
|
|||||||
//lua_call_init();
|
//lua_call_init();
|
||||||
}
|
}
|
||||||
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
||||||
|
execute_run();
|
||||||
lua_quit();
|
lua_quit();
|
||||||
reinit();
|
reinit();
|
||||||
lua_init(lua_filename);
|
lua_init(lua_filename);
|
||||||
|
|||||||
2
ascii.h
2
ascii.h
@@ -128,6 +128,8 @@
|
|||||||
#define COLOR_WHITE 15
|
#define COLOR_WHITE 15
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
|
void execute_run();
|
||||||
|
|
||||||
const char* get_filename();
|
const char* get_filename();
|
||||||
|
|
||||||
void cls(uint8_t value=32);
|
void cls(uint8_t value=32);
|
||||||
|
|||||||
5
lua.cpp
5
lua.cpp
@@ -420,7 +420,6 @@ void lua_init(const char* filename, const bool start_playing) {
|
|||||||
debug("BOOT ERROR:", false);
|
debug("BOOT ERROR:", false);
|
||||||
debug(lua_tostring(L, -1));
|
debug(lua_tostring(L, -1));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
setmode(0);
|
|
||||||
file_loaded = false;
|
file_loaded = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -428,7 +427,6 @@ void lua_init(const char* filename, const bool start_playing) {
|
|||||||
debug("ERROR LOADING GAME:",false);
|
debug("ERROR LOADING GAME:",false);
|
||||||
debug(lua_tostring(L, -1));
|
debug(lua_tostring(L, -1));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
setmode(0);
|
|
||||||
file_loaded = false;
|
file_loaded = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -618,7 +616,6 @@ void lua_init(const char* filename, const bool start_playing) {
|
|||||||
debug("RUNTIME ERROR:", false);
|
debug("RUNTIME ERROR:", false);
|
||||||
debug(lua_tostring(L, -1));
|
debug(lua_tostring(L, -1));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
setmode(0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -663,7 +660,6 @@ void lua_call_init() {
|
|||||||
debug("RUNTIME ERROR:",false);
|
debug("RUNTIME ERROR:",false);
|
||||||
debug(lua_tostring(L, -1));
|
debug(lua_tostring(L, -1));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
setmode(0);
|
|
||||||
lua_state = STATE_STOPPED;
|
lua_state = STATE_STOPPED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -676,7 +672,6 @@ void lua_call_update() {
|
|||||||
debug("RUNTIME ERROR:",false);
|
debug("RUNTIME ERROR:",false);
|
||||||
debug(lua_tostring(L, -1));
|
debug(lua_tostring(L, -1));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
setmode(0);
|
|
||||||
lua_state = STATE_STOPPED;
|
lua_state = STATE_STOPPED;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
150
main.cpp
150
main.cpp
@@ -12,6 +12,7 @@ void do_terminal();
|
|||||||
|
|
||||||
void init_code_editor();
|
void init_code_editor();
|
||||||
void do_code_editor();
|
void do_code_editor();
|
||||||
|
void save_code();
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (btnp(KEY_TAB)) {
|
if (btnp(KEY_TAB)) {
|
||||||
@@ -35,6 +36,10 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void execute_run() {
|
||||||
|
if (current_editor == 1) save_code();
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t get_char(uint8_t key) {
|
uint8_t get_char(uint8_t key) {
|
||||||
SDL_Keymod mods = SDL_GetModState();
|
SDL_Keymod mods = SDL_GetModState();
|
||||||
if (key != KEY_UNKNOWN) {
|
if (key != KEY_UNKNOWN) {
|
||||||
@@ -93,6 +98,15 @@ void do_terminal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void save_code() {
|
||||||
|
const char* file = get_filename();
|
||||||
|
FILE *f = fopen(file, "w");
|
||||||
|
for (std::list<std::string>::iterator it = code.begin(); it != code.end(); it++) {
|
||||||
|
fprintf(f, "%s\n", (*it).c_str());
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
void load_code() {
|
void load_code() {
|
||||||
const char* file = get_filename();
|
const char* file = get_filename();
|
||||||
FILE *f = fopen(file, "rb");
|
FILE *f = fopen(file, "rb");
|
||||||
@@ -114,7 +128,7 @@ void load_code() {
|
|||||||
buffer[pos]='\0';
|
buffer[pos]='\0';
|
||||||
code.push_back(&buffer[start]);
|
code.push_back(&buffer[start]);
|
||||||
start=pos+1;
|
start=pos+1;
|
||||||
if (buffer[start] == '\n') start++;
|
if (buffer[start] == '\n') {pos++; start++;}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -127,6 +141,117 @@ static int line = 0;
|
|||||||
bool cursor_inverted = false;
|
bool cursor_inverted = false;
|
||||||
int blink_wait = 30;
|
int blink_wait = 30;
|
||||||
|
|
||||||
|
#define PARSER_NOTHING 0
|
||||||
|
#define PARSER_NUMBER 1
|
||||||
|
#define PARSER_IDENTIFIER 2
|
||||||
|
#define PARSER_STRING 3
|
||||||
|
#define PARSER_COMMENT 4
|
||||||
|
#define PARSER_FUNCTION 5
|
||||||
|
|
||||||
|
uint16_t parser_offset = 0;
|
||||||
|
uint8_t parser_pos = 0;
|
||||||
|
uint8_t parser_start=0;
|
||||||
|
void parser_setup(uint16_t address) {
|
||||||
|
parser_offset = address;
|
||||||
|
parser_pos = 0;
|
||||||
|
parser_start = 0;
|
||||||
|
}
|
||||||
|
const uint8_t parser_get_char(uint8_t offset=0) {
|
||||||
|
if ((parser_pos+offset) >= 80) return 0;
|
||||||
|
return peek(parser_offset+parser_pos+offset);
|
||||||
|
}
|
||||||
|
char alpha[80] = "";
|
||||||
|
uint8_t alpha_pos = 0;
|
||||||
|
void parser_next(const bool keep=false) {
|
||||||
|
if (keep) { alpha[alpha_pos++] = parser_get_char(); alpha[alpha_pos]=0; } else { alpha[0]=alpha_pos=0; }
|
||||||
|
if (parser_pos < 80) parser_pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ISZERO (parser_get_char()=='0')
|
||||||
|
#define ISX (parser_get_char()=='z' || parser_get_char()=='Z')
|
||||||
|
#define ISDIGIT (parser_get_char()>='0' && parser_get_char() <='9')
|
||||||
|
#define WILLBEDIGIT (parser_get_char(1)>='0' && parser_get_char(1) <='9')
|
||||||
|
#define ISPOINT (parser_get_char()=='.')
|
||||||
|
#define ISALPHA (parser_get_char()>='_' || (parser_get_char()>='a' && parser_get_char()<='z') || (parser_get_char()>='A' && parser_get_char()<='Z'))
|
||||||
|
#define ISQUOTES (parser_get_char()=='"' || parser_get_char()=='\'')
|
||||||
|
#define ISMINUS (parser_get_char()=='-')
|
||||||
|
#define WILLBEMINUS (parser_get_char(1)=='-')
|
||||||
|
#define ISPAREN (parser_get_char()=='(')
|
||||||
|
#define ENDED (parser_get_char()==0)
|
||||||
|
|
||||||
|
const char* keywords[] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while" };
|
||||||
|
|
||||||
|
const bool is_keyword() {
|
||||||
|
for (int i=0;i<22;++i) if (strcmp(alpha, keywords[i]) == 0) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void parser_do_color(uint8_t type) {
|
||||||
|
uint8_t color = COLOR_WHITE;
|
||||||
|
switch (type) {
|
||||||
|
case PARSER_NUMBER: color = COLOR_LIGHT_GREEN; break;
|
||||||
|
case PARSER_COMMENT: color = COLOR_LIGHT_GRAY; break;
|
||||||
|
case PARSER_STRING: color = COLOR_RED; break;
|
||||||
|
case PARSER_IDENTIFIER: if (is_keyword()) color = COLOR_MAGENTA; break;
|
||||||
|
case PARSER_FUNCTION: color = COLOR_YELLOW; break;
|
||||||
|
};
|
||||||
|
color = (color&0x0f)+(0x10);
|
||||||
|
for (int i=parser_start;i<parser_pos;++i) poke(0x1200+parser_offset+i, color);
|
||||||
|
parser_start = parser_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void refresh_line_color(uint8_t line) {
|
||||||
|
parser_setup(80*(line+1));
|
||||||
|
while (!ENDED) {
|
||||||
|
uint8_t result = PARSER_NOTHING;
|
||||||
|
if (ISZERO) {
|
||||||
|
result=PARSER_NUMBER; parser_next();
|
||||||
|
if (ISX) {
|
||||||
|
if (WILLBEDIGIT) {
|
||||||
|
parser_next();
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
}
|
||||||
|
} else if (ISDIGIT) {
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
if (ISPOINT) {
|
||||||
|
parser_next();
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
}
|
||||||
|
} else if (ISPOINT) {
|
||||||
|
parser_next();
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
}
|
||||||
|
} else if (ISDIGIT) {
|
||||||
|
result = PARSER_NUMBER;
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
if (ISPOINT) {
|
||||||
|
parser_next();
|
||||||
|
while (ISDIGIT) parser_next();
|
||||||
|
}
|
||||||
|
} else if (ISALPHA) {
|
||||||
|
result = PARSER_IDENTIFIER;
|
||||||
|
while (ISALPHA || ISDIGIT) parser_next(true);
|
||||||
|
if (ISPAREN) result = PARSER_FUNCTION;
|
||||||
|
} else if (ISQUOTES) {
|
||||||
|
result = PARSER_STRING; parser_next();
|
||||||
|
while (!ISQUOTES && !ENDED) parser_next();
|
||||||
|
parser_next();
|
||||||
|
} else if (ISMINUS) {
|
||||||
|
if (WILLBEMINUS) {
|
||||||
|
result = PARSER_COMMENT;
|
||||||
|
while (!ENDED) parser_next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result == PARSER_NOTHING) parser_next();
|
||||||
|
parser_do_color(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void refresh_color_highlight() {
|
||||||
|
cursor_inverted = false;
|
||||||
|
for (int i=0;i<28;++i) refresh_line_color(i);
|
||||||
|
}
|
||||||
|
|
||||||
void refresh_code_editor() {
|
void refresh_code_editor() {
|
||||||
color(COLOR_WHITE, COLOR_BLUE);
|
color(COLOR_WHITE, COLOR_BLUE);
|
||||||
cls();
|
cls();
|
||||||
@@ -141,10 +266,12 @@ void refresh_code_editor() {
|
|||||||
print((*it).c_str(), 0, i+1);
|
print((*it).c_str(), 0, i+1);
|
||||||
++i; ++it;
|
++i; ++it;
|
||||||
}
|
}
|
||||||
|
refresh_color_highlight();
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_code_editor() {
|
void init_code_editor() {
|
||||||
setmode(3);
|
setmode(3);
|
||||||
|
code.clear();
|
||||||
load_code();
|
load_code();
|
||||||
refresh_code_editor();
|
refresh_code_editor();
|
||||||
}
|
}
|
||||||
@@ -204,6 +331,18 @@ void move_cursor_left() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void move_line_end() {
|
||||||
|
if (cursor_inverted) invert_cursor();
|
||||||
|
col = get_current_line().size();
|
||||||
|
blink_wait=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_line_home() {
|
||||||
|
if (cursor_inverted) invert_cursor();
|
||||||
|
col = 0;
|
||||||
|
blink_wait=1;
|
||||||
|
}
|
||||||
|
|
||||||
void split_line() {
|
void split_line() {
|
||||||
std::string str = get_current_line();
|
std::string str = get_current_line();
|
||||||
*ls[line] = str.substr(col);
|
*ls[line] = str.substr(col);
|
||||||
@@ -244,6 +383,12 @@ void delete_char() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void supr_char() {
|
||||||
|
if ((col == (*ls[line]).size()) && (line == code.size()-1)) return;
|
||||||
|
move_cursor_right();
|
||||||
|
delete_char();
|
||||||
|
}
|
||||||
|
|
||||||
void add_char(uint8_t chr) {
|
void add_char(uint8_t chr) {
|
||||||
std::string str = get_current_line();
|
std::string str = get_current_line();
|
||||||
std::string a = str.substr(0,col);
|
std::string a = str.substr(0,col);
|
||||||
@@ -266,6 +411,9 @@ void do_code_editor() {
|
|||||||
else if (key == KEY_LEFT) move_cursor_left();
|
else if (key == KEY_LEFT) move_cursor_left();
|
||||||
else if (key == KEY_RIGHT) move_cursor_right();
|
else if (key == KEY_RIGHT) move_cursor_right();
|
||||||
else if (key == KEY_BACKSPACE) delete_char();
|
else if (key == KEY_BACKSPACE) delete_char();
|
||||||
|
else if (key == KEY_DELETE) supr_char();
|
||||||
|
else if (key == KEY_END) move_line_end();
|
||||||
|
else if (key == KEY_HOME) move_line_home();
|
||||||
else {
|
else {
|
||||||
uint8_t chr = get_char(key);
|
uint8_t chr = get_char(key);
|
||||||
if (chr != 0) add_char(chr);
|
if (chr != 0) add_char(chr);
|
||||||
|
|||||||
Reference in New Issue
Block a user