Commits

Brian McKenna committed 92ba902

Initial commit

Comments (0)

Files changed (2)

+name := "Scalispa"
+
+mainClass := Some("scalispa.Main")

src/main/scala/Main.scala

+package scalispa
+
+import util.parsing.combinator.RegexParsers
+
+import java.io.{ File, FileOutputStream, PrintStream }
+
+import me.qmx.jitescript.{ JiteClass, CodeBlock }
+import me.qmx.jitescript.util.CodegenUtils.{ p, ci, sig }
+
+import org.objectweb.asm.Opcodes._
+
+trait Node
+case class SExp(l: List[Node]) extends Node
+case class SInt(i: Int) extends Node
+case class SIdent(s: String) extends Node
+
+object Parser extends RegexParsers {
+  def int = regex("""[0-9]+""".r) ^^ { (i: String) => SInt(i.toInt) }
+  def ident = regex("""[A-Za-z+-/\*]+""".r) ^^ SIdent
+  def sexp = "(" ~> rep(node) <~ ")" ^^ SExp
+  def node: Parser[Node] = int | ident | sexp
+  def apply(s: String) = parse(sexp, s)
+}
+
+object Evaluator {
+  def compile(b: CodeBlock, n: Node) {
+    n match {
+      case SExp(Nil) => {}
+      case SExp(x::xs) => {
+        xs.foreach(compile(b, _))
+        compile(b, x)
+      }
+      case SInt(x) => b.pushInt(x)
+      case SIdent("+") => b.iadd()
+      case SIdent("-") => b.isub()
+      case SIdent("*") => b.imul()
+      case SIdent("/") => b.idiv()
+    }
+  }
+
+  def apply(program: SExp) {
+    val b = new CodeBlock
+    compile(b, program)
+    b.iprintln().voidreturn()
+
+    val name = "Lisp"
+    val c = new JiteClass(name)
+    c.defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(classOf[Unit], classOf[Array[String]]), b)
+
+    val stream = new FileOutputStream(new File(name + ".class"))
+    stream.write(c.toBytes)
+    stream.close()
+  }
+}
+
+object Main {
+  def main(args: Array[String]) {
+    Parser(args mkString " ") match {
+      case Parser.Success(p, _) => Evaluator(p)
+      case a => println(a)
+    }
+  }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.