all repos — min @ 7ef91095ba132b45fa796b684f4d9759f3e51da9

A small but practical concatenative programming language.

Implemented and tested new fs symbols; moved some symbols from sys to fs.
h3rald h3rald@h3rald.com
Sun, 14 Nov 2021 12:22:25 +0100
commit

7ef91095ba132b45fa796b684f4d9759f3e51da9

parent

a94a3fc7395f4001e0ac5899a4955191859c40ff

M minpkg/core/baseutils.nimminpkg/core/baseutils.nim

@@ -18,6 +18,9 @@ if pwd == "":

result = file else: result = pwd&"/"&file + +proc unix*(s: string): string = + return s.replace("\\", "/") proc parentDirEx*(s: string): string = return s.parentDir
M minpkg/lib/min_fs.nimminpkg/lib/min_fs.nim

@@ -1,8 +1,12 @@

import os, - times + times, + strutils, + critbits import + ../core/env, ../core/parser, + ../core/baseutils, ../core/value, ../core/interpreter, ../core/utils,

@@ -63,5 +67,96 @@ def.symbol("fperms") 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.newVal + + def.symbol("expand-filename") do (i: In): + let vals = i.expect("'sym") + i.push vals[0].getString.expandFilename.newVal + + def.symbol("expand-symlink") do (i: In): + let vals = i.expect("'sym") + i.push vals[0].getString.expandSymlink.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).newVal + + def.symbol("absolute-path") do (i: In): + let vals = i.expect("'sym") + i.push vals[0].getString.absolutePath.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.finalize("fs")
M minpkg/lib/min_sys.nimminpkg/lib/min_sys.nim

@@ -2,21 +2,17 @@ import

os, osproc, strutils, - critbits, logging, tables import ../core/parser, - ../core/env, + ../core/baseutils, ../core/value, ../core/interpreter, ../core/utils, ../core/fileutils import zippy/ziparchives - -proc unix(s: string): string = - return s.replace("\\", "/") proc sys_module*(i: In)= let def = i.define()

@@ -90,37 +86,6 @@ i.push hostOS.newVal

def.symbol("cpu") do (i: In): i.push hostCPU.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("rm") do (i: In): let vals = i.expect("'sym")

@@ -177,11 +142,6 @@ let vals = i.expect("int", "str")

let perms = vals[0] let s = vals[1] s.getString.setFilePermissions(perms.intVal.toFilePermissions) - - def.symbol("symlink?") do (i: In): - let vals = i.expect("'sym") - let s = vals[0] - i.push s.getString.symlinkExists.newVal def.symbol("symlink") do (i: In): let vals = i.expect("'sym", "'sym")

@@ -195,16 +155,6 @@ let dest = vals[0]

let src = vals[1] src.getString.createHardlink dest.getString - 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("$") do (i: In): i.pushSym("get-env")

@@ -245,6 +195,9 @@ archive.addDir(entry)

else: archive.contents[entry] = ArchiveEntry(contents: readFile(entry)) archive.writeZipArchive(file.getString) + + def.symbol("admin?") do (i: In): + i.push isAdmin().newVal def.finalize("sys")
M next-release.mdnext-release.md

@@ -1,10 +1,28 @@

### BREAKING CHANGES -* Runtime checks (expectations) must now be manually activated by specifying `-d` or `--dev`. +* Runtime checks (expectations, stack pollution checks, operator output validation) must now be manually activated by specifying `-d` or `--dev`. Activating checks can be very useful while developing a min program, at cost of performance. +* Moved the following symbols from the **sys** to the **fs** module: + * exists? + * dir? + * file? + * symlink? + * filename + * dirname ### New Features * Added **dev?** symbol to check if we are running in development mode. +* Added new symbols to the **fs** module: + * join-path + * expand-filename + * expand-symlink + * normalized-path + * absolute-path + * relative-path + * windows-path + * unix-path + * absolute-path? +* Added **admin?** symbol to the **sys** module. ### Fixes and Improvements
M site/contents/reference-fs.mdsite/contents/reference-fs.md

@@ -10,6 +10,21 @@

