all repos — min @ 52c40db3ec70cf78cce070f01eccf5d350c45778

A small but practical concatenative programming language.

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

# I/O 


proc io_module*(i: In) =
  i.define("io")
    
    .symbol("newline") do (i: In):
      echo ""
  
    .symbol("puts") do (i: In):
      let a = i.peek
      echo $$a
  
    .symbol("column-print") do (i: In):
      var n, q: MinValue
      i.reqIntAndQuotation n, q
      var c = 0
      for s in q.qVal:
        c.inc
        stdout.write $$s & spaces(max(0, 15 - ($$s).len))
        if c mod n.intVal == 0:
          echo ""
      echo ""
  
    .symbol("gets") do (i: In):
      var ed = initEditor()
      i.push ed.readLine().newVal

    .symbol("password") do (i: In):
      var ed = initEditor()
      i.push ed.password("Enter Password: ").newVal

    .symbol("confirm") do (i: In):
      var s: MinValue
      var ed = initEditor()
      i.reqString s
      stdout.write(s.getString & " [yes/no]: ")
      proc confirm(): bool =
        let answer = ed.readLine()
        if answer.match("^y(es)?$", "i"):
          return true
        elif answer.match("^no?$", "i"):
          return false
        else:
          stdout.write "Invalid answer. Please enter 'yes' or 'no': "
          return confirm()
      i.push confirm().newVal
  
    .symbol("print") do (i: In):
      let a = i.peek
      a.print
  
    .symbol("fread") do (i: In):
      var a: MinValue
      i.reqString a
      i.push newVal(a.strVal.readFile)
  
    .symbol("fwrite") do (i: In):
      var a, b: MinValue
      i.reqTwoStrings a, b
      a.strVal.writeFile(b.strVal)
  
    .symbol("fappend") do (i: In):
      var a, b: MinValue
      i.reqTwoStrings a, b
      var f:File
      discard f.open(a.strVal, fmAppend)
      f.write(b.strVal)
      f.close()

    .finalize()