all repos — min @ f7c8ccacbc3c0a2ff5d0ca7bde239e881157ec5a

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

# I/O 

minsym "puts", i:
  let a = i.peek
  echo a

minsym "gets", i:
  i.push newVal(stdin.readLine())

minsym "print", i:
  let a = i.peek
  a.print

minsym "read", i:
  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")

minsym "write", i:
  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")