updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Sat, 29 Aug 2020 16:05:59 +0100
changeset 342 aa47abb9e723
parent 341 e9d5287c3107
child 343 c8fcc0e0a57f
updated
misc/parser.sh
misc/smeta.sc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/parser.sh	Sat Aug 29 16:05:59 2020 +0100
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+# to make the script fail safely
+set -euo pipefail
+
+
+out=${1:-output}
+
+# compilation tests
+
+function scala_compile {
+  (ulimit -t 60; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> ptmp 1> ptmp)
+}
+
+# purity test
+
+function scala_vars {
+   (egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|mutable|new Array' ptmp 2> /dev/null 1> /dev/null)
+}
+
+
+### compilation test
+
+echo "collatz.scala runs?" | tee -a $out
+
+if (scala_compile collatz.scala)
+then
+  echo "  --> success" | tee -a $out
+  tsts0=$(( 0 ))
+else
+  echo "  --> scala collatz.scala did not run successfully" | tee -a $out
+  tsts0=$(( 1 )) 
+fi
+
+# var, .par return, ListBuffer test
+#
+
+if  [ $tsts0 -eq 0 ]
+then
+  echo "collatz.scala does not contain var, return etc?" | tee -a $out
+
+  if (scala_vars tmp)
+  then
+    echo "  --> test failed" | tee -a $out
+    tsts=$(( 1 ))
+  else
+    echo "  --> success" | tee -a $out
+    tsts=$(( 0 )) 
+  fi
+fi  
+
+###rm tmp
+
+
--- a/misc/smeta.sc	Fri Aug 28 16:54:49 2020 +0100
+++ b/misc/smeta.sc	Sat Aug 29 16:05:59 2020 +0100
@@ -1,6 +1,30 @@
-import $ivy.`org.scalameta::scalameta:4.3.21`
+#!/usr/bin/env amm
+
+import $ivy.`org.scalameta::scalameta:4.3.20`
 
 import scala.meta._
+import ammonite.ops._
 
-println("a + b".parse[Stat].get.structure)
-   
\ No newline at end of file
+@main
+def main(s: String) = {
+  val file = read(pwd / RelPath(s))
+  val tree = file.parse[Source].get
+  val vrs = tree.collect {
+      case Defn.Var(_, x, _, _) => x.toString
+      case Term.Return(t) => t.toString 
+      }    
+  println(vrs.mkString("\n"))
+}
+
+
+/*
+q"val x = 2".traverse {
+  case node =>
+    println(s"${node.productPrefix}: $node")
+}
+*/
+
+//https://scalameta.org/docs/trees/guide.html#custom-traversals
+
+
+//https://geirsson.com/post/2016/02/scalameta/
\ No newline at end of file