all repos — min @ 53e02d84748acebec51341296829a592bdc00cd5

A small but practical concatenative programming language.

minpkg/lib/min_num.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
import 
  random
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils

proc num_module*(i: In)=

  let def = i.define()

  def.symbol("nan") do (i: In):
    i.push newVal(NaN)
  
  def.symbol("inf") do (i: In):
    i.push newVal(Inf)
  
  def.symbol("-inf") do (i: In):
    i.push newVal(NegInf)
  
  def.symbol("+") do (i: In):
    let vals = i.expect("num", "num")
    let a = vals[0]
    let b = vals[1]
    if a.isInt:
      if b.isInt:
        i.push newVal(a.intVal + b.intVal)
      else:
        i.push newVal(a.intVal.float + b.floatVal)
    else:
      if b.isFloat:
        i.push newVal(a.floatVal + b.floatVal)
      else:
        i.push newVal(a.floatVal + b.intVal.float)
  
  def.symbol("-") do (i: In):
    let vals = i.expect("num", "num")
    let a = vals[0]
    let b = vals[1]
    if a.isInt:
      if b.isInt:
        i.push newVal(b.intVal - a.intVal)
      else:
        i.push newVal(b.floatVal - a.intVal.float)
    else:
      if b.isFloat:
        i.push newVal(b.floatVal - a.floatVal)
      else:
        i.push newVal(b.intVal.float - a.floatVal) 
  
  def.symbol("*") do (i: In):
    let vals = i.expect("num", "num")
    let a = vals[0]
    let b = vals[1]
    if a.isInt:
      if b.isInt:
        i.push newVal(a.intVal * b.intVal)
      else:
        i.push newVal(a.intVal.float * b.floatVal)
    else:
      if b.isFloat:
        i.push newVal(a.floatVal * b.floatVal)
      else:
        i.push newVal(a.floatVal * b.intVal.float)
  
  def.symbol("/") do (i: In):
    let vals = i.expect("num", "num")
    let a = vals[0]
    let b = vals[1]
    if a.isInt:
      if b.isInt:
        i.push newVal(b.intVal.int / a.intVal.int)
      else:
        i.push newVal(b.floatVal / a.intVal.float)
    else:
      if b.isFloat:
        i.push newVal(b.floatVal / a.floatVal)
      else:
        i.push newVal(b.intVal.float / a.floatVal) 
  
  def.symbol("randomize") do (i: In):
    randomize()

  def.symbol("random") do (i: In):
    let vals = i.expect("int")
    let n = vals[0]
    i.push n.intVal.int.rand.newVal

  def.symbol("div") do (i: In):
    let vals = i.expect("int", "int")
    let b = vals[0]
    let a = vals[1]
    i.push(newVal(a.intVal div b.intVal))
  
  def.symbol("mod") do (i: In):
    let vals = i.expect("int", "int")
    let b = vals[0]
    let a = vals[1]
    i.push(newVal(a.intVal mod b.intVal))

  def.symbol("succ") do (i: In):
    let vals = i.expect("int")
    let n = vals[0]
    i.push newVal(n.intVal + 1)

  def.symbol("pred") do (i: In):
    let vals = i.expect("int")
    let n = vals[0]
    i.push newVal(n.intVal - 1)
  
  def.symbol("even?") do (i: In):
    let vals = i.expect("int")
    let n = vals[0]
    i.push newVal(n.intVal mod 2 == 0)

  def.symbol("odd?") do (i: In):
    let vals = i.expect("int")
    let n = vals[0]
    i.push newVal(n.intVal mod 2 != 0)

  def.symbol("sum") do (i: In):
    var s: MinValue
    i.reqQuotationOfNumbers s
    var c = 0.float
    var isInt = true
    for n in s.qVal:
      if n.isFloat:
        isInt = false
        c = + n.floatVal
      else:
        c = c + n.intVal.float
    if isInt:
      i.push c.int.newVal
    else:
      i.push c.newVal

  def.symbol("product") do (i: In):
    var s: MinValue
    i.reqQuotationOfNumbers s
    var c = 1.float
    var isInt = true
    for n in s.qVal:
      if n.isFloat:
        isInt = false
        c = c * n.floatVal
      else:
        c = c * n.intVal.float
    if isInt:
      i.push c.int.newVal
    else:
      i.push c.newVal

  def.symbol("avg") do (i: In):
    var s: MinValue
    i.reqQuotationOfNumbers s
    var c = 0.float
    for n in s.qVal:
      if n.isFloat:
        c = + n.floatVal
      else:
        c = c + n.intVal.float
    c = c / len(s.qVal).float
    i.push c.newVal
 
  def.symbol("med") do (i: In):
    var s: MinValue
    i.reqQuotationOfNumbers s
    let first = s.qVal[(s.qVal.len-1) div 2]
    let second = s.qVal[((s.qVal.len-1) div 2)+1]
    if s.qVal.len mod 2 == 1:
      i.push first
    else:
      if first.isFloat:
        if second.isFloat:
          i.push ((first.floatVal+second.floatVal)/2).newVal
        else:
          i.push ((first.floatVal+second.intVal.float)/2).newVal
      else:
        if second.isFloat:
          i.push ((first.intVal.float+second.floatVal)/2).newVal
        else:
          i.push ((first.intVal+second.intVal).float/2).newVal
    
  def.symbol("range") do (i: In):
    var s: MinValue
    i.reqQuotationOfNumbers s
    var a = s.qVal[0]
    var b = s.qVal[1]
    var step = 1.newVal
    var res = newSeq[MinValue](0)
    if len(s.qVal)==3:
      a = s.qVal[0]
      b = s.qVal[1]
      step = s.qVal[2]
    var j = a
    if a.intVal < b.intVal:
      while j.intVal <= b.intVal:
        res.add j
        j = (j.intVal + step.intVal).newVal
    else:
      while j.intVal >= b.intVal:
        res.add j
        j = (j.intVal - step.intVal).newVal
    i.push res.newVal

  def.finalize("num")