Skip to content

Commit

Permalink
+ Added $:foldl, $:foldr, and $:filter commands
Browse files Browse the repository at this point in the history
* $:cons and $:econs should work on linked lists
  • Loading branch information
bluebear94 committed Feb 3, 2014
1 parent 1e110d7 commit f01c27d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/cmdreader/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object Global {
val TWO = new BigInteger("2")
val vM = 0
val vm = 5
val vr = 11
val vr = 12
val version = vM + "." + vm + "." + vr
val r: Random = new Random
}
2 changes: 1 addition & 1 deletion src/main/scala/cmdreader/std/Cons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Cons extends Command {
try {
l match {
case a: LArray => new LArray((t +: a.l).to[ArrayBuffer])
case a: LLinked => new LLinked((t +: a.l).tail.to[ListBuffer])
case a: LLinked => new LLinked((t +: a.l).to[ListBuffer])
case s: TString => {
new TString(new String(Array[Char](
t match {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/cmdreader/std/ECons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ECons extends Command {
try {
l match {
case a: LArray => new LArray((a.l :+ t).to[ArrayBuffer])
case a: LLinked => new LLinked((a.l :+ t).tail.to[ListBuffer])
case a: LLinked => new LLinked((a.l :+ t).to[ListBuffer])
case s: TString => {
new TString(s.getVal + new String(Array[Char](
t match {
Expand Down
20 changes: 20 additions & 0 deletions src/main/scala/cmdreader/std/FoldL.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmdreader.std

import cmdreader.Command
import types._
import scala.collection.mutable._
import util._

class FoldL extends Command {
override def getName(): String = "foldl"
override def isValidArg0(n: Int): Boolean = n == 3
override def apply(args: Array[Type]): Type = {
val collection = args(0)
val base = args(1)
val f: TFunction = args(2) match {
case func: TFunction => func
case _ => return new TError(1)
}
CollectionOps.ctv[Type](_.foldLeft(base)((a, b) => f(Array(a, b))))(collection)
}
}
20 changes: 20 additions & 0 deletions src/main/scala/cmdreader/std/FoldR.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmdreader.std

import cmdreader.Command
import types._
import scala.collection.mutable._
import util._

class FoldR extends Command {
override def getName(): String = "foldr"
override def isValidArg0(n: Int): Boolean = n == 3
override def apply(args: Array[Type]): Type = {
val collection = args(0)
val base = args(1)
val f: TFunction = args(2) match {
case func: TFunction => func
case _ => return new TError(1)
}
CollectionOps.ctv[Type](_.foldRight(base)((a, b) => f(Array(a, b))))(collection)
}
}
3 changes: 3 additions & 0 deletions src/main/scala/cmdreader/std/Loader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ class Loader {
s => List(s, s + "h", "A" + s, "A" + s + "h").map(Global.liblist("std").loadCmd(_))
})
Global.liblist("std").loadCmd("Expr")
Global.liblist("std").loadCmd("FoldL")
Global.liblist("std").loadCmd("FoldR")
Global.liblist("std").loadCmd("OFilter")
}
}
24 changes: 24 additions & 0 deletions src/main/scala/cmdreader/std/OFilter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmdreader.std

import cmdreader._
import types._
import scala.collection.mutable._
import util._

class OFilter extends CommandOperator {
override def getName(): String = "filter"
override def isValidArg0(n: Int): Boolean = n == 2
override def apply(args: Array[Type]): Type = {
val collection = args(0)
val f: TFunction = args(1) match {
case func: TFunction => func
case _ => return new TError(1)
}
CollectionOps.ctc(_.filter(a => f(Array(a)).toBoolean))(collection)
}
override def getOpAlias(): String = "|>"
def getPrecedence(): Int = PStandard.MAP
def isReversed(): Boolean = false
def hasAssignmentEquiv(): Boolean = true
def getDoubleBase(): Option[Type] = None
}
15 changes: 13 additions & 2 deletions src/main/scala/run/RunningInstance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import parse.ast._
import java.io.File

// If fname starts with "code:", then it is an instance of code.
class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
class RunningInstance(fn: String, c: RunningInstance, args: Array[Type]) {
// initial stuffs
var bytecode: Array[Byte] = Array[Byte]()
/*if (!fname.startsWith("code:")) {
Expand All @@ -27,6 +27,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
}*/
val needle: Int = 0
val calling = c
val fname = fn
var environment = new HashMap[String, Type]()
var stack = List[Type]()
var symstack = List[LValue]()
Expand Down Expand Up @@ -124,6 +125,14 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
(new String(bytecode.slice(strSt, strSt + length), "UTF-8"), strSt + length)
}
def tus(n: Byte) = if (n >= 0) n else 0x100 + n
def printStackTrace = {
println("Stacktrace: ")
var curNode = this
while (curNode != null) {
println((curNode.needle - 2).toHexString + "@" + curNode.fname)
curNode = curNode.calling
}
}
def run = { // runs the bytecode
var needle = 0
var isDone = false
Expand Down Expand Up @@ -244,6 +253,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
stack = VariableReader.readData(bytecode.slice(needle, needle + size), valtype, "[ANON]") :: stack
needle += size
} else {
printStackTrace
throw new RuntimeException("Invalid command: " + cmd + "@" + (needle - 2).toHexString)
}
}
Expand All @@ -253,8 +263,9 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
if (!stack.isEmpty && stack.head.isInstanceOf[TError]) {
val e = stack.head
stack = stack.tail
printStackTrace
throw new RuntimeException("Runtime " + e + ": " + (needle - 2).toHexString + "@" + fname + ": " +
cmd.toHexString)
cmd.toHexString)
}
}
}
Expand Down

0 comments on commit f01c27d

Please sign in to comment.