Skip to content

Commit f01c27d

Browse files
committed
+ Added $:foldl, $:foldr, and $:filter commands
* $:cons and $:econs should work on linked lists
1 parent 1e110d7 commit f01c27d

File tree

8 files changed

+83
-5
lines changed

8 files changed

+83
-5
lines changed

src/main/scala/cmdreader/Global.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object Global {
3737
val TWO = new BigInteger("2")
3838
val vM = 0
3939
val vm = 5
40-
val vr = 11
40+
val vr = 12
4141
val version = vM + "." + vm + "." + vr
4242
val r: Random = new Random
4343
}

src/main/scala/cmdreader/std/Cons.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Cons extends Command {
1313
try {
1414
l match {
1515
case a: LArray => new LArray((t +: a.l).to[ArrayBuffer])
16-
case a: LLinked => new LLinked((t +: a.l).tail.to[ListBuffer])
16+
case a: LLinked => new LLinked((t +: a.l).to[ListBuffer])
1717
case s: TString => {
1818
new TString(new String(Array[Char](
1919
t match {

src/main/scala/cmdreader/std/ECons.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ECons extends Command {
1313
try {
1414
l match {
1515
case a: LArray => new LArray((a.l :+ t).to[ArrayBuffer])
16-
case a: LLinked => new LLinked((a.l :+ t).tail.to[ListBuffer])
16+
case a: LLinked => new LLinked((a.l :+ t).to[ListBuffer])
1717
case s: TString => {
1818
new TString(s.getVal + new String(Array[Char](
1919
t match {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmdreader.std
2+
3+
import cmdreader.Command
4+
import types._
5+
import scala.collection.mutable._
6+
import util._
7+
8+
class FoldL extends Command {
9+
override def getName(): String = "foldl"
10+
override def isValidArg0(n: Int): Boolean = n == 3
11+
override def apply(args: Array[Type]): Type = {
12+
val collection = args(0)
13+
val base = args(1)
14+
val f: TFunction = args(2) match {
15+
case func: TFunction => func
16+
case _ => return new TError(1)
17+
}
18+
CollectionOps.ctv[Type](_.foldLeft(base)((a, b) => f(Array(a, b))))(collection)
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmdreader.std
2+
3+
import cmdreader.Command
4+
import types._
5+
import scala.collection.mutable._
6+
import util._
7+
8+
class FoldR extends Command {
9+
override def getName(): String = "foldr"
10+
override def isValidArg0(n: Int): Boolean = n == 3
11+
override def apply(args: Array[Type]): Type = {
12+
val collection = args(0)
13+
val base = args(1)
14+
val f: TFunction = args(2) match {
15+
case func: TFunction => func
16+
case _ => return new TError(1)
17+
}
18+
CollectionOps.ctv[Type](_.foldRight(base)((a, b) => f(Array(a, b))))(collection)
19+
}
20+
}

src/main/scala/cmdreader/std/Loader.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ class Loader {
5050
s => List(s, s + "h", "A" + s, "A" + s + "h").map(Global.liblist("std").loadCmd(_))
5151
})
5252
Global.liblist("std").loadCmd("Expr")
53+
Global.liblist("std").loadCmd("FoldL")
54+
Global.liblist("std").loadCmd("FoldR")
55+
Global.liblist("std").loadCmd("OFilter")
5356
}
5457
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmdreader.std
2+
3+
import cmdreader._
4+
import types._
5+
import scala.collection.mutable._
6+
import util._
7+
8+
class OFilter extends CommandOperator {
9+
override def getName(): String = "filter"
10+
override def isValidArg0(n: Int): Boolean = n == 2
11+
override def apply(args: Array[Type]): Type = {
12+
val collection = args(0)
13+
val f: TFunction = args(1) match {
14+
case func: TFunction => func
15+
case _ => return new TError(1)
16+
}
17+
CollectionOps.ctc(_.filter(a => f(Array(a)).toBoolean))(collection)
18+
}
19+
override def getOpAlias(): String = "|>"
20+
def getPrecedence(): Int = PStandard.MAP
21+
def isReversed(): Boolean = false
22+
def hasAssignmentEquiv(): Boolean = true
23+
def getDoubleBase(): Option[Type] = None
24+
}

src/main/scala/run/RunningInstance.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import parse.ast._
1010
import java.io.File
1111

1212
// If fname starts with "code:", then it is an instance of code.
13-
class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
13+
class RunningInstance(fn: String, c: RunningInstance, args: Array[Type]) {
1414
// initial stuffs
1515
var bytecode: Array[Byte] = Array[Byte]()
1616
/*if (!fname.startsWith("code:")) {
@@ -27,6 +27,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
2727
}*/
2828
val needle: Int = 0
2929
val calling = c
30+
val fname = fn
3031
var environment = new HashMap[String, Type]()
3132
var stack = List[Type]()
3233
var symstack = List[LValue]()
@@ -124,6 +125,14 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
124125
(new String(bytecode.slice(strSt, strSt + length), "UTF-8"), strSt + length)
125126
}
126127
def tus(n: Byte) = if (n >= 0) n else 0x100 + n
128+
def printStackTrace = {
129+
println("Stacktrace: ")
130+
var curNode = this
131+
while (curNode != null) {
132+
println((curNode.needle - 2).toHexString + "@" + curNode.fname)
133+
curNode = curNode.calling
134+
}
135+
}
127136
def run = { // runs the bytecode
128137
var needle = 0
129138
var isDone = false
@@ -244,6 +253,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
244253
stack = VariableReader.readData(bytecode.slice(needle, needle + size), valtype, "[ANON]") :: stack
245254
needle += size
246255
} else {
256+
printStackTrace
247257
throw new RuntimeException("Invalid command: " + cmd + "@" + (needle - 2).toHexString)
248258
}
249259
}
@@ -253,8 +263,9 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
253263
if (!stack.isEmpty && stack.head.isInstanceOf[TError]) {
254264
val e = stack.head
255265
stack = stack.tail
266+
printStackTrace
256267
throw new RuntimeException("Runtime " + e + ": " + (needle - 2).toHexString + "@" + fname + ": " +
257-
cmd.toHexString)
268+
cmd.toHexString)
258269
}
259270
}
260271
}

0 commit comments

Comments
 (0)