-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added $:map (+>), plus some useful math functions
- Loading branch information
1 parent
4631ad0
commit 0133684
Showing
9 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ object PStandard { | |
val DISJUNCTION = 50 | ||
val DOUBLE_OP = 1600 | ||
val SHIFT = 175 | ||
val MAP = 2000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmdreader.std | ||
|
||
import cmdreader._ | ||
import types._ | ||
import util._ | ||
import java.math.BigInteger | ||
|
||
class OMap extends CommandOperator { | ||
override def getName(): String = "map" | ||
override def getOpAlias() = "+>" | ||
override def isValidArg0(n: Int) = n >= 2 | ||
override def apply(args: Array[Type]): Type = { | ||
val argc = args.length | ||
val lists = args.dropRight(1) | ||
val f0 = args(argc - 1) | ||
val f = f0 match { | ||
case f: TFunction => f | ||
case _ => return new TError(1, f0 + "is not a function") | ||
} | ||
val trueLists = lists.map(CollectionOps.decodeToList(_)) | ||
val flipped = TurnOnSide(trueLists) | ||
val newList = flipped.map(f(_)) | ||
CollectionOps.encodeFromList(newList, lists(0).getType) | ||
} | ||
def getPrecedence() = PStandard.MAP | ||
def isReversed() = false | ||
def hasAssignmentEquiv() = true | ||
def getDoubleBase() = None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cmdreader.std | ||
|
||
import cmdreader._ | ||
import types._ | ||
import util._ | ||
|
||
class Exp extends Command { | ||
override def getName(): String = "exp" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.exp(args(0)) | ||
} | ||
class Ln extends Command { | ||
override def getName(): String = "ln" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.ln(args(0)) | ||
} | ||
class Pi extends Command { | ||
override def getName(): String = "pi" | ||
override def isValidArg0(n: Int): Boolean = n == 0 | ||
override def apply(args: Array[Type]): Type = TFish(Math.PI) | ||
} | ||
class Sin extends Command { | ||
override def getName(): String = "sin" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.sin(args(0)) | ||
} | ||
class Cos extends Command { | ||
override def getName(): String = "cos" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.cos(args(0)) | ||
} | ||
class Tan extends Command { | ||
override def getName(): String = "tan" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.tan(args(0)) | ||
} | ||
class ASin extends Command { | ||
override def getName(): String = "asin" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.asin(args(0)) | ||
} | ||
class ACos extends Command { | ||
override def getName(): String = "acos" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.acos(args(0)) | ||
} | ||
class ATan extends Command { | ||
override def getName(): String = "atan" | ||
override def isValidArg0(n: Int): Boolean = n == 1 || n == 2 | ||
override def apply(args: Array[Type]): Type = { | ||
if (args.length == 1) MathUtil.atan(args(0)) | ||
else MathUtil.atan(args(1), args(0)) | ||
} | ||
} | ||
class Sinh extends Command { | ||
override def getName(): String = "sinh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.sinh(args(0)) | ||
} | ||
class Cosh extends Command { | ||
override def getName(): String = "cosh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.cosh(args(0)) | ||
} | ||
class Tanh extends Command { | ||
override def getName(): String = "tanh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.tanh(args(0)) | ||
} | ||
class ASinh extends Command { | ||
override def getName(): String = "asinh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.asinh(args(0)) | ||
} | ||
class ACosh extends Command { | ||
override def getName(): String = "acosh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.acosh(args(0)) | ||
} | ||
class ATanh extends Command { | ||
override def getName(): String = "atanh" | ||
override def isValidArg0(n: Int): Boolean = n == 1 | ||
override def apply(args: Array[Type]): Type = MathUtil.atanh(args(0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package util | ||
|
||
import types.TMountain | ||
import types._ | ||
import java.math.BigInteger | ||
|
||
object BTI { | ||
def bti(b: Boolean): TMountain = { | ||
new TMountain(if (b) BigInteger.ONE else BigInteger.ZERO) | ||
} | ||
def btl(b: Boolean): THill = { | ||
new THill(if (b) 1L else 0L) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package util | ||
import types._ | ||
import java.math.BigInteger | ||
import scala.collection.mutable._ | ||
|
||
object CollectionOps { | ||
def decodeToList(t: Type): List[Type] = { | ||
t match { | ||
case t: TMountain => { | ||
val n = t.getVal | ||
List.range(0, n.bitCount).map(b => BTI.btl(n.testBit(b))) | ||
} | ||
case t: THill => { | ||
val n = t.getVal | ||
List.range(0, 64).map(b => BTI.btl((n & (1L << b)) != 0L)) | ||
} | ||
case t: TString => { | ||
t.getVal.toCharArray.toList.map(i => new THill(i)) | ||
} | ||
case t: LList => { | ||
t.l.toList | ||
} | ||
} | ||
} | ||
def encodeFromList(l: List[Type], mode: Int): Type = { | ||
mode match { | ||
case 1 => { | ||
var n = BigInteger.ZERO | ||
for (e <- l) { | ||
e match { | ||
case e: TNumerical => { | ||
n = n.shiftLeft(1).add(BigInteger.valueOf(e.intValue)) | ||
} | ||
case _ => return new TError(1) | ||
} | ||
} | ||
TMountain(n) | ||
} | ||
case 2 => { | ||
new THill(l.map(_ match { | ||
case e: TNumerical => { | ||
e.intValue | ||
} | ||
case _ => return new TError(1) | ||
}).foldLeft(0L)((a, b) => (a << 1) + b)) | ||
} | ||
case 3 => { | ||
new TString(new String(l.map(_ match { | ||
case e: TNumerical => { | ||
e.intValue.toChar | ||
} | ||
case _ => return new TError(1) | ||
}).toArray)) | ||
} | ||
case 5 => | ||
new LArray(l.to[ArrayBuffer]) | ||
case 6 => | ||
new LLinked(l.to[ListBuffer]) | ||
} | ||
} | ||
def ctv[T](f: (List[Type]) => T): (Type) => T = { | ||
(t: Type) => | ||
f(decodeToList(t)) | ||
} | ||
def ctc(f: (List[Type]) => List[Type]): (Type) => Type = { | ||
(t: Type) => | ||
encodeFromList(ctv(f)(t), t.getType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util | ||
import types._ | ||
|
||
object TurnOnSide { | ||
// Trying to write a generalized function drove me crazy. | ||
def apply(mat: Array[List[Type]]): List[Array[Type]] = { | ||
mat(0) match { | ||
case Nil => Nil | ||
case _ => { | ||
mat.map(_.head) :: apply(mat.map(_.tail)) | ||
} | ||
} | ||
} | ||
} |