Skip to content

Commit bd04149

Browse files
committed
Now negation is defined also with -
1 parent 24a65a1 commit bd04149

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/codegen/codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
18831883
}
18841884
}
18851885
if (node.args.size() == 1) {
1886-
if (node.identifier->value == "negate") {
1886+
if (node.identifier->value == "-[negation]") {
18871887
if (args[0]->getType()->isDoubleTy()) {
18881888
return this->builder->CreateFNeg(args[0], "negation");
18891889
}

src/parser.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ Result<ast::Node*, Error> Parser::parse_function() {
581581
}
582582
}
583583

584+
// Check if function is - with just one argument
585+
if (function.identifier->value == "-"
586+
&& function.args.size() == 1) {
587+
function.identifier->value = "-[negation]";
588+
}
589+
584590
this->ast.push_back(function);
585591
return this->ast.last_element();
586592
}
@@ -649,6 +655,12 @@ Result<ast::Node*, Error> Parser::parse_interface() {
649655

650656
interface.return_type = type.get_value();
651657

658+
// Check if interface is - with just one argument
659+
if (interface.identifier->value == "-"
660+
&& interface.args.size() == 1) {
661+
interface.identifier->value = "-[negation]";
662+
}
663+
652664
this->ast.push_back(interface);
653665
return this->ast.last_element();
654666
}
@@ -725,6 +737,12 @@ Result<ast::Node*, Error> Parser::parse_builtin() {
725737
builtin.state = ast::FunctionGenericCompletelyTyped;
726738
}
727739

740+
// Check if builtin is - with just one argument
741+
if (builtin.identifier->value == "-"
742+
&& builtin.args.size() == 1) {
743+
builtin.identifier->value = "-[negation]";
744+
}
745+
728746
this->ast.push_back(builtin);
729747
return this->ast.last_element();
730748
}
@@ -1522,16 +1540,6 @@ Result<ast::Node*, Error> Parser::parse_binary(int precedence) {
15221540

15231541
// Add identifier to call
15241542
call.identifier = (ast::IdentifierNode*) identifier.get_value();
1525-
// if (call.identifier->value == "==") call.identifier->value = "equal";
1526-
// else if (call.identifier->value == "<") call.identifier->value = "less";
1527-
// else if (call.identifier->value == "<=") call.identifier->value = "lessEqual";
1528-
// else if (call.identifier->value == ">") call.identifier->value = "greater";
1529-
// else if (call.identifier->value == ">=") call.identifier->value = "greaterEqual";
1530-
// else if (call.identifier->value == "+") call.identifier->value = "add";
1531-
// else if (call.identifier->value == "-") call.identifier->value = "subtract";
1532-
// else if (call.identifier->value == "*") call.identifier->value = "multiply";
1533-
// else if (call.identifier->value == "/") call.identifier->value = "divide";
1534-
// else if (call.identifier->value == "%") call.identifier->value = "modulo";
15351543

15361544
// Add left node to call
15371545
this->ast.push_back(left_node);
@@ -1641,7 +1649,7 @@ Result<ast::Node*, Error> Parser::parse_negation() {
16411649
auto identifier = this->parse_identifier(token::Minus);
16421650
if (identifier.is_error()) return identifier;
16431651
call.identifier = (ast::IdentifierNode*) identifier.get_value();
1644-
call.identifier->value = "negate";
1652+
call.identifier->value = "-[negation]";
16451653

16461654
// Parse expression
16471655
auto arg = ast::CallArgumentNode {this->current().line, this->current().column};

std/std.dmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface >=[t](left: t, right: t): Bool
88

99
interface +[t](left: t, right: t): t
1010
interface -[t](left: t, right: t): t
11-
interface negate[t](number: t): t
11+
interface -[t](number: t): t
1212

1313
interface *[t](left: t, right: t): t
1414
interface /[t](left: t, right: t): t
@@ -55,10 +55,10 @@ builtin -(left: Int64, right: Int64): Int64
5555
builtin -(left: Int32, right: Int32): Int32
5656
builtin -(left: Int8, right: Int8): Int8
5757

58-
builtin negate(operand: Float64): Float64
59-
builtin negate(operand: Int64): Int64
60-
builtin negate(operand: Int32): Int32
61-
builtin negate(operand: Int8): Int8
58+
builtin -(number: Float64): Float64
59+
builtin -(number: Int64): Int64
60+
builtin -(number: Int32): Int32
61+
builtin -(number: Int8): Int8
6262

6363
builtin *(left: Float64, right: Float64): Float64
6464
builtin *(left: Int64, right: Int64): Int64

0 commit comments

Comments
 (0)