Skip to content

Commit c514daf

Browse files
committed
Changed identifier of subscript function to []
1 parent 982699a commit c514daf

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/codegen/codegen.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,8 @@ llvm::Value* codegen::Context::get_pointer_to(ast::Node* expression) {
10411041
return pointer;
10421042
}
10431043
else if (expression->index() == ast::Call
1044-
&& (std::get<ast::CallNode>(*expression).identifier->value == "subscript"
1045-
|| std::get<ast::CallNode>(*expression).identifier->value == "subscript_mut")) {
1044+
&& (std::get<ast::CallNode>(*expression).identifier->value == "[]"
1045+
|| std::get<ast::CallNode>(*expression).identifier->value == "[]:mut")) {
10461046
auto& node = std::get<ast::CallNode>(*expression);
10471047
auto pointer = this->get_index_access_pointer(node);
10481048
return pointer;
@@ -1867,7 +1867,7 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
18671867

18681868
// Codegen args
18691869
std::vector<llvm::Value*> args;
1870-
if (node.identifier->value != "subscript"
1870+
if (node.identifier->value != "[]"
18711871
&& node.identifier->value != "size"
18721872
&& node.identifier->value != "print"
18731873
&& node.identifier->value != "printStruct") {
@@ -1963,7 +1963,7 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
19631963
}
19641964
}
19651965
if (node.args.size() == 1) {
1966-
if (node.identifier->value == "-[negation]") {
1966+
if (node.identifier->value == "-:negation") {
19671967
if (args[0]->getType()->isDoubleTy()) {
19681968
return this->builder->CreateFNeg(args[0], "negation");
19691969
}
@@ -1987,8 +1987,8 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
19871987
// Make call
19881988
return this->builder->CreateCall(llvm_function, args, "calltmp");
19891989
}
1990-
if (node.identifier->value == "subscript"
1991-
|| node.identifier->value == "subscript_mut") {
1990+
if (node.identifier->value == "[]"
1991+
|| node.identifier->value == "[]:mut") {
19921992
if (node.args.size() == 2) {
19931993
return this->builder->CreateLoad(
19941994
this->as_llvm_type(ast::get_concrete_type((ast::Node*)&node, this->type_bindings)),

src/parser.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Result<ast::Node*, Error> Parser::parse_statement() {
350350
if (result.is_error()) return result;
351351

352352
if (result.get_value()->index() == ast::Call) {
353-
if (std::get<ast::CallNode>(*result.get_value()).identifier->value == "subscript") {
353+
if (std::get<ast::CallNode>(*result.get_value()).identifier->value == "[]") {
354354
return this->parse_index_assignment(result.get_value());
355355
}
356356
else {
@@ -584,7 +584,11 @@ Result<ast::Node*, Error> Parser::parse_function() {
584584
// Check if function is - with just one argument
585585
if (function.identifier->value == "-"
586586
&& function.args.size() == 1) {
587-
function.identifier->value = "-[negation]";
587+
function.identifier->value = "-:negation";
588+
}
589+
else if (function.identifier->value == "[]"
590+
&& function.args[0]->is_mutable) {
591+
function.identifier->value = "[]:mut";
588592
}
589593

590594
this->ast.push_back(function);
@@ -658,7 +662,11 @@ Result<ast::Node*, Error> Parser::parse_interface() {
658662
// Check if interface is - with just one argument
659663
if (interface.identifier->value == "-"
660664
&& interface.args.size() == 1) {
661-
interface.identifier->value = "-[negation]";
665+
interface.identifier->value = "-:negation";
666+
}
667+
else if (interface.identifier->value == "[]"
668+
&& interface.args[0]->is_mutable) {
669+
interface.identifier->value = "[]:mut";
662670
}
663671

664672
this->ast.push_back(interface);
@@ -740,7 +748,11 @@ Result<ast::Node*, Error> Parser::parse_builtin() {
740748
// Check if builtin is - with just one argument
741749
if (builtin.identifier->value == "-"
742750
&& builtin.args.size() == 1) {
743-
builtin.identifier->value = "-[negation]";
751+
builtin.identifier->value = "-:negation";
752+
}
753+
else if (builtin.identifier->value == "[]"
754+
&& builtin.args[0]->is_mutable) {
755+
builtin.identifier->value = "[]:mut";
744756
}
745757

746758
this->ast.push_back(builtin);
@@ -1116,7 +1128,7 @@ Result<ast::Node*, Error> Parser::parse_index_assignment(ast::Node* index_access
11161128
// Parse index access
11171129
assignment.assignable = index_access;
11181130
((ast::CallNode*) index_access)->args[0]->is_mutable = true;
1119-
((ast::CallNode*) index_access)->identifier->value = "subscript_mut";
1131+
((ast::CallNode*) index_access)->identifier->value = "[]:mut";
11201132

11211133
// Parse equal
11221134
auto equal = this->parse_token(token::Equal);
@@ -1649,7 +1661,7 @@ Result<ast::Node*, Error> Parser::parse_negation() {
16491661
auto identifier = this->parse_identifier(token::Minus);
16501662
if (identifier.is_error()) return identifier;
16511663
call.identifier = (ast::IdentifierNode*) identifier.get_value();
1652-
call.identifier->value = "-[negation]";
1664+
call.identifier->value = "-:negation";
16531665

16541666
// Parse expression
16551667
auto arg = ast::CallArgumentNode {this->current().line, this->current().column};
@@ -1807,6 +1819,14 @@ Result<ast::Node*, Error> Parser::parse_function_identifier() {
18071819
if (this->current() == token::LessEqual) return this->parse_identifier(token::LessEqual);
18081820
if (this->current() == token::Greater) return this->parse_identifier(token::Greater);
18091821
if (this->current() == token::GreaterEqual) return this->parse_identifier(token::GreaterEqual);
1822+
if (this->match({token::LeftBracket, token::RightBracket})) {
1823+
auto identifier = ast::IdentifierNode {this->current().line, this->current().column};
1824+
identifier.value = "[]";
1825+
this->advance();
1826+
this->advance();
1827+
this->ast.push_back(identifier);
1828+
return this->ast.last_element();
1829+
}
18101830
return this->parse_identifier(token::Identifier);
18111831
}
18121832

@@ -1932,7 +1952,7 @@ Result<ast::Node*, Error> Parser::parse_index_access(ast::Node* expression) {
19321952

19331953
// Create identifier node
19341954
auto identifier = ast::IdentifierNode {this->current().line, this->current().column};
1935-
identifier.value = "subscript";
1955+
identifier.value = "[]";
19361956
this->ast.push_back(identifier);
19371957
index_access.identifier = (ast::IdentifierNode*) this->ast.last_element();
19381958

std/std.dmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ builtin not(boolean: Bool): Bool
7979
builtin and(left: Bool, right: Bool): Bool
8080
builtin or(left: Bool, right: Bool): Bool
8181

82-
builtin subscript[t](array: Array[t], index: Int64): t
83-
builtin subscript_mut[t](mut array: Array[t], index: Int64): mut t
82+
builtin [][t](array: Array[t], index: Int64): t
83+
builtin [][t](mut array: Array[t], index: Int64): mut t
8484
builtin size[t](array: Array[t]): Int64
8585

8686
interface printWithoutLineEnding[t](object: t): None

0 commit comments

Comments
 (0)