Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Shell.phs
Go to the documentation of this file.
1// Simple Shell
2// (C) 2026 Daniel McGuire
3// This file is licensed under the MIT License
4
5include_stdio();
6include_stdstr();
7include_stdsys();
8include_stdfile();
9
10puts("Phasor Shell\n(C) 2026 Daniel McGuire\n");
11
12fn trim(s: string) -> string {
13 var out = "";
14 var i = 0;
15 var n = len(s);
16
17 while (i < n && substr(s, i, 1) == " ") {
18 i = i + 1;
19 }
20
21 while (i < n) {
22 out = concat(out, substr(s, i, 1));
23 i = i + 1;
24 }
25
26 var end = len(out) - 1;
27 while (end >= 0 && substr(out, end, 1) == " ") {
28 end = end - 1;
29 }
30
31 if (end < 0) return "";
32 return substr(out, 0, end + 1);
33}
34
35fn starts_with(input: string, prefix: string) -> bool {
36 if (len(input) < len(prefix)) return false;
37 return substr(input, 0, len(prefix)) == prefix;
38}
39
40fn main() {
41 while (true) {
42 printf("%s$ ", fcd());
43
44 var line = gets();
45
46 line = trim(line);
47 if (line.len() == 0) {
48 continue;
49 }
50
51 if (starts_with(line, "exit")) {
52 line = null;
53 return 0;
54 }
55
56 if (line == null) {
57 line = "";
58 continue;
59 }
60
61 sys_execute(line);
62 }
63 return 0;
64}
65
66if (!main()) {
67 puts_error("Unknown error");
68}