-
Notifications
You must be signed in to change notification settings - Fork 39
Add more (infix) operators #1091
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -975,10 +975,13 @@ class Parser(positions: Positions, tokens: Seq[Token], source: Source) { | |
| } | ||
|
|
||
| def orExpr(): Term = infix(andExpr, `||`) | ||
| def andExpr(): Term = infix(eqExpr, `&&`) | ||
| def eqExpr(): Term = infix(relExpr, `===`, `!==`) | ||
| def relExpr(): Term = infix(addExpr, `<=`, `>=`, `<`, `>`) | ||
| def addExpr(): Term = infix(mulExpr, `++`, `+`, `-`) | ||
| def andExpr(): Term = infix(pipeExpr, `&&`) | ||
| def pipeExpr(): Term = infix(bitOrExpr, `<|`, `|>`) | ||
| def bitOrExpr(): Term = infix(bitAndExpr, TokenKind.`|`) | ||
| def bitAndExpr(): Term = infix(eqExpr, `&`) | ||
| def eqExpr(): Term = infix(rangeExpr, `===`, `!==`, `<=`, `>=`, `<`, `>`) | ||
| def rangeExpr(): Term = infix(addExpr, `..`, `...`, TokenKind.`~`, TokenKind.`~>`, TokenKind.`<~`) | ||
| def addExpr(): Term = infix(mulExpr, `++`, `--`, `+`, `-`, `<<`, `>>`, `^`, `^^`) | ||
| def mulExpr(): Term = infix(callExpr, `*`, `/`) | ||
|
|
||
| inline def infix(nonTerminal: () => Term, ops: TokenKind*): Term = | ||
|
|
@@ -1022,15 +1025,36 @@ class Parser(positions: Positions, tokens: Seq[Token], source: Source) { | |
| case `&&` => "infixAnd" | ||
| case `===` => "infixEq" | ||
| case `!==` => "infixNeq" | ||
| case `<` => "infixLt" | ||
| case `>` => "infixGt" | ||
| case `<` => "infixLt" | ||
| case `>` => "infixGt" | ||
| case `<=` => "infixLte" | ||
| case `>=` => "infixGte" | ||
| case `+` => "infixAdd" | ||
| case `-` => "infixSub" | ||
| case `*` => "infixMul" | ||
| case `/` => "infixDiv" | ||
| case `+` => "infixAdd" | ||
| case `+=` => "infixAdd" | ||
| case `-` => "infixSub" | ||
| case `-=` => "infixSub" | ||
| case `*` => "infixMul" | ||
| case `*=` => "infixMul" | ||
| case `/` => "infixDiv" | ||
| case `/=` => "infixDiv" | ||
| case `++` => "infixConcat" | ||
| case `>>` => "infixShr" | ||
| case `<<` => "infixShl" | ||
| case `--` => "infixRemove" | ||
|
|
||
| case TokenKind.`~` => "infixTilde" | ||
| case TokenKind.`~>` => "infixTildeRight" | ||
| case TokenKind.`<~` => "infixTildeLeft" | ||
| case TokenKind.`|` => "infixPipe" | ||
| case TokenKind.`&` => "infixAmp" | ||
|
|
||
| case `<|` => "infixPipeLeft" | ||
| case `|>` => "infixPipeRight" | ||
| case `..` => "infixDotDot" | ||
| case `...` => "infixDotDotDot" | ||
| case `^^` => "infixHatHat" | ||
| case `^` => "infixHat" | ||
|
|
||
| case _ => sys.error(s"Internal compiler error: not a valid operator ${op}") | ||
| } | ||
|
|
||
|
|
@@ -1052,18 +1076,32 @@ class Parser(positions: Positions, tokens: Seq[Token], source: Source) { | |
|
|
||
| while (peek(`.`) || isArguments) | ||
| peek.kind match { | ||
| // member selection (or method call) | ||
| // member selection, postfix acess, or method call | ||
| // <EXPR>.<NAME> | ||
| // | <EXPR>.<NAME>( ... ) | ||
| case `.` => | ||
| val dot = peek | ||
| consume(`.`) | ||
| val member = idRef() | ||
| // method call | ||
| if (isArguments) { | ||
| val (targs, vargs, bargs) = arguments() | ||
| e = Term.MethodCall(e, member, targs, vargs, bargs, span()) | ||
| } else { | ||
| e = Term.MethodCall(e, member, Nil, Nil, Nil, span()) | ||
| peek.kind match { | ||
| // <EXPR>.[<EXPR>, ...] | ||
| case `[` => | ||
| val bracket = peek | ||
| val arguments = some(expr, `[`, `,`, `]`) | ||
| e = MethodCall(e, | ||
| IdRef(Nil, "postfixAccess", Span(source, dot.start, bracket.end, Synthesized)), | ||
| Nil, arguments.unspan.map(a => ValueArg.Unnamed(a)), Nil, | ||
| Span(source, e.span.from, arguments.span.to, Synthesized) | ||
| ) | ||
|
|
||
| case _ => | ||
| val member = idRef() | ||
| // method call | ||
| if (isArguments) { | ||
| val (targs, vargs, bargs) = arguments() | ||
| e = Term.MethodCall(e, member, targs, vargs, bargs, span()) | ||
| } else { | ||
| e = Term.MethodCall(e, member, Nil, Nil, Nil, span()) | ||
| } | ||
|
Comment on lines
+1086
to
+1104
Collaborator
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. Extract into separate PR. Needs further discussion, e.g. setting an array. Do we also want a dedicated syntax for that? |
||
| } | ||
|
|
||
| // function call | ||
|
|
@@ -1168,7 +1206,20 @@ class Parser(positions: Positions, tokens: Seq[Token], source: Source) { | |
| case _ if isVariable => | ||
| peek(1).kind match { | ||
| case _: Str => templateString() | ||
| case _ => variable() | ||
| case _ => | ||
| val lhs = variable() | ||
| peek.kind match { | ||
| case `+=` | `-=` | `*=` | `/=` => | ||
|
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. what about |
||
| val op = next() | ||
| val operand = expr() | ||
| val rhs = Call( | ||
| IdTarget(IdRef(Nil, opName(op.kind), op.span(source).synthesized)), | ||
| Nil, List(ValueArg.Unnamed(lhs), ValueArg.Unnamed(operand)), Nil, | ||
| Span(source, lhs.span.from, operand.span.to, Synthesized) | ||
| ) | ||
| Assign(lhs.id, rhs, span()) | ||
| case _ => lhs | ||
| } | ||
| } | ||
| case _ if isHole => hole() | ||
| case _ if isTupleOrGroup => tupleOrGroup() | ||
|
|
@@ -1295,7 +1346,7 @@ class Parser(positions: Positions, tokens: Seq[Token], source: Source) { | |
| private def isUnitLiteral: Boolean = peek(`(`) && peek(1, `)`) | ||
|
|
||
| def isVariable: Boolean = isIdRef | ||
| def variable(): Term = | ||
| def variable(): Term.Var = | ||
| nonterminal: | ||
| Var(idRef(), span()) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1 | ||
| 2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| def postfixAccess[T](arr: Array[T], index: Int): T = unsafeGet(arr, index) | ||
|
|
||
| // access as a function | ||
| def example1() = { | ||
| val arr = array[Int](10, 1) | ||
| println(arr.[2]) | ||
| } | ||
|
|
||
| // access on a capability / object | ||
| interface MyRef[T] { | ||
| def postfixAccess(index: Int): T | ||
| } | ||
|
|
||
| def example2() = { | ||
| def arr = new MyRef[Int] { | ||
| def postfixAccess(index) = 1 | ||
| }; | ||
| println(arr.[0] + arr.[1]) | ||
| } | ||
|
|
||
| // multi dimensional access | ||
| def example3(arr: Array[Array[Int]]) = | ||
| println(arr.[0].[1] + arr.[1].[0]) | ||
|
|
||
| def main() = { | ||
| example1() | ||
| example2() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 3 | ||
| 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| def main() = { | ||
| var x = 1 | ||
| var y = 2 | ||
|
|
||
| x += y | ||
| println(x) // 3 | ||
| y -= 1 | ||
| println(y) // 1 | ||
| } |
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.
We decided to delete these.