Consume Scala syntax-trees from external tool - scala

I would like to develop a tool that would consume scala syntax-trees (as the title suggests). More specifically it would be great if I could consume the trees after each compilation phase.
My research led me to Dotty's TASTY interchange format which seemed to be what I was looking for. Perhaps it is.
However, I was not able to find adequate documentation on-line to figure out how to extract it and consume it.
I also looked at dotc compiler flags and couldn't figure out an obvious approach.
I noticed the option : "-print-tasty" but I couldn't verify the expected output or perhaps I am missing something ?
Of course I can always print the AST after each phase using the scala printer (i.e., -Yshow-trees etc.). Is this my only option ? If it is, then fine.
Ideally, it would be great if I could consume the ASTs in a more "machine-friendly" format if you will. TASTY seems to be what I want in theory, i.e., a serialization of the AST, but I am not sure how to extract this after each phase.
I do apologize if my question is too trivial or has already been addressed. Any feedback would be highly appreciated ! Thanks !
P.S.: What if the ASTs were encoded in a JSON format ? Would a scala tool like that make sense, (i.e., a tool that converts Scala ASTs to JSON and back) ?

Related

How to package/publish add-on for two incompatible versions of underlying scala library?

I'm developing a Scala project at https://github.com/jonaskoelker/equate/ which gives you equality assertions for ScalaTest which print a diff of observed vs. expected if they're unequal. This is particularly useful for long strings and large case classes.
I'd like to publish one version of equate for ScalaTest v3.0.8 and one for ScalaTest v3.1.1.
What are best practices for doing so? My web searches came up empty. My own first idea is to publish two things with different names, where the name says which version of ScalaTest each thing is compatible with. Is there a better way?
My way seems rather low-tech. It seems to me that some grunt work could be automated away if the ScalaTest version information was encoded some other way. This seems obvious enough that someone else has probably thought about it and done something about it. I'd like to know what, such that I can release my code the smart way rather than the dumb way.

Reading & writing text in Scala, getting the encoding right?

I'm reading and writings some text files in Scala. As a complete beginner in the language, I wanted to make sure to find the right way to do it, e.g. get the encoding right.
So most of the stuff I found (also on SO ) recommends I use io.Source.fromFile.However, after trying it out like so, reading a UTF-8 file:
val user_list = Source.fromFile("usernames.txt").getLines.toList
val user_list = Source.fromFile("usernames.txt", enc="UTF8").getLines.toList
I looked at the docs but was left with some questions.
Get the encoding right:
the docs show that I can set an encoding in Source.fromFile as I tried above. Looking at the man on Codec and the types listed there, I was wondering if those are all my codec options - is there e.g. no Utf-16, Big-Endian vs Little-Endian, etc.?
I am slightly obsessed with this since it used to trip me up in Python a lot. Is this less of concern with Scala for some reason?
Get the reading in right:
All the examples I looked at used the getLines method and postprocessed it with MkString or List, etc. Is there any advantage to that over just reading in the entire file (my files are small) in one go?
Get the writing out right:
Every source I could find tells me that Scala has no file writing function and to use the Java FileWriter. I was surprised by this - is this still accurate?
Looking at it I feel the question might be a little broad for SO, so I'd be happy to take it back if it does not meet the requirements. At this point, I'm not struggling with specific examples but rather trying to set things up in a way I don't get in trouble later.
Thanks!
Scala only has a basic IO api in the standard library. For the most part you just use the java apis. The fact that a decent api from java exists is probably why the Scala team is not prioritizing having a robust and fully featured IO api.
There are also third party scala libraries you could use as well however. Better Files I've never used but heard good things about as a Scala file api. As well as fs2 which provides functional, streaming IO. I'm sure there are others out there as well.
For encoding, there are many possible encoding available. It's just that only a couple of the most common ones are available as static fields, the rest you typically access through Codec("Encoding Name"). Most apis will also let you just enter a String directly instead of needing to get a Codec instance first. The codec is really just a wrapper over java.nio.charset.Charset. You can run java.nio.charset.Charset.availableCharsets() to see all of the encodings available on your system.
As far as reading, if the files are small you can load them fully into memory if you prefer that. The only reason not to do so is if you want to avoid the extra memory use of loading the entire file at once if reading through line by line is enough. You may want to use Vector instead of List for efficiency reasons (Vector is better in many cases and should probably be preferred as a default collection, but tradition and old habits die hard and most people/guides seem to default to List, but this is a whole other topic)

