Add a couple more characters to lex

This commit is contained in:
Deukhoofd 2019-05-19 12:49:26 +02:00
parent 600315d401
commit 8285811fb7
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 83 additions and 5 deletions

View File

@ -35,11 +35,22 @@ IToken* Lexer::LexNext(char c){
switch (c) {
case '\0':
return new SimpleToken(TokenKind::EndOfFile);
case ' ':
case '\t':
case '\n':
case '\r':
case ' ': case '\t': case '\n': case '\r':
return new SimpleToken(TokenKind::WhiteSpace);
case '+':
return new SimpleToken(TokenKind::PlusToken);
case '-':
return new SimpleToken(TokenKind::MinusToken);
case '/':
return new SimpleToken(TokenKind::SlashToken);
case '*':
return new SimpleToken(TokenKind::StarToken);
case '=':
if (Lexer::Peek() == '='){
Lexer::Next();
return new SimpleToken(TokenKind::EqualityToken);
}
return new SimpleToken(TokenKind::AssignmentToken);
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
return LexNumber(c);
default:
@ -101,7 +112,7 @@ IToken* Lexer::LexNumber(char c){
}
}
if (has_point){
throw;
return new FloatToken(float_value);
}
else{
return new IntegerToken(int_value);
@ -139,6 +150,39 @@ TEST_CASE( "Lex Null Terminator as EOF", "[lexer]" ) {
REQUIRE(lexer.LexNext('\0') -> GetKind() == TokenKind::EndOfFile);
}
TEST_CASE( "Lex Plus Token", "[lexer]" ) {
Lexer lexer = Lexer("");
REQUIRE(lexer.LexNext('+') -> GetKind() == TokenKind::PlusToken);
}
TEST_CASE( "Lex Minus Token", "[lexer]" ) {
Lexer lexer = Lexer("");
REQUIRE(lexer.LexNext('-') -> GetKind() == TokenKind::MinusToken);
}
TEST_CASE( "Lex Slash Token", "[lexer]" ) {
Lexer lexer = Lexer("");
REQUIRE(lexer.LexNext('/') -> GetKind() == TokenKind::SlashToken);
}
TEST_CASE( "Lex Star Token", "[lexer]" ) {
Lexer lexer = Lexer("");
REQUIRE(lexer.LexNext('*') -> GetKind() == TokenKind::StarToken);
}
TEST_CASE( "Lex Assignment Token", "[lexer]" ) {
Lexer lexer = Lexer("");
REQUIRE(lexer.LexNext('=') -> GetKind() == TokenKind::AssignmentToken);
}
TEST_CASE( "Lex Equality Token", "[lexer]" ) {
Lexer lexer = Lexer("==");
auto tokens = lexer.Lex();
REQUIRE(tokens.size() == 2);
IToken* firstToken = tokens[0];
REQUIRE(firstToken -> GetKind() == TokenKind::EqualityToken);
}
TEST_CASE( "Lex Whitespace", "[lexer]" ) {
Lexer lexer = Lexer("");
CHECK(lexer.LexNext(' ') -> GetKind() == TokenKind::WhiteSpace);
@ -174,4 +218,17 @@ TEST_CASE( "Lex Longer Integers", "[lexer]" ) {
}
}
TEST_CASE( "Lex Floats", "[lexer]" ) {
double floats[] {0.5, 0.8, 100.7, 52.3548, 8461354.1324886};
for (double f : floats){
Lexer lexer = Lexer(std::to_string(f));
auto tokens = lexer.Lex();
REQUIRE(tokens.size() == 2);
IToken* firstToken = tokens[0];
REQUIRE(firstToken -> GetKind() == TokenKind::Float);
auto* floatToken = (FloatToken *)firstToken;
CHECK(floatToken -> Value == Approx(f));
}
}
#endif

View File

@ -34,4 +34,17 @@ public:
}
};
class FloatToken : public IToken{
public:
double Value;
explicit FloatToken(double value){
Value = value;
}
TokenKind GetKind() override{
return TokenKind::Float;
}
};
#endif //PORYGONLANG_TOKEN_HPP

View File

@ -5,7 +5,15 @@ enum TokenKind{
EndOfFile,
WhiteSpace,
PlusToken,
MinusToken,
SlashToken,
StarToken,
AssignmentToken,
EqualityToken,
Integer,
Float,
};