Skip to content

Commit

Permalink
feat: Added basic string support
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 6, 2022
1 parent 2b87a7b commit caa3c25
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [ ] Fix import error while reading file being caught by Utils function instead of module, which could show where import was stated
- [ ] Fix error reporting not handling multiline errors well
- [ ] Fix indentation on the second line after line continuation not working
- [ ] Fix strings escaping all forward slashes, cannot use any escape sequences

## Refactoring
- [x] Replace SourceLocation by Span
Expand Down Expand Up @@ -38,4 +39,5 @@
- [ ] Add generics
- [ ] Add inheritance (or traits)
- [ ] Add string interpolation
- [ ] Add standard library (things like print, input, etc)
- [ ] Add standard library (things like print, input, etc)
- [ ] Add multithreading using pthread for now (ala Spice)
4 changes: 4 additions & 0 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ llvm::Type *Codegen::visit(lesma::Type *node) {
return Builder->getDoubleTy();
else if (node->getType() == TokenType::BOOL_TYPE)
return Builder->getInt1Ty();
else if (node->getType() == TokenType::STRING_TYPE)
return Builder->getInt8PtrTy();
else if (node->getType() == TokenType::VOID_TYPE)
return Builder->getVoidTy();

Expand Down Expand Up @@ -753,6 +755,8 @@ llvm::Value *Codegen::visit(Literal *node) {
return ConstantInt::getSigned(Builder->getInt64Ty(), std::stoi(node->getValue()));
else if (node->getType() == TokenType::BOOL)
return ConstantInt::getBool(*TheContext, node->getValue() == "true");
else if (node->getType() == TokenType::STRING)
return Builder->CreateGlobalStringPtr(node->getValue());
else if (node->getType() == TokenType::IDENTIFIER) {
// Look this variable up in the function.
auto val = Scope->lookup(node->getValue());
Expand Down
7 changes: 6 additions & 1 deletion src/liblesma/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ Expression *Parser::ParseTerm() {
case TokenType::STRING:
case TokenType::INTEGER:
case TokenType::DOUBLE:
case TokenType::NIL:
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);
}
case TokenType::IDENTIFIER: {
if (CheckAny<TokenType::LEFT_PAREN>(1))
return ParseFunctionCall();
Expand Down
2 changes: 1 addition & 1 deletion src/liblesma/Token/Token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static std::map<std::string, TokenType> g_reserved_map{
{"in", TokenType::IN},
{"int", TokenType::INT_TYPE},
{"float", TokenType::FLOAT_TYPE},
{"string", TokenType::STRING_TYPE},
{"str", TokenType::STRING_TYPE},
{"bool", TokenType::BOOL_TYPE},
{"void", TokenType::VOID_TYPE},
{"import", TokenType::IMPORT},
Expand Down
7 changes: 7 additions & 0 deletions tests/lesma/print_str.les
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ret=101
def extern exit(x: int)
def extern printf(x: str)

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

0 comments on commit caa3c25

Please sign in to comment.