Eclipse, lambdas and Java 8 templates

Is there a reason the Eclipse content assits doesn't work in/around lambdas? In a normal case Eclipse usually does this after writing a dot:
Which works just fine like anywhere else. However just a couple of lines later I get nothing:
Both objects are of the same type. Unfortunately I use these all the time as they make everything much faster and I don't understand why it works in one lambda and not the other.
Attempting an explanation (you asked for a reason): parsing lambda expressions in Java is a technical challenge, as the Java grammar was not made for parser generators. Code completion, OTOH, inevitably depends on parsing incomplete code, i.e., heuristics must be used to continue parsing after a syntax error. These two just don't nicely cooperate. As a result in some situations your incomplete code will look like garbage to the compiler and hence content assist is not able to figure out, what would be meaningful proposals.
The applied heuristics are constantly being improved. I recommend trying your examples on a recent milestone build. If the problem still exists, you might help the team by filing a bug providing a code example, and describing your expectations and what actual behavior you observe.

Control Data Flow graphs or intermediate representation

we are working on a project to come up with an intermediate representation for the code in terms of something called an assignment decision diagram. So it would be very helpful if someone can tell us how you guys are compiling the code and how to access the graphs generated during compilation i.e after parsing the code for grammar.
Even help regarding accessing the code after parsing of the compiler is fine. Any help regarding how to go about doing it is also appreciated.
Currently, there is not a well defined intermediate representation of Chisel as it goes between the user source code and the specified C++ or Verilog backends.
However, I believe this is a current project amongst the Chisel devs to break apart the backend and allow access to the IR (and allow for user-defined compiler passes).
In the meantime, check out Backend.scala (particularly the elaborate() method). That's where a lot of the magic originates. I believe it is possible to jump into the Scala command line in the middle of elaboration, which will give you access to the hardware tree representation, but I'm not sure how meaningful or useful that will be for you.

Prolog as a DSL to generate perl code?

Does anyone know of any examples of code written in prolog to implement a DSL to generate perl code?
DCGs might be an excellent choice!
I have used a similar approach for generation of UML class diagrams (really, graphviz code for such diagrams) from simple English sentences (shameless-plug: paper here). It should be possible to do something similar with generation of Perl code instead.
In the paper above, we use a constraint store (CHR) as intermediate representation which allows some extra reasoning power. Alternatively you can build a representation as an output feature/argument of the DCG.
Note that DCGs can be useful both for the parsing of your sentences and the generation of your Perl code.
Well, not exactly what you are asking for, but maybe you can use AI::Prolog for what you are looking for. That way you may be able to use Perl and generate the Perl code you want.
I'm not sure why you would want to do that?
Perl is a very expressive language, I'm not sure why you'd want to try to generate Perl code from Prolog; in order to make it useful, you'd be getting closer and closer to Perl in your "DSL", by which point you'd be better off just writing some Perl, surely?
I think you need to expand this question a bit to cover what you're trying to achieve in a little more detail.
SWI-Prolog library(http/html_write) library builds on DCG a DSL for page layout.
It shows a well tought model for integrating Prolog and HTML, but doesn't attempt to cover the entire problem. The 'residual logic' on the client side remains underspecified, but this is reasonable, being oriented on practical issues 'reporting' from RDF.
Thus the 'small detail' client interaction logic is handled in a 'black box' fashion, and such demanded to YUI components in the published application (the award winner Cliopatria).
The library it's extensible, but being very detailed, I guess for your task you should eventually reuse just the ideas behind.