-
Notifications
You must be signed in to change notification settings - Fork 48
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
Arbitrary arity tuple #435
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cd25ed9
Add arbitrary arity tuple Quat
Primetalk 3753bd7
Decode arbitrary arity tuple
Primetalk 748d619
Use runtime.Tuples to construct Tuple
Primetalk 71958d9
Fat finger fix
Primetalk 306c524
Support beta reduction with arbitrary tuples
Primetalk c577120
Support empty tuple decoding
Primetalk 8690ffa
Parse arbitrary tuples construction
Primetalk 348eab2
Merge branch 'master' into arbitrary-arity-tuple
Primetalk 53daedb
Remove commented out code
Primetalk cb74dfb
Merge branch 'arbitrary-arity-tuple' of github.com:Primetalk/zio-prot…
Primetalk 892b566
Merge branch 'master' into arbitrary-arity-tuple
deusaquilus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ trait ParserLibrary extends ParserFactory { | |
protected def functionParser(using Quotes, TranspileConfig) = ParserChain.attempt(FunctionParser(_)) | ||
protected def functionApplyParser(using Quotes, TranspileConfig) = ParserChain.attempt(FunctionApplyParser(_)) | ||
protected def valParser(using Quotes, TranspileConfig) = ParserChain.attempt(ValParser(_)) | ||
protected def arbitraryTupleParser(using Quotes, TranspileConfig) = ParserChain.attempt(ArbitraryTupleBlockParser(_)) | ||
protected def blockParser(using Quotes, TranspileConfig) = ParserChain.attempt(BlockParser(_)) | ||
protected def extrasParser(using Quotes, TranspileConfig) = ParserChain.attempt(ExtrasParser(_)) | ||
protected def operationsParser(using Quotes, TranspileConfig) = ParserChain.attempt(OperationsParser(_)) | ||
|
@@ -88,6 +89,7 @@ trait ParserLibrary extends ParserFactory { | |
.orElse(functionParser) // decided to have it be it's own parser unlike Quill3 | ||
.orElse(patMatchParser) | ||
.orElse(valParser) | ||
.orElse(arbitraryTupleParser) | ||
.orElse(blockParser) | ||
.orElse(operationsParser) | ||
.orElse(extrasParser) | ||
|
@@ -153,6 +155,126 @@ class ValParser(val rootParse: Parser)(using Quotes, TranspileConfig) | |
case Unseal(ValDefTerm(ast)) => ast | ||
} | ||
} | ||
/** | ||
* Matches `runtime.Tuples.cons(head,tail)`. | ||
*/ | ||
object TupleCons { | ||
def unapply(using Quotes)(t: quotes.reflect.Term): Option[(quotes.reflect.Term, quotes.reflect.Term)] = { | ||
import quotes.reflect.* | ||
t match { | ||
case Apply(Select(Select(Ident("runtime"), "Tuples"), "cons"), List(head, tail)) => | ||
Some((head, tail)) | ||
case _ => | ||
None | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Matches inner.asInstanceOf[T]: T | ||
*/ | ||
object AsInstanceOf { | ||
def unapply(using Quotes)(term: quotes.reflect.Term): Option[quotes.reflect.Term] = { | ||
import quotes.reflect._ | ||
term match { | ||
case TypeApply(Select(inner, "asInstanceOf"), _) => Some(inner) | ||
case _ => None | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Matches an inlined call to `Tuple.*:`: | ||
* {{{ | ||
* { | ||
* val Tuple_this: scala.Tuple$package.EmptyTuple.type = scala.Tuple$package.EmptyTuple | ||
* | ||
* (scala.runtime.Tuples.cons(i, Tuple_this).asInstanceOf[scala.*:[scala.Int, scala.Tuple$package.EmptyTuple.type]]: scala.*:[scala.Int, scala.Tuple$package.EmptyTuple]) | ||
* } | ||
* }}} | ||
*/ | ||
object ArbitraryTupleConstructionInlined { | ||
def unapply(using Quotes)(t: quotes.reflect.Term): Option[(quotes.reflect.Term, quotes.reflect.Term)] = { | ||
import quotes.reflect.{Ident => TIdent, *} | ||
t match { | ||
case | ||
Inlined( | ||
// Some(Apply( | ||
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. Question: should we also match on |
||
// TypeApply( | ||
// Select( | ||
// Inlined(_,_, | ||
// Typed(AsInstanceOf(TupleCons(a,b)), _) | ||
// ), | ||
// "*:" | ||
// ), | ||
// _ | ||
// ), | ||
// _ | ||
// )), | ||
_, | ||
List(ValDef("Tuple_this", _, Some(prevTuple))), | ||
Typed(AsInstanceOf(TupleCons(head, TIdent("Tuple_this"))), _) | ||
) => | ||
Some((head, prevTuple)) | ||
case _ => None | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Parses a few cases of arbitrary tuples. | ||
* | ||
* Scala 3 produces a few different trees for arbitrary tuples. Method `*:` is marked as inline. | ||
* Under the hood it actually invokes `Tuples.cons` function: | ||
* {{{ | ||
* inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This = | ||
* runtime.Tuples.cons(x, this).asInstanceOf[H *: This] | ||
* }}} | ||
* So, at least we have to match Tuples.cons. | ||
* However, it's not the only variation. Scala also produces a block with intermediate val `Tuple_this` definitions: | ||
* {{{ | ||
* { | ||
* val Tuple_this: scala.Tuple$package.EmptyTuple.type = scala.Tuple$package.EmptyTuple | ||
* val `Tuple_this₂`: scala.*:[java.lang.String, scala.Tuple$package.EmptyTuple] = (scala.runtime.Tuples.cons("", Tuple_this).asInstanceOf[scala.*:[java.lang.String, scala.Tuple$package.EmptyTuple.type]]: scala.*:[java.lang.String, scala.Tuple$package.EmptyTuple]) | ||
* | ||
* (scala.runtime.Tuples.cons(1, `Tuple_this₂`).asInstanceOf[scala.*:[scala.Int, scala.*:[java.lang.String, scala.Tuple$package.EmptyTuple]]]: scala.*:[scala.Int, scala.*:[java.lang.String, scala.Tuple$package.EmptyTuple]]) | ||
* } | ||
* }}} | ||
*/ | ||
class ArbitraryTupleBlockParser(val rootParse: Parser)(using Quotes, TranspileConfig) | ||
extends Parser(rootParse) | ||
with PatternMatchingValues { | ||
|
||
import quotes.reflect.{Block => TBlock, Ident => TIdent, _} | ||
|
||
def attempt = { | ||
case '{EmptyTuple} => | ||
ast.Tuple(List()) | ||
case '{$a *: EmptyTuple} => | ||
val aAst = rootParse(a) | ||
ast.Tuple(List(aAst)) | ||
case inlined@Unseal(ArbitraryTupleConstructionInlined(singleValue, prevTuple)) => | ||
//printExpr(inlined, "singleValue ArbitraryTupleConstructionInlined") | ||
val headAst = rootParse(singleValue.asExpr) | ||
val prevTupleAst = rootParse(prevTuple.asExpr) | ||
prevTupleAst match { | ||
case ast.Tuple(lst) => ast.Tuple(headAst :: lst) | ||
case _ => | ||
throw IllegalArgumentException(s"Unexpected tuple ast ${prevTupleAst}") | ||
} | ||
case block@Unseal(TBlock(parts, ArbitraryTupleConstructionInlined(head,Typed(AsInstanceOf(TupleCons(head2,TIdent("Tuple_this"))), _)) )) if (parts.length > 0) => | ||
//println(s"Large block. parts.size=${parts.size}; parts:\n- ${parts.mkString("\n- ")}") | ||
val headAst = rootParse(head.asExpr) | ||
val head2Ast = rootParse(head2.asExpr) | ||
val partsAsts = headAst :: head2Ast :: parts.reverse.flatMap{ | ||
case ValDef("Tuple_this", tpe, Some(TIdent("EmptyTuple"))) => List() | ||
case ValDef("Tuple_this", tpe, Some(Typed(AsInstanceOf(TupleCons(next,TIdent("Tuple_this"))), _))) => List(rootParse(next.asExpr)) | ||
case ValDef("Tuple_this", tpe, Some(unknown)) => | ||
throw IllegalArgumentException(s"Unexpected Tuple_this = ${unknown.show}") | ||
} | ||
Tuple(partsAsts) | ||
} | ||
} | ||
|
||
class BlockParser(val rootParse: Parser)(using Quotes, TranspileConfig) | ||
extends Parser(rootParse) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 keeping commented code? 🤔
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.
I wanted to consult which version is preferred - array based or recursion-based.