# HG changeset patch # User Christian Urban # Date 1694345095 -3600 # Node ID 89e05a230d2d10beac9b3b6ebd1ac254eae0e83f # Parent 10f834eb0a9ed6d2245ffc021fdfe0255fa72f82 updated diff -r 10f834eb0a9e -r 89e05a230d2d cws/cw05.pdf Binary file cws/cw05.pdf has changed diff -r 10f834eb0a9e -r 89e05a230d2d cws/cw05.tex --- a/cws/cw05.tex Sat Sep 09 14:14:31 2023 +0100 +++ b/cws/cw05.tex Sun Sep 10 12:24:55 2023 +0100 @@ -9,25 +9,27 @@ \begin{document} -%\color{pansypurple} -%\section*{RESIT / REPLACEMENT} -% -%{\bf -%The resit / replacement task is essentially CW5 (listed below) with -%the exception that the lexer and parser is already provided. The -%parser will generate an AST (see file \texttt{fun\_llvm.sc}). Your task -%is to generate an AST for the K-intermediate language and supply -%sufficient type annotations such that you can generate valid code for -%the LLVM-IR. The submission deadline is 9th August at 16:00. At the -%deadline, please send me an email containing a zip-file with your -%files. -%Feel free to reuse the files I have uploaded on KEATS (especially -%the files generating simple LLVM-IR code). Of help might also be the -%videos of Week~10.\bigskip -% -%\noindent -%Good Luck!} -%\color{black} +\color{pansypurple} +\section*{RESIT / REPLACEMENT} + +{\bf +The resit / replacement task is essentially CW5 (listed below) with +the exception that the lexer and parser is already provided. The +parser will generate an AST (see file \texttt{fun\_llvm.sc}). Your task +is to generate an AST for the K-intermediate language and supply +sufficient type annotations such that you can generate valid code for +the LLVM-IR. The submission deadline is 4th August at 16:00. At the +deadline, please send me an email containing a zip-file with your +files. +Feel free to reuse the files I have uploaded on KEATS (especially +the files generating simple LLVM-IR code). Of help might also be the +videos of Week~10.\bigskip + +\noindent +Good Luck!}\smallskip\\ +\noindent +Christian +\color{black} \section*{Coursework 5} diff -r 10f834eb0a9e -r 89e05a230d2d handouts/amm-ho.pdf Binary file handouts/amm-ho.pdf has changed diff -r 10f834eb0a9e -r 89e05a230d2d handouts/amm-ho.tex --- a/handouts/amm-ho.tex Sat Sep 09 14:14:31 2023 +0100 +++ b/handouts/amm-ho.tex Sun Sep 10 12:24:55 2023 +0100 @@ -2,35 +2,43 @@ \usepackage{../style} \usepackage{../langs} \usepackage{marvosym} +\usepackage{tcolorbox} +%% \usepackage[]{hvextern} %cheat sheet %http://worldline.github.io/scala-cheatsheet/ \begin{document} -\fnote{\copyright{} Christian Urban, King's College London, 2020, 2021} +\fnote{\copyright{} Christian Urban, King's College London, 2020, 2021, 2023} -\section*{Scala in 6CCS3CFL} +\section*{Scala 3 in 6CCS3CFL} For the coursework in this module you are free to use any programming language you like, but I will show you all my code using Scala---I -hope you have fond memories of Scala from PEP. If you need a reminder -of the Scala handouts for PEP have a look -\hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf} - +hope you have fond memories of Scala from PEP. The only difference +with PEP is that I will use the current +stable version of Scala, which at teh time of writing is Scala 3.3.1. -But as said, you do not need to use Scala for your own -code.\footnote{Haskell, Rust, Ocaml were other languages that have - been used previously in CFL. I recommend to not use Java or C for +\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] +If you intend to submit your code for the CW in Scala, you \underline{MUST} submit code that +is compatible with Scala 3!! This is to make it easier for me to test +your code and the changes between Scala 2 from last year PEP to Scala +3 in CFL are not that great. In fact, most changes are just some new +syntax. +\end{tcolorbox}\medskip + +\noindent +If you need a reminder of the Scala handouts from PEP updated to Scala 3 +have a look here +\hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}. But as said, you do not need to use Scala for the CWs.\footnote{Haskell, Rust, Ocaml were other languages that have + been used previously in CFL. I do not recommend to use Java or C or C++ for writing a compiler, but if you insist, feel free. It has been done -before.} I will use the -current stable version of Scala, which is 2.13.6. For various reasons, -I am NOT GOING TO USE THE LATEST VERSION OF SCALA 3.0! Please be -aware of this when you run my code. + before.} \bigskip \noindent -The main difference to the Scala I showed you in PEP is that in CFL -I will use the Ammonite REPL +The other difference between the Scala I showed you in PEP is that in CFL +I will use the Ammonite REPL (with underlying Scala Version 3): \begin{quote} \url{https://ammonite.io/#Ammonite-REPL} @@ -42,12 +50,15 @@ \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] $ amm -Loading... -Welcome to the Ammonite Repl 2.4.0 (Scala 2.13.6 Java 9) +Loading... +Welcome to the Ammonite Repl 2.5.9 (Scala 3.2.2 Java 17.0.7) scala> 1 + 2 res0: Int = 3 \end{lstlisting} %% $ +%%\runExtCmd[redirect]{ls -la}{voss} + + \noindent Ammonite uses the same Scala compiler, just adds some useful features on top of it. It is quite main-stream in the Scala community and it should @@ -56,30 +67,29 @@ is by using \texttt{curl}: \begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small] -$ curl -L https://github.com/com-lihaoyi/Ammonite/ - releases/download/2.4.0/2.13-2.4.0 --output amm -\end{lstlisting} +$ curl -L https://github.com/com-lihaoyi/Ammonite/releases/\ + download/2.5.9/3.2-2.5.9 --output amm +\end{lstlisting} %% $ - +\noindent The big advantage of Ammonite is that it comes with some additional libraries already built-in and also allows one to easily break up code into smaller modules. For example reading and writing files in Ammonite can be achieved with \begin{lstlisting}[numbers=none,language=Scala] -scala> import os._ - +scala> import os._ +scala> write.over(pwd / "file.name", "foo bar") scala> read(pwd / "file.name") -res1: String = """...""" - -scala> write.over(pwd / "file.name", "foo bar") +res1: String = "foo bar" \end{lstlisting} \noindent -The latter writes the string \code{"foo bar"} into the file +The second line writes the string \code{"foo bar"} into the file \code{"file.name"}, which is located in the current working -directory. For loading and accessing code from another Scala file, you -can import it as follows: +directory (\code{pwd}). For loading and accessing code from +another Scala file, you can import the code into Ammonite +as follows: \begin{lstlisting}[numbers=none,language=Scala] import $file.name-of-the-file @@ -93,9 +103,8 @@ for the compiler we implement in CFL, because it allows us to easily break up the code into the lexer, parser and code generator. -Another feature which exists in Ammonite, but not yet in the -current version of Scala (it will be in the next version called dotty) -is that you can mark functions as \texttt{@main}. For example +Another handy feature of Ammonite is that you can mark functions as +\texttt{@main}. For example \begin{lstlisting}[numbers=none,language=Scala] @main @@ -126,7 +135,7 @@ \end{lstlisting} %% $ \noindent -What is also good in Ammonite that you can specify more than one +What is also good in Ammonite is that you can specify more than one function to be ``main'' and then specify on the command line which function you want to run as entry-point. @@ -139,7 +148,7 @@ \end{lstlisting} %% $ \noindent Of course this requires that you use \texttt{println} for -inspecting any data, as otherwise nothing will be displayed at the +inspecting any data as otherwise nothing will be displayed at the commandline. \bigskip diff -r 10f834eb0a9e -r 89e05a230d2d handouts/ho01.pdf Binary file handouts/ho01.pdf has changed diff -r 10f834eb0a9e -r 89e05a230d2d handouts/ho09.pdf Binary file handouts/ho09.pdf has changed diff -r 10f834eb0a9e -r 89e05a230d2d handouts/ho09.tex --- a/handouts/ho09.tex Sat Sep 09 14:14:31 2023 +0100 +++ b/handouts/ho09.tex Sun Sep 10 12:24:55 2023 +0100 @@ -785,6 +785,44 @@ \end{figure} +\section*{Alternatives to CPS} + +While I appreciate that this handout is already pretty long, this +section is for students who think the CPS-translation is too much of +voodoo programming---there is a perhaps simpler alternative. This +alternative is along the lines: if you cannot bridge the gap in +a single step, do it in two simpler steps. Let's look at the +simple expression $1 + (2 + 3)$. The CPS-translation correctly +generates the expression + +\begin{lstlisting}[language=LLVMIR,numbers=left,escapeinside={(*@}{@*)}] +let tmp0 = add 2 3 in +let tmp1 = add 1 tmp0 in + return tmp1 +\end{lstlisting} + +\noindent +where $(2 + 3)$ is pulled out and calculated first. The problem is that it +requires a bit of magic. But with the ability to give a separate variable +to each indivifual computation, we could do the following: The expression +$1 + (2 + 3)$ is a tree like this + +\begin{center} +\begin{tikzpicture}[line width=1mm] + \node {root} [grow'=up] + child {node {1}} + child {node[circle,draw] {+} + child {node {2}} + child {node {3}} + }; +\end{tikzpicture} +\end{center} + +\noindent +and we could perform a completely standard recursive traversal of the +tree: each inner node gets a new variable and assignment. + + \noindent \end{document} diff -r 10f834eb0a9e -r 89e05a230d2d style.sty --- a/style.sty Sat Sep 09 14:14:31 2023 +0100 +++ b/style.sty Sun Sep 10 12:24:55 2023 +0100 @@ -98,3 +98,16 @@ \def\cwFIVE{12 January} %%\def\cwISABELLE{11 December} + + +%%%%%%%%%%%%%% +%% lualatex testing commandline arguments +\newcommand{\luaif}[1]{\ifnum1=\directlua{if #1 then tex.sprint(1) else tex.sprint(0) end} } + +\luaif{arg[2] == "sol"} +\newcommand{\solution}[1]{% + \begin{quote}\sf% + #1% + \end{quote}} \else +\newcommand{\solution}[1]{} +\fi