all repos — min @ 41a367c2ef810139724e5df1b65b01d47990830f

A small but practical concatenative programming language.

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

  # OS
  
define("sys")

  .symbol("pwd") do (i: In):
    i.push newVal(getCurrentDir())
  
  .symbol("cd") do (i: In):
    let f = i.pop
    if f.isString:
      try:
        f.strVal.setCurrentDir
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "A string is required on the stack"
  
  .symbol("ls") do (i: In):
    let a = i.pop
    var list = newSeq[MinValue](0)
    if a.isString:
      if a.strVal.existsDir:
        for i in walkdir(a.strVal):
          list.add newVal(i.path)
        i.push list.newVal
      else:
        i.error errRuntime, "Directory '$1' not found" % [a.strVal]
    else:
      i.error(errIncorrect, "A string is required on the stack")
  
  .symbol("system") do (i: In):
    let a = i.pop
    if a.isString:
      i.push execShellCmd(a.strVal).newVal
    else:
      i.error(errIncorrect, "A string is required on the stack")
  
  .symbol("run") do (i: In):
    let a = i.pop
    if a.isString:
      let words = a.strVal.split(" ")
      let cmd = words[0]
      var args = newSeq[string](0)
      if words.len > 1:
        args = words[1..words.len-1]
      i.push execProcess(cmd, args, nil, {poUsePath}).newVal
    else:
      i.error(errIncorrect, "A string is required on the stack")
  
  .symbol("getenv") do (i: In):
    let a = i.pop
    if a.isString:
      i.push a.strVal.getEnv.newVal
    else:
      i.error(errIncorrect, "A string is required on the stack")
  
  .symbol("putenv") do (i: In):
    let value = i.pop
    let key = i.pop
    if value.isString and key.isString:
      key.strVal.putEnv value.strVal
    else:
      i.error(errIncorrect, "Two strings are required on the stack")
  
  .symbol("os") do (i: In):
    i.push hostOS.newVal
  
  .symbol("cpu") do (i: In):
    i.push hostCPU.newVal
  
  .symbol("file?") do (i: In):
    let f = i.pop
    if f.isString:
      i.push f.strVal.fileExists.newVal
    else:
      i.error errIncorrect, "A string is required on the stack"
  
  .symbol("dir?") do (i: In):
    let f = i.pop
    if f.isString:
      i.push f.strVal.dirExists.newVal
    else:
      i.error errIncorrect, "A string is required on the stack"
  
  .symbol("rm") do (i: In):
    let f = i.pop
    if f.isString:
      try:
        f.strVal.removeFile
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "A string is required on the stack"
  
  .symbol("cp") do (i: In):
    let b = i.pop
    let a = i.pop
    if a.isString and b.isString:
      try:
        copyFile a.strVal, b.strVal
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "Two strings are required on the stack"
  
  .symbol("mv") do (i: In):
    let b = i.pop
    let a = i.pop
    if a.isString and b.isString:
      try:
        moveFile a.strVal, b.strVal
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "Two strings are required on the stack"
  
  .symbol("rmdir") do (i: In):
    let f = i.pop
    if f.isString:
      try:
        f.strVal.removeDir
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "A string is required on the stack"
  
  .symbol("mkdir") do (i: In):
    let f = i.pop
    if f.isString:
      try:
        f.strVal.createDir
      except:
        i.error errRuntime, getCurrentExceptionMsg()
    else:
      i.error errIncorrect, "A string is required on the stack"

  .finalize()