-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
System.nim
150 lines (126 loc) · 4.29 KB
/
System.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
######################################################
# Arturo
# Programming Language + Bytecode VM compiler
# (c) 2019-2021 Yanis Zafirópulos
#
# @file: library/Shell.nim
######################################################
#=======================================
# Pragmas
#=======================================
{.used.}
#=======================================
# Libraries
#=======================================
import os, osproc, sequtils, sugar
import vm/[common, errors, globals, stack, value]
#=======================================
# Methods
#=======================================
proc defineSymbols*() =
when defined(VERBOSE):
echo "- Importing: System"
builtin "execute",
alias = unaliased,
rule = PrefixPrecedence,
description = "execute given shell command",
args = {
"command" : {String}
},
attrs = NoAttrs,
returns = {String},
example = """
print execute "pwd"
; /Users/admin/Desktop
split.lines execute "ls"
; => ["tests" "var" "data.txt"]
""":
##########################################################
let res = execCmdEx(x.s)
stack.push(newString(res[0]))
builtin "exit",
alias = unaliased,
rule = PrefixPrecedence,
description = "exit program",
args = NoArgs,
attrs = NoAttrs,
returns = {Nothing},
example = """
exit ; (terminates the program)
exit.with: 3 ; (terminates the program with code 3)
""":
##########################################################
quit()
builtin "list",
alias = unaliased,
rule = PrefixPrecedence,
description = "get files at given path",
args = {
"path" : {String}
},
attrs = {
"select" : ({String},"select files satisfying given pattern"),
"relative" : ({Boolean},"get relative paths")
},
returns = {Block},
example = """
loop list "." 'file [
___print file
]
; ./tests
; ./var
; ./data.txt
loop list.relative "tests" 'file [
___print file
]
; test1.art
; test2.art
; test3.art
""":
##########################################################
let findRelative = (popAttr("relative") != VNULL)
let contents = toSeq(walkDir(x.s, relative=findRelative))
if (let aSelect = popAttr("select"); aSelect != VNULL):
stack.push(newStringBlock((contents.map((x)=>x[1])).filter((x) => x.contains aSelect.s)))
else:
stack.push(newStringBlock(contents.map((x)=>x[1])))
# TODO add example
builtin "panic",
alias = unaliased,
rule = PrefixPrecedence,
description = "exit program with error message",
args = {
"message" : {String}
},
attrs = {
"code" : ({Integer},"return given exit code")
},
returns = {Boolean},
example = """
""":
##########################################################
vmPanic = true
vmError = x.s
showVMErrors()
if (let aCode = popAttr("code"); aCode != VNULL):
quit(aCode.i)
else:
quit()
# TODO add example
builtin "pause",
alias = unaliased,
rule = PrefixPrecedence,
description = "pause program's execution~for the given amount of milliseconds",
args = {
"time" : {Integer}
},
attrs = NoAttrs,
returns = {Nothing},
example = """
""":
##########################################################
sleep(x.i)
#=======================================
# Add Library
#=======================================
Libraries.add(defineSymbols)