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
  3. 2
      tests/test.dmb

@ -18,6 +18,8 @@ namespace dumb {
"OPERATOR_COLON", "OPERATOR_COLON",
"OPERATOR_ARROW", "OPERATOR_ARROW",
"OPERATOR_DOUBLE_COLON", "OPERATOR_DOUBLE_COLON",
"OPERATOR_AMPERSAND",
"OPERATOR_PIPE",
"INTEGER_LITERAL", "INTEGER_LITERAL",
"FLOAT_LITERAL", "FLOAT_LITERAL",
"STRING_LITERAL", "STRING_LITERAL",
@ -63,7 +65,9 @@ namespace dumb {
{"*", Token::Type::OPERATOR_ASTERISK}, {"*", Token::Type::OPERATOR_ASTERISK},
{"=", Token::Type::OPERATOR_EQUALS}, {"=", Token::Type::OPERATOR_EQUALS},
{":", Token::Type::OPERATOR_COLON}, {":", 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) { 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; size_t value;
switch (kind) { switch (kind) {
case 'x': case 'x': {
while (std::isdigit(this->current()) || (this->current() >= 'A' && this->current() <= 'F') || (this->current() >= 'a' && this->current() <= 'f')) while (std::isdigit(this->current()) || (this->current() >= 'A' && this->current() <= 'F') || (this->current() >= 'a' && this->current() <= 'f'))
this->buffer << this->consume(); 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; break;
case 'b': }
case 'b': {
while (this->current() == '0' || this->current() == '1') while (this->current() == '0' || this->current() == '1')
this->buffer << this->consume(); 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; break;
}
default: default:
std::cout << "Unexpected token '" << this->current() << "' on line " << this->line << " column " << this->column << std::endl; std::cout << "Unexpected token '" << this->current() << "' on line " << this->line << " column " << this->column << std::endl;
exit(1); 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; std::cout << "Unexpected token '" << this->current() << "' on line " << this->line << " column " << this->column << std::endl;
exit(1); exit(1);
} }

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

@ -9,7 +9,7 @@ that goes for ages
*/ */
binary := 0b01110; binary := 0b01110;
hex := 0xFF; hex := 0xFF;
bar : string = "lol"; // more comments bar : string = "lol"; // more comments

Loading…
Cancel
Save