A simple scripting language in C++
Ferenc Szontágh
2025-04-18 e398a47fc9ef8ded356c56b5739808ce1192e3d2
fix syntax, add expected to errors
5 files modified
26 ■■■■ changed files
README.md 2 ●●● patch | view | raw | blame | history
src/Parser/Parser.cpp 3 ●●●● patch | view | raw | blame | history
src/Parser/Parser.hpp 16 ●●●●● patch | view | raw | blame | history
test_scripts/test1.vs 3 ●●●●● patch | view | raw | blame | history
test_scripts/test2.vs 2 ●●●●● patch | view | raw | blame | history
README.md
@@ -30,7 +30,7 @@
string name = "VoidScript";
// Functions
function $add(int $a, double $b, string $help) {
function add = (int $a, double $b, string $help) {
    int $result = $a + $b;
    print("The sum is: ", $result, "\n");
    print("Help: ", $help, "\n");
src/Parser/Parser.cpp
@@ -175,7 +175,8 @@
    if (startIt != tokens_.end() && endIt != tokens_.end() && startIt < endIt) {
        filtered_tokens = std::vector<Lexer::Tokens::Token>(startIt + 1, endIt);
    }
    std::string_view input_string = input_str_view_.substr(opening_brace.end_pos, closing_brace.end_pos);
    auto len = closing_brace.start_pos - opening_brace.end_pos;
    std::string_view input_string = input_str_view_.substr(opening_brace.end_pos, len);
    current_token_index_ = tokenIndex;
    expect(Lexer::Tokens::Type::PUNCTUATION, "}");
src/Parser/Parser.hpp
@@ -22,15 +22,21 @@
      public:
        using BaseException::BaseException;
        Exception(const std::string & msg, const Lexer::Tokens::Token & token) {
        Exception(const std::string & msg, const std::string & expected, const Lexer::Tokens::Token & token) {
            rawMessage_ = msg + ": " + token.dump();
            context_ =
                " at line: " + std::to_string(token.line_number) + ", column: " + std::to_string(token.column_number);
            if (expected.empty() == false) {
                rawMessage_ += " (expected: " + expected + ")";
            }
            formattedMessage_ = formatMessage();
        }
        Exception(const std::string & msg, int line, int col) {
        Exception(const std::string & msg, std::string & expected, int line, int col) {
            rawMessage_       = msg;
            if (expected.empty() == false) {
                rawMessage_ += " (expected: " + expected + ")";
            }
            context_          = " at line: " + std::to_string(line) + ", column: " + std::to_string(col);
            formattedMessage_ = formatMessage();
        }
@@ -146,13 +152,13 @@
               (current_token_index_ == tokens_.size() - 1 && tokens_.back().type == Lexer::Tokens::Type::END_OF_FILE);
    }
    [[noreturn]] void reportError(const std::string & message) {
    [[noreturn]] void reportError(const std::string & message, const std::string expected = "") {
        if (current_token_index_ < tokens_.size()) {
            throw Exception(message, tokens_[current_token_index_]);
            throw Exception(message, expected, tokens_[current_token_index_]);
        }
        int line = tokens_.empty() ? 0 : tokens_.back().line_number;
        int col  = tokens_.empty() ? 0 : tokens_.back().column_number;
        throw Exception(message, line, col);
        throw Exception(message, expected, line, col);
    }
    // parseStatement (változatlan)
test_scripts/test1.vs
@@ -1,4 +1,3 @@
<?void
string $name = "World 😀"; # world test
string $greeting = "Hello ";
string $smiley = "😀 = \\U0001F600 = \U0001F600\n";
@@ -10,5 +9,3 @@
print("The number2: ", $number2, "\n");
print("Unicode: \u00E9 \U0001F600, hex: \x41, newline:\nEnd\t",$greeting, $name, "\n\nSmiley test: ", $smiley);
?>
test_scripts/test2.vs
@@ -1,5 +1,3 @@
<?void
int $num = 123;
double $double = 12.3;
string $variable = "This is a string content with a number: 123";