Initial Commit + Lexer

This commit is contained in:
2026-01-10 15:20:52 +03:00
parent e89228a3f8
commit e00cc1b488
18 changed files with 731 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
build/
cmake-*/
.env

10
.idea/.gitignore generated vendored Normal file
View File

@@ -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/

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
mbscript

101
.idea/editor.xml generated Normal file
View File

@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BackendCodeEditorSettings">
<option name="/Default/CodeStyle/CodeFormatting/CppClangFormat/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_ARGUMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXPRESSION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXTENDS_LIST/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_FOR_STMT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_PARAMETER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_ARGUMENT/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_PARAMETER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_TERNARY/@EntryValue" value="ALIGN_ALL" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_CLASS_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_DECLARATIONS/@EntryValue" value="0" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DECLARATION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BREAK_TEMPLATE_DECLARATION/@EntryValue" value="LINE_BREAK" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CASE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_ACCESS_SPECIFIERS_FROM_CLASS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CASE_FROM_SWITCH/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CLASS_MEMBERS_FROM_ACCESS_SPECIFIERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_COMMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_STYLE/@EntryValue" value="Space" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INITIALIZER_BRACES/@EntryValue" value="END_OF_LINE_NO_SPACE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INT_ALIGN_EQ/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INVOCABLE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_CODE/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_USER_LINEBREAKS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/LINE_BREAK_AFTER_COLON_IN_MEMBER_INITIALIZER_LISTS/@EntryValue" value="ON_SINGLE_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/MEMBER_INITIALIZER_LIST_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_INDENTATION/@EntryValue" value="All" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/OTHER_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_CATCH_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_ELSE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_NAMESPACE_DEFINITIONS_ON_SAME_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_WHILE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_BLOCK_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_PARAMS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_SEMICOLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_NESTED_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_UNARY_OPERATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_COLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_SEMICOLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BETWEEN_CLOSING_ANGLE_BRACKETS_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_ARRAY_ACCESS_BRACKETS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_DECLARATION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_BLOCKS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_METHOD_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPECIAL_ELSE_IF_TREATMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TYPE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_BINARY_OPSIGN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_ARGUMENTS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_TERNARY_OPSIGNS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_PARAMETERS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/EditorConfig/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
</component>
</project>

2
.idea/mbs.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CIDR" type="CPP_MODULE" version="4" />

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mbs.iml" filepath="$PROJECT_DIR$/.idea/mbs.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

18
CMakeLists.txt Normal file
View File

@@ -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
)

View File

@@ -0,0 +1,20 @@
#ifndef MBSCRIPT_EXCEPTIONS_H
#define MBSCRIPT_EXCEPTIONS_H
#include <string>
#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

View File

@@ -0,0 +1,36 @@
#ifndef MBS_LEXER_H
#define MBS_LEXER_H
#include <format>
#include <string>
#include <vector>
#include "token.h"
namespace mbs {
struct Lexer {
std::vector<Token> tokens;
const std::vector<Token> &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

View File

@@ -0,0 +1,4 @@
#ifndef MBS_PARSER_H
#define MBS_PARSER_H
#endif //MBS_PARSER_H

View File

@@ -0,0 +1,81 @@
#ifndef MBSCRIPT_UTILS_H
#define MBSCRIPT_UTILS_H
#include <format>
#include <ostream>
#include <string>
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

View File

@@ -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();
}

283
src/frontend/lexer.cpp Normal file
View File

@@ -0,0 +1,283 @@
#include "../../includes/mbs/frontend/lexer.h"
#include "../../includes/mbs/frontend/exceptions.h"
#include <iostream>
const std::vector<mbs::Token> &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
}
}
};
}

2
src/frontend/parser.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "../../includes/mbs/frontend/parser.h"

104
src/frontend/token.cpp Normal file
View File

@@ -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;
}

29
src/main.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#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;
}