# HG changeset patch # User Christian Urban # Date 1569503531 -3600 # Node ID 96a91e4a8ac8aa50e431fdf023dd59f5a93ff05f # Parent 7c2c0f10c42492f384ef5774c48e14f46bd72ea8 updated diff -r 7c2c0f10c424 -r 96a91e4a8ac8 pics/javaduke.png Binary file pics/javaduke.png has changed diff -r 7c2c0f10c424 -r 96a91e4a8ac8 progs/bfc0.scala --- a/progs/bfc0.scala Thu Sep 26 12:59:33 2019 +0100 +++ b/progs/bfc0.scala Thu Sep 26 14:12:11 2019 +0100 @@ -46,6 +46,8 @@ fw.close() } +// running the c-compiler over the transpiled +// BF program and running the result import sys.process._ def compile_run(prog: String) = { @@ -62,8 +64,10 @@ (end - start)/(n * 1.0e9) } +// mandelbrot program +val b0 = load_bff("mandelbrot.bf") -println(s"${time_needed(1, compile_run(load_bff("mandelbrot.bf")))} secs") +println(s"${time_needed(1, compile_run(b0))} secs") diff -r 7c2c0f10c424 -r 96a91e4a8ac8 progs/bfc1.scala --- a/progs/bfc1.scala Thu Sep 26 12:59:33 2019 +0100 +++ b/progs/bfc1.scala Thu Sep 26 14:12:11 2019 +0100 @@ -9,6 +9,7 @@ def load_bff(name: String) : String = Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("") +// "splicing" a BF program counting occurrences def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match { case (Nil, acc) => acc case (c :: cs, Nil) => splice(cs, List((c, 1))) @@ -19,6 +20,7 @@ def spl(s: String) = splice(s.toList, Nil).reverse +// generating "compound" c-instructions def instr2(c: Char, n: Int) : String = c match { case '>' => "ptr += " + n.toString + ";" case '<' => "ptr -= " + n.toString + ";" @@ -59,7 +61,7 @@ def compile_run(prog: String) = { compile("tmp", prog) - "gcc -O3 -o tmp tmp.c".! + "gcc -O0 -o tmp tmp.c".! "./tmp".! () } @@ -71,8 +73,10 @@ (end - start) / (n * 1.0e9) } +// mandelbrot program +val b0 = load_bff("mandelbrot.bf") -println(s"${time_needed(1, compile_run(load_bff("mandelbrot.bf")))} secs") +println(s"${time_needed(1, compile_run(b0))} secs") diff -r 7c2c0f10c424 -r 96a91e4a8ac8 progs/bfi.scala --- a/progs/bfi.scala Thu Sep 26 12:59:33 2019 +0100 +++ b/progs/bfi.scala Thu Sep 26 14:12:11 2019 +0100 @@ -1,16 +1,14 @@ // An Interpreter for BF*** Programs //=================================== - import io.Source import scala.util._ - // loding a bf-file def load_bff(name: String) : String = Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("") - +// BF memory as a map type Mem = Map[Int, Int] // reading and writing BF memory @@ -42,7 +40,7 @@ } } - +// main interpreter loop def compute(prog: String, pc: Int, mp: Int, mem: Mem) : Mem = { if (0 <= pc && pc < prog.length) { val (new_pc, new_mp, new_mem) = prog(pc) match { @@ -67,7 +65,7 @@ def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m) - +// helper code for timing information def time_needed[T](n: Int, code: => T) = { val start = System.nanoTime() for (i <- 0 until n) code @@ -75,12 +73,14 @@ (end - start)/(n * 1.0e9) } - +// Testcases +//=========== // a Mandelbrot set generator in brainf*** written by Erik Bosman // (http://esoteric.sange.fi/brainfuck/utils/mandelbrot/) +val b0 = load_bff("mandelbrot.bf") -println(s"${time_needed(1, run(load_bff("mandelbrot.bf")))} secs") +println(s"${time_needed(1, run(b0))} secs") // a benchmark program (counts down from 'Z' to 'A') diff -r 7c2c0f10c424 -r 96a91e4a8ac8 slides/slides01.pdf Binary file slides/slides01.pdf has changed diff -r 7c2c0f10c424 -r 96a91e4a8ac8 slides/slides01.tex --- a/slides/slides01.tex Thu Sep 26 12:59:33 2019 +0100 +++ b/slides/slides01.tex Thu Sep 26 14:12:11 2019 +0100 @@ -41,7 +41,7 @@ \begin{tabular}{ll} Email: & christian.urban at kcl.ac.uk\\ Office Hours: & Thursdays 12 -- 14\\ - & N\liningnums{7.07} (North Wing, Bush House)\\ + Location: & N7.07 (North Wing, Bush House)\\ Slides \& Progs: & KEATS\\ \end{tabular} \end{center} @@ -433,7 +433,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] -\frametitle{Remember BF***?} +\frametitle{Remember BF*** from PEP?} \begin{center} \begin{tabular}{lcl} @@ -454,7 +454,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[c] - \frametitle{A Compiler for BF***} + \frametitle{A ``Compiler'' for BF***} \begin{center} \begin{tabular}{lcl} @@ -498,6 +498,29 @@ \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{frame}[c] + \frametitle{Lectures 5 - 10} + + code generation for a small imperative and a small functional languages\\[10mm] + + {\LARGE\bf Interpreters}\medskip\\ + \hspace{5mm}(directly runs a program)\\[6mm] + + {\LARGE\bf Compilers}\medskip\\ + \hspace{5mm}(generates JVM code) + + \begin{textblock}{1}(10,8.1) + \begin{tabular}{c} + \includegraphics[scale=0.4]{../pics/javaduke.png} + \end{tabular} + \end{textblock} + + \end{frame} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame}[t] \frametitle{Familiar Regular Expr.}