add const keyword

This commit is contained in:
sfja 2026-01-19 19:43:54 +01:00
parent 2f99ef56d7
commit c9d2fad4c8
5 changed files with 66 additions and 31 deletions

View File

@ -1,7 +1,7 @@
MAKEFLAGS += -j16 MAKEFLAGS += -j16
CXXFLAGS := -std=c++23 -Wall -Wextra -pedantic-errors -fsanitize=address,undefined CXXFLAGS := -std=c++23 -Wall -Wextra -pedantic-errors -fsanitize=address
LDFLAGS := LDFLAGS :=
CXXFLAGS += $(shell pkgconf sdl2 --cflags) CXXFLAGS += $(shell pkgconf sdl2 --cflags)

View File

@ -365,23 +365,6 @@ const auto reg_idents = std::unordered_map<std::string_view, vc5::Reg> {
{ "Rip", vc5::Reg::Rip }, { "Rip", vc5::Reg::Rip },
}; };
char escape_char(char ch)
{
switch (ch) {
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case '0':
return '\0';
default:
return ch;
}
}
} }
auto Parser::parse_operand() -> std::unique_ptr<Expr> auto Parser::parse_operand() -> std::unique_ptr<Expr>
@ -425,7 +408,8 @@ auto Parser::parse_operand() -> std::unique_ptr<Expr>
auto text = std::string(m_tok.text); auto text = std::string(m_tok.text);
step(); step();
int value = text.at(1) == '\\' ? escape_char(text.at(2)) : text.at(1); int value = text.at(1) == '\\' ? unescape_escape_char(text.at(2))
: text.at(1);
return std::make_unique<Expr>( return std::make_unique<Expr>(
loc, Expr::Ty::Int, static_cast<int>(value)); loc, Expr::Ty::Int, static_cast<int>(value));
@ -433,17 +417,7 @@ auto Parser::parse_operand() -> std::unique_ptr<Expr>
auto text = std::string(m_tok.text); auto text = std::string(m_tok.text);
step(); step();
auto value = std::string(); auto value = unescape_string(text.substr(1, text.size() - 2));
size_t i = 1;
while (i < text.size() - 1) {
if (text.at(i) == '\\') {
i += 1;
value.push_back(escape_char(text.at(i)));
} else {
value.push_back(text.at(i));
}
i += 1;
}
return std::make_unique<Expr>(loc, Expr::Ty::Str, std::move(value)); return std::make_unique<Expr>(loc, Expr::Ty::Str, std::move(value));
} else if (eat('(')) { } else if (eat('(')) {

View File

@ -89,6 +89,18 @@ namespace asmer {
Args args; Args args;
}; };
struct Const {
Loc loc;
std::string_view ident;
std::unique_ptr<Expr> expr;
};
struct Align {
Loc loc;
std::string_view ident;
std::unique_ptr<Expr> expr;
};
class Parser { class Parser {
public: public:
explicit Parser(std::string_view text) explicit Parser(std::string_view text)

View File

@ -1,7 +1,9 @@
#include "scanner.hpp" #include "scanner.hpp"
#include <cassert>
#include <print> #include <print>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <unordered_map>
using namespace vc5::tools; using namespace vc5::tools;
using namespace std::literals; using namespace std::literals;
@ -32,7 +34,18 @@ auto Scanner::next() -> Tok
or test_range('a', 'z')) { or test_range('a', 'z')) {
step(); step();
} }
return tok(TT::Ident, loc);
auto ident_tok = tok(TT::Ident, loc);
static const auto keywords = std::unordered_map<std::string_view, TT> {
{ "const", TT::KwConst },
};
if (keywords.contains(ident_tok.text)) {
return tok(keywords.at(ident_tok.text), loc);
}
return ident_tok;
} }
if (test_range('1', '9')) { if (test_range('1', '9')) {
while (test_range('0', '9')) { while (test_range('0', '9')) {
@ -232,3 +245,35 @@ void Loc::print_error(std::string_view text, std::string_view message) const
message, message,
clear); clear);
} }
auto vc5::tools::unescape_escape_char(char ch) -> char
{
switch (ch) {
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case '0':
return '\0';
default:
return ch;
}
}
auto vc5::tools::unescape_string(std::string_view value) -> std::string
{
auto result = std::string();
size_t i = 0;
while (i < value.size()) {
if (value[0] == '\\') {
i += 1;
result += unescape_escape_char(value[i]);
} else {
result += value[i];
}
i += 1;
}
return result;
}

View File

@ -22,6 +22,7 @@ struct Tok {
Bin, Bin,
Char, Char,
Str, Str,
KwConst,
Newline = '\n', Newline = '\n',
LParen = '(', LParen = '(',
RParen = ')', RParen = ')',
@ -76,4 +77,7 @@ private:
bool m_error_occured = false; bool m_error_occured = false;
}; };
auto unescape_escape_char(char ch) -> char;
auto unescape_string(std::string_view value) -> std::string;
} }