2021-01-02 16:41:53 +00:00
|
|
|
#include <ncurses.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include "../src/Parser/Lexer/Lexer.hpp"
|
|
|
|
#include "../src/Parser/Parser.hpp"
|
2021-01-03 14:10:43 +00:00
|
|
|
#include "../src/Parser/Statements/ParsedStatementStringifier.hpp"
|
2021-01-04 23:13:42 +00:00
|
|
|
#include "InputWindow.hpp"
|
2021-01-02 18:08:58 +00:00
|
|
|
|
2021-01-05 11:37:13 +00:00
|
|
|
void ParseAndUpdate(const std::vector<std::u8string>& lines, WINDOW* diagnosticsWindow, WINDOW* parsedWindow,
|
2021-01-04 23:13:42 +00:00
|
|
|
MalachScriptRepl::InputWindow& inputWindow) {
|
2021-01-02 16:41:53 +00:00
|
|
|
std::u8string script;
|
|
|
|
for (const auto& line : lines) {
|
|
|
|
script += line;
|
|
|
|
script += '\n';
|
|
|
|
}
|
|
|
|
auto logger = MalachScript::Diagnostics::Logger();
|
|
|
|
auto lexer = MalachScript::Parser::Lexer(u8"diag", script, &logger);
|
|
|
|
const auto* firstToken = lexer.Lex();
|
|
|
|
[[maybe_unused]] const auto* parsedResult = MalachScript::Parser::Parser::Parse(firstToken, u8"diag", &logger);
|
2021-01-02 18:08:58 +00:00
|
|
|
|
|
|
|
const MalachScript::Diagnostics::Diagnostic* diag = nullptr;
|
2021-01-02 16:41:53 +00:00
|
|
|
wclear(diagnosticsWindow);
|
|
|
|
|
2021-01-03 13:42:26 +00:00
|
|
|
if (!logger.GetMessages().empty()) {
|
2021-01-02 18:08:58 +00:00
|
|
|
diag = &logger.GetMessages()[0];
|
|
|
|
|
|
|
|
wattron(diagnosticsWindow, COLOR_PAIR(1));
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, '[');
|
|
|
|
waddstr(diagnosticsWindow, "Error");
|
|
|
|
waddch(diagnosticsWindow, ']');
|
2021-01-02 18:08:58 +00:00
|
|
|
wattroff(diagnosticsWindow, COLOR_PAIR(1));
|
|
|
|
waddch(diagnosticsWindow, ' ');
|
|
|
|
waddstr(diagnosticsWindow, std::to_string(diag->GetSpan().GetStart() + 1).c_str());
|
|
|
|
waddch(diagnosticsWindow, '-');
|
|
|
|
waddstr(diagnosticsWindow, std::to_string(diag->GetSpan().GetEnd() + 1).c_str());
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, ' ');
|
2021-01-02 18:08:58 +00:00
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
waddstr(diagnosticsWindow,
|
2021-01-02 18:08:58 +00:00
|
|
|
MalachScript::Diagnostics::DiagnosticTypeHelper::ToEnglishString(diag->GetType()).c_str());
|
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, '\n');
|
|
|
|
}
|
|
|
|
wrefresh(diagnosticsWindow);
|
|
|
|
|
|
|
|
wclear(parsedWindow);
|
2021-01-05 11:37:13 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
MalachScript::Parser::ParsedStatementStringifier::Stringify(parsedResult, ss, "", true);
|
2021-01-02 16:41:53 +00:00
|
|
|
|
2021-01-05 11:37:13 +00:00
|
|
|
waddstr(parsedWindow, ss.str().c_str());
|
2021-01-02 16:41:53 +00:00
|
|
|
wrefresh(parsedWindow);
|
2021-01-02 18:08:58 +00:00
|
|
|
|
2021-01-04 23:13:42 +00:00
|
|
|
inputWindow.SetScriptWithDiagnostics(script, diag);
|
2021-01-04 12:33:52 +00:00
|
|
|
delete parsedResult;
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
2021-01-03 13:42:26 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2021-01-02 16:41:53 +00:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
keypad(stdscr, true);
|
|
|
|
set_tabsize(4);
|
|
|
|
|
2021-01-02 18:08:58 +00:00
|
|
|
start_color();
|
|
|
|
init_pair(1, COLOR_BLACK, COLOR_RED);
|
|
|
|
clear();
|
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
int maxX;
|
|
|
|
int maxY;
|
|
|
|
getmaxyx(stdscr, maxY, maxX);
|
|
|
|
wresize(stdscr, maxY, maxX);
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
|
|
|
const int inputFieldSize = 30;
|
|
|
|
|
|
|
|
wborder(stdscr, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
wmove(stdscr, 31, 1);
|
|
|
|
whline(stdscr, ACS_HLINE, maxX - 2);
|
|
|
|
wmove(stdscr, 32, maxX / 2);
|
|
|
|
wvline(stdscr, ACS_VLINE, 25);
|
|
|
|
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
2021-01-04 23:13:42 +00:00
|
|
|
[[maybe_unused]] auto* diagnosticsWindow = newwin(25, (maxX - 4) / 2, 32, 2);
|
2021-01-02 16:41:53 +00:00
|
|
|
|
|
|
|
auto* parsedResultWindow = newwin(25, (maxX - 4) / 2 - 2, 32, (maxX - 4) / 2 + 4);
|
|
|
|
wrefresh(parsedResultWindow);
|
|
|
|
|
2021-01-04 23:13:42 +00:00
|
|
|
auto inputWindow = MalachScriptRepl::InputWindow(inputFieldSize, maxX - 3, 1, 2);
|
|
|
|
inputWindow.RegisterOnChange(
|
|
|
|
[diagnosticsWindow, parsedResultWindow, &inputWindow](const std::vector<std::u8string>& lines) {
|
|
|
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
|
|
|
});
|
2021-01-02 16:41:53 +00:00
|
|
|
|
|
|
|
int c;
|
|
|
|
bool running = true;
|
|
|
|
while (running) {
|
2021-01-04 23:13:42 +00:00
|
|
|
c = inputWindow.GetInput();
|
2021-01-02 16:41:53 +00:00
|
|
|
switch (c) {
|
|
|
|
case 27: running = false; break;
|
|
|
|
|
2021-01-04 23:13:42 +00:00
|
|
|
case KEY_LEFT: inputWindow.MoveCursorLeft(); break;
|
|
|
|
case KEY_RIGHT: inputWindow.MoveCursorRight(); break;
|
|
|
|
case KEY_UP: inputWindow.MoveCursorUp(); break;
|
|
|
|
case KEY_DOWN: inputWindow.MoveCursorDown(); break;
|
2021-01-02 16:41:53 +00:00
|
|
|
case KEY_BACKSPACE:
|
2021-01-04 23:13:42 +00:00
|
|
|
case 127: inputWindow.Backspace(); break;
|
2021-01-02 16:41:53 +00:00
|
|
|
case 10:
|
2021-01-04 23:13:42 +00:00
|
|
|
case KEY_ENTER: inputWindow.Return(); break;
|
2021-01-02 16:41:53 +00:00
|
|
|
case 9:
|
2021-01-04 23:13:42 +00:00
|
|
|
case KEY_STAB: inputWindow.Tab(); break;
|
|
|
|
case KEY_END: inputWindow.GoToEndOfLine(); break;
|
|
|
|
case KEY_HOME: inputWindow.GoToStartOfLine(); break;
|
|
|
|
default: inputWindow.Input(c); break;
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
2021-01-04 23:13:42 +00:00
|
|
|
inputWindow.Refresh();
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
endwin();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|