fixes and improvements:

* now detecting wrong chars in binary and hex literals
* added operators `&` and `|`
development
Michael Ochmann 3 years ago
parent e45abf48ba
commit 2686964d3e
  1. 20
      src/Lexer.cpp
  2. 4
      src/Lexer.hpp

@ -18,6 +18,8 @@ namespace dumb {
"OPERATOR_COLON",
"OPERATOR_ARROW",
"OPERATOR_DOUBLE_COLON",
"OPERATOR_AMPERSAND",
"OPERATOR_PIPE",
"INTEGER_LITERAL",
"FLOAT_LITERAL",
"STRING_LITERAL",
@ -63,7 +65,9 @@ namespace dumb {
{"*", Token::Type::OPERATOR_ASTERISK},
{"=", Token::Type::OPERATOR_EQUALS},
{":", Token::Type::OPERATOR_COLON},
{".", Token::Type::OPERATOR_DOT}
{".", Token::Type::OPERATOR_DOT},
{"&", Token::Type::OPERATOR_AMPERSAND},
{"|", Token::Type::OPERATOR_PIPE}
};
dumb::Lexer::Lexer(Compiler& compiler, std::filesystem::path sourceFile) : compiler(compiler), line(1), column(1), pointer(0) {
@ -252,21 +256,25 @@ namespace dumb {
size_t value;
switch (kind) {
case 'x':
case 'x': {
while (std::isdigit(this->current()) || (this->current() >= 'A' && this->current() <= 'F') || (this->current() >= 'a' && this->current() <= 'f'))
this->buffer << this->consume();
value = std::stoi(this->buffer.str(), nullptr, 16);
std::string stringValue(this->buffer.str());
value = stringValue.length() > 0 ? std::stoi(stringValue, nullptr, 16) : 0;
break;
case 'b':
}
case 'b': {
while (this->current() == '0' || this->current() == '1')
this->buffer << this->consume();
value = std::stoi(this->buffer.str(), nullptr, 2);
std::string stringValue(this->buffer.str());
value = stringValue.length() > 0 ? std::stoi(stringValue, nullptr, 2) : 0;
break;
}
default:
std::cout << "Unexpected token '" << this->current() << "' on line " << this->line << " column " << this->column << std::endl;
exit(1);
}
if (std::isdigit(this->current())) {
if (std::isalnum(this->current())) {
std::cout << "Unexpected token '" << this->current() << "' on line " << this->line << " column " << this->column << std::endl;
exit(1);
}

@ -22,6 +22,8 @@ namespace dumb {
OPERATOR_COLON,
OPERATOR_ARROW,
OPERATOR_DOUBLE_COLON,
OPERATOR_AMPERSAND,
OPERATOR_PIPE,
INTEGER_LITERAL,
FLOAT_LITERAL,
STRING_LITERAL,
@ -95,6 +97,8 @@ namespace dumb {
case '=':
case ':':
case '.':
case '&':
case '|':
return true;
default:
return false;

Loading…
Cancel
Save