123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- package core
- import "fmt"
- type Any interface{}
- func MOD(a, b Any) int {
- var n, m int
- if isInt(a) {
- n = a.(int)
- } else if isFloat(a) {
- n = int(a.(float64))
- } else {
- panic("need int/float argument to mod!")
- }
- if isInt(b) {
- m = b.(int)
- } else if isFloat(a) {
- m = int(b.(float64))
- } else {
- panic("need int/float argument to mod!")
- }
- return n % m
- }
- func ADD(args ...Any) float64 {
- var sum float64 = 0
- for i := 0; i < len(args); i++ {
- switch n := args[i]; {
- case isInt(n):
- sum += float64(n.(int))
- case isFloat(n):
- sum += n.(float64)
- }
- }
- return sum
- }
- func SUB(args ...Any) float64 {
- var result float64
- if isInt(args[0]) {
- result = float64(args[0].(int))
- } else if isFloat(args[0]) {
- result = args[0].(float64)
- } else {
- panic("need int/float for SUB")
- }
- for i := 1; i < len(args); i++ {
- switch n := args[i]; {
- case isInt(n):
- result -= float64(n.(int))
- case isFloat(n):
- result -= n.(float64)
- }
- }
- return result
- }
- func MUL(args ...Any) float64 {
- var prod float64 = 1
- for i := 0; i < len(args); i++ {
- switch n := args[i]; {
- case isInt(n):
- prod *= float64(n.(int))
- case isFloat(n):
- prod *= n.(float64)
- }
- }
- return prod
- }
- func DIV() {}
- // TODO: can only compare ints and slice lens for now.
- func LT(args ...Any) bool {
- if len(args) < 2 {
- panic("can't compare less than 2 values!")
- }
- for i := 0; i < len(args)-1; i++ {
- var n float64
- if isInt(args[i]) {
- n = float64(args[i].(int))
- } else if isFloat(args[i]) {
- n = args[i].(float64)
- } else {
- panic("you can't compare that!")
- }
- var m float64
- if isInt(args[i+1]) {
- m = float64(args[i+1].(int))
- } else if isFloat(args[i+1]) {
- m = args[i+1].(float64)
- } else {
- panic("you can't compare that!")
- }
- if n >= m {
- return false
- }
- }
- return true
- }
- // TODO: can only compare ints and slice lens for now.
- func GT(args ...Any) bool {
- if len(args) < 2 {
- panic("can't compare less than 2 values!")
- }
- for i := 0; i < len(args)-1; i++ {
- var n float64
- if isInt(args[i]) {
- n = float64(args[i].(int))
- } else if isFloat(args[i]) {
- n = args[i].(float64)
- } else {
- panic("you can't compare that!")
- }
- var m float64
- if isInt(args[i+1]) {
- m = float64(args[i+1].(int))
- } else if isFloat(args[i+1]) {
- m = args[i+1].(float64)
- } else {
- panic("you can't compare that!")
- }
- if n <= m {
- return false
- }
- }
- return true
- }
- func EQ(args ...Any) bool {
- if len(args) < 2 {
- panic("can't compare less than 2 values!")
- }
- for i := 0; i < len(args)-1; i++ {
- var n float64
- if isInt(args[i]) {
- n = float64(args[i].(int))
- } else if isFloat(args[i]) {
- n = args[i].(float64)
- } else {
- panic("you can't compare that!")
- }
- var m float64
- if isInt(args[i+1]) {
- m = float64(args[i+1].(int))
- } else if isFloat(args[i+1]) {
- m = args[i+1].(float64)
- } else {
- panic("you can't compare that!")
- }
- if n != m {
- return false
- }
- }
- return true
- }
- // greater than or equal
- func GTEQ(args ...Any) bool {
- if GT(args...) || EQ(args...) {
- return true
- }
- return false
- }
- // less than or equal
- func LTEQ(args ...Any) bool {
- if LT(args...) || EQ(args...) {
- return true
- }
- return false
- }
- func isFloat(n Any) bool {
- _, ok := n.(float64)
- return ok
- }
- func isInt(n Any) bool {
- _, ok := n.(int)
- return ok
- }
- func Get(args ...Any) Any {
- if len(args) != 2 && len(args) != 3 {
- panic(fmt.Sprintf("get needs 2 or 3 arguments %d given.", len(args)))
- }
- if len(args) == 2 {
- if a, ok := args[1].([]Any); ok {
- return a[args[0].(int)]
- } else if a, ok := args[1].(string); ok {
- return a[args[0].(int)]
- } else {
- panic("arguments to get must include slice/vector/string")
- }
- } else {
- if a, ok := args[2].([]Any); ok {
- if args[1].(int) == -1 {
- return a[args[0].(int):]
- }
- return a[args[0].(int):args[1].(int)]
- } else if a, ok := args[2].(string); ok {
- if args[1].(int) == -1 {
- return a[args[0].(int):]
- }
- return a[args[0].(int):args[1].(int)]
- } else {
- panic("arguments to get must include slice/vector/string")
- }
- }
- }
|