Skip to content

Commit

Permalink
rename the Interpreter to MoeInterpreter and remove the InterpreterUt…
Browse files Browse the repository at this point in the history
…ils and put that stuff into guts.Utils
  • Loading branch information
Stevan Little committed May 2, 2013
1 parent b6168d5 commit a139539
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 73 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/org/moe/Moe.scala
Expand Up @@ -60,7 +60,7 @@ object Moe {
return
}

val interpreter = new Interpreter()
val interpreter = new MoeInterpreter()
val runtime = new MoeRuntime(
warnings = cmd.hasOption("w"),
debug = cmd.hasOption("d"),
Expand Down Expand Up @@ -145,7 +145,7 @@ object Moe {
}

object REPL {
def enter (interpreter: Interpreter, runtime: MoeRuntime, dumpAST: Boolean = false): Unit = {
def enter (interpreter: MoeInterpreter, runtime: MoeRuntime, dumpAST: Boolean = false): Unit = {
import jline.{ConsoleReader, History}

def isReplCommand(input: String) = input(0) == ':'
Expand Down Expand Up @@ -181,7 +181,7 @@ object Moe {
}
}

def evalLine(interpreter: Interpreter, runtime: MoeRuntime, line: String, options: Map[String, Boolean]) = {
def evalLine(interpreter: MoeInterpreter, runtime: MoeRuntime, line: String, options: Map[String, Boolean]) = {
try {
val nodes = MoeParser.parseFromEntry(line)
val ast = CompilationUnitNode(
Expand Down
Expand Up @@ -7,7 +7,7 @@ import org.moe.parser._

import org.moe.interpreter.guts._

class Interpreter {
class MoeInterpreter {

private var compiler : PartialFunction[(MoeEnvironment, AST), MoeObject] = _
private var evaluator : PartialFunction[(MoeEnvironment, AST), MoeObject] = _
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/org/moe/interpreter/guts/Classes.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._
object Classes extends Utils {

object Classes {

def declaration (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def declaration (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, ClassDeclarationNode(name, superclass, body, version, authority)) => {
val pkg = env.getCurrentPackage.getOrElse(
throw new MoeErrors.PackageNotFound("__PACKAGE__")
Expand Down Expand Up @@ -76,7 +74,7 @@ object Classes {
}
}

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, ClassAccessNode(name)) => r.lookupClass(
name,
env.getCurrentPackage.getOrElse(throw new MoeErrors.PackageNotFound("__PACKAGE__"))
Expand Down
Expand Up @@ -7,7 +7,7 @@ import org.moe.ast._

object CompilationUnits {

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, CompilationUnitNode(body)) => i.eval(r, env, body)
case (env, ScopeNode(body)) => i.compile_and_evaluate(new MoeEnvironment(Some(env)), body)
case (env, StatementsNode(nodes)) => {
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/org/moe/interpreter/guts/ElementAccess.scala
Expand Up @@ -7,11 +7,9 @@ import org.moe.ast._

import scala.util.{Try, Success, Failure}

import InterpreterUtils._

object ElementAccess {

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {

case (env, ArrayElementAccessNode(arrayName: String, indices: List[AST])) => {
var array_value = env.get(arrayName) match {
Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/org/moe/interpreter/guts/Literals.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._
object Literals extends Utils {

object Literals {

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, IntLiteralNode(value)) => r.NativeObjects.getInt(value)
case (env, FloatLiteralNode(value)) => r.NativeObjects.getNum(value)
case (env, StringLiteralNode(value)) => r.NativeObjects.getStr(value)
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/org/moe/interpreter/guts/Operators.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._

object Operators {

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
// unary operators

case (env, PrefixUnaryOpNode(lhs: AST, operator: String)) => {
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/org/moe/interpreter/guts/Packages.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._

object Packages {

def declaration (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def declaration (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, PackageDeclarationNode(name, body, version, authority)) => {
val newEnv = new MoeEnvironment(Some(env))
val parent = env.getCurrentPackage.getOrElse(throw new MoeErrors.PackageNotFound("__PACKAGE__"))
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/org/moe/interpreter/guts/Signatures.scala
Expand Up @@ -4,11 +4,9 @@ import org.moe.interpreter._
import org.moe.runtime._
import org.moe.ast._

import InterpreterUtils._

object Signatures {

def declaration (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def declaration (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, ParameterNode(name, optional, slurpy, named)) => (optional, slurpy, named) match {
case (false, false, false) => new MoePositionalParameter(name)
case (true, false, false) => new MoeOptionalParameter(name)
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/org/moe/interpreter/guts/Statements.scala
Expand Up @@ -6,15 +6,13 @@ import org.moe.runtime.nativeobjects._
import org.moe.ast._
import org.moe.parser._

import InterpreterUtils._

import scala.io.Source

object Statements {

private val stub = new MoeObject()

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, UseStatement(name)) => {
val path = r.findFilePathForPackageName(name).getOrElse(
throw new MoeErrors.MoeProblems(
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/org/moe/interpreter/guts/Subroutines.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._
object Subroutines extends Utils {

object Subroutines {

def declaration (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def declaration (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, SubroutineDeclarationNode(name, signature, body, traits)) => {
val sig = i.compile(env, signature).asInstanceOf[MoeSignature]
throwForUndeclaredVars(env, sig, body)
Expand All @@ -32,7 +30,7 @@ object Subroutines {
}
}

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, SubroutineCallNode(function_name, args)) => {
val sub = r.lookupSubroutine(
function_name,
Expand Down
@@ -1,15 +1,32 @@
package org.moe.interpreter
package org.moe.interpreter.guts

import org.moe.runtime._
import org.moe.ast._

object InterpreterUtils {
def inNewEnv[T](env: MoeEnvironment)(body: MoeEnvironment => T): T = {
val newEnv = new MoeEnvironment(Some(env))
trait Utils {

body(env)
// Throw an exception if a variable isn't closed over at declaration time
// This is to prevent variables in the same env but after declaration getting
// sucked into the closure and causing unexpected behavior.
def throwForUndeclaredVars(env: MoeEnvironment, signature: MoeSignature, body: StatementsNode): Unit = {
var declared: Set[String] = signature.getParams.map(_.getName).toSet
walkAST(
body,
{ ast: AST =>
ast match {
case VariableDeclarationNode(varname, _) =>
declared += varname
case VariableAccessNode(varname) =>
if (!env.has(varname) && !declared(varname) && !env.isSpecialMarker(varname)) {
throw new MoeErrors.VariableNotFound(varname)
}
case _ => Unit
}
}
)
}

// XXX - this no longer captures all the AST nodes anymore
def walkAST(ast: AST, callback: (AST) => Unit): Unit = {
callback(ast)
ast match {
Expand Down Expand Up @@ -106,26 +123,6 @@ object InterpreterUtils {
}
}

// Throw an exception if a variable isn't closed over at declaration time
// This is to prevent variables in the same env but after declaration getting
// sucked into the closure and causing unexpected behavior.
def throwForUndeclaredVars(env: MoeEnvironment, signature: MoeSignature, body: StatementsNode): Unit = {
var declared: Set[String] = signature.getParams.map(_.getName).toSet
walkAST(
body,
{ ast: AST =>
ast match {
case VariableDeclarationNode(varname, _) =>
declared += varname
case VariableAccessNode(varname) =>
if (!env.has(varname) && !declared(varname) && !env.isSpecialMarker(varname)) {
throw new MoeErrors.VariableNotFound(varname)
}
case _ => Unit
}
}
)
}

}

6 changes: 2 additions & 4 deletions src/main/scala/org/moe/interpreter/guts/Variables.scala
Expand Up @@ -5,11 +5,9 @@ import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.ast._

import InterpreterUtils._

object Variables {

def declaration (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def declaration (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, VariableDeclarationNode(name, expression)) => {
val value = i.evaluate(env, expression)
if (!MoeType.checkType(name, value)) throw new MoeErrors.IncompatibleType(
Expand All @@ -19,7 +17,7 @@ object Variables {
}
}

def apply (i: Interpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
def apply (i: MoeInterpreter, r: MoeRuntime): PartialFunction[(MoeEnvironment, AST), MoeObject] = {
case (env, VariableAccessNode(name)) => env.get(name).getOrElse(
if (name.startsWith("&")) {
val function_name = name.drop(1)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/runtime/MoeLazyEval.scala
Expand Up @@ -10,7 +10,7 @@ import org.moe.ast._
*/

class MoeLazyEval (
interpreter: Interpreter,
interpreter: MoeInterpreter,
runtime: MoeRuntime,
env: MoeEnvironment,
node: AST
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/runtime/MoeRuntime.scala
Expand Up @@ -11,7 +11,7 @@ class MoeRuntime (
private val system: MoeSystem = new MoeSystem(),
private val warnings: Boolean = true,
private val debug: Boolean = false,
private val interpreter: Option[Interpreter] = None
private val interpreter: Option[MoeInterpreter] = None
) extends MoeObject {

private val VERSION = "0.0.0"
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/org/moe/runtime/builtins/IntClass.scala
Expand Up @@ -2,7 +2,6 @@ package org.moe.runtime.builtins

import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.interpreter.InterpreterUtils._

/**
* setup class Int
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/org/moe/runtime/builtins/NumClass.scala
Expand Up @@ -2,7 +2,6 @@ package org.moe.runtime.builtins

import org.moe.runtime._
import org.moe.runtime.nativeobjects._
import org.moe.interpreter.InterpreterUtils._

/**
* setup class Num
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/org/moe/interpreter/InterpreterTestUtils.scala
Expand Up @@ -12,11 +12,11 @@ trait InterpreterTestUtils
with BeforeAndAfter
with ShouldMatchers {

var interpreter : Interpreter = _
var interpreter : MoeInterpreter = _
var runtime : MoeRuntime = _

before {
interpreter = new Interpreter()
interpreter = new MoeInterpreter()
runtime = new MoeRuntime()
runtime.bootstrap()
}
Expand Down
@@ -1,13 +1,14 @@
package org.moe.interpreter

import org.moe.ast._
import org.moe.interpreter.guts._

import org.scalatest.FunSuite
import org.scalatest.matchers._

import scala.util.parsing.json._

class InterpreterUtilsTestSuite extends FunSuite with ShouldMatchers {
class InterpreterUtilsTestSuite extends FunSuite with ShouldMatchers with Utils {
test("... test walking ASTs") {
var cb_count = 0
var node_list: List[String] = List()
Expand All @@ -17,7 +18,7 @@ class InterpreterUtilsTestSuite extends FunSuite with ShouldMatchers {
case JSONObject(map) => node_list ++= map.keys
}
}
InterpreterUtils.walkAST(
walkAST(
StatementsNode(
List(
VariableDeclarationNode("$n", IntLiteralNode(0)),
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/org/moe/parser/ParserTestUtils.scala
Expand Up @@ -10,11 +10,11 @@ import org.scalatest.BeforeAndAfter

trait ParserTestUtils extends FunSuite with BeforeAndAfter {

var interpreter: Interpreter = _
var interpreter: MoeInterpreter = _
var runtime : MoeRuntime = _

before {
interpreter = new Interpreter()
interpreter = new MoeInterpreter()
runtime = new MoeRuntime()
runtime.bootstrap()
}
Expand Down

0 comments on commit a139539

Please sign in to comment.