all repos — min @ 8dc873a6adb297076184b979dbb1427a81476c07

A small but practical concatenative programming language.

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
 136
 137
 138
 139
 140
 141
 142
import 
  tables,
  random
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils
  
# Operations on the whole stack
proc stack_module*(i: In)=

  i.define()

    .symbol("clear-stack") do (i: In):
      while i.stack.len > 0:
        discard i.pop
  
    .symbol("get-stack") do (i: In):
      i.push i.stack.newVal(i.scope)
  
    .symbol("set-stack") do (i: In):
      var q: MinValue
      i.reqQuotation q
      i.stack = q.qVal
    
    .symbol("id") do (i: In):
      discard
    
    .symbol("pop") do (i: In):
      if i.stack.len < 1:
        raiseEmptyStack()
      discard i.pop
    
    .symbol("dup") do (i: In):
      i.push i.peek

    .symbol("dip") do (i: In):
      var q: MinValue
      i.reqQuotation q
      let v = i.pop
      i.unquote(q)
      i.push v

    .symbol("nip") do (i: In):
      var a, b: MinValue
      a = i.pop
      b = i.pop
      i.push a
    
    .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.unquote(s1)
    
    .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.unquote(s1)
        count.dec
    
    .symbol("keep") do (i: In):
      var q: MinValue
      i.reqQuotation q
      let v = i.pop
      i.push v
      i.unquote(q)
      i.push v
    
    .symbol("swap") do (i: In):
      i.reqStackSize 2
      let a = i.pop
      let b = i.pop
      i.push a
      i.push b

    .symbol("over") do (i: In):
      i.reqStackSize 2
      let a = i.pop
      let b = i.pop
      i.push b
      i.push a
      i.push b

    .symbol("pick") do (i: In):
      i.reqStackSize 3
      let a = i.pop
      let b = i.pop
      let c = i.pop
      i.push c
      i.push b
      i.push a
      i.push c

    .symbol("rollup") do (i: In):
      i.reqStackSize 3
      let first = i.pop
      let second = i.pop
      let third = i.pop
      i.push first
      i.push second
      i.push third

    .symbol("rolldown") do (i: In):
      i.reqStackSize 3
      let first = i.pop
      let second = i.pop
      let third = i.pop
      i.push second
      i.push first
      i.push third

    .symbol("cons") do (i: In):
      var q: MinValue
      i.reqQuotation q
      let v = i.pop
      q.qVal = @[v] & q.qVal
      i.push q

    .symbol("swons") do (i: In):
      i.push "swap".newSym
      i.push "cons".newSym
    
    .symbol("sip") do (i: In):
      var a, b: MinValue 
      i.reqTwoQuotations a, b
      i.push b
      i.unquote(a)
      i.push b
  
    .finalize("stack")