all repos — min @ 8f33ecdeaa50fda100ad7443070a8c6627a469a9

A small but practical concatenative programming language.

lib/min_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
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
import 
  tables, 
  os, 
  osproc, 
  strutils,
  sequtils
import 
  ../core/parser, 
  ../core/value, 
  ../core/interpreter, 
  ../core/utils,
  ../core/fileutils,
  ../core/zip

proc unix(s: string): string =
  return s.replace("\\", "/")

proc sys_module*(i: In)=
  i.define("sys")
  
    .symbol(".") do (i: In):
      i.push newVal(getCurrentDir().unix)
    
    .symbol("..") do (i: In):
      i.push newVal(getCurrentDir().parentDir.unix)
    
    .symbol("cd") do (i: In):
      var f: MinValue
      i.reqStringLike f
      f.getString.setCurrentDir
    
    .symbol("ls") do (i: In):
      var a: MinValue
      i.reqStringLike a
      var list = newSeq[MinValue](0)
      for i in walkDir(a.getString):
        list.add newVal(i.path.unix)
      i.push list.newVal
    
    .symbol("ls-r") do (i: In):
      var a: MinValue
      i.reqStringLike a
      var list = newSeq[MinValue](0)
      for i in walkDirRec(a.getString):
        list.add newVal(i.unix)
      i.push list.newVal
  
    .symbol("system") do (i: In):
      var a: MinValue
      i.reqStringLike a
      i.push execShellCmd(a.getString).newVal
    
    .symbol("run") do (i: In):
      var cmd: MinValue
      i.reqStringLike cmd
      let res = execCmdEx(cmd.getString)
      i.push @[@["output".newSym, res.output.newVal].newVal, @["code".newSym, res.exitCode.newVal].newVal].newVal
    
    .symbol("getenv") do (i: In):
      var a: MinValue
      i.reqStringLike a
      i.push a.getString.getEnv.newVal
    
    .symbol("putenv") do (i: In):
      var key, value: MinValue
      i.reqTwoStringLike key, value
      key.getString.putEnv value.getString
    
    .symbol("env?") do (i: In):
      var s: MinValue
      i.reqStringLike s
      i.push s.getString.existsEnv.newVal

    .symbol("which") do (i: In):
      var s: MinValue
      i.reqStringLike s
      i.push s.getString.findExe.newVal

    .symbol("os") do (i: In):
      i.push hostOS.newVal
    
    .symbol("cpu") do (i: In):
      i.push hostCPU.newVal
    
    .symbol("file?") do (i: In):
      var f: MinValue
      i.reqStringLike f
      i.push f.getString.fileExists.newVal
    
    .symbol("dir?") do (i: In):
      var f: MinValue
      i.reqStringLike f
      i.push f.getString.dirExists.newVal
    
    .symbol("rm") do (i: In):
      var v: MinValue
      i.reqStringLike v
      let f = v.getString
      if f.existsFile:
        f.removeFile
      elif f.existsDir:
        f.removeDir
      else:
        raiseInvalid("File '$1' does not exist." % f)
    
    .symbol("cp") do (i: In):
      var a, b: MinValue
      i.reqTwoStringLike a, b
      let src = b.getString
      var dest = a.getString
      if src.dirExists:
        copyDirWithPermissions src, dest
      elif dest.dirExists:
        if src.dirExists:
          copyDirWithPermissions src, dest
        else:
          copyFileWithPermissions src, dest / src.extractFilename 
      else:
        copyFileWithPermissions src, dest 
    
    .symbol("mv") do (i: In):
      var a, b: MinValue
      i.reqTwoStringLike a, b
      let src = b.getString
      var dest = a.getString
      if dest.dirExists:
        dest = dest / src.extractFilename 
      moveFile src, dest
    
    .symbol("rmdir") do (i: In):
      var f: MinValue
      i.reqStringLike f
      f.getString.removeDir
    
    .symbol("mkdir") do (i: In):
      var f: MinValue
      i.reqStringLike f
      f.getString.createDir
  
     .symbol("sleep") do (i: In):
       var ms: MinValue
       i.reqInt ms
       sleep ms.intVal.int
  
    .symbol("chmod") do (i: In):
      var s, perms: MinValue
      i.reqIntAndString perms, s
      s.getString.setFilePermissions(perms.intVal.toFilePermissions)

    .symbol("symlink?") do (i: In):
      var s: MinValue
      i.reqStringLike s
      i.push s.getString.symlinkExists.newVal

    .symbol("symlink") do (i: In):
      var src, dest: MinValue
      i.reqTwoStringLike dest, src
      src.getString.createSymlink dest.getString

    .symbol("hardlink") do (i: In):
      var src, dest: MinValue
      i.reqTwoStringLike dest, src
      src.getString.createHardlink dest.getString

    .symbol("filename") do (i: In):
      var f: MinValue
      i.reqStringLike f
      i.push f.getString.extractFilename.unix.newVal

    .symbol("dirname") do (i: In):
      var f: MinValue
      i.reqStringLike f
      i.push f.getString.parentDir.unix.newVal

    .symbol("unzip") do (i: In):
      var f, dir: MinValue
      i.reqTwoStringLike dir, f
      unzip(f.getString, dir.getString)

    .symbol("zip") do (i: In):
      var files, file: MinValue
      i.reqStringLikeAndQuotation file, files
      zip(files.qVal.mapIt(it.getString), file.getString)

    .finalize()