all repos — min @ 348fc9a09c3c5d63115719799a132645dd2e953d

A small but practical concatenative programming language.

lib/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
import tables, os, strutils
import 
  ../core/types,
  ../core/parser, 
  ../core/interpreter, 
  ../core/utils

# I/O 


define("io")
  
  .symbol("newline") do (i: In):
    echo ""

  .symbol("put") do (i: In):
    let a = i.peek
    echo $$a

  .symbol("get") do (i: In):
    i.push newVal(stdin.readLine())

  .symbol("print") do (i: In):
    let a = i.peek
    a.print

  .symbol("fread") do (i: In):
    let a = i.pop
    if a.isString:
      if a.strVal.fileExists:
        try:
          i.push newVal(a.strVal.readFile)
        except:
          i.error errRuntime, getCurrentExceptionMsg()
      else:
        i.error errRuntime, "File '$1' not found" % [a.strVal]
    else:
      i.error(errIncorrect, "A string is required on the stack")

  .symbol("fwrite") do (i: In):
    let a = i.pop
    let b = i.pop
    if a.isString and b.isString:
      try:
        a.strVal.writeFile(b.strVal)
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error(errIncorrect, "Two strings are required on the stack")

  .symbol("fappend") do (i: In):
    let a = i.pop
    let b = i.pop
    if a.isString and b.isString:
      try:
        var f:File
        discard f.open(a.strVal, fmAppend)
        f.write(b.strVal)
        f.close()
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error(errIncorrect, "Two strings are required on the stack")


  .finalize()