Skip to content

Commit

Permalink
feat: Added a few escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 6, 2022
1 parent caa3c25 commit 1ddba5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
32 changes: 29 additions & 3 deletions src/liblesma/Frontend/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,47 @@ char Lexer::Peek(int offset) {
}

Token Lexer::AddStringToken() {
std::string string;

while (Peek() != '"' && !IsAtEnd()) {
// Should we allow newlines in strings? Probably not
if (Peek() == '\n') {
loc.Line++;
loc.Col = 1;
}
Advance();
// If it's not an escape sequence, proceed as usual
if (Peek() != '\\') {
string.push_back(Advance());
continue;
}

switch(Peek(1)){
case 'n': string.push_back('\n'); break;
case 'r': string.push_back('\r'); break;
case 't': string.push_back('\t'); break;
case 'b': string.push_back('\b'); break;
case '0': string.push_back('\0'); break;
case '"': string.push_back('"'); break;
case 'e': string.push_back(0x1B); break;
case '\'': string.push_back('\''); break;
case '\\': string.push_back('\\'); break;
default:
Error("Unknown escape sequence.");
}

// Skip the backslash and the escape sequence.
Advance(); Advance();
}

if (IsAtEnd())
Error("Unterminated string.");

// The closing ".
// Skip the closing ".
Advance();

return AddToken(TokenType::STRING);
auto ret = Token(TokenType::STRING, string, Span{begin_loc, loc});
ResetTokenBeg();
return ret;
}
Token Lexer::AddNumToken() {
while (IsDigit(Peek())) Advance();
Expand Down
3 changes: 1 addition & 2 deletions src/liblesma/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ Expression *Parser::ParseTerm() {
case TokenType::NIL: {
auto token = Peek();
Consume(token->type);
auto token_value = token->type == TokenType::STRING ? token->lexeme.substr(1, token->lexeme.size() - 2) : token->lexeme;
return new Literal(token->span, token_value, token->type);
return new Literal(token->span, token->lexeme, token->type);
}
case TokenType::IDENTIFIER: {
if (CheckAny<TokenType::LEFT_PAREN>(1))
Expand Down
2 changes: 1 addition & 1 deletion tests/lesma/print_str.les
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
def extern exit(x: int)
def extern printf(x: str)

let x = "Hello World!"
let x = "Hello World! 🎉\n"
printf(x)
exit(101)

0 comments on commit 1ddba5d

Please sign in to comment.