all repos — min @ 64b05549511a9d2368aa1dd4f3e487bc5a3d7318

A small but practical concatenative programming language.

minpkg/lib/min_stack.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
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils

proc stack_module*(i: In)=

  let def = i.define()

  def.symbol("clear-stack") do (i: In):
    while i.stack.len > 0:
      discard i.pop

  def.symbol("get-stack") do (i: In):
    i.push i.stack.newVal

  def.symbol("set-stack") do (i: In):
    let vals = i.expect("quot")
    let q = vals[0]
    i.stack = q.qVal
  
  def.symbol("id") do (i: In):
    discard
  
  def.symbol("pop") do (i: In):
    discard i.pop
  
  def.symbol("dup") do (i: In):
    i.push i.peek

  def.symbol("dip") do (i: In):
    let vals = i.expect("quot", "a")
    var q = vals[0]
    let v = vals[1]
    i.dequote(q)
    i.push v

  def.symbol("nip") do (i: In):
    let vals = i.expect("a", "a")
    let a = vals[0]
    i.push a
  
  def.symbol("cleave") do (i: In):
    var q: MinValue
    i.reqQuotationOfQuotations q
    let v = i.pop
    for s in q.qVal:
      var s1 = s
      i.push v
      i.dequote(s1)
  
  def.symbol("spread") do (i: In):
    var q: MinValue
    i.reqQuotationOfQuotations q
    var els = newSeq[MinValue](0)
    for el in 0..q.qVal.len-1:
      els.add i.pop
    var count = els.len-1
    for s in q.qVal:
      var s1 = s
      i.push els[count]
      i.dequote(s1)
      count.dec
  
  def.symbol("keep") do (i: In):
    let vals = i.expect("quot", "a")
    var q = vals[0]
    let v = vals[1]
    i.push v
    i.dequote(q)
    i.push v
  
  def.symbol("swap") do (i: In):
    let vals = i.expect("a", "a")
    let a = vals[0]
    let b = vals[1]
    i.push a
    i.push b

  def.symbol("over") do (i: In):
    let vals = i.expect("a", "a")
    let a = vals[0]
    let b = vals[1]
    i.push b
    i.push a
    i.push b

  def.symbol("pick") do (i: In):
    let vals = i.expect("a", "a", "a")
    let a = vals[0]
    let b = vals[1]
    let c = vals[2]
    i.push c
    i.push b
    i.push a
    i.push c

  def.symbol("rollup") do (i: In):
    let vals = i.expect("a", "a", "a")
    let first = vals[0]
    let second = vals[1]
    let third = vals[2]
    i.push first
    i.push second
    i.push third

  def.symbol("rolldown") do (i: In):
    let vals = i.expect("a", "a", "a")
    let first = vals[0]
    let second = vals[1]
    let third = vals[2]
    i.push second
    i.push first
    i.push third

  def.symbol("cons") do (i: In):
    let vals = i.expect("quot", "a")
    let q = vals[0]
    let v = vals[1]
    i.push newVal(@[v] & q.qVal)

  def.symbol("swons") do (i: In):
    i.pushSym "swap"
    i.pushSym "cons"
  
  def.symbol("sip") do (i: In):
    let vals = i.expect("quot", "quot")
    var a = vals[0]
    let b = vals[1]
    i.push b
    i.dequote(a)
    i.push b

  def.finalize("stack")