Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Last active August 2, 2021 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otobrglez/ddd56a1becd215796e3ccae81297f60f to your computer and use it in GitHub Desktop.
Save otobrglez/ddd56a1becd215796e3ccae81297f60f to your computer and use it in GitHub Desktop.
Exploring and visualising Scala 2.* compilation phases
object HelloWorld extends App {
val add2: Int => Int = _ + 2
println(s"Hello world! This is Scala. ${add2(40)}")
}
#!/usr/bin/env bash
set -e
# Author: Oto Brglez, @otobrglez
# Usage: ./phases.sh What.scala
# Generates final.gif
# Cleanup from previous runs
rm -rf *.scala.html *.scala.pdf *.gif
vimhtml() { [[ -f "$1" ]] || return 1; vim +'syn on | run! syntax/2html.vim | wq | q' "$1";}
SCALAC_FLAGS="-Dfile.encoding=UTF-8"
# Get all phases that scalac compiler has
PHASES=($(scalac -Xshow-phases | tail -n +3 | tr -d ' ' | awk -F '[0-9]+' '{print $1}' | tr '\n' ' '))
# Loop over phases and do magic
for i in "${!PHASES[@]}"; do
fi=$(printf "%02d\n" $i)
phase=${PHASES[$i]}
output=${fi}-${phase}-$1
echo "Phase: ${phase} - ${fi}"
scalac -Xprint:${phase} ${SCALAC_FLAGS} $1 2>&1 | tee ${output}
# Scala to HTML
vimhtml ${output}
# HTML to PDF
wkhtmltopdf --disable-smart-shrinking --zoom 1.2 --page-size A4 ${output}.html ${output}.pdf
done
# Convert PDFs to GIF
echo "Converting PDFs to GIF"
convert -verbose -delay 150 -density 150 \
-page a4 "*.scala.pdf[0]" final.gif
// A trivial example; asked by @Krever01 on https://twitter.com/Krever01/status/1419568385134845953
object What extends App {
val y = 2
var x = 40
x += y
println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment