Skip to content

Commit

Permalink
Six new tasty functions for you
Browse files Browse the repository at this point in the history
$:abs(x) - returns absolute value of number. If list is passed, then applies the function element by element.
$:floor(x) - returns floor of number. If list is passed, then applies the function element by element.
$:fpart(x) - returns fractional part of number. If list is passed, then applies the function element by element.
$:now() - returns number of milliseconds since midnight Jan. 1 1970
$:rrbzo() - returns a Random Real Between Zero and One.
$:sub(c, s[, e]) - returns a subcollection of a collection `c' with starting index `s' and ending index `e' if specified
  • Loading branch information
bluebear94 committed Jan 18, 2014
1 parent ea4e2a1 commit 8aaa1b3
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/scala/cmdreader/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io._
import java.lang._
import run.RunningInstance
import java.math.BigInteger
import scala.util.Random

object Global {
var liblist: HashMap[String, CmdList] = new HashMap[String, CmdList]()
Expand Down Expand Up @@ -36,6 +37,7 @@ object Global {
val TWO = new BigInteger("2")
val vM = 0
val vm = 5
val vr = 5
val vr = 6
val version = vM + "." + vm + "." + vr
val r: Random = new Random
}
14 changes: 14 additions & 0 deletions src/main/scala/cmdreader/std/Abs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmdreader.std

import cmdreader.Command
import types._
import gui._
import util._

class Abs extends Command {
override def getName(): String = "abs"
override def isValidArg0(n: Int): Boolean = n == 1
override def apply(args: Array[Type]): Type = {
MathUtil.abs(args(0))
}
}
14 changes: 14 additions & 0 deletions src/main/scala/cmdreader/std/FPart.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmdreader.std

import cmdreader.Command
import types._
import gui._
import util._

class FPart extends Command {
override def getName(): String = "fpart"
override def isValidArg0(n: Int): Boolean = n == 1
override def apply(args: Array[Type]): Type = {
MathUtil.fpart(args(0))
}
}
16 changes: 16 additions & 0 deletions src/main/scala/cmdreader/std/Floor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmdreader.std

import cmdreader.Command
import types._
import gui._
import util._

// What TI-Basic-inspired language could be complete without a floor function?

class Floor extends Command {
override def getName(): String = "floor"
override def isValidArg0(n: Int): Boolean = n == 1
override def apply(args: Array[Type]): Type = {
MathUtil.floor(args(0))
}
}
6 changes: 6 additions & 0 deletions src/main/scala/cmdreader/std/Loader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ class Loader {
Global.liblist("std").loadCmd("OAndB")
Global.liblist("std").loadCmd("OOrB")
Global.liblist("std").loadCmd("OXorB")
Global.liblist("std").loadCmd("Now")
Global.liblist("std").loadCmd("Sub")
Global.liblist("std").loadCmd("Rrbzo")
Global.liblist("std").loadCmd("Abs")
Global.liblist("std").loadCmd("Floor")
Global.liblist("std").loadCmd("FPart")
}
}
12 changes: 12 additions & 0 deletions src/main/scala/cmdreader/std/Now.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmdreader.std

import cmdreader.Command
import types._
import java.util.Date

class Now extends Command {
override def getName(): String = "now"
override def apply(args: Array[Type]): Type = {
new THill(new Date().getTime)
}
}
12 changes: 12 additions & 0 deletions src/main/scala/cmdreader/std/Rrbzo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmdreader.std

import cmdreader._
import types._

class Rrbzo extends Command {
override def getName(): String = "rrbzo"
override def isValidArg0(n: Int): Boolean = n == 0
override def apply(args: Array[Type]): Type = {
TFish(Global.r.nextDouble)
}
}
33 changes: 33 additions & 0 deletions src/main/scala/cmdreader/std/Sub.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmdreader.std

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

class Sub extends Command {
override def getName(): String = "sub"
override def isValidArg0(n: Int): Boolean = n == 2 || n == 3
override def apply(args: Array[Type]): Type = {
val whole = args(0)
(whole, args(1), if (args.length == 2) 0 else args(2)) match {
case (w: LList, n: TNumerical, p: TNumerical) => {
val const = (b: Buffer[Type]) => {
if (w.getType == 5) new LArray(b.to[ArrayBuffer])
else new LLinked(b.to[ListBuffer])
}
const({
if (args.length == 2) w.l.drop(n.intValue)
else w.l.slice(n.intValue, p.intValue)
})
}
case (w: TString, n: TNumerical, p: TNumerical) => {
new TString({
if (args.length == 2) w.getVal.substring(n.intValue)
else w.getVal.substring(n.intValue, p.intValue)
})
}
case (_, _, _) => new TError(1)
}

}
}
28 changes: 28 additions & 0 deletions src/main/scala/util/MathUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,32 @@ object MathUtil {
case (_, _) => new TError(1)
}
}
def abs(x: Type): Type = {
x match {
case xm: TMountain => new TMountain(xm.getVal.abs)
case xh: THill => new THill(Math.abs(xh.getVal))
case xf: TFish => new TFish(Math.abs(xf.getVal))
case xl: LList => mool(abs(_), xl)
case _ => new TError(1)
}
}
def floor(x: Type): Type = {
x match {
case xm: TMountain => new TMountain(xm.getVal)
case xh: THill => new THill(Math.abs(xh.getVal))
case xf: TFish => new TMountain(new BigDecimal(xf.getVal).toBigInteger)
case xl: LList => mool(floor(_), xl)
case _ => new TError(1)
}
}
def fpart(x: Double) = x - x.floor
def fpart(x: Type): Type = {
x match {
case xm: TMountain => new TMountain(BigInteger.ZERO)
case xh: THill => new THill(0L)
case xf: TFish => new TFish(fpart(xf.getVal))
case xl: LList => mool(abs(_), xl)
case _ => new TError(1)
}
}
}

0 comments on commit 8aaa1b3

Please sign in to comment.