diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88657e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +cmake-*/ +.env diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..5f0283a --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +mbscript \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..ca29dcc --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,101 @@ + + + + + \ No newline at end of file diff --git a/.idea/mbs.iml b/.idea/mbs.iml new file mode 100644 index 0000000..4c94235 --- /dev/null +++ b/.idea/mbs.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c3ebccd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5029898 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 4.1) +project(mbscript) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(mbs + src/main.cpp + + includes/mbs/frontend/lexer.h + src/frontend/lexer.cpp + + includes/mbs/frontend/parser.h + src/frontend/parser.cpp + includes/mbs/frontend/token.h + src/frontend/token.cpp + includes/mbs/frontend/exceptions.h + src/frontend/exceptions.cpp +) diff --git a/includes/mbs/frontend/exceptions.h b/includes/mbs/frontend/exceptions.h new file mode 100644 index 0000000..a2d6ede --- /dev/null +++ b/includes/mbs/frontend/exceptions.h @@ -0,0 +1,20 @@ +#ifndef MBSCRIPT_EXCEPTIONS_H +#define MBSCRIPT_EXCEPTIONS_H +#include + +#include "token.h" + +namespace mbs { + struct LexerException : std::runtime_error { + LexerException(const std::string &msg, Token token); + + [[nodiscard]] char const *what() const noexcept override; + + private: + Token m_token; + std::string m_msg; + std::string m_err; + }; +} + +#endif //MBSCRIPT_EXCEPTIONS_H \ No newline at end of file diff --git a/includes/mbs/frontend/lexer.h b/includes/mbs/frontend/lexer.h new file mode 100644 index 0000000..9038472 --- /dev/null +++ b/includes/mbs/frontend/lexer.h @@ -0,0 +1,36 @@ +#ifndef MBS_LEXER_H +#define MBS_LEXER_H + +#include +#include +#include + +#include "token.h" + +namespace mbs { + struct Lexer { + std::vector tokens; + const std::vector &lex(const std::string &source); + + private: + void lexNumericals(); + void lexStrings(); + void lexIdentifiers(); + void lexWhitespace(); + void lexOperators(); + + void makeToken(const std::string &val, TokenType type, int start); + void makeToken(const std::string &val, TokenType type, int start, int line); + + [[nodiscard]] bool isEOF() const; + [[nodiscard]] char peek(int offset = 0) const; + + char advance(); + void expect(char c, const std::string &err); + + int m_current = 0, m_line = 1, m_index = 0; + std::string m_src; // Source string to lex + }; +}; + +#endif //MBS_LEXER_H diff --git a/includes/mbs/frontend/parser.h b/includes/mbs/frontend/parser.h new file mode 100644 index 0000000..a05ceda --- /dev/null +++ b/includes/mbs/frontend/parser.h @@ -0,0 +1,4 @@ +#ifndef MBS_PARSER_H +#define MBS_PARSER_H + +#endif //MBS_PARSER_H \ No newline at end of file diff --git a/includes/mbs/frontend/token.h b/includes/mbs/frontend/token.h new file mode 100644 index 0000000..2f7de16 --- /dev/null +++ b/includes/mbs/frontend/token.h @@ -0,0 +1,81 @@ +#ifndef MBSCRIPT_UTILS_H +#define MBSCRIPT_UTILS_H + +#include +#include +#include + +enum class TokenType; + +namespace mbs { + enum class TokenType { + // LITERALS + TOK_STRING, // "abc" or 'abc' + TOK_NUMBER, // 123 or 123.45 + TOK_TRUE, // true + TOK_FALSE, // false + TOK_NULL, // nil + + // MISC + TOK_IDENT, + TOK_OPEN_PAREN, // '(' + TOK_CLOSE_PAREN, // ')' + TOK_DOT, // '.' + TOK_COLON, // ':' + TOK_COMMA, // ',' + TOK_QUESTION, // '?' + + // UNARY + TOK_NEGATE, // -a + TOK_NOT, // !a + TOK_INC, // x++ + TOK_DEC, // x-- + + // BINARY + TOK_PLUS, // a + b + TOK_MINUS, // a - b + TOK_MUL, // a * b + TOK_DIV, // a / b + TOK_MOD, // a % b + TOK_POW, // a ** b or a ^ b + + // COMPARISON BINARY + TOK_EQUALS, // a == b + TOK_NOT_EQUALS, // a != b + TOK_LESS, // a < b + TOK_GREATER, // a > b + TOK_LESS_OR_EQUALS, // a <= b + TOK_GREATER_OR_EQUALS, // a >= b + TOK_AND, // a && b + TOK_OR, // a || b + + // OTHER TOKENS + TOK_INVALID, + + // End Of File + TOK_EOF + }; + + + std::string toString(TokenType tt); + + struct Token { + std::string value; // Token Value + struct Position { + int start = -1, end = -1, line = -1; + } pos; // Token position in the input str + TokenType type = TokenType::TOK_INVALID; // Token type + + friend std::ostream &operator<<(std::ostream &os, const Token &m) { + os << std::string{"{"} << std::endl; + os << std::string{"\tvalue: "} << m.value << "," << std::endl; + os << std::string{"\tpos: { start: "} << m.pos.start << ", end: " << m.pos.end << ", line: " << m.pos.line + << " }," << std::endl; + os << std::string{"\ttype: "} << std::format("{}", toString(m.type)) << std::endl; + os << std::string{"}"} << std::endl; + return os; + } + }; +} + +#endif //MBSCRIPT_UTILS_H \ No newline at end of file diff --git a/src/frontend/exceptions.cpp b/src/frontend/exceptions.cpp new file mode 100644 index 0000000..b1a2a8c --- /dev/null +++ b/src/frontend/exceptions.cpp @@ -0,0 +1,16 @@ +#include "../../includes/mbs/frontend/exceptions.h" + +mbs::LexerException::LexerException(const std::string &msg, Token token) + : std::runtime_error(msg), + m_token(std::move(token)), + m_msg(msg), + m_err(std::format("Lexer Error: {}\nLine: {}, Start: {}, End: {}", + m_msg == std::string{'\0'} + ? "" + : m_msg, + m_token.pos.line, m_token.pos.start, m_token.pos.end)) { +} + +char const *mbs::LexerException::what() const noexcept { + return m_err.c_str(); +} diff --git a/src/frontend/lexer.cpp b/src/frontend/lexer.cpp new file mode 100644 index 0000000..1e01d4c --- /dev/null +++ b/src/frontend/lexer.cpp @@ -0,0 +1,283 @@ +#include "../../includes/mbs/frontend/lexer.h" +#include "../../includes/mbs/frontend/exceptions.h" + +#include + +const std::vector &mbs::Lexer::lex(const std::string &source) { + tokens.clear(); + m_src = source; + m_line = 1; + m_current = 0; + + // Iterate over all chars + while (!isEOF()) { + if (std::isdigit(peek())) lexNumericals(); + else if (peek() == '"' || peek() == '\'') lexStrings(); + else if (std::isalpha(peek()) || peek() == '_') lexIdentifiers(); + else if (std::isspace(peek())) lexWhitespace(); + else if (peek() == '(') { + advance(); + makeToken("(", TokenType::TOK_OPEN_PAREN, m_current - 1); + } else if (peek() == ')') { + advance(); + makeToken(")", TokenType::TOK_CLOSE_PAREN, m_current - 1); + } else if (peek() == '.') { + advance(); + makeToken(".", TokenType::TOK_DOT, m_current - 1); + } else if (peek() == '?') { + advance(); + makeToken("?", TokenType::TOK_QUESTION, m_current - 1); + } else if (peek() == ':') { + advance(); + makeToken(":", TokenType::TOK_COLON, m_current - 1); + } else lexOperators(); + } + + // Add EOF token + makeToken("EOF", TokenType::TOK_EOF, m_current); + + return tokens; +} + +void mbs::Lexer::lexNumericals() { + const int _start = m_current; + std::string num; + + // TODO check for num.num.num ... + + // Support the following structure .. + // 123 // 123.456 // 123.to_u8() // 123.456.abs() // 123.123.floor() // etc + while (std::isdigit(peek()) || peek() == '.') { + num += advance(); + } + + makeToken(num, TokenType::TOK_NUMBER, _start); +} + +void mbs::Lexer::lexStrings() { + // Support both single quoted strings 'abc' and + // double-quoted strings "abc" + const int _start = m_current, _line = m_line; + std::string str; + + const char quote = advance(); // Consume opening quotation mark + while (!isEOF() && peek() != quote) { + // Check for strings spanning multiple lines + if (peek() == '\n') { + m_line++; + m_index = 0; + } + str += advance(); + } + + expect(quote, std::format("Expected `{}` to terminate string starting at line {}, pos {}", + quote, _line, _start)); + + tokens.emplace_back( + Token{ + .value = str, + .pos{.start = _start, .end = m_current, .line = _line}, + .type = TokenType::TOK_STRING + } + ); +} + +void mbs::Lexer::lexIdentifiers() { + // Support c style token naming + const int _start = m_current, _line = m_line; + std::string ident; + + while (!isEOF() && std::isalnum(peek()) || peek() == '_') { + ident += advance(); + } + + if (ident == "true") + makeToken(ident, TokenType::TOK_TRUE, _start); + else if (ident == "false") + makeToken(ident, TokenType::TOK_FALSE, _start); + else if (ident == "nil") + makeToken(ident, TokenType::TOK_NULL, _start); + else + makeToken(ident, TokenType::TOK_IDENT, _start); +} + +void mbs::Lexer::lexWhitespace() { + while (isspace(peek())) { + if (peek() == '\n') { + m_line++; + m_index = 0; + } + advance(); // Move to the next token + } +} + +void mbs::Lexer::lexOperators() { + const int _start = m_current; + const char op = advance(); + + if (op == '+') { + if (peek() == '+') { + // ++ operator + makeToken("++", TokenType::TOK_INC, _start); + advance(); // Consume last '+' + } else { + // + operator + makeToken("+", TokenType::TOK_PLUS, _start); + } + } else if (op == '-') { + if (peek() == '-') { + // -- operator + makeToken("--", TokenType::TOK_DEC, _start); + advance(); // Consume last '-' + } else { + // - operator + makeToken("-", TokenType::TOK_MINUS, _start); + } + } else if (op == '*') { + if (peek() == '*') { + // ** operator + makeToken("**", TokenType::TOK_POW, _start); + advance(); // Consume last '*' + } else { + // * operator + makeToken("*", TokenType::TOK_MUL, _start); + } + } else if (op == '/') { + // / operator + makeToken("/", TokenType::TOK_DIV, _start); + } else if (op == '^') { + // ^ operator + makeToken("^", TokenType::TOK_POW, _start); + } else if (op == '%') { + // % operator + makeToken("%", TokenType::TOK_MOD, _start); + } else if (op == '=') { + if (peek() == '=') { + // == operator + makeToken("==", TokenType::TOK_EQUALS, _start); + advance(); // Consume last '=' + } // For now, we don't support assignment OP + else { + throw LexerException{ + "Assignment operator not allowed!", + { + .value = std::string{peek()}, + .pos = {m_current, m_current, m_line} + } + }; + } + } else if (op == '>') { + if (peek() == '=') { + // >= operator + makeToken(">=", TokenType::TOK_GREATER_OR_EQUALS, _start); + advance(); // Consume last '=' + } else { + // > operator + makeToken(">", TokenType::TOK_GREATER, _start); + } + } else if (op == '<') { + if (peek() == '=') { + // <= operator + makeToken("<=", TokenType::TOK_LESS_OR_EQUALS, _start); + advance(); // Consume last '*' + } else { + // < operator + makeToken("<", TokenType::TOK_LESS, _start); + } + } else if (op == '!') { + if (peek() == '=') { + // != operator + makeToken("!=", TokenType::TOK_NOT_EQUALS, _start); + advance(); // Consume last '=' + } else { + // ! operator + makeToken("!", TokenType::TOK_NOT, _start); + } + } else if (op == '&') { + if (peek() == '&') { + // && operator + makeToken("&&", TokenType::TOK_AND, _start); + advance(); // Consume last '&' + } else { + // Throw an error, we expected a second '&' + throw LexerException{ + "Expected `&` after `&` token to form BITWISE AND token!", + { + .value = std::string{peek()}, + .pos = {m_current, m_current, m_line} + } + }; + } + } else if (op == '|') { + if (peek() == '|') { + // || operator + makeToken("||", TokenType::TOK_OR, _start); + advance(); // Consume last '|' + } else { + // Throw an error, we expected a second '|' + throw LexerException{ + "Expected `|` after `|` token to form BITWISE OR token!", + { + .value = std::string{peek()}, + .pos = {m_current, m_current, m_line} + } + }; + } + } else { + // If we reached here, probably having an invalid token! + // Let's throw a Lexer exception. + throw LexerException{ + "Unknown Token", + { + .value = std::string{peek()}, + .pos = {m_current, m_current, m_line} + } + }; + } +} + +void mbs::Lexer::makeToken(const std::string &val, const TokenType type, const int start) { + makeToken(val, type, start, m_line); +} + +void mbs::Lexer::makeToken(const std::string &val, const TokenType type, const int start, const int line) { + tokens.emplace_back(Token{ + .value = val, + .pos{.start = start, .end = m_current, .line = line}, + .type = type + }); +} + +bool mbs::Lexer::isEOF() const { + return m_current >= m_src.size(); +} + +char mbs::Lexer::peek(const int offset) const { + if (isEOF() || m_current + offset >= m_src.size()) + return '\0'; + + return m_src[m_current + offset]; +} + +char mbs::Lexer::advance() { + return m_src[m_current++]; +} + +void mbs::Lexer::expect(const char c, const std::string &err) { + if (!isEOF() && peek() == c) { + advance(); + return; + } + + throw LexerException{ + err.empty() ? std::format("Expected `{}` token!", c) : err, + Token{ + .value = std::string{peek()}, + .pos{ + .start = m_current, + .end = m_current, + .line = m_line + } + } + }; +} diff --git a/src/frontend/parser.cpp b/src/frontend/parser.cpp new file mode 100644 index 0000000..86a36c5 --- /dev/null +++ b/src/frontend/parser.cpp @@ -0,0 +1,2 @@ +#include "../../includes/mbs/frontend/parser.h" + diff --git a/src/frontend/token.cpp b/src/frontend/token.cpp new file mode 100644 index 0000000..bff277e --- /dev/null +++ b/src/frontend/token.cpp @@ -0,0 +1,104 @@ +#include "../../includes/mbs/frontend/token.h" + +std::string mbs::toString(const TokenType tt) { + std::string name; + switch (tt) { + case TokenType::TOK_STRING: + name = "TokenType::TOK_STRING"; + break; + case TokenType::TOK_NUMBER: + name = "TokenType::TOK_NUMBER"; + break; + case TokenType::TOK_TRUE: + name = "TokenType::TOK_TRUE"; + break; + case TokenType::TOK_FALSE: + name = "TokenType::TOK_FALSE"; + break; + case TokenType::TOK_NULL: + name = "TokenType::TOK_NULL"; + break; + case TokenType::TOK_IDENT: + name = "TokenType::TOK_IDENT"; + break; + case TokenType::TOK_OPEN_PAREN: + name = "TokenType::TOK_OPEN_PAREN"; + break; + case TokenType::TOK_CLOSE_PAREN: + name = "TokenType::TOK_CLOSE_PAREN"; + break; + case TokenType::TOK_DOT: + name = "TokenType::TOK_DOT"; + break; + case TokenType::TOK_COLON: + name = "TokenType::TOK_COLON"; + break; + case TokenType::TOK_COMMA: + name = "TokenType::TOK_COMMA"; + break; + case TokenType::TOK_QUESTION: + name = "TokenType::TOK_QUESTION"; + break; + case TokenType::TOK_NEGATE: + name = "TokenType::TOK_NEGATE"; + break; + case TokenType::TOK_NOT: + name = "TokenType::TOK_NOT"; + break; + case TokenType::TOK_INC: + name = "TokenType::TOK_INC"; + break; + case TokenType::TOK_DEC: + name = "TokenType::TOK_DEC"; + break; + case TokenType::TOK_PLUS: + name = "TokenType::TOK_PLUS"; + break; + case TokenType::TOK_MINUS: + name = "TokenType::TOK_MINUS"; + break; + case TokenType::TOK_MUL: + name = "TokenType::TOK_MUL"; + break; + case TokenType::TOK_DIV: + name = "TokenType::TOK_DIV"; + break; + case TokenType::TOK_MOD: + name = "TokenType::TOK_MOD"; + break; + case TokenType::TOK_POW: + name = "TokenType::TOK_POW"; + break; + case TokenType::TOK_EQUALS: + name = "TokenType::TOK_EQUALS"; + break; + case TokenType::TOK_NOT_EQUALS: + name = "TokenType::TOK_NOT_EQUALS"; + break; + case TokenType::TOK_LESS: + name = "TokenType::TOK_LESS"; + break; + case TokenType::TOK_GREATER: + name = "TokenType::TOK_GREATER"; + break; + case TokenType::TOK_LESS_OR_EQUALS: + name = "TokenType::TOK_LESS_OR_EQUALS"; + break; + case TokenType::TOK_GREATER_OR_EQUALS: + name = "TokenType::TOK_GREATER_OR_EQUALS"; + break; + case TokenType::TOK_AND: + name = "TokenType::TOK_AND"; + break; + case TokenType::TOK_OR: + name = "TokenType::TOK_OR"; + break; + case TokenType::TOK_EOF: + name = "TokenType::TOK_EOF"; + break; + default: + name = "TokenType::TOK_INVALID"; + } + + return name; +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..91c8843 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include "../includes/mbs/frontend/lexer.h" +#include "../includes/mbs/frontend/parser.h" + +int main() { + std::cout << "\nmb-script v0.0.1\n" << std::endl; + std::string cmd; + + // Infinite cmd loop + while (true) { + std::cout << ">> "; + std::getline(std::cin, cmd); + std::cout << "CMD: '" << cmd << "'" << std::endl; + + if (cmd == "exit") { + std::cout << "Bye!" << std::endl; + break; + } + + mbs::Lexer lexer; + auto tokens = lexer.lex(cmd); + for (auto token : tokens) { + std::cout << token << std::endl; + } + } + + return 0; +}