Skip to content

Commit

Permalink
feat: Added pointer and reference unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 27, 2022
1 parent 04cf47f commit ce4ea17
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,14 @@ llvm::Value *Codegen::visit(UnaryOp *node) {
return Builder->CreateNot(operand, ".tmp");
else
throw CodegenError(node->getSpan(), "Cannot apply {} to {}", NAMEOF_ENUM(node->getOperator()), node->getExpression()->toString(SourceManager.get(), 0));
} else
} else if (node->getOperator() == TokenType::STAR) {
return Builder->CreateLoad(operand->getType()->getPointerElementType(), operand);
} else if (node->getOperator() == TokenType::AMPERSAND) {
auto ptr = Builder->CreateAlloca(operand->getType(), nullptr, ".tmp");
Builder->CreateStore(operand, ptr);
return ptr;
}
else
throw CodegenError(node->getSpan(), "Unknown unary operator, cannot apply {} to {}", NAMEOF_ENUM(node->getOperator()), node->getExpression()->toString(SourceManager.get(), 0));
}

Expand Down
2 changes: 2 additions & 0 deletions src/liblesma/Frontend/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Token *Lexer::ScanOne(bool continuation) {
return AddToken(TokenType::STAR_EQUAL);
return AddToken(TokenType::STAR);
}
case '&':
return AddToken(TokenType::AMPERSAND);
case '!':
return AddToken(MatchAndAdvance('=') ? TokenType::BANG_EQUAL : TokenType::BANG);
case '=': {
Expand Down
2 changes: 1 addition & 1 deletion src/liblesma/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Expression *Parser::ParseDot() {

Expression *Parser::ParseUnary() {
Expression *left = nullptr;
while (AdvanceIfMatchAny<TokenType::MINUS>()) {
while (AdvanceIfMatchAny<TokenType::MINUS, TokenType::STAR, TokenType::AMPERSAND>()) {
auto op = Previous();
auto expr = ParseDot();
left = new UnaryOp({op->getStart(), expr->getEnd()}, op->type, expr);
Expand Down
1 change: 1 addition & 0 deletions src/liblesma/Token/TokenType.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace lesma {
STAR,
MOD,
POWER,
AMPERSAND,
RANGE,
ELLIPSIS,

Expand Down

0 comments on commit ce4ea17

Please sign in to comment.