-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add &+ &- &* &** operators parsing #6329
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
Changes from 2 commits
71f72f4
12e0e54
25cab7c
ad0a3c8
96db8fd
d3b1c0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| require "../../spec_helper" | ||
|
|
||
| describe "Normalize: op assign" do | ||
| it "normalizes var +=" do | ||
| assert_normalize "a = 1; a += 2", "a = 1\na = a + 2" | ||
| ["+", "-", "*", "&+", "&-", "&*"].each do |op| | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seem like in the rest of this file each test case is broken out. I think it might make it easier to understand if they are different test cases. |
||
| it "normalizes var #{op}=" do | ||
| assert_normalize "a = 1; a #{op}= 2", "a = 1\na = a #{op} 2" | ||
| end | ||
| end | ||
|
|
||
| it "normalizes var ||=" do | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,7 @@ module Crystal | |
| it_parses "~ 1", Call.new(1.int32, "~") | ||
| it_parses "1 && 2", And.new(1.int32, 2.int32) | ||
| it_parses "1 || 2", Or.new(1.int32, 2.int32) | ||
| it_parses "&- 1", Call.new(1.int32, "&-") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is support for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I just added
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I asked since AFAIR there's also prefix
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can prefix
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asterite
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that I am not planning on implementing prefix |
||
|
|
||
| it_parses "1 <=> 2", Call.new(1.int32, "<=>", 2.int32) | ||
| it_parses "1 !~ 2", Call.new(1.int32, "!~", 2.int32) | ||
|
|
@@ -419,7 +420,7 @@ module Crystal | |
| it_parses "f.x = Foo.new", Call.new("f".call, "x=", [Call.new("Foo".path, "new")] of ASTNode) | ||
| it_parses "f.x = - 1", Call.new("f".call, "x=", [Call.new(1.int32, "-")] of ASTNode) | ||
|
|
||
| ["+", "-", "*", "/", "%", "|", "&", "^", "**", "<<", ">>"].each do |op| | ||
| ["+", "-", "*", "/", "%", "|", "&", "^", "**", "<<", ">>", "&+", "&-", "&*"].each do |op| | ||
| it_parses "f.x #{op}= 2", OpAssign.new(Call.new("f".call, "x"), op, 2.int32) | ||
| end | ||
|
|
||
|
|
@@ -430,9 +431,10 @@ module Crystal | |
| it_parses "def %(); end;", Def.new("%") | ||
| it_parses "def /(); end;", Def.new("/") | ||
|
|
||
| ["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "%", "|", "&", "^", "**", "===", "=~", "!~"].each do |op| | ||
| ["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "%", "|", "&", "^", "**", "===", "=~", "!~", "&+", "&-", "&*", "&**"].each do |op| | ||
| it_parses "1 #{op} 2", Call.new(1.int32, op, 2.int32) | ||
| it_parses "n #{op} 2", Call.new("n".call, op, 2.int32) | ||
| it_parses "def #{op}(); end", Def.new(op) | ||
| end | ||
|
|
||
| ["bar", "+", "-", "*", "/", "<", "<=", "==", ">", ">=", "%", "|", "&", "^", "**", "===", "=~", "!~"].each do |name| | ||
|
|
@@ -441,7 +443,7 @@ module Crystal | |
| it_parses "foo.#{name}(1, 2)", Call.new("foo".call, name, 1.int32, 2.int32) | ||
| end | ||
|
|
||
| ["+", "-", "*", "/", "%", "|", "&", "^", "**", "<<", ">>"].each do |op| | ||
| ["+", "-", "*", "/", "%", "|", "&", "^", "**", "<<", ">>", "&+", "&-", "&*"].each do |op| | ||
| it_parses "a = 1; a #{op}= 1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, op, 1.int32)] | ||
| it_parses "a = 1; a #{op}=\n1", [Assign.new("a".var, 1.int32), OpAssign.new("a".var, op, 1.int32)] | ||
| it_parses "a.b #{op}=\n1", OpAssign.new(Call.new("a".call, "b"), op, 1.int32) | ||
|
|
@@ -642,7 +644,8 @@ module Crystal | |
| assert_syntax_error "#{keyword} ? 1 : 2", "void value expression" | ||
| assert_syntax_error "+#{keyword}", "void value expression" | ||
|
|
||
| ["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "%", "|", "&", "^", "**", "==="].each do |op| | ||
| ["<<", "<", "<=", "==", ">>", ">", ">=", "+", "-", "*", "/", "%", "|", | ||
| "&", "^", "**", "===", "&+", "&-", "&*", "&**"].each do |op| | ||
| assert_syntax_error "#{keyword} #{op} 1", "void value expression" | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -914,7 +914,7 @@ describe "Block inference" do | |
| )) { types["Moo"].types["Bar"] } | ||
| end | ||
|
|
||
| it "passes &->f" do | ||
| it "passes &(->f)" do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes should no longer be necessary. |
||
| assert_type(%( | ||
| def foo | ||
| end | ||
|
|
@@ -924,7 +924,7 @@ describe "Block inference" do | |
| 1 | ||
| end | ||
|
|
||
| bar &->foo | ||
| bar &(->foo) | ||
| )) { int32 } | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add a lookahead to avoid this breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the current lexer does not do lookahead AFAIK. So is changing a bit the nature of it for this case. And that syntax is not wide used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
peek_next_char?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use this diff:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: I just edited the diff because you can call
peek_next_charinstead ofreader.peek_next_charThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated again to revert the change in the spec that uses
&->where parentheses were added.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diff merged. Thanks @asterite