{#op||ctime||{{sl}}||{{flt}}|| Returns a timestamp corresponding to the time that file/directory {{sl}} was created.#} +{#op||dirname||{{sl}}||{{s}}|| +Returns the path of the directory containing path {{sl}}.#} + +{#op||dir?||{{sl}}||{{b}}|| +Returns {{t}} if the specified path {{sl}} exists and is a directory. #} + +{#op||exists?||{{sl}}||{{b}}|| +Returns {{t}} if the specified file or directory {{sl}} exists. #} + +{#op||file?||{{sl}}||{{b}}|| +Returns {{t}} if the specified path {{sl}} exists and is a file. #} + +{#op||filename||{{sl}}||{{s}}|| +Returns the file name of path {{sl}}.#} + {#op||fperms||{{sl}}||{{i}}|| Returns the Unix permissions (expressed as a three-digit number) of file/directory {{sl}}.#}

@@ -49,3 +64,5 @@

{#op||mtime||{{sl}}||{{flt}}|| Returns a timestamp corresponding to the time that file/directory {{sl}} was last modified.#} +{#op||symlink?||{{sl}}||{{b}}|| +Returns {{t}} if the specified path {{sl}} exists and is a symbolic link. #}
M site/contents/reference-sys.mdsite/contents/reference-sys.md

@@ -44,21 +44,6 @@

{#op||env?||{{sl}}||{{b}}|| Returns {{t}} if environment variable {{sl}} exists, {{f}} otherwise. #} -{#op||dir?||{{sl}}||{{b}}|| -Returns {{t}} if the specified path {{sl}} exists and is a directory. #} - -{#op||dirname||{{sl}}||{{s}}|| -Returns the path of the directory containing path {{sl}}.#} - -{#op||exists?||{{sl}}||{{b}}|| -Returns {{t}} if the specified file or directory {{sl}} exists. #} - -{#op||file?||{{sl}}||{{b}}|| -Returns {{t}} if the specified path {{sl}} exists and is a file. #} - -{#op||filename||{{sl}}||{{s}}|| -Returns the file name of path {{sl}}.#} - {#op||get-env||{{sl}}||{{s}}|| Returns environment variable {{sl}}. #}

@@ -97,9 +82,6 @@ Halts program execution for {{i}} milliseconds.#}

{#op||symlink||{{sl1}} {{sl2}}||{{none}}|| Creates symlink {{sl2}} for file or directory {{sl1}}. #} - -{#op||symlink?||{{sl}}||{{b}}|| -Returns {{t}} if the specified path {{sl}} exists and is a symbolic link. #} {#op||system||{{sl}}||{{i}}|| Executes the external command {{sl}} in the current directory and pushes its return code on the stack. #}
M tasks/build.mintasks/build.min

@@ -6,6 +6,10 @@ (

symbol cz ('sym :stage 'sym :variant 'sym :target-os ==>) ( + true :pack-result + (stage "^(release-nopack|dev)$" match?) + (false @pack-result) + when "-d:release" :d-stage (stage "dev" ==) ("-d:dev" @d-stage)

@@ -24,12 +28,14 @@ (musl-gcc length 0 >)

("--gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc" @musl) when "nim c $# -d:ssl $# --opt:size --cpu:amd64 --os:$# $#-o:$# min" (d-stage musl target-os d-variant o-variant) =% puts ! pop - {} - target-os %os - config /version %version - o-variant %exe - (stage "dev" !=) - (pack) + (pack-result) + ( + {} + target-os %os + config /version %version + o-variant %exe + pack + ) when ) ) ::

@@ -108,7 +114,7 @@ ;; Builds min (dev version) on the current OS.

( symbol default (==>) - ("" "" os cz) + ("release-nopack" "" os cz) ) :: ;; Builds min on the current OS. (
M tests/fs.mintests/fs.min

@@ -14,7 +14,31 @@

("test.txt" hidden? false ==) *test/assert ("test.txt" fstats 'type dget "file" ==) *test/assert + + ("tests" dir?) *test/assert + + ("tests" exists?) *test/assert + + ("tests/fs.min" file?) *test/assert + + (("a" "b" "c") join-path "a/b/c" ==) *test/assert + + ("/home/h3rald" windows-path "\\home\\h3rald" ==) *test/assert + + ("/home/h3rald" absolute-path?) *test/assert + + ("tests/sys.min" expand-filename unix-path . "/tests/sys.min" suffix unix-path ==) *test/assert + + ("./../tests" normalized-path unix-path "../tests" ==) *test/assert + + ((. "tests/lang.min") => join-path . relative-path unix-path "tests/lang.min" ==) *test/assert + + ("tests/lang.min" absolute-path unix-path (. "tests/lang.min") => join-path unix-path ==) *test/assert + + ("./test" absolute-path? not) *test/assert + + ("c:/windows" windows-path "c:\\windows" ==) *test/assert *test/report clear-stack - "test.txt" rm+ "test.txt" rm
M tests/sys.mintests/sys.min

@@ -44,10 +44,6 @@ (cpu length 0 >) *test/assert

("TEST" "test.txt" fwrite "test.txt" file?) *test/assert - ("systest" exists?) *test/assert - - ("systest" dir?) *test/assert - ("test.txt" "test2.txt" cp "test2.txt" file?) *test/assert ("test.txt" "test1.txt" mv "test1.txt" file?) *test/assert

@@ -66,8 +62,6 @@

.. cd ("systest" rmdir . ls (. "systest") => "/" join in? false ==) *test/assert - - *test/report clear-stack