all repos — min @ 63c0bddb422750a51c5f951c7727501157632da0

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
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("read") do (i: In):
    let a = i.pop
    if a.isString:
      if a.strVal.fileExists:
        try:
          i.push newVal(a.strVal.readFile)
        except:
          warn getCurrentExceptionMsg()
      else:
        warn "File '$1' not found" % [a.strVal]
    else:
      i.error(errIncorrect, "A string is required on the stack")

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

  .finalize()