all repos — min @ 2465db761b6bc7b668e881cce803048de678d445

A small but practical concatenative programming language.

minpkg/lib/min_fs.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
 186
 187
 188
 189
 190
import
  std/[os,
  times,
  strutils,
  critbits]
import
  ../core/env,
  ../core/parser,
  ../core/baseutils,
  ../core/value,
  ../core/interpreter,
  ../core/utils,
  ../core/fileutils

proc fs_module*(i: In) =

  let def = i.define()

  def.symbol("mtime") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getLastModificationTime.toUnix.newVal

  def.symbol("atime") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getLastAccessTime.toUnix.newVal

  def.symbol("ctime") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getCreationTime.toUnix.newVal

  def.symbol("hidden?") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.isHidden.newVal

  def.symbol("size") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getFileSize.newVal

  def.symbol("stats") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    let fi = s.getString.getFileInfo
    var info = newDict(i.scope)
    i.dset(info, "name", s)
    i.dset(info, "device", fi.id.device.BiggestInt.newVal)
    i.dset(info, "file", fi.id.file.BiggestInt.newVal)
    i.dset(info, "type", fi.kind.filetype.newVal)
    i.dset(info, "size", fi.size.newVal)
    i.dset(info, "permissions", fi.permissions.unixPermissions.newVal)
    i.dset(info, "nlinks", fi.linkCount.newVal)
    i.dset(info, "ctime", fi.creationTime.toUnix.newVal)
    i.dset(info, "atime", fi.lastAccessTime.toUnix.newVal)
    i.dset(info, "mtime", fi.lastWriteTime.toUnix.newVal)
    i.push info

  def.symbol("type") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getFileInfo.kind.filetype.newVal

  def.symbol("permissions") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.getFilePermissions.unixPermissions.newVal

  def.symbol("exists?") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0].getString
    var found = false
    if MINCOMPILED:
      let cf = strutils.replace(strutils.replace(f, "\\", "/"), "./", "")

      found = COMPILEDASSETS.hasKey(cf)
    if found:
      i.push true.newVal
    else:
      i.push newVal(f.fileExists or f.dirExists)

  def.symbol("file?") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0].getString
    var found = false
    if MINCOMPILED:
      let cf = strutils.replace(strutils.replace(f, "\\", "/"), "./", "")

      found = COMPILEDASSETS.hasKey(cf)
    if found:
      i.push true.newVal
    else:
      i.push f.fileExists.newVal

  def.symbol("dir?") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0]
    i.push f.getString.dirExists.newVal

  def.symbol("symlink?") do (i: In):
    let vals = i.expect("'sym")
    let s = vals[0]
    i.push s.getString.symlinkExists.newVal

  def.symbol("filename") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0]
    i.push f.getString.extractFilename.unix.newVal

  def.symbol("dirname") do (i: In):
    let vals = i.expect("'sym")
    let f = vals[0]
    i.push f.getString.parentDir.unix.newVal

  def.symbol("join-path") do (i: In):
    let vals = i.expect("quot")
    var fragments = newSeq[string](0)
    for p in vals[0].qVal:
      if not p.isStringLike:
        raiseInvalid("A quotation of strings is required")
      fragments.add(p.getString)
    i.push fragments.joinPath.unix.newVal

  def.symbol("expand-filename") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.expandFilename.unix.newVal

  def.symbol("expand-symlink") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.expandSymlink.unix.newVal

  def.symbol("normalized-path") do (i: In):
    let vals = i.expect("'sym")
    var s = vals[0].getString
    s.normalizePath()
    i.push s.newVal

  def.symbol("relative-path") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let p = vals[1].getString
    let base = vals[0].getString
    i.push relativePath(p, base).unix.newVal

  def.symbol("absolute-path") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.absolutePath.unix.newVal

  def.symbol("windows-path") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.replace("/", "\\").newVal

  def.symbol("unix-path") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.replace("\\", "/").newVal

  def.symbol("absolute-path?") do (i: In):
    let vals = i.expect("'sym")
    i.push vals[0].getString.isAbsolute.newVal

  def.symbol("read") do (i: In):
    let vals = i.expect("str")
    let file = vals[0].strVal
    var contents = ""
    if MINCOMPILED:
      var compiledFile = strutils.replace(strutils.replace(file, "\\", "/"),
          "./", "")
      if COMPILEDASSETS.hasKey(compiledFile):
        contents = COMPILEDASSETS[compiledFile]
    if contents == "":
      contents = file.readFile
    i.push newVal(contents)

  def.symbol("write") do (i: In):
    let vals = i.expect("str", "str")
    let a = vals[0]
    let b = vals[1]
    a.strVal.writeFile(b.strVal)

  def.symbol("append") do (i: In):
    let vals = i.expect("str", "str")
    let a = vals[0]
    let b = vals[1]
    var f: File
    discard f.open(a.strVal, fmAppend)
    f.write(b.strVal)
    f.close()

  def.finalize("fs")