A simple scripting language in C++
Ferenc Szontágh
2025-04-19 48d9278f0b75098e83e58c589ea86d006358604d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef LEXER_OPERATORS_HPP
#define LEXER_OPERATORS_HPP
 
#include <string>
#include <vector>
 
#include "Lexer/Token.hpp"
#include "Parser/ParsedExpression.hpp"
 
namespace Lexer {
 
// two chars
extern const std::vector<std::string> OPERATOR_RELATIONAL;
extern const std::vector<std::string> OPERATOR_INCREMENT;
extern const std::vector<std::string> OPERATOR_ASSIGNMENT;
extern const std::vector<std::string> OPERATOR_LOGICAL;
 
// one char
extern const std::vector<std::string> OPERATOR_ARITHMETIC;
extern const std::vector<std::string> PUNCTUATION;
 
bool contains(const std::vector<std::string> & vec, const std::string & value);
bool isUnaryOperator(const std::string & op);
bool isBinaryOperator(const std::string & op);
 
inline int getPrecedence(const std::string & op) {
    if (op == "->") {
        return 5;  // Member access has highest precedence
    }
    if (op == "u-" || op == "u+" || op == "u!") {
        return 4;
    }
    if (op == "*" || op == "/" || op == "%") {
        return 3;
    }
    if (op == "+" || op == "-") {
        return 2;
    }
    if (op == "==" || op == "!=" || op == "<" || op == ">" || op == "<=" || op == ">=") {
        return 1;
    }
    if (op == "&&" || op == "||") {
        return 0;
    }
    return -1;
}
 
inline bool isLeftAssociative(const std::string & op) {
    return !(op == "u-" || op == "u+");
}
 
inline Parser::ParsedExpressionPtr applyOperator(const std::string & op, Parser::ParsedExpressionPtr rhs,
                                                 Parser::ParsedExpressionPtr lhs = nullptr) {
    if (op.starts_with("u")) {
        std::string real_op = op.substr(1);  // "u!" -> "!"
        return Parser::ParsedExpression::makeUnary(real_op, std::move(rhs));
    }
    return Parser::ParsedExpression::makeBinary(op, std::move(lhs), std::move(rhs));
}
 
[[nodiscard]] inline bool pushOperand(const Tokens::Token & token, const Symbols::Variables::Type & expected_var_type,
                                      std::vector<Parser::ParsedExpressionPtr> & output_queue) {
    // Literal operands: number, string, or keyword literals (e.g., true/false/null)
    if (token.type == Tokens::Type::NUMBER) {
        // Numeric literal: only allowed if expected is numeric or unspecified
        if (expected_var_type != Symbols::Variables::Type::NULL_TYPE &&
            expected_var_type != Symbols::Variables::Type::INTEGER &&
            expected_var_type != Symbols::Variables::Type::DOUBLE &&
            expected_var_type != Symbols::Variables::Type::FLOAT) {
            return false;
        }
        // Auto-detect or cast to expected numeric type
        auto val = Symbols::Value::fromString(token.value, /*autoDetectType*/ true);
        output_queue.push_back(Parser::ParsedExpression::makeLiteral(val));
        return true;
    }
    if (token.type == Tokens::Type::STRING_LITERAL) {
        // String literal: only allowed if expected is string or unspecified
        if (expected_var_type != Symbols::Variables::Type::NULL_TYPE &&
            expected_var_type != Symbols::Variables::Type::STRING) {
            return false;
        }
        output_queue.push_back(Parser::ParsedExpression::makeLiteral(Symbols::Value(token.value)));
        return true;
    }
    if (token.type == Tokens::Type::KEYWORD) {
        // Keyword literal: e.g., true, false, null
        auto val = Symbols::Value::fromString(token.value, /*autoDetectType*/ true);
        auto vtype = val.getType();
        // only allowed if expected matches or unspecified
        if (expected_var_type != Symbols::Variables::Type::NULL_TYPE && expected_var_type != vtype) {
            return false;
        }
        output_queue.push_back(Parser::ParsedExpression::makeLiteral(val));
        return true;
    }
    if (token.type == Tokens::Type::VARIABLE_IDENTIFIER) {
        std::string name = token.value;
        if (!name.empty() && name[0] == '$') {
            name = name.substr(1);
        }
        output_queue.push_back(Parser::ParsedExpression::makeVariable(name));
        return true;
    }
    return false;
}
 
};  // namespace Lexer
 
#endif  // LEXER_OPERATORS_HPP