aboutsummaryrefslogtreecommitdiff
path: root/doc/sys.md
blob: 184ecde4cd78e1ca41c7e1f5eaace196743da649 (plain)
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
System Interaction
==================

Some facilities have been developed to access the outside world.

`sys .exit`
-----------

Exists the program with the indicated error code.

    0 sys .exit       # successful termination


`sys .file`
-----------

Creates a scope representing a file. This object supports `.open`, `.close`, `.read`, `.write`, `.writeall`, `.eachLine`.
Three files `sys .in`, `sys .out`, `sys .err` are predefined and represent the standard input, output and error streams respectively.

    sys .file ":" via
      "foo.txt" :open
      8 :read dump          # first 8 bytes of foo.txt, possibly less if foo.txt is shorter
      { dump } :eachLine    # dump each line of foo.txt (excluding the 8 bytes already read)
      :close
    "Hallo Welt!\n" sys .out .writeall
    Hallo Welt!

As `.write` directly maps to the write(2) syscall, it might not write all bytes. Instead it returns the number of bytes written as an integer.
Usually, you want to use `.writeall` which will call write(2) repeatedly, until all bytes are written.

`sys .fdToFile` will create a file representing scope directly from a unix file descriptor number.


`sys .freeze`
-------------

To create stand-alone executables, `sys .freeze` takes a filename and a function object and creates an executable which will
execute the function object when started.

    { "Hello World!\n" sys .out .writeall 0 sys .exit } "hello" sys .freeze

An elymas interpreter can be implemented via `include` easily:

    {
      sys .argv len { 0 sys .argv * } { "/proc/self/fd/0" } ? * include
      0 sys .exit
    } "interpreter" sys .freeze


`sys .mkdir`
------------

Creates a new directory.


`sys .ls` / `sys .readdir`
--------------------------

List the contents of a directory. `sys .ls` excludes files with a leading dot.


`sys .rename`
-------------

Takes two filenames. Renames the first (stack second-to-top) to the second (stack top).