changed os-lib as a replacement for ammonite-ops
authorChristian Urban <christian.urban@kcl.ac.uk>
Mon, 24 Jan 2022 00:00:33 +0000
changeset 869 81ee93b87258
parent 868 8fb3b6d3be70
child 870 739039774cee
changed os-lib as a replacement for ammonite-ops
handouts/amm-ho.pdf
handouts/amm-ho.tex
progs/fun/fun.sc
progs/fun/fun_llvm.sc
progs/fun/fun_parser.sc
progs/fun/fun_tokens.sc
progs/fun/funt.sc
progs/while-arrays/compile_arrays.sc
progs/while-arrays/compile_arrays2.sc
progs/while-arrays/compile_bfc.sc
solution/cw3/lexer.sc
solution/cw3/parser.sc
solution/cw3/parser2.sc
solution/cw4/lexer.sc
solution/cw5/fun_tokens.sc
solution/cw5/sqr.fun
Binary file handouts/amm-ho.pdf has changed
--- a/handouts/amm-ho.tex	Thu Jan 20 08:58:38 2022 +0000
+++ b/handouts/amm-ho.tex	Mon Jan 24 00:00:33 2022 +0000
@@ -67,7 +67,8 @@
 Ammonite can be achieved with
 
 \begin{lstlisting}[numbers=none,language=Scala]
-scala> import ammonite.ops._
+scala> import $ivy.`com.lihaoyi::os-lib:0.8.0`
+scala> import os._  
   
 scala> read(pwd / "file.name")   
 res1: String = """..."""
--- a/progs/fun/fun.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/fun/fun.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -166,7 +166,12 @@
 }
 
 
-import ammonite.ops._
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
+import os._
 
 def compile_to_file(prog: List[Decl], class_name: String) : Unit = 
   write.over(pwd / s"$class_name.j", compile(prog, class_name))  
--- a/progs/fun/fun_llvm.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/fun/fun_llvm.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -264,7 +264,11 @@
   prelude ++ (prog.map(compile_decl).mkString)
 
 
-import ammonite.ops._
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
 
 
 @main
--- a/progs/fun/fun_parser.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/fun/fun_parser.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -158,7 +158,11 @@
 
 // Reading tokens and Writing parse trees
 
-import ammonite.ops._
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
 
 def parse_tks(tks: List[Token]) : List[Decl] = 
   Prog.parse_single(tks)
--- a/progs/fun/fun_tokens.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/fun/fun_tokens.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -249,7 +249,11 @@
   else { println (s"Tokenise Error") ; sys.exit(-1) }     
 }
 
-import ammonite.ops._
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
 
 //@doc("Tokenising a file.")
 @main
--- a/progs/fun/funt.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/fun/funt.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -161,7 +161,11 @@
   (library + instructions).replaceAllLiterally("XXX", class_name)
 }
 
-import ammonite.ops._
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
 
 def compile_to_file(prog: List[Decl], class_name: String) : Unit = 
   write.over(pwd / s"$class_name.j", compile(prog, class_name))  
--- a/progs/while-arrays/compile_arrays.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/while-arrays/compile_arrays.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -214,7 +214,15 @@
 //    java arr/arr
 
 // automating the above
-import ammonite.ops._
+
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
+import os._
+
+
 
 def compile_to_file(bl: Block, class_name: String) : Unit = 
   write.over(pwd / s"$class_name.j", compile(bl, class_name))  
--- a/progs/while-arrays/compile_arrays2.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/while-arrays/compile_arrays2.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -231,7 +231,14 @@
 //    java arr/arr
 
 // automating the above
-import ammonite.ops._
+
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
+import os._
+
 
 def compile_to_file(bl: Block, class_name: String) : Unit = 
   write.over(pwd / s"$class_name.j", compile(bl, class_name))  
--- a/progs/while-arrays/compile_bfc.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/progs/while-arrays/compile_bfc.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -63,7 +63,14 @@
 }
 
 // automating the above
-import ammonite.ops._
+
+// pre-2.5.0 ammonite 
+// import ammonite.ops._
+
+// post 2.5.0 ammonite
+import $ivy.`com.lihaoyi::os-lib:0.8.0` 
+import os._
+
 
 def compile_to_file(bl: Block, class_name: String) : Unit = 
   write.over(pwd / s"$class_name.j", compile(bl, class_name))  
--- a/solution/cw3/lexer.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw3/lexer.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -202,16 +202,17 @@
 val KEYWORD : Rexp = "while" | "if" | "then" | "else" | "do" | "for" | "to" | "true" | "false" | "read" | "write" | "skip" 
 val OP : Rexp = "+" | "-" | "*" | "%" | "/" | "==" | "!=" | ">" | "<" | ">=" | "<=" | ":=" | "&&" | "||"
 val LET: Rexp = RANGE(('A' to 'Z').toSet ++ ('a' to 'z'))
-val SYM : Rexp = LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':'))
+val SYM : Rexp = (LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':')))
 val PARENS : Rexp = "(" | "{" | ")" | "}"
 val SEMI : Rexp = ";"
-val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t"
+val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
 val DIGIT : Rexp = RANGE(('0' to '9').toSet)
 val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
 val STRING : Rexp = "\"" ~ (SYM | " " | "\\n" | DIGIT).% ~ "\""
 val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
 val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
-val COMMENT : Rexp = "//" ~ (SYM | " " | DIGIT).% ~ "\n" 
+val EOL : Rexp = "\n" | "\r\n"
+val COMMENT : Rexp = "//" ~ (SYM | PARENS | " " | DIGIT).% ~ EOL 
 
 val WHILE_REGS = (("k" $ KEYWORD) | 
                   ("o" $ OP) | 
@@ -248,3 +249,18 @@
   lexing_simp(WHILE_REGS, s).collect(token)
 
 
+val fact = """
+write "Input n please";
+read n;
+write "The factors of n are";
+f := 2;
+while n != 1 do {
+    while (n / f) * f == n do {
+        write f;
+        n := n / f
+    };
+    f := f + 1
+}
+"""
+println(tokenise(fact))
+
--- a/solution/cw3/parser.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw3/parser.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -132,8 +132,8 @@
    (AExp ~ T_OP("!=") ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
    (AExp ~ T_OP("<") ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
    (AExp ~ T_OP(">") ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } ||
-   (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("&&")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => And(y, v): BExp } ||
-   (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("||")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => Or(y, v): BExp } ||
+   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
+   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp) ==> { case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
    (T_KEYWORD("true") ==> (_ => True: BExp )) || 
    (T_KEYWORD("false") ==> (_ => False: BExp )) ||
    (T_PAREN("(") ~ BExp ~ T_PAREN(")")) ==> { case _ ~ x ~ _ => x }
@@ -145,7 +145,9 @@
     (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
     (T_KEYWORD("read") ~ IdParser()) ==> { case _ ~ id => Read(id): Stmt} ||
     (T_KEYWORD("write") ~ IdParser()) ==> { case _ ~ id => WriteId(id): Stmt} ||
-    (T_KEYWORD("write") ~ StringParser()) ==> { case _ ~ s => WriteString(s): Stmt}
+    (T_KEYWORD("write") ~ StringParser()) ==> { case _ ~ s => WriteString(s): Stmt} || 
+    (T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")) ==> { case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
+    (T_KEYWORD("write") ~  T_PAREN("(") ~ StringParser() ~ T_PAREN(")")) ==> { case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
 
 lazy val Stmts: Parser[List[Token], Block] =
     (Stmt ~ T_SEMI ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
@@ -224,6 +226,10 @@
 
 def eval(bl: Block) : Env = eval_bl(bl, Map())
 
+println("Primes eval")
+println(tokenise(os.read(os.pwd / "primes.while")))
+println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head))
+
 println("Factors eval")
 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "factors.while"))).head))
 
--- a/solution/cw3/parser2.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw3/parser2.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -240,6 +240,10 @@
 println("Factors eval")
 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "factors.while"))).head))
 
+println("Primes eval")
+println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head))
+
+
 println("Collatz2 eval")
 println(eval(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head))
 
--- a/solution/cw4/lexer.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw4/lexer.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -205,13 +205,14 @@
 val SYM : Rexp = LET | RANGE(Set('.', '_', '>', '<', '=', ';', ',', ':'))
 val PARENS : Rexp = "(" | "{" | ")" | "}"
 val SEMI : Rexp = ";"
-val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t"
+val WHITESPACE : Rexp = PLUS(" ") | "\n" | "\t" | "\r"
 val DIGIT : Rexp = RANGE(('0' to '9').toSet)
 val DIGIT1 : Rexp = RANGE(('1' to '9').toSet)
 val STRING : Rexp = "\"" ~ (SYM | " " | "\\n" | DIGIT).% ~ "\""
 val ID : Rexp = LET ~ (LET | "_" | DIGIT).%
 val NUM : Rexp = "0" | (DIGIT1 ~ DIGIT.%)
-val COMMENT : Rexp = "//" ~ (SYM | " " | DIGIT).% ~ "\n" 
+val EOL : Rexp = "\n" | "\r\n"
+val COMMENT : Rexp = "//" ~ (SYM | PARENS | " " | DIGIT).% ~ EOL
 
 val WHILE_REGS = (("k" $ KEYWORD) | 
                   ("o" $ OP) | 
--- a/solution/cw5/fun_tokens.sc	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw5/fun_tokens.sc	Mon Jan 24 00:00:33 2022 +0000
@@ -195,7 +195,7 @@
           "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" |"E" | "F" | "G" |
           "H" | "I" | "J" | "K" |"L" | "M" | "N" |
           "O" | "P" | "Q" | "R" |"S" | "T" | "U" |
-          "V" | "W" | "X" | "Y" | "Z" | "_"
+          "V" | "W" | "X" | "Y" | "Z" | "_" | ":"
 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 val ID = SYM ~ (SYM | DIGIT).% 
 val NUM = PLUS(DIGIT)
--- a/solution/cw5/sqr.fun	Thu Jan 20 08:58:38 2022 +0000
+++ b/solution/cw5/sqr.fun	Mon Jan 24 00:00:33 2022 +0000
@@ -8,5 +8,8 @@
   else skip()
 };
 
-all(0)
- 
\ No newline at end of file
+{
+  print_string("Squares");
+  new_line();
+  all(0)
+} 
\ No newline at end of file