all repos — min @ 8bd1ab66b91a1a82263b095db6d708d3f955b456

A small but practical concatenative programming language.

lib/min_str.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, 
  strutils, 
  sequtils
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils,
  ../core/regex

proc str_module*(i: In) = 
  i.define("str")

  .symbol("strip") do (i: In):
    var s: MinValue
    i.reqStringLike s
    i.push s.getString.strip.newVal
    
  .symbol("split") do (i: In):
    var sep, s: MinValue
    i.reqTwoStrings sep, s
    var q = newSeq[MinValue](0)
    for e in s.strVal.split(sep.strVal):
      q.add e.newVal
    i.push q.newVal(i.scope)

  .symbol("join") do (i: In):
    var q, s: MinValue
    i.reqStringLikeAndQuotation s, q
    i.push q.qVal.mapIt($$it).join(s.getString).newVal 

  .symbol("search") do (i: In):
    var reg, str: MinValue
    i.reqTwoStrings reg, str
    var matches = str.strVal.search(reg.strVal)
    var res = newSeq[MinValue](matches.len)
    for i in 0..matches.len-1:
      res[i] = matches[i].newVal
    i.push res.newVal(i.scope)

  .symbol("match") do (i: In):
    var reg, str: MinValue
    i.reqTwoStrings reg, str
    if str.strVal.match(reg.strVal):
      i.push true.newVal
    else:
      i.push false.newVal

  .symbol("replace") do (i: In):
    var s_replace, reg, s_find: MinValue
    i.reqThreeStrings s_replace, reg, s_find
    i.push regex.replace(s_find.strVal, reg.strVal, s_replace.strVal).newVal

  .symbol("regex") do (i: In):
    var reg, str: MinValue
    i.reqTwoStrings reg, str
    let results = str.strVal =~ reg.strVal
    var res = newSeq[MinValue](0)
    for r in results:
      res.add(r.newVal)
    i.push res.newVal(i.scope)

  .symbol("lowercase") do (i: In):
    var s: MinValue
    i.reqStringLike s
    i.push s.getString.toLowerAscii.newVal

  .symbol("uppercase") do (i: In):
    var s: MinValue
    i.reqStringLike s
    i.push s.getString.toUpperAscii.newVal

  .symbol("capitalize") do (i: In):
    var s: MinValue
    i.reqStringLike s
    i.push s.getString.capitalizeAscii.newVal

  .symbol("titleize") do (i: In):
    var s: MinValue
    i.reqStringLike s
    i.push s.getString.split(" ").mapIt(it.capitalizeAscii).join(" ").newVal

  .symbol("repeat") do (i: In):
    var s, n: MinValue
    i.reqIntAndString n, s
    i.push s.getString.repeat(n.intVal).newVal

  .symbol("indent") do (i: In):
    var s, n: MinValue
    i.reqIntAndString n, s
    i.push s.getString.indent(n.intVal).newVal

  .symbol("string") do (i: In):
    var s = i.pop
    i.push(($$s).newVal)

  .symbol("bool") do (i: In):
    var v = i.pop
    let strcheck = (v.isString and (v.getString == "false" or v.getString == ""))
    let intcheck = v.isInt and v.intVal == 0
    let floatcheck = v.isFloat and v.floatVal == 0
    let boolcheck = v.isBool and v.boolVal == false
    let quotcheck = v.isQuotation and v.qVal.len == 0
    if strcheck or intcheck or floatcheck or boolcheck or quotcheck:
      i.push false.newVal
    else:
      i.push true.newVal

  .symbol("int") do (i: In):
    var s = i.pop
    if s.isString:
      i.push s.getString.parseInt.newVal
    elif s.isFloat:
      i.push s.floatVal.int.newVal
    elif s.isInt:
      i.push s
    elif s.isBool:
      if s.boolVal == true:
        i.push 1.int.newVal
      else:
        i.push 0.int.newVal
    else:
      raiseInvalid("Cannot convert a quotation to an integer.")

  .symbol("float") do (i: In):
    var s = i.pop
    if s.isString:
      i.push s.getString.parseFloat.newVal
    elif s.isInt:
      i.push s.intVal.float.newVal
    elif s.isFloat:
      i.push s
    elif s.isBool:
      if s.boolVal == true:
        i.push 1.float.newVal
      else:
        i.push 0.float.newVal
    else:
      raiseInvalid("Cannot convert a quotation to float.")

  .finalize()