Skip to content

Commit 24a65a1

Browse files
committed
Initial steps torwards having custom syntax for operators identifiers
1 parent 3ca2a52 commit 24a65a1

File tree

7 files changed

+135
-100
lines changed

7 files changed

+135
-100
lines changed

src/codegen/codegen.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,74 +1800,74 @@ llvm::Value* codegen::Context::codegen_call(ast::CallNode& node, std::optional<l
18001800

18011801
// Intrinsics
18021802
if (node.args.size() == 2) {
1803-
if (node.identifier->value == "add") {
1803+
if (node.identifier->value == "+") {
18041804
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18051805
return this->builder->CreateFAdd(args[0], args[1], "addtmp");
18061806
}
18071807
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18081808
return this->builder->CreateAdd(args[0], args[1], "addtmp");
18091809
}
18101810
}
1811-
if (node.identifier->value == "subtract") {
1811+
if (node.identifier->value == "-") {
18121812
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18131813
return this->builder->CreateFSub(args[0], args[1], "subtmp");
18141814
}
18151815
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18161816
return this->builder->CreateSub(args[0], args[1], "addtmp");
18171817
}
18181818
}
1819-
if (node.identifier->value == "multiply") {
1819+
if (node.identifier->value == "*") {
18201820
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18211821
return this->builder->CreateFMul(args[0], args[1], "multmp");
18221822
}
18231823
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18241824
return this->builder->CreateMul(args[0], args[1], "addtmp");
18251825
}
18261826
}
1827-
if (node.identifier->value == "divide") {
1827+
if (node.identifier->value == "/") {
18281828
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18291829
return this->builder->CreateFDiv(args[0], args[1], "divtmp");
18301830
}
18311831
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18321832
return this->builder->CreateSDiv(args[0], args[1], "divtmp");
18331833
}
18341834
}
1835-
if (node.identifier->value == "modulo") {
1835+
if (node.identifier->value == "%") {
18361836
return this->builder->CreateSRem(args[0], args[1], "remtmp");
18371837
}
1838-
if (node.identifier->value == "less") {
1838+
if (node.identifier->value == "<") {
18391839
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18401840
return this->builder->CreateFCmpULT(args[0], args[1], "cmptmp");
18411841
}
18421842
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18431843
return this->builder->CreateICmpULT(args[0], args[1], "addtmp");
18441844
}
18451845
}
1846-
if (node.identifier->value == "lessEqual") {
1846+
if (node.identifier->value == "<=") {
18471847
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18481848
return this->builder->CreateFCmpULE(args[0], args[1], "cmptmp");
18491849
}
18501850
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18511851
return this->builder->CreateICmpULE(args[0], args[1], "addtmp");
18521852
}
18531853
}
1854-
if (node.identifier->value == "greater") {
1854+
if (node.identifier->value == ">") {
18551855
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18561856
return this->builder->CreateFCmpUGT(args[0], args[1], "cmptmp");
18571857
}
18581858
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18591859
return this->builder->CreateICmpUGT(args[0], args[1], "addtmp");
18601860
}
18611861
}
1862-
if (node.identifier->value == "greaterEqual") {
1862+
if (node.identifier->value == ">=") {
18631863
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18641864
return this->builder->CreateFCmpUGE(args[0], args[1], "cmptmp");
18651865
}
18661866
if (args[0]->getType()->isIntegerTy() && args[1]->getType()->isIntegerTy()) {
18671867
return this->builder->CreateICmpUGE(args[0], args[1], "addtmp");
18681868
}
18691869
}
1870-
if (node.identifier->value == "equal") {
1870+
if (node.identifier->value == "==") {
18711871
if (args[0]->getType()->isDoubleTy() && args[1]->getType()->isDoubleTy()) {
18721872
return this->builder->CreateFCmpUEQ(args[0], args[1], "eqtmp");
18731873
}

src/parser.cpp

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct Parser {
6565
Result<ast::Node*, Error> parse_integer();
6666
Result<ast::Node*, Error> parse_boolean();
6767
Result<ast::Node*, Error> parse_identifier();
68+
Result<ast::Node*, Error> parse_function_identifier();
6869
Result<ast::Node*, Error> parse_identifier(token::TokenVariant token);
6970
Result<ast::Node*, Error> parse_string();
7071
Result<ast::Node*, Error> parse_interpolated_string();
@@ -499,7 +500,7 @@ Result<ast::Node*, Error> Parser::parse_function() {
499500
if (keyword.is_error()) return Error {};
500501

501502
// Parse indentifier
502-
auto identifier = this->parse_identifier();
503+
auto identifier = this->parse_function_identifier();
503504
if (identifier.is_error()) return identifier;
504505
function.identifier = (ast::IdentifierNode*) identifier.get_value();
505506

@@ -595,7 +596,7 @@ Result<ast::Node*, Error> Parser::parse_interface() {
595596
if (keyword.is_error()) return Error {};
596597

597598
// Parse indentifier
598-
auto identifier = this->parse_identifier();
599+
auto identifier = this->parse_function_identifier();
599600
if (identifier.is_error()) return identifier;
600601
interface.identifier = (ast::IdentifierNode*) identifier.get_value();
601602

@@ -664,7 +665,7 @@ Result<ast::Node*, Error> Parser::parse_builtin() {
664665
if (keyword.is_error()) return Error {};
665666

666667
// Parse indentifier
667-
auto identifier = this->parse_identifier();
668+
auto identifier = this->parse_function_identifier();
668669
if (identifier.is_error()) return identifier;
669670
builtin.identifier = (ast::IdentifierNode*) identifier.get_value();
670671

@@ -1521,16 +1522,16 @@ Result<ast::Node*, Error> Parser::parse_binary(int precedence) {
15211522

15221523
// Add identifier to call
15231524
call.identifier = (ast::IdentifierNode*) identifier.get_value();
1524-
if (call.identifier->value == "==") call.identifier->value = "equal";
1525-
else if (call.identifier->value == "<") call.identifier->value = "less";
1526-
else if (call.identifier->value == "<=") call.identifier->value = "lessEqual";
1527-
else if (call.identifier->value == ">") call.identifier->value = "greater";
1528-
else if (call.identifier->value == ">=") call.identifier->value = "greaterEqual";
1529-
else if (call.identifier->value == "+") call.identifier->value = "add";
1530-
else if (call.identifier->value == "-") call.identifier->value = "subtract";
1531-
else if (call.identifier->value == "*") call.identifier->value = "multiply";
1532-
else if (call.identifier->value == "/") call.identifier->value = "divide";
1533-
else if (call.identifier->value == "%") call.identifier->value = "modulo";
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";
15341535

15351536
// Add left node to call
15361537
this->ast.push_back(left_node);
@@ -1767,6 +1768,40 @@ Result<ast::Node*, Error> Parser::parse_identifier() {
17671768
return this->parse_identifier(token::Identifier);
17681769
}
17691770

1771+
// identifier → IDENTIFIER
1772+
// | AND
1773+
// | OR
1774+
// | NOT
1775+
// | PLUS
1776+
// | MINUS
1777+
// | STAR
1778+
// | SLASH
1779+
// | MODULO
1780+
// | EQUAL
1781+
// | EQUAL_EQUAL
1782+
// | NOT_EQUAL
1783+
// | LESS
1784+
// | LESS_EQUAL
1785+
// | GREATER
1786+
// | GREATER_EQUAL
1787+
Result<ast::Node*, Error> Parser::parse_function_identifier() {
1788+
if (this->current() == token::And) return this->parse_identifier(token::And);
1789+
if (this->current() == token::Or) return this->parse_identifier(token::Or);
1790+
if (this->current() == token::Not) return this->parse_identifier(token::Not);
1791+
if (this->current() == token::Plus) return this->parse_identifier(token::Plus);
1792+
if (this->current() == token::Minus) return this->parse_identifier(token::Minus);
1793+
if (this->current() == token::Star) return this->parse_identifier(token::Star);
1794+
if (this->current() == token::Slash) return this->parse_identifier(token::Slash);
1795+
if (this->current() == token::Modulo) return this->parse_identifier(token::Modulo);
1796+
if (this->current() == token::EqualEqual) return this->parse_identifier(token::EqualEqual);
1797+
if (this->current() == token::NotEqual) return this->parse_identifier(token::NotEqual);
1798+
if (this->current() == token::Less) return this->parse_identifier(token::Less);
1799+
if (this->current() == token::LessEqual) return this->parse_identifier(token::LessEqual);
1800+
if (this->current() == token::Greater) return this->parse_identifier(token::Greater);
1801+
if (this->current() == token::GreaterEqual) return this->parse_identifier(token::GreaterEqual);
1802+
return this->parse_identifier(token::Identifier);
1803+
}
1804+
17701805
Result<ast::Node*, Error> Parser::parse_identifier(token::TokenVariant token) {
17711806
// Create node
17721807
auto identifier = ast::IdentifierNode {this->current().line, this->current().column};

std/std.dmd

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
1-
interface equal[t](left: t, right: t): Bool
2-
interface notEqual[t](left: t, right: t): Bool
1+
interface ==[t](left: t, right: t): Bool
2+
interface !=[t](left: t, right: t): Bool
33

4-
interface less[t](left: t, right: t): Bool
5-
interface greater[t](left: t, right: t): Bool
6-
interface lessEqual[t](left: t, right: t): Bool
7-
interface greaterEqual[t](left: t, right: t): Bool
4+
interface <[t](left: t, right: t): Bool
5+
interface >[t](left: t, right: t): Bool
6+
interface <=[t](left: t, right: t): Bool
7+
interface >=[t](left: t, right: t): Bool
88

9-
interface add[t](left: t, right: t): t
10-
interface subtract[t](left: t, right: t): t
9+
interface +[t](left: t, right: t): t
10+
interface -[t](left: t, right: t): t
1111
interface negate[t](number: t): t
1212

13-
interface multiply[t](left: t, right: t): t
14-
interface divide[t](left: t, right: t): t
15-
interface modulo[t](left: t, right: t): t
16-
17-
builtin equal(left: Float64, right: Float64): Bool
18-
builtin equal(left: Int64, right: Int64): Bool
19-
builtin equal(left: Int32, right: Int32): Bool
20-
builtin equal(left: Int8, right: Int8): Bool
21-
builtin equal(left: Bool, right: Bool): Bool
22-
23-
builtin notEqual(left: Float64, right: Float64): Bool
24-
builtin notEqual(left: Int64, right: Int64): Bool
25-
builtin notEqual(left: Int32, right: Int32): Bool
26-
builtin notEqual(left: Int8, right: Int8): Bool
27-
28-
builtin less(left: Float64, right: Float64): Bool
29-
builtin less(left: Int64, right: Int64): Bool
30-
builtin less(left: Int32, right: Int32): Bool
31-
builtin less(left: Int8, right: Int8): Bool
32-
33-
builtin lessEqual(left: Float64, right: Float64): Bool
34-
builtin lessEqual(left: Int64, right: Int64): Bool
35-
builtin lessEqual(left: Int32, right: Int32): Bool
36-
builtin lessEqual(left: Int8, right: Int8): Bool
37-
38-
builtin greater(left: Float64, right: Float64): Bool
39-
builtin greater(left: Int64, right: Int64): Bool
40-
builtin greater(left: Int32, right: Int32): Bool
41-
builtin greater(left: Int8, right: Int8): Bool
42-
43-
builtin greaterEqual(left: Float64, right: Float64): Bool
44-
builtin greaterEqual(left: Int64, right: Int64): Bool
45-
builtin greaterEqual(left: Int32, right: Int32): Bool
46-
builtin greaterEqual(left: Int8, right: Int8): Bool
47-
48-
builtin add(left: Float64, right: Float64): Float64
49-
builtin add(left: Int64, right: Int64): Int64
50-
builtin add(left: Int32, right: Int32): Int32
51-
builtin add(left: Int8, right: Int8): Int8
52-
53-
builtin subtract(left: Float64, right: Float64): Float64
54-
builtin subtract(left: Int64, right: Int64): Int64
55-
builtin subtract(left: Int32, right: Int32): Int32
56-
builtin subtract(left: Int8, right: Int8): Int8
13+
interface *[t](left: t, right: t): t
14+
interface /[t](left: t, right: t): t
15+
interface %[t](left: t, right: t): t
16+
17+
builtin ==(left: Float64, right: Float64): Bool
18+
builtin ==(left: Int64, right: Int64): Bool
19+
builtin ==(left: Int32, right: Int32): Bool
20+
builtin ==(left: Int8, right: Int8): Bool
21+
builtin ==(left: Bool, right: Bool): Bool
22+
23+
builtin !=(left: Float64, right: Float64): Bool
24+
builtin !=(left: Int64, right: Int64): Bool
25+
builtin !=(left: Int32, right: Int32): Bool
26+
builtin !=(left: Int8, right: Int8): Bool
27+
28+
builtin <(left: Float64, right: Float64): Bool
29+
builtin <(left: Int64, right: Int64): Bool
30+
builtin <(left: Int32, right: Int32): Bool
31+
builtin <(left: Int8, right: Int8): Bool
32+
33+
builtin <=(left: Float64, right: Float64): Bool
34+
builtin <=(left: Int64, right: Int64): Bool
35+
builtin <=(left: Int32, right: Int32): Bool
36+
builtin <=(left: Int8, right: Int8): Bool
37+
38+
builtin >(left: Float64, right: Float64): Bool
39+
builtin >(left: Int64, right: Int64): Bool
40+
builtin >(left: Int32, right: Int32): Bool
41+
builtin >(left: Int8, right: Int8): Bool
42+
43+
builtin >=(left: Float64, right: Float64): Bool
44+
builtin >=(left: Int64, right: Int64): Bool
45+
builtin >=(left: Int32, right: Int32): Bool
46+
builtin >=(left: Int8, right: Int8): Bool
47+
48+
builtin +(left: Float64, right: Float64): Float64
49+
builtin +(left: Int64, right: Int64): Int64
50+
builtin +(left: Int32, right: Int32): Int32
51+
builtin +(left: Int8, right: Int8): Int8
52+
53+
builtin -(left: Float64, right: Float64): Float64
54+
builtin -(left: Int64, right: Int64): Int64
55+
builtin -(left: Int32, right: Int32): Int32
56+
builtin -(left: Int8, right: Int8): Int8
5757

5858
builtin negate(operand: Float64): Float64
5959
builtin negate(operand: Int64): Int64
6060
builtin negate(operand: Int32): Int32
6161
builtin negate(operand: Int8): Int8
6262

63-
builtin multiply(left: Float64, right: Float64): Float64
64-
builtin multiply(left: Int64, right: Int64): Int64
65-
builtin multiply(left: Int32, right: Int32): Int32
66-
builtin multiply(left: Int8, right: Int8): Int8
63+
builtin *(left: Float64, right: Float64): Float64
64+
builtin *(left: Int64, right: Int64): Int64
65+
builtin *(left: Int32, right: Int32): Int32
66+
builtin *(left: Int8, right: Int8): Int8
6767

68-
builtin divide(left: Float64, right: Float64): Float64
69-
builtin divide(left: Int64, right: Int64): Int64
70-
builtin divide(left: Int32, right: Int32): Int32
71-
builtin divide(left: Int8, right: Int8): Int8
68+
builtin /(left: Float64, right: Float64): Float64
69+
builtin /(left: Int64, right: Int64): Int64
70+
builtin /(left: Int32, right: Int32): Int32
71+
builtin /(left: Int8, right: Int8): Int8
7272

73-
builtin modulo(left: Float64, right: Float64): Float64
74-
builtin modulo(left: Int64, right: Int64): Int64
75-
builtin modulo(left: Int32, right: Int32): Int32
76-
builtin modulo(left: Int8, right: Int8): Int8
73+
builtin %(left: Float64, right: Float64): Float64
74+
builtin %(left: Int64, right: Int64): Int64
75+
builtin %(left: Int32, right: Int32): Int32
76+
builtin %(left: Int8, right: Int8): Int8
7777

7878
builtin not(boolean: Bool): Bool
7979
builtin and(left: Bool, right: Bool): Bool

test/assignments/type_anotations.dmd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
interface myAdd[t](a: t, b: t): t
1+
interface add[t](a: t, b: t): t
22

3-
function myAdd(a: Float64, b: Float64): Float64
3+
function add(a: Float64, b: Float64): Float64
44
return a + b
55

6-
function myAdd(a: Int64, b: Int64): Int64
6+
function add(a: Int64, b: Int64): Int64
77
return a + b
88

99
a = 10 : Float64
1010
print(a)
11-
print(myAdd(a, 3.6))
12-
print(myAdd(a, 20))
11+
print(add(a, 3.6))
12+
print(add(a, 20))
1313

14-
result = myAdd(20, 30) : Float64
14+
result = add(20, 30) : Float64
1515
print(result)
1616

1717
--- Output

test/functions/functions8.dmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function myAdd[t](a: t, b: t): t
1+
function add[t](a: t, b: t): t
22
c = 10
33
print(c)
44
return a + b
55

6-
print(myAdd(10, 20))
7-
print(myAdd(4.5, 1.3))
6+
print(add(10, 20))
7+
print(add(4.5, 1.3))
88

99
--- Output
1010
10

test/type_inference/type_inference_3.dmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function myAdd(a: Float64, b: Float64): Float64
1+
function add(a: Float64, b: Float64): Float64
22
a + b
33

4-
print(myAdd(10, 20))
4+
print(add(10, 20))
55

66
--- Output
77
30

test/type_inference/type_inference_4.dmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function myAdd(a: Float64, b: Float64): Float64
1+
function add(a: Float64, b: Float64): Float64
22
return a + b
33

4-
print(myAdd(2 + 2, 4))
4+
print(add(2 + 2, 4))
55

66
--- Output
77
8

0 commit comments

Comments
 (0)