# HG changeset patch # User Christian Urban # Date 1732279327 0 # Node ID 0cb4bf2469d15e83a67a4907ab5d5697945174b9 # Parent f25d338d16c90fa21be1fe080ec6a1d84cb52798 updated diff -r f25d338d16c9 -r 0cb4bf2469d1 cws/cw04.pdf Binary file cws/cw04.pdf has changed diff -r f25d338d16c9 -r 0cb4bf2469d1 cws/cw04.tex --- a/cws/cw04.tex Sat Nov 09 06:31:45 2024 +0000 +++ b/cws/cw04.tex Fri Nov 22 12:42:07 2024 +0000 @@ -17,8 +17,7 @@ at 16:00. You are asked to implement a compiler for the WHILE language that targets the assembler language provided by Jasmin or Krakatau (both have a very similar -syntax). Please submit your answers to the questions -below as PDF. You can do the implementation in any programming +syntax). You can do the implementation in any programming language you like, but you need to submit the source code with which you answered the questions, otherwise a mark of 0\% will be awarded. You should use the lexer and parser from the @@ -100,7 +99,7 @@ \noindent If possible use Jasmin for the coursework. The Krakatau assembler -below as a slightly different syntax. +below has a slightly different syntax. \subsection*{Krakatau Assembler (Version 1 \& 2)} @@ -165,12 +164,7 @@ Code instructions for the Jasmin assembler (or Krakatau assembler). For this you should use the ASTs defined in CW3 (including logical operators). As part of the solution you need to submit the assembler -instructions for the Fibonacci and Factorial programs. Both should be -so modified that a user can input on the console which Fibonacci -number and which Factorial should be calculated. The Fibonacci program -is given in Figure~\ref{fibs}. You can write your own program for -calculating factorials. Submit your assembler code as a file that can -be run, not as PDF-text. +instructions for the Fibonacci and Factorial programs. \begin{figure}[t] \lstinputlisting[language=while]{../cwtests/cw04/fib.while} @@ -228,10 +222,10 @@ \end{minipage} \end{center} -\subsection*{Question 3} +\subsection*{Question 3 (OPTIONAL)} -\noindent In this question you are supposed to give the -assembler instructions for the program +\noindent In this question you are asked to think about the following +program: \begin{center} \begin{minipage}{12cm} @@ -247,9 +241,8 @@ \noindent Note that in this program the variable \pcode{i} is used -twice. You need to make a decision how it should be compiled? -Explain your decision and indicate what this program would -print out. Give your answer in the file \texttt{q3-answer.txt}. +twice. Therefore you need to make a decision about how it should be compiled? +What should the program print? \subsection*{Question 4} diff -r f25d338d16c9 -r 0cb4bf2469d1 progs/parser-combinators/comb1.sc --- a/progs/parser-combinators/comb1.sc Sat Nov 09 06:31:45 2024 +0000 +++ b/progs/parser-combinators/comb1.sc Fri Nov 22 12:42:07 2024 +0000 @@ -179,7 +179,7 @@ lazy val F: Parser[String, Int] = { (p"(" ~ E ~ p")").map{ case ((_, y), _) => y } || NumParserInt } -println(E.parse_all("2*2*2")) +println(E.parse_all("2 * 2 * 2")) println(E.parse_all("1+3+4")) println(E.parse("1+3+4")) println(E.parse_all("4*2+3")) diff -r f25d338d16c9 -r 0cb4bf2469d1 progs/while-arrays/compile_arrays.sc --- a/progs/while-arrays/compile_arrays.sc Sat Nov 09 06:31:45 2024 +0000 +++ b/progs/while-arrays/compile_arrays.sc Fri Nov 22 12:42:07 2024 +0000 @@ -7,7 +7,7 @@ // // amm compile_arrays.sc -//> using toolkit latest +//> using toolkit 0.6.0 // the abstract syntax trees for WHILE @@ -90,9 +90,10 @@ // convenient string interpolations // for generating instructions and labels + extension (sc: StringContext) { - def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" - def l(args: Any*): String = sc.s(args:_*) ++ ":\n" + def i(args: Any*): String = " " ++ sc.s(args*) ++ "\n" + def l(args: Any*): String = sc.s(args*) ++ ":\n" } @@ -128,7 +129,7 @@ def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match { case Skip => ("", env) case Assign(x, a) => { - val index = env.getOrElse(x, env.keys.size) + val index = env.getOrElse(x, env.keys.size) (compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) } case If(b, bl1, bl2) => { @@ -218,7 +219,7 @@ // automating the above -import os._ +import os.* def compile_to_file(bl: Block, class_name: String) : Unit = write.over(pwd / s"$class_name.j", compile(bl, class_name)) diff -r f25d338d16c9 -r 0cb4bf2469d1 progs/while-arrays/compile_bfc.sc --- a/progs/while-arrays/compile_bfc.sc Sat Nov 09 06:31:45 2024 +0000 +++ b/progs/while-arrays/compile_bfc.sc Fri Nov 22 12:42:07 2024 +0000 @@ -16,7 +16,7 @@ // // Call with scala-cli: // -// scala-cli --dep com.lihaoyi::fastparse:3.0.2 compile_bfc.sc +// scala-cli --dep com.lihaoyi::fastparse:3.1.1 compile_bfc.sc // // Scala-cli is another REPL for scala. Unfortunately // fastparse used in this file is not yet supported @@ -25,8 +25,8 @@ // compile_arrays.sc (no peephole optimisations) // compile_arrays2.sc (peephole optimisations applied) -//> using file compile_arrays2.sc -import compile_arrays2._ +//> using file compile_arrays.sc +import compile_arrays.* def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() @@ -68,7 +68,7 @@ } // post 2.5.0 ammonite -import os._ +import os.* def compile_to_file(bl: Block, class_name: String) : Unit = @@ -92,10 +92,10 @@ // Grammar Rules for WHILE with arrays //===================================== -//> using dep com.lihaoyi::fastparse:3.0.2 +//> using dep com.lihaoyi::fastparse:3.1.1 -import fastparse._ -import MultiLineWhitespace._ +import fastparse.* +import MultiLineWhitespace.* def lowercase [$ : P] = P( CharIn("a-z") ) def uppercase[$ : P] = P( CharIn("A-Z") ) @@ -249,7 +249,7 @@ val bf_string = bf_str(prog) println(s"BF parsing (program length ${bf_string.length} characters)") val (time, bf_prog) = - time_needed(1, fastparse.parse(bf_string, Stmts(_)).get.value) + time_needed(1, fastparse.parse(bf_string, implicit p => Stmts).get.value) println(s"BF generated WHILE program (needed $time secs for parsing)") compile_and_run(bf_prog, name) } diff -r f25d338d16c9 -r 0cb4bf2469d1 progs/while/compile.sc --- a/progs/while/compile.sc Sat Nov 09 06:31:45 2024 +0000 +++ b/progs/while/compile.sc Fri Nov 22 12:42:07 2024 +0000 @@ -81,7 +81,7 @@ extension (sc: StringContext) { def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" - def l(args: Any*): String = sc.s(args:_*) ++ ":" + def l(args: Any*): String = sc.s(args:_*) ++ ":\n" } // this allows us to write things like diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides05.pdf Binary file slides/slides05.pdf has changed diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides06.pdf Binary file slides/slides06.pdf has changed diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides07.pdf Binary file slides/slides07.pdf has changed diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides07.tex --- a/slides/slides07.tex Sat Nov 09 06:31:45 2024 +0000 +++ b/slides/slides07.tex Fri Nov 22 12:42:07 2024 +0000 @@ -26,7 +26,7 @@ \begin{center} \begin{tabular}{ll} Email: & christian.urban at kcl.ac.uk\\ - Office Hour: & Thurdays 15 -- 16\\ + Office Hour: & Fridays 12 -- 14\\ Location: & N7.07 (North Wing, Bush House)\\ Slides \& Progs: & KEATS (also homework is there)\\ Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides08.pdf Binary file slides/slides08.pdf has changed diff -r f25d338d16c9 -r 0cb4bf2469d1 slides/slides08.tex --- a/slides/slides08.tex Sat Nov 09 06:31:45 2024 +0000 +++ b/slides/slides08.tex Fri Nov 22 12:42:07 2024 +0000 @@ -41,7 +41,7 @@ \begin{center} \begin{tabular}{ll} Email: & christian.urban at kcl.ac.uk\\ - Office Hour: & Thurdays 15 -- 16\\ + Office Hour: & Fridays 12 -- 14\\ Location: & N7.07 (North Wing, Bush House)\\ Slides \& Progs: & KEATS\\ Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ diff -r f25d338d16c9 -r 0cb4bf2469d1 solutions/cw3/parser.sc --- a/solutions/cw3/parser.sc Sat Nov 09 06:31:45 2024 +0000 +++ b/solutions/cw3/parser.sc Fri Nov 22 12:42:07 2024 +0000 @@ -135,6 +135,7 @@ } + // WHILE Language Parsing lazy val AExp: Parser[List[Token], AExp] = (Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } ||