Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <atomic>
3#include <chrono>
4#include <thread>
5#if defined(_MSC_VER)
6#include <vcruntime_startup.h>
7#endif
8
9#include "core/system.h"
10
11#if defined(_WIN32)
12#include <windows.h>
13#else
14#include <unistd.h>
15#endif
16
17namespace Phasor
18{
19
42
43Value StdLib::sys_time(const std::vector<Value> &args, VM *)
44{
45 checkArgCount(args, 0, "time");
46 auto now = std::chrono::steady_clock::now();
47 auto duration = now.time_since_epoch();
48 double millis = std::chrono::duration<double, std::milli>(duration).count();
49 return millis;
50}
51
52Value StdLib::sys_time_formatted(const std::vector<Value> &args, VM *)
53{
54 checkArgCount(args, 1, "timef");
55 std::string format = args[0].asString();
56
57 auto now = std::chrono::system_clock::now();
58 std::time_t t = std::chrono::system_clock::to_time_t(now);
59
60 std::tm tm{};
61#if defined(_WIN32)
62 localtime_s(&tm, &t);
63#else
64 localtime_r(&t, &tm);
65#endif
66
67 char buffer[256];
68 if (std::strftime(buffer, sizeof(buffer), format.c_str(), &tm) == 0)
69 {
70 return Value(" ");
71 }
72
73 return std::string(buffer);
74}
75
76Value StdLib::sys_sleep(const std::vector<Value> &args, VM *)
77{
78 checkArgCount(args, 1, "sleep");
79 int64_t ms = args[0].asInt();
80 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
81 return Value(" ");
82}
83
84Value StdLib::sys_clear(const std::vector<Value> &args, VM *vm)
85{
86 checkArgCount(args, 0, "clear");
87 return io_prints(std::vector<Value>{"\033[2J\033[H"}, vm);
88}
89
90Value StdLib::sys_os(const std::vector<Value> &args, VM *)
91{
92 checkArgCount(args, 0, "sys_os");
93#if defined(_WIN32)
94 return Value("win32");
95#elif defined(__linux__)
96 return Value("Linux");
97#elif defined(__APPLE__)
98 return Value("Darwin");
99#elif defined(__FreeBSD__)
100 return Value("FreeBSD");
101#elif defined(__unix__)
102 return Value("UNIX");
103#else
104 return Value("Unknown");
105#endif
106 return false;
107}
108
109Value StdLib::sys_env(const std::vector<Value> &args, VM *)
110{
111 checkArgCount(args, 1, "sys_env");
112 std::string key = args[0].asString();
113 std::string value;
114 dupenv(value, key.c_str(), envp);
115 return value;
116}
117
118Value StdLib::sys_argv(const std::vector<Value> &args, VM *)
119{
120 if (args.size() == 0)
121 {
122 auto l_argv = std::make_shared<Value::StructInstance>();
123
124 for (size_t i = 0; i < static_cast<size_t>(argc); i++)
125 {
126 l_argv->fields["arg" + std::to_string(i)] = Value(argv[i]);
127 }
128
129 return Value(l_argv);
130 }
131
132 checkArgCount(args, 1, "sys_argv");
133 int64_t index = args[0].asInt();
134 if (argv != nullptr)
135 return argv[index];
136 else
137 return Value();
138}
139
140Value StdLib::sys_argc(const std::vector<Value> &args, VM *)
141{
142 checkArgCount(args, 0, "sys_argc");
143 return argc;
144}
145
146Value StdLib::system_get_free_memory(const std::vector<Value> &args, VM *)
147{
148 checkArgCount(args, 0, "sys_get_memory");
149 return static_cast<int64_t>(getAvailableMemory());
150}
151
152Value StdLib::sys_wait_for_input(const std::vector<Value> &args, VM *vm)
153{
154 checkArgCount(args, 0, "wait_for_input");
155 io_gets({}, vm);
156 return Value("");
157}
158
159Value StdLib::sys_exec(const std::vector<Value> &args, VM *vm)
160{
161 checkArgCount(args, 1, "sys_execute");
162 vm->setRegister(VM::Register::r1, args[0]); // Load string into r1
164}
165
166Value StdLib::sys_exec_get_output(const std::vector<Value> &args, VM *vm)
167{
168 checkArgCount(args, 1, "sys_exec_output");
169 vm->setRegister(VM::Register::r1, args[0]); // Load string into r1
171}
172
173Value StdLib::sys_exec_get_error(const std::vector<Value> &args, VM *vm)
174{
175 checkArgCount(args, 1, "sys_exec_error");
176 vm->setRegister(VM::Register::r1, args[0]); // Load string into r1
178}
179
180Value StdLib::sys_crash(const std::vector<Value> &args, VM *vm)
181{
182 checkArgCount(args, 1, "error");
183 throw std::runtime_error(args[0].asString());
184 vm->reset();
185}
186
187Value StdLib::sys_reset(const std::vector<Value> &args, VM *vm)
188{
189 checkArgCount(args, 0, "reset");
190 vm->reset();
191 return Value();
192}
193
194Value StdLib::sys_shutdown(const std::vector<Value> &args, VM *vm)
195{
196 checkArgCount(args, 1, "shutdown");
197 vm->reset();
198 int ret = static_cast<int>(args[0].asInt());
199 std::exit(ret);
200 return Value(ret);
201}
202
203Value StdLib::sys_pid(const std::vector<Value> &args, VM *)
204{
205 checkArgCount(args, 0, "sys_pid");
206#if defined(_WIN32)
207 return static_cast<int64_t>(GetCurrentProcessId());
208#else
209 return static_cast<int64_t>(getpid());
210#endif
211}
212
213} // namespace Phasor
static Value sys_clear(const std::vector< Value > &args, VM *vm)
Clear the console.
Definition system.cpp:84
static Value sys_env(const std::vector< Value > &args, VM *vm)
Get the current environment variables.
Definition system.cpp:109
static Value sys_reset(const std::vector< Value > &args, VM *vm)
Reset the VM.
Definition system.cpp:187
static Value sys_exec_get_output(const std::vector< Value > &args, VM *vm)
Run a shell command and get output.
Definition system.cpp:166
static Value sys_exec_get_error(const std::vector< Value > &args, VM *vm)
Run a shell command and get error output.
Definition system.cpp:173
static char ** argv
Command line arguments.
Definition StdLib.hpp:30
static Value sys_time(const std::vector< Value > &args, VM *vm)
Current time.
Definition system.cpp:43
static Value sys_argc(const std::vector< Value > &args, VM *vm)
Get the current number of command line arguments.
Definition system.cpp:140
static Value sys_crash(const std::vector< Value > &args, VM *vm)
Crash the VM / Program.
Definition system.cpp:180
static Value io_prints(const std::vector< Value > &args, VM *vm)
Print string without newline.
Definition io.cpp:61
static Value system_get_free_memory(const std::vector< Value > &args, VM *vm)
Get current free memory.
Definition system.cpp:146
static Value sys_os(const std::vector< Value > &args, VM *vm)
Get the current OS.
Definition system.cpp:90
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:50
static Value sys_argv(const std::vector< Value > &args, VM *vm)
Get the current command line arguments.
Definition system.cpp:118
static int argc
Number of command line arguments.
Definition StdLib.hpp:31
static Value sys_wait_for_input(const std::vector< Value > &args, VM *vm)
Wait for input.
Definition system.cpp:152
static Value sys_shutdown(const std::vector< Value > &args, VM *vm)
Shutdown the VM.
Definition system.cpp:194
static Value registerSysFunctions(const std::vector< Value > &args, VM *vm)
Definition system.cpp:20
static Value sys_pid(const std::vector< Value > &args, VM *vm)
Get the current process ID.
Definition system.cpp:203
static Value sys_sleep(const std::vector< Value > &args, VM *vm)
Sleep for a specified amount of time.
Definition system.cpp:76
static char ** envp
Environment variables.
Definition StdLib.hpp:32
static Value sys_time_formatted(const std::vector< Value > &args, VM *vm)
Current time formatted.
Definition system.cpp:52
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
Definition io.cpp:97
static int dupenv(std::string &out, const char *name, char *const argp[])
Definition StdLib.cpp:21
static Value sys_exec(const std::vector< Value > &args, VM *vm)
Run a shell command.
Definition system.cpp:159
Virtual Machine.
Definition VM.hpp:18
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition VM.cpp:869
void setRegister(uint8_t index, const Value &value)
Set a register value.
Definition VM.cpp:960
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0, const int &operand4=0, const int &operand5=0)
Execute a single operation.
Definition VM.cpp:26
void reset(const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true)
Reset the virtual machine.
Definition VM.cpp:904
A value in the Phasor VM.
Definition Value.hpp:33
The Phasor Programming Language and Runtime.
Definition AST.hpp:8
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
Definition CodeGen.hpp:161
@ SYSTEM_ERR_R
Run shell command and get output: system_out(R[rA], R[rB]).
Definition CodeGen.hpp:163
size_t getAvailableMemory()
Definition system.c:3