From fe9589cc829d69534f7b9db526e3088807469196 Mon Sep 17 00:00:00 2001 From: Ruben Dahl Date: Sun, 30 Jul 2023 19:22:43 +0200 Subject: [PATCH] Started cleanup of code Restarting work on this, going to do some refactoring on the project first. --- src/README.md | 28 +--------- src/colors.cc | 140 ++++++++++++++++++++++++++++++++++------------ src/colors.h | 70 +++++++++++------------ src/lang.cc | 74 ++++++------------------ src/lang.h | 94 +------------------------------ src_old/README.md | 27 +++++++++ src_old/colors.cc | 37 ++++++++++++ src_old/colors.h | 39 +++++++++++++ src_old/lang.cc | 66 ++++++++++++++++++++++ src_old/lang.h | 102 +++++++++++++++++++++++++++++++++ syntax/install.sh | 3 + 11 files changed, 435 insertions(+), 245 deletions(-) create mode 100644 src_old/README.md create mode 100644 src_old/colors.cc create mode 100644 src_old/colors.h create mode 100644 src_old/lang.cc create mode 100644 src_old/lang.h diff --git a/src/README.md b/src/README.md index 298e388..77ab00d 100644 --- a/src/README.md +++ b/src/README.md @@ -1,27 +1 @@ -# CeeVLang Documentation - -## Includes - -```cpp -#include "colors.h" -#include -#include -#include -#include -#include -#include -#include -``` - -## Functions - -```cpp -int read_file(std::deque args); -``` -* Param: std::deque\ args - The arguments from main() in ceev.cc, with used -values stripped out. - -* Returns: int exit_code - -* Reads the file and outputs it. -Currently a work in progress. +# CeeV Lang Rewrite diff --git a/src/colors.cc b/src/colors.cc index 14616db..d150dfe 100644 --- a/src/colors.cc +++ b/src/colors.cc @@ -1,37 +1,107 @@ #include "colors.h" -std::string black(std::string input) {return "\x1b[30m" + input + "\x1b[0m";} -std::string red(std::string input) {return "\x1b[31m" + input + "\x1b[0m";} -std::string green(std::string input) {return "\x1b[32m" + input + "\x1b[0m";} -std::string yellow(std::string input) {return "\x1b[33m" + input + "\x1b[0m";} -std::string blue(std::string input) {return "\x1b[34m" + input + "\x1b[0m";} -std::string magenta(std::string input) {return "\x1b[35m" + input + "\x1b[0m";} -std::string cyan(std::string input) {return "\x1b[36m" + input + "\x1b[0m";} -std::string white(std::string input) {return "\x1b[37m" + input + "\x1b[0m";} -std::string bright_black(std::string input) {return "\x1b[90m" + input + "\x1b[0m";} -std::string bright_red(std::string input) {return "\x1b[91m" + input + "\x1b[0m";} -std::string bright_green(std::string input) {return "\x1b[92m" + input + "\x1b[0m";} -std::string bright_yellow(std::string input) {return "\x1b[93m" + input + "\x1b[0m";} -std::string bright_blue(std::string input) {return "\x1b[94m" + input + "\x1b[0m";} -std::string bright_magenta(std::string input) {return "\x1b[95m" + input + "\x1b[0m";} -std::string bright_cyan(std::string input) {return "\x1b[96m" + input + "\x1b[0m";} -std::string bright_white(std::string input) {return "\x1b[97m" + input + "\x1b[0m";} -std::string bg_black(std::string input) {return "\x1b[40m" + input + "\x1b[0m";} -std::string bg_red(std::string input) {return "\x1b[41m" + input + "\x1b[0m";} -std::string bg_green(std::string input) {return "\x1b[42m" + input + "\x1b[0m";} -std::string bg_yellow(std::string input) {return "\x1b[43m" + input + "\x1b[0m";} -std::string bg_blue(std::string input) {return "\x1b[44m" + input + "\x1b[0m";} -std::string bg_magenta(std::string input) {return "\x1b[45m" + input + "\x1b[0m";} -std::string bg_cyan(std::string input) {return "\x1b[46m" + input + "\x1b[0m";} -std::string bg_white(std::string input) {return "\x1b[47m" + input + "\x1b[0m";} -std::string bg_bright_black(std::string input) {return "\x1b[100m" + input + "\x1b[0m";} -std::string bg_bright_red(std::string input) {return "\x1b[101m" + input + "\x1b[0m";} -std::string bg_bright_green(std::string input) {return "\x1b[102m" + input + "\x1b[0m";} -std::string bg_bright_yellow(std::string input) {return "\x1b[103m" + input + "\x1b[0m";} -std::string bg_bright_blue(std::string input) {return "\x1b[104m" + input + "\x1b[0m";} -std::string bg_bright_magenta(std::string input) {return "\x1b[105m" + input + "\x1b[0m";} -std::string bg_bright_cyan(std::string input) {return "\x1b[106m" + input + "\x1b[0m";} -std::string bg_bright_white(std::string input) {return "\x1b[107m" + input + "\x1b[0m";} -std::string bold(std::string input) {return "\x1b[1m" + input + "\x1b[0m";} -std::string underline(std::string input) {return "\x1b[4m" + input + "\x1b[0m";} -std::string reverse(std::string input) {return "\x1b[7m" + input + "\x1b[0m";} \ No newline at end of file +static std::string black(const std::string& input) { + return "\x1b[30m" + input + "\x1b[0m"; +} +static std::string red(const std::string& input) { + return "\x1b[31m" + input + "\x1b[0m"; +} +static std::string green(const std::string& input) { + return "\x1b[32m" + input + "\x1b[0m"; +} +static std::string yellow(const std::string& input) { + return "\x1b[33m" + input + "\x1b[0m"; +} +static std::string blue(const std::string& input) { + return "\x1b[34m" + input + "\x1b[0m"; +} +static std::string magenta(const std::string& input) { + return "\x1b[35m" + input + "\x1b[0m"; +} +static std::string cyan(const std::string& input) { + return "\x1b[36m" + input + "\x1b[0m"; +} +static std::string white(const std::string& input) { + return "\x1b[37m" + input + "\x1b[0m"; +} +static std::string bright_black(const std::string& input) { + return "\x1b[90m" + input + "\x1b[0m"; +} +static std::string bright_red(const std::string& input) { + return "\x1b[91m" + input + "\x1b[0m"; +} +static std::string bright_green(const std::string& input) { + return "\x1b[92m" + input + "\x1b[0m"; +} +static std::string bright_yellow(const std::string& input) { + return "\x1b[93m" + input + "\x1b[0m"; +} +static std::string bright_blue(const std::string& input) { + return "\x1b[94m" + input + "\x1b[0m"; +} +static std::string bright_magenta(const std::string& input) { + return "\x1b[95m" + input + "\x1b[0m"; +} +static std::string bright_cyan(const std::string& input) { + return "\x1b[96m" + input + "\x1b[0m"; +} +static std::string bright_white(const std::string& input) { + return "\x1b[97m" + input + "\x1b[0m"; +} +static std::string bg_black(const std::string& input) { + return "\x1b[40m" + input + "\x1b[0m"; +} +static std::string bg_red(const std::string& input) { + return "\x1b[41m" + input + "\x1b[0m"; +} +static std::string bg_green(const std::string& input) { + return "\x1b[42m" + input + "\x1b[0m"; +} +static std::string bg_yellow(const std::string& input) { + return "\x1b[43m" + input + "\x1b[0m"; +} +static std::string bg_blue(const std::string& input) { + return "\x1b[44m" + input + "\x1b[0m"; +} +static std::string bg_magenta(const std::string& input) { + return "\x1b[45m" + input + "\x1b[0m"; +} +static std::string bg_cyan(const std::string& input) { + return "\x1b[46m" + input + "\x1b[0m"; +} +static std::string bg_white(const std::string& input) { + return "\x1b[47m" + input + "\x1b[0m"; +} +static std::string bg_bright_black(const std::string& input) { + return "\x1b[100m" + input + "\x1b[0m"; +} +static std::string bg_bright_red(const std::string& input) { + return "\x1b[101m" + input + "\x1b[0m"; +} +static std::string bg_bright_green(const std::string& input) { + return "\x1b[102m" + input + "\x1b[0m"; +} +static std::string bg_bright_yellow(const std::string& input) { + return "\x1b[103m" + input + "\x1b[0m"; +} +static std::string bg_bright_blue(const std::string& input) { + return "\x1b[104m" + input + "\x1b[0m"; +} +static std::string bg_bright_magenta(const std::string& input) { + return "\x1b[105m" + input + "\x1b[0m"; +} +static std::string bg_bright_cyan(const std::string& input) { + return "\x1b[106m" + input + "\x1b[0m"; +} +static std::string bg_bright_white(const std::string& input) { + return "\x1b[107m" + input + "\x1b[0m"; +} +static std::string bold(const std::string& input) { + return "\x1b[1m" + input + "\x1b[0m"; +} +static std::string underline(const std::string& input) { + return "\x1b[4m" + input + "\x1b[0m"; +} +static std::string reverse(const std::string& input) { + return "\x1b[7m" + input + "\x1b[0m"; +} diff --git a/src/colors.h b/src/colors.h index d177240..a7afa62 100644 --- a/src/colors.h +++ b/src/colors.h @@ -2,38 +2,38 @@ #include -std::string black(std::string input); -std::string red(std::string input); -std::string green(std::string input); -std::string yellow(std::string input); -std::string blue(std::string input); -std::string magenta(std::string input); -std::string cyan(std::string input); -std::string white(std::string input); -std::string bright_black(std::string input); -std::string bright_red(std::string input); -std::string bright_green(std::string input); -std::string bright_yellow(std::string input); -std::string bright_blue(std::string input); -std::string bright_magenta(std::string input); -std::string bright_cyan(std::string input); -std::string bright_white(std::string input); -std::string bg_black(std::string input); -std::string bg_red(std::string input); -std::string bg_green(std::string input); -std::string bg_yellow(std::string input); -std::string bg_blue(std::string input); -std::string bg_magenta(std::string input); -std::string bg_cyan(std::string input); -std::string bg_white(std::string input); -std::string bg_bright_black(std::string input); -std::string bg_bright_red(std::string input); -std::string bg_bright_green(std::string input); -std::string bg_bright_yellow(std::string input); -std::string bg_bright_blue(std::string input); -std::string bg_bright_magenta(std::string input); -std::string bg_bright_cyan(std::string input); -std::string bg_bright_white(std::string input); -std::string bold(std::string input); -std::string underline(std::string input); -std::string reverse(std::string input); \ No newline at end of file +static std::string black(const std::string& input); +static std::string red(const std::string& input); +static std::string green(const std::string& input); +static std::string yellow(const std::string& input); +static std::string blue(const std::string& input); +static std::string magenta(const std::string& input); +static std::string cyan(const std::string& input); +static std::string white(const std::string& input); +static std::string bright_black(const std::string& input); +static std::string bright_red(const std::string& input); +static std::string bright_green(const std::string& input); +static std::string bright_yellow(const std::string& input); +static std::string bright_blue(const std::string& input); +static std::string bright_magenta(const std::string& input); +static std::string bright_cyan(const std::string& input); +static std::string bright_white(const std::string& input); +static std::string bg_black(const std::string& input); +static std::string bg_red(const std::string& input); +static std::string bg_green(const std::string& input); +static std::string bg_yellow(const std::string& input); +static std::string bg_blue(const std::string& input); +static std::string bg_magenta(const std::string& input); +static std::string bg_cyan(const std::string& input); +static std::string bg_white(const std::string& input); +static std::string bg_bright_black(const std::string& input); +static std::string bg_bright_red(const std::string& input); +static std::string bg_bright_green(const std::string& input); +static std::string bg_bright_yellow(const std::string& input); +static std::string bg_bright_blue(const std::string& input); +static std::string bg_bright_magenta(const std::string& input); +static std::string bg_bright_cyan(const std::string& input); +static std::string bg_bright_white(const std::string& input); +static std::string bold(const std::string&& input); +static std::string underline(const std::string& input); +static std::string reverse(const std::string& input); diff --git a/src/lang.cc b/src/lang.cc index 3eb082d..536df23 100644 --- a/src/lang.cc +++ b/src/lang.cc @@ -1,63 +1,25 @@ #include "lang.h" int read_file(std::deque args) { - if (args.empty()) { - std::cout << bg_red(bold("ERROR")) << ": No file input" << std::endl; - return 1; - } - fs::path filename = args[0]; - args.pop_front(); - std::ifstream file(filename); - if (!file) { - std::cout << bg_red(bold("ERROR")) << ": Could not find file" << std::endl; - return 1; - } - std::string line; - while (std::getline(file, line)) { - std::cout << line << std::endl; - } - return 0; + if (args.empty()) { + std::cout << bg_red(bold("ERROR")) << ": No file input" << std::endl; + return 1; + } + fs::path filename = args[0]; + args.pop_front(); + std::ifstream file(filename); + if (!file) { + std::cout << bg_red(bold("ERROR")) << ": Could not find file" + << std::endl; + return 1; + } + std::string line; + while (std::getline(file, line)) { + std::cout << line << std::endl; + } + return 0; } int parse_line(std::string line) { - struct parsed_token parsed { - empty_token, empty_token - }; - if (int sub = line.find("//")) - line = line.substr(sub); - std::deque tokens; - int start = 0; - size_t end = line.find(" "); - while (end != std::string::npos) { - tokens.push_back(line.substr(start, end - start)); - start = end + 1; - end = line.find(" ", start); - } - for (std::string &token : tokens) { - try { - Token curr_token = CV_Tokens.at(token); - parsed.lastToken = parsed.currentToken; - if (curr_token == parsed.lastToken && curr_token.type == TokenType::SPACE) - continue; - else { - // TODO: Line number handling for better errors - std::cerr << "Invalid token `" << token << "'." << std::endl; - return 1; - } - parsed.currentToken = curr_token; - } catch (std::out_of_range &oor) { - // this is most likely a variable or a string - if (parsed.lastToken.type == TokenType::INTRINSIC && - parsed.lastToken.intrinsic == Intrinsic::LET) { - VariableToken var{TokenType::VARIABLE, Intrinsic::EMPTY, - Constant::EMPTY, "", token}; - parsed.currentToken = var; - var_tokens.push_back(var); - } - // string parsing - std::cerr << "Not implemented. Found `" << token << "'." << std::endl; - continue; - } - } - return 0; + return 0; } diff --git a/src/lang.h b/src/lang.h index 2a9ce16..1253629 100644 --- a/src/lang.h +++ b/src/lang.h @@ -1,102 +1,12 @@ #pragma once -#include "colors.h" #include #include #include #include -#include -#include -#include -#include -#include +#include "colors.h" namespace fs = std::filesystem; -enum class TokenType { - EMPTY, - SPACE, - LEFT_PAREN, - RIGHT_PAREN, - COLONCOLON, - EQ, - COMMA, - STRING, - INTRINSIC, - CONSTANT, - VARIABLE -}; - -enum class Intrinsic { EMPTY, LET, UNLET, ENTER, EXIT, SHOW, HIDE }; - -enum class Constant { EMPTY, CHARACTER, LOCATION, IMAGE }; - -struct Token { - TokenType type; - Intrinsic intrinsic; - Constant constant; - std::string string; - std::string name; - inline bool operator==(const Token &right) { - if (this->type == right.type && this->intrinsic == right.intrinsic && - this->constant == right.constant && this->string == right.string && - this->name == right.name) - return true; - return false; - } - inline bool operator!=(const Token &right) { return !(*this == right); } -}; - -struct VariableToken : Token { - Token value; - inline bool operator==(const VariableToken &right) { - if ((Token) * this == (Token)right && this->value == right.value) - return true; - return false; - } - inline bool operator!=(const VariableToken &right) { - return !(*this == right); - } -}; - -std::map CV_Tokens{ - {"", Token{TokenType::EMPTY, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {" ", Token{TokenType::SPACE, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {"(", - Token{TokenType::LEFT_PAREN, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {")", - Token{TokenType::RIGHT_PAREN, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {"::", - Token{TokenType::COLONCOLON, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {"=", Token{TokenType::EQ, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {",", Token{TokenType::COMMA, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, - {"let", - Token{TokenType::INTRINSIC, Intrinsic::LET, Constant::EMPTY, "", ""}}, - {"unlet", - Token{TokenType::INTRINSIC, Intrinsic::UNLET, Constant::EMPTY, "", ""}}, - {"enter", - Token{TokenType::INTRINSIC, Intrinsic::ENTER, Constant::EMPTY, "", ""}}, - {"exit", - Token{TokenType::INTRINSIC, Intrinsic::EXIT, Constant::EMPTY, "", ""}}, - {"show", - Token{TokenType::INTRINSIC, Intrinsic::SHOW, Constant::EMPTY, "", ""}}, - {"hide", - Token{TokenType::INTRINSIC, Intrinsic::HIDE, Constant::EMPTY, "", ""}}, - {"Character", - Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::CHARACTER, "", ""}}, - {"Location", - Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::LOCATION, "", ""}}, - {"Image", - Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::IMAGE, "", ""}}}; - -Token empty_token{TokenType::EMPTY, Intrinsic::EMPTY, Constant::EMPTY, "", ""}; - -struct parsed_token { - Token lastToken; - Token currentToken; -}; - -std::vector var_tokens; - int read_file(std::deque args); -int parse_line(std::string line); +int parse_file(std::string file); diff --git a/src_old/README.md b/src_old/README.md new file mode 100644 index 0000000..298e388 --- /dev/null +++ b/src_old/README.md @@ -0,0 +1,27 @@ +# CeeVLang Documentation + +## Includes + +```cpp +#include "colors.h" +#include +#include +#include +#include +#include +#include +#include +``` + +## Functions + +```cpp +int read_file(std::deque args); +``` +* Param: std::deque\ args - The arguments from main() in ceev.cc, with used +values stripped out. + +* Returns: int exit_code + +* Reads the file and outputs it. +Currently a work in progress. diff --git a/src_old/colors.cc b/src_old/colors.cc new file mode 100644 index 0000000..14616db --- /dev/null +++ b/src_old/colors.cc @@ -0,0 +1,37 @@ +#include "colors.h" + +std::string black(std::string input) {return "\x1b[30m" + input + "\x1b[0m";} +std::string red(std::string input) {return "\x1b[31m" + input + "\x1b[0m";} +std::string green(std::string input) {return "\x1b[32m" + input + "\x1b[0m";} +std::string yellow(std::string input) {return "\x1b[33m" + input + "\x1b[0m";} +std::string blue(std::string input) {return "\x1b[34m" + input + "\x1b[0m";} +std::string magenta(std::string input) {return "\x1b[35m" + input + "\x1b[0m";} +std::string cyan(std::string input) {return "\x1b[36m" + input + "\x1b[0m";} +std::string white(std::string input) {return "\x1b[37m" + input + "\x1b[0m";} +std::string bright_black(std::string input) {return "\x1b[90m" + input + "\x1b[0m";} +std::string bright_red(std::string input) {return "\x1b[91m" + input + "\x1b[0m";} +std::string bright_green(std::string input) {return "\x1b[92m" + input + "\x1b[0m";} +std::string bright_yellow(std::string input) {return "\x1b[93m" + input + "\x1b[0m";} +std::string bright_blue(std::string input) {return "\x1b[94m" + input + "\x1b[0m";} +std::string bright_magenta(std::string input) {return "\x1b[95m" + input + "\x1b[0m";} +std::string bright_cyan(std::string input) {return "\x1b[96m" + input + "\x1b[0m";} +std::string bright_white(std::string input) {return "\x1b[97m" + input + "\x1b[0m";} +std::string bg_black(std::string input) {return "\x1b[40m" + input + "\x1b[0m";} +std::string bg_red(std::string input) {return "\x1b[41m" + input + "\x1b[0m";} +std::string bg_green(std::string input) {return "\x1b[42m" + input + "\x1b[0m";} +std::string bg_yellow(std::string input) {return "\x1b[43m" + input + "\x1b[0m";} +std::string bg_blue(std::string input) {return "\x1b[44m" + input + "\x1b[0m";} +std::string bg_magenta(std::string input) {return "\x1b[45m" + input + "\x1b[0m";} +std::string bg_cyan(std::string input) {return "\x1b[46m" + input + "\x1b[0m";} +std::string bg_white(std::string input) {return "\x1b[47m" + input + "\x1b[0m";} +std::string bg_bright_black(std::string input) {return "\x1b[100m" + input + "\x1b[0m";} +std::string bg_bright_red(std::string input) {return "\x1b[101m" + input + "\x1b[0m";} +std::string bg_bright_green(std::string input) {return "\x1b[102m" + input + "\x1b[0m";} +std::string bg_bright_yellow(std::string input) {return "\x1b[103m" + input + "\x1b[0m";} +std::string bg_bright_blue(std::string input) {return "\x1b[104m" + input + "\x1b[0m";} +std::string bg_bright_magenta(std::string input) {return "\x1b[105m" + input + "\x1b[0m";} +std::string bg_bright_cyan(std::string input) {return "\x1b[106m" + input + "\x1b[0m";} +std::string bg_bright_white(std::string input) {return "\x1b[107m" + input + "\x1b[0m";} +std::string bold(std::string input) {return "\x1b[1m" + input + "\x1b[0m";} +std::string underline(std::string input) {return "\x1b[4m" + input + "\x1b[0m";} +std::string reverse(std::string input) {return "\x1b[7m" + input + "\x1b[0m";} \ No newline at end of file diff --git a/src_old/colors.h b/src_old/colors.h new file mode 100644 index 0000000..d177240 --- /dev/null +++ b/src_old/colors.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +std::string black(std::string input); +std::string red(std::string input); +std::string green(std::string input); +std::string yellow(std::string input); +std::string blue(std::string input); +std::string magenta(std::string input); +std::string cyan(std::string input); +std::string white(std::string input); +std::string bright_black(std::string input); +std::string bright_red(std::string input); +std::string bright_green(std::string input); +std::string bright_yellow(std::string input); +std::string bright_blue(std::string input); +std::string bright_magenta(std::string input); +std::string bright_cyan(std::string input); +std::string bright_white(std::string input); +std::string bg_black(std::string input); +std::string bg_red(std::string input); +std::string bg_green(std::string input); +std::string bg_yellow(std::string input); +std::string bg_blue(std::string input); +std::string bg_magenta(std::string input); +std::string bg_cyan(std::string input); +std::string bg_white(std::string input); +std::string bg_bright_black(std::string input); +std::string bg_bright_red(std::string input); +std::string bg_bright_green(std::string input); +std::string bg_bright_yellow(std::string input); +std::string bg_bright_blue(std::string input); +std::string bg_bright_magenta(std::string input); +std::string bg_bright_cyan(std::string input); +std::string bg_bright_white(std::string input); +std::string bold(std::string input); +std::string underline(std::string input); +std::string reverse(std::string input); \ No newline at end of file diff --git a/src_old/lang.cc b/src_old/lang.cc new file mode 100644 index 0000000..eb7d297 --- /dev/null +++ b/src_old/lang.cc @@ -0,0 +1,66 @@ +#include "lang.h" + +int read_file(std::deque args) { + if (args.empty()) { + std::cout << bg_red(bold("ERROR")) << ": No file input" << std::endl; + return 1; + } + fs::path filename = args[0]; + args.pop_front(); + std::ifstream file(filename); + if (!file) { + std::cout << bg_red(bold("ERROR")) << ": Could not find file" + << std::endl; + return 1; + } + std::string line; + while (std::getline(file, line)) { + std::cout << line << std::endl; + } + return 0; +} + +int parse_line(std::string line) { + struct parsed_token parsed { + empty_token, empty_token + }; + if (int sub = line.find("//")) + line = line.substr(sub); + std::deque tokens; + int start = 0; + size_t end = line.find(" "); + while (end != std::string::npos) { + tokens.push_back(line.substr(start, end - start)); + start = end + 1; + end = line.find(" ", start); + } + for (std::string& token : tokens) { + try { + Token curr_token = CV_Tokens.at(token); + parsed.lastToken = parsed.currentToken; + if (curr_token == parsed.lastToken && + curr_token.type == TokenType::SPACE) + continue; + else { + // TODO: Line number handling for better errors + std::cerr << "Invalid token `" << token << "'." << std::endl; + return 1; + } + parsed.currentToken = curr_token; + } catch (std::out_of_range& oor) { + // this is most likely a variable or a string + if (parsed.lastToken.type == TokenType::INTRINSIC && + parsed.lastToken.intrinsic == Intrinsic::LET) { + VariableToken var{TokenType::VARIABLE, Intrinsic::EMPTY, + Constant::EMPTY, "", token}; + parsed.currentToken = var; + var_tokens.push_back(var); + } + // string parsing + std::cerr << "Not implemented. Found `" << token << "'." + << std::endl; + continue; + } + } + return 0; +} diff --git a/src_old/lang.h b/src_old/lang.h new file mode 100644 index 0000000..2a9ce16 --- /dev/null +++ b/src_old/lang.h @@ -0,0 +1,102 @@ +#pragma once + +#include "colors.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +enum class TokenType { + EMPTY, + SPACE, + LEFT_PAREN, + RIGHT_PAREN, + COLONCOLON, + EQ, + COMMA, + STRING, + INTRINSIC, + CONSTANT, + VARIABLE +}; + +enum class Intrinsic { EMPTY, LET, UNLET, ENTER, EXIT, SHOW, HIDE }; + +enum class Constant { EMPTY, CHARACTER, LOCATION, IMAGE }; + +struct Token { + TokenType type; + Intrinsic intrinsic; + Constant constant; + std::string string; + std::string name; + inline bool operator==(const Token &right) { + if (this->type == right.type && this->intrinsic == right.intrinsic && + this->constant == right.constant && this->string == right.string && + this->name == right.name) + return true; + return false; + } + inline bool operator!=(const Token &right) { return !(*this == right); } +}; + +struct VariableToken : Token { + Token value; + inline bool operator==(const VariableToken &right) { + if ((Token) * this == (Token)right && this->value == right.value) + return true; + return false; + } + inline bool operator!=(const VariableToken &right) { + return !(*this == right); + } +}; + +std::map CV_Tokens{ + {"", Token{TokenType::EMPTY, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {" ", Token{TokenType::SPACE, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {"(", + Token{TokenType::LEFT_PAREN, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {")", + Token{TokenType::RIGHT_PAREN, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {"::", + Token{TokenType::COLONCOLON, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {"=", Token{TokenType::EQ, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {",", Token{TokenType::COMMA, Intrinsic::EMPTY, Constant::EMPTY, "", ""}}, + {"let", + Token{TokenType::INTRINSIC, Intrinsic::LET, Constant::EMPTY, "", ""}}, + {"unlet", + Token{TokenType::INTRINSIC, Intrinsic::UNLET, Constant::EMPTY, "", ""}}, + {"enter", + Token{TokenType::INTRINSIC, Intrinsic::ENTER, Constant::EMPTY, "", ""}}, + {"exit", + Token{TokenType::INTRINSIC, Intrinsic::EXIT, Constant::EMPTY, "", ""}}, + {"show", + Token{TokenType::INTRINSIC, Intrinsic::SHOW, Constant::EMPTY, "", ""}}, + {"hide", + Token{TokenType::INTRINSIC, Intrinsic::HIDE, Constant::EMPTY, "", ""}}, + {"Character", + Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::CHARACTER, "", ""}}, + {"Location", + Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::LOCATION, "", ""}}, + {"Image", + Token{TokenType::CONSTANT, Intrinsic::EMPTY, Constant::IMAGE, "", ""}}}; + +Token empty_token{TokenType::EMPTY, Intrinsic::EMPTY, Constant::EMPTY, "", ""}; + +struct parsed_token { + Token lastToken; + Token currentToken; +}; + +std::vector var_tokens; + +int read_file(std::deque args); +int parse_line(std::string line); diff --git a/syntax/install.sh b/syntax/install.sh index 7a85502..bb03173 100755 --- a/syntax/install.sh +++ b/syntax/install.sh @@ -2,6 +2,9 @@ if [[ -z $1 ]]; then echo "Expected a file but got nothing"; exit 1; fi +if [[ ! -d ~/.vim/syntax ]]; then mkdir -p ~/.vim/syntax; fi +if [[ ! -d ~/.vim/ftdetect ]]; then mkdir -p ~/.vim/ftdetect; fi + cp $1 ~/.vim/syntax/$1 cp "${1}.ftd" ~/.vim/ftdetect/$1