Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): addition and subtraction assignment operators implementation #4018

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8e591ee
add and subtract assignment operators implementation
wzslr321 Aug 30, 2023
db95724
add forgotten subtract assignment to expressions.txt
wzslr321 Aug 30, 2023
b0b826b
add subtract assignment test to examples
wzslr321 Aug 30, 2023
5460ec2
modify test in order to pnpm build to pass; requires change of behavior
wzslr321 Aug 30, 2023
d6636ec
change incr/decr assignment type from binary operator to assignment s…
wzslr321 Sep 2, 2023
0bbadee
Merge branch 'main' into feature/compiler/add-and-sub-assignment-ops
monadabot Sep 2, 2023
b41f764
chore: self mutation (e2e-1of2.diff)
monadabot Sep 2, 2023
489a0f1
valid assignment grammar;incorect logic fails tests
wzslr321 Sep 2, 2023
badee4f
Merge branch 'feature/compiler/add-and-sub-assignment-ops' of https:/…
wzslr321 Sep 2, 2023
252458a
Merge branch 'main' into feature/compiler/add-and-sub-assignment-ops
monadabot Sep 2, 2023
0616dc6
chore: self mutation (build.diff)
monadabot Sep 2, 2023
d3e8fdb
fix typo in tests
wzslr321 Sep 2, 2023
a1cb096
Merge branch 'feature/compiler/add-and-sub-assignment-ops' of https:/…
wzslr321 Sep 2, 2023
189a97b
Update docs/docs/03-language-reference.md
wzslr321 Sep 4, 2023
78a95b3
address PR comments - improve type_check
wzslr321 Sep 4, 2023
6c47990
Merge branch 'feature/compiler/add-and-sub-assignment-ops' of https:/…
wzslr321 Sep 4, 2023
05368f1
Merge branch 'main' into feature/compiler/add-and-sub-assignment-ops
monadabot Sep 4, 2023
7d70879
chore: self mutation (build.diff)
monadabot Sep 4, 2023
241d99f
remove duplication from grammar by modyfing it and adjusting parser
wzslr321 Sep 5, 2023
92edbf2
Merge branch 'feature/compiler/add-and-sub-assignment-ops' of https:/…
wzslr321 Sep 5, 2023
8937c34
Merge branch 'main' into feature/compiler/add-and-sub-assignment-ops
monadabot Sep 5, 2023
9eca4b0
chore: self mutation (build.diff)
monadabot Sep 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2035,15 +2035,17 @@ Ternary or conditional operators are not supported.

#### 6.4.3 Mathematics Operators

| Operator | Description | Example |
| -------- | -------------- | -------- |
| `*` | Multiplication | `a * b` |
| `/` | Division | `a / b` |
| `\` | Floor Division | `a \ b` |
| `%` | Modulus | `a % b` |
| `+` | Addition | `a + b` |
| `-` | Subtraction | `a - b` |
| `**` | Power | `a ** b` |
| Operator | Description | Example |
| -------- | ---------------------- | -------- |
| `*` | Multiplication | `a * b` |
| `/` | Division | `a / b` |
| `\` | Floor Division | `a \ b` |
| `%` | Modulus | `a % b` |
| `+` | Addition | `a + b` |
| `-` | Subtraction | `a - b` |
| `**` | Power | `a ** b` |
| `-=` | Subtraction Assignment | `a -= b` |
| `+=` | Addition Assignment | `a += b` |

[`▲ top`][top]

Expand Down
8 changes: 7 additions & 1 deletion examples/tests/valid/expressions_binary_operators.w
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ assert(xyznf == -5);
let xyznfj = 501.9 \ (-99.1 - 0.91);
assert(xyznfj == -5);
let xynfj = -501.9 \ (-99.1 - 0.91);
assert(xynfj == 5);
assert(xynfj == 5);
let xyas = 5;
xyas += 10;
assert(xyas == 15);
let xyss = 5;
xyss -= 10;
assert(xyss == -5);
18 changes: 11 additions & 7 deletions libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const PREC = {
RELATIONAL: 70,
UNWRAP_OR: 80,
SHIFT: 90,
ADD: 100,
MULTIPLY: 110,
UNARY: 120,
OPTIONAL_TEST: 130,
POWER: 140,
MEMBER: 150,
CALL: 160,
ADD_ASSIGNMENT: 100,
SUBTRACT_ASSIGNMENT: 110,
ADD: 120,
MULTIPLY: 130,
UNARY: 140,
OPTIONAL_TEST: 150,
POWER: 160,
MEMBER: 170,
CALL: 180,
};

module.exports = grammar({
Expand Down Expand Up @@ -607,6 +609,8 @@ module.exports = grammar({
//['>>', PREC.SHIFT],
//['>>>', PREC.SHIFT],
["??", PREC.UNWRAP_OR],
["+=", PREC.ADD_ASSIGNMENT],
["-=", PREC.SUBTRACT_ASSIGNMENT],
wzslr321 marked this conversation as resolved.
Show resolved Hide resolved
];

return choice(
Expand Down
10 changes: 10 additions & 0 deletions libs/tree-sitter-wing/test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Binary expressions
================================================================================

5 * 1;
1 += 5;
1 -= 5;
(5+1) % 1;
5+1 % 1;
5 % 1 + 1;
Expand All @@ -38,6 +40,14 @@ Binary expressions
--------------------------------------------------------------------------------

(source
(expression_statement
(binary_expression
left: (number)
right: (number)))
(expression_statement
(binary_expression
left: (number)
right: (number)))
(expression_statement
(binary_expression
left: (number)
Expand Down
2 changes: 2 additions & 0 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,8 @@ pub enum BinaryOperator {
LogicalAnd,
LogicalOr,
UnwrapOr,
SubtractAssignment,
AddAssignment,
wzslr321 marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug)]
Expand Down
4 changes: 3 additions & 1 deletion libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ impl<'a> JSifier<'a> {
// Use JS nullish coalescing operator which treats undefined and null the same
// this is inline with how wing jsifies optionals
"??"
}
},
BinaryOperator::AddAssignment => "+=",
BinaryOperator::SubtractAssignment => "-=",
};
format!("({} {} {})", js_left, js_op, js_right)
}
Expand Down
2 changes: 2 additions & 0 deletions libs/wingc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,8 @@ impl<'s> Parser<'s> {
"\\" => BinaryOperator::FloorDiv,
"**" => BinaryOperator::Power,
"??" => BinaryOperator::UnwrapOr,
"-=" => BinaryOperator::SubtractAssignment,
"+=" => BinaryOperator::AddAssignment,
"ERROR" => self.with_error::<BinaryOperator>("Expected binary operator", expression_node)?,
other => return self.report_unimplemented_grammar(other, "binary operator", expression_node),
},
Expand Down
10 changes: 10 additions & 0 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,16 @@ impl<'a> TypeChecker<'a> {
(inner_type, ltype_phase)
}
}
BinaryOperator::SubtractAssignment => {
self.validate_type(ltype, self.types.number(), left);
self.validate_type(rtype, self.types.number(), right);
(self.types.number(), Phase::Independent)
}
BinaryOperator::AddAssignment => {
self.validate_type(ltype, self.types.number(), left);
self.validate_type(rtype, self.types.number(), right);
(self.types.number(), Phase::Independent)
}
}
}
ExprKind::Unary { op, exp: unary_exp } => {
Expand Down