Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
PhasorStdLib.hpp
Go to the documentation of this file.
1// Copyright 2026 Daniel McGuire
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5// http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software
7// distributed under the License is distributed on an "AS IS" BASIS,
8// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9// See the License for the specific language governing permissions and
10// limitations under the License.
11
12// README
13//
14// Usage:
15// ```cpp
16// #include "PhasorStdLib.hpp"
17// // later
18// Phasor::StdLib::registerFunctions(vm);
19// ```
20
21#pragma once
22
23#include <cmath>
24#include <cstdlib>
25#include <cstring>
26
27#include <functional>
28#include <map>
29#include <string>
30#include <vector>
31#include <algorithm>
32#include <fstream>
33#include <sstream>
34#include <stdexcept>
35
36#include "PhasorVM.hpp"
37#include "../Value.hpp"
38
40namespace Phasor
41{
42
44using NativeFunction = std::function<Value(const std::vector<Value> &args, VM *vm)>;
45
52class StdLib
53{
54 public:
55 inline static void registerFunctions(VM &vm)
56 {
57#ifdef TRACING
58 vm.log(std::format("StdLib::{}(&VM@{:#x})\n", __func__, reinterpret_cast<std::uintptr_t>(&vm)));
59 vm.flush();
60#endif
62#ifndef SANDBOXED
64#endif
65 }
66
67 static char **argv;
68 static int argc;
69
70 static void checkArgCount(const std::vector<Value> &args, size_t minimumArguments, const std::string &name,
71 bool allowMoreArguments = false);
72
73 private:
74 static bool std_import(const std::vector<Value> &args, VM *vm);
75#ifndef SANDBOXED
76 static Value std_assert(const std::vector<Value> &args, VM *vm);
77#endif
78
79 enum class dupenv_ret {
83 };
84
85 static dupenv_ret dupenv(std::string &out, const char *name);
86
87 static void registerMetaFunctions(VM *vm);
88 static void registerMemoryFunctions(VM *vm);
89 static void registerMathFunctions(VM *vm);
90 static void registerRandomFunctions(VM *vm);
91 static void registerStringFunctions(VM *vm);
92 static void registerTypeConvFunctions(VM *vm);
93#ifndef SANDBOXED
94 static void registerFileFunctions(VM *vm);
95#endif
96 static void registerSysFunctions(VM *vm);
97 static void registerIOFunctions(VM *vm);
98
99#pragma region stdmeta
100#ifndef SANDBOXED
101 static int64_t meta_operation(const std::vector<Value> &args, VM *vm);
102 static Value meta_stack_run(const std::vector<Value> &args, VM *vm);
103#endif
104 static std::string meta_get_version(const std::vector<Value> &args, VM *vm);
105
106#pragma endregion stdmeta
107
108#pragma region stdmemory
109 static Value var_free(const std::vector<Value> &args, VM *vm);
110#pragma endregion
111
112#pragma region stdmath
113 static double math_sqrt(const std::vector<Value> &args, VM *vm);
114 static double math_pow(const std::vector<Value> &args, VM *vm);
115 static Value math_abs(const std::vector<Value> &args, VM *vm);
116 static double math_floor(const std::vector<Value> &args, VM *vm);
117 static double math_ceil(const std::vector<Value> &args, VM *vm);
118 static double math_round(const std::vector<Value> &args, VM *vm);
119 static Value math_min(const std::vector<Value> &args, VM *vm);
120 static Value math_max(const std::vector<Value> &args, VM *vm);
121 static double math_log(const std::vector<Value> &args, VM *vm);
122 static double math_exp(const std::vector<Value> &args, VM *vm);
123 static double math_sin(const std::vector<Value> &args, VM *vm);
124 static double math_cos(const std::vector<Value> &args, VM *vm);
125 static double math_tan(const std::vector<Value> &args, VM *vm);
126#pragma endregion
127
128#pragma region stdfile
129#ifndef SANDBOXED
130 static std::string file_absolute(const std::vector<Value> &args, VM *vm);
131 static Value file_read(const std::vector<Value> &args, VM *vm);
132 static bool file_write(const std::vector<Value> &args, VM *vm);
133 static bool file_exists(const std::vector<Value> &args, VM *vm);
134 static std::string file_read_line(const std::vector<Value> &args, VM *vm);
135 static bool file_write_line(const std::vector<Value> &args, VM *vm);
136 static bool file_append(const std::vector<Value> &args, VM *vm);
137 static bool file_delete(const std::vector<Value> &args, VM *vm);
138 static bool file_rename(const std::vector<Value> &args, VM *vm);
139 static Value file_current_directory(const std::vector<Value> &args, VM *vm);
140 static bool file_copy(const std::vector<Value> &args, VM *vm);
141 static bool file_move(const std::vector<Value> &args, VM *vm);
142 static bool file_property_edit(const std::vector<Value> &args, VM *vm);
143 static int64_t file_property_get(const std::vector<Value> &args, VM *vm);
144 static bool file_create(const std::vector<Value> &args, VM *vm);
145 static Value file_read_directory(const std::vector<Value> &args, VM *vm);
146 static bool file_create_directory(const std::vector<Value> &args, VM *vm);
147 static bool file_remove_directory(const std::vector<Value> &args, VM *vm);
148 static std::string file_join_path(const std::vector<Value> &args, VM *vm);
149 static std::string file_stem(const std::vector<Value> &args, VM *vm);
150 static std::string file_filename(const std::vector<Value> &args, VM *vm);
151 static std::string file_extension(const std::vector<Value> &args, VM *vm);
152 static bool file_is_directory(const std::vector<Value> &args, VM *vm);
153 static std::string file_parent(const std::vector<Value> &args, VM *vm);
154 static int64_t file_get_size(const std::vector<Value> &args, VM *vm);
155#pragma endregion
156
157#pragma region stdsys
158 static int64_t sys_get_free_memory(const std::vector<Value> &args, VM *vm);
159 static Value sys_wait_for_input(const std::vector<Value> &args, VM *vm);
160 static Value sys_shell(const std::vector<Value> &args, VM *vm);
161 static int64_t sys_fork(const std::vector<Value> &args, VM *vm);
162 static int64_t sys_fork_detached(const std::vector<Value> &args, VM *vm);
163 static Value sys_crash(const std::vector<Value> &args, VM *vm);
164 static Value sys_reset(const std::vector<Value> &args, VM *vm);
165 static int64_t sys_pid(const std::vector<Value> &args, VM *vm);
166 static std::string sys_os(const std::vector<Value> &args, VM *vm);
167 static Value sys_isatty(const std::vector<Value> &args, VM *vm);
168#endif
169 static Value sys_env(const std::vector<Value> &args, VM *vm);
170 static Value sys_argv(const std::vector<Value> &args, VM *vm);
171 static int64_t sys_argc(const std::vector<Value> &args, VM *vm);
172 static double sys_time(const std::vector<Value> &args, VM *vm);
173 static Value sys_time_formatted(const std::vector<Value> &args, VM *vm);
174 static Value sys_sleep(const std::vector<Value> &args, VM *vm);
175 static Value sys_shutdown(const std::vector<Value> &args, VM *vm);
176#pragma endregion
177
178#pragma region stdtype
179 static int64_t to_int(const std::vector<Value> &args, VM *vm);
180 static double to_float(const std::vector<Value> &args, VM *vm);
181 static std::string to_string(const std::vector<Value> &args, VM *vm);
182 static bool to_bool(const std::vector<Value> &args, VM *vm);
183#pragma endregion
184
185#pragma region stdrand
186
187 static Value rand_seed(const std::vector<Value> &args, VM *vm);
188 static int64_t rand_next_range(const std::vector<Value> &args, VM *vm);
189 static double rand_next_float(const std::vector<Value> &args,
190 VM *vm);
191
192#pragma endregion
193
194#pragma region stdstr
195 static int64_t str_find(const std::vector<Value> &args, VM *vm);
196 static int64_t str_len(const std::vector<Value> &args, VM *vm);
197 static Value str_char_at(const std::vector<Value> &args, VM *vm);
198 static Value str_substr(const std::vector<Value> &args, VM *vm);
199 static std::string str_concat(const std::vector<Value> &args, VM *vm);
200 static std::string str_upper(const std::vector<Value> &args, VM *vm);
201 static std::string str_lower(const std::vector<Value> &args, VM *vm);
202 static Value str_starts_with(const std::vector<Value> &args, VM *vm);
203 static Value str_ends_with(const std::vector<Value> &args, VM *vm);
204 // StringBuilder functions
205 static int64_t sb_new(const std::vector<Value> &args, VM *vm);
206 static Value sb_append(const std::vector<Value> &args, VM *vm);
207 static std::string sb_to_string(const std::vector<Value> &args, VM *vm);
208 static Value sb_clear(const std::vector<Value> &args, VM *vm);
209 static std::string sb_free(const std::vector<Value> &args, VM *vm);
210#pragma endregion
211
212#pragma region stdio
213 static std::string io_c_format(const std::vector<Value> &args, VM *vm);
214#ifndef SANDBOXED
215 static Value io_clear(const std::vector<Value> &args, VM *vm);
216#endif
217 static std::string io_prints(const std::vector<Value> &args, VM *vm);
218 static std::string io_printf(const std::vector<Value> &args, VM *vm);
219 static std::string io_puts(const std::vector<Value> &args, VM *vm);
220 static std::string io_putf(const std::vector<Value> &args, VM *vm);
221#ifndef SANDBOXED
222 static Value io_gets(const std::vector<Value> &args, VM *vm);
223#endif
224 static std::string io_putf_error(const std::vector<Value> &args,
225 VM *vm);
226 static std::string io_puts_error(const std::vector<Value> &args,
227 VM *vm);
228#pragma endregion
229};
230
231} // namespace Phasor
232
Phasor Standard library.
Definition StdLib.hpp:33
static bool file_create_directory(const std::vector< Value > &args, VM *vm)
static int64_t file_get_size(const std::vector< Value > &args, VM *vm)
static void registerIOFunctions(VM *vm)
static double sys_time(const std::vector< Value > &args, VM *vm)
Current time.
static std::string sb_free(const std::vector< Value > &args, VM *vm)
Free string builder.
static Value rand_seed(const std::vector< Value > &args, VM *vm)
Seed the random number generator.
static Value sys_sleep(const std::vector< Value > &args, VM *vm)
Sleep for a specified amount of time.
static void registerStringFunctions(VM *vm)
static Value sys_argv(const std::vector< Value > &args, VM *vm)
Get the current command line arguments.
static Value sys_crash(const std::vector< Value > &args, VM *vm)
Crash the VM / Program.
static Value math_max(const std::vector< Value > &args, VM *vm)
Maximum.
static int64_t str_find(const std::vector< Value > &args, VM *vm)
Find string in string.
static Value sb_append(const std::vector< Value > &args, VM *vm)
Append to string builder.
static Value math_min(const std::vector< Value > &args, VM *vm)
Minimum.
static char ** argv
Command line arguments.
Definition StdLib.hpp:47
static double math_exp(const std::vector< Value > &args, VM *vm)
Exponential.
static bool file_is_directory(const std::vector< Value > &args, VM *vm)
Check if path is directory.
static Value io_gets(const std::vector< Value > &args, VM *vm)
Get string.
static std::string file_filename(const std::vector< Value > &args, VM *vm)
Get the filename.
static Value str_starts_with(const std::vector< Value > &args, VM *vm)
Check if string starts with.
static double math_round(const std::vector< Value > &args, VM *vm)
Round.
static double math_ceil(const std::vector< Value > &args, VM *vm)
Ceiling.
static void registerSysFunctions(VM *vm)
static std::string file_read_line(const std::vector< Value > &args, VM *vm)
Read a line from file.
static int64_t sys_get_free_memory(const std::vector< Value > &args, VM *vm)
Get current free memory.
static double math_log(const std::vector< Value > &args, VM *vm)
Natural logarithm.
static double math_sqrt(const std::vector< Value > &args, VM *vm)
Square root.
static int64_t meta_operation(const std::vector< Value > &args, VM *vm)
static Value sys_time_formatted(const std::vector< Value > &args, VM *vm)
Current time formatted.
static bool file_delete(const std::vector< Value > &args, VM *vm)
Delete file.
static std::string to_string(const std::vector< Value > &args, VM *vm)
Convert to string.
static bool file_copy(const std::vector< Value > &args, VM *vm)
Copy file.
static Value sys_wait_for_input(const std::vector< Value > &args, VM *vm)
Wait for input.
static std::string io_prints(const std::vector< Value > &args, VM *vm)
Print string without newline.
static std::string file_absolute(const std::vector< Value > &args, VM *vm)
Get full path to relative path.
static std::string io_c_format(const std::vector< Value > &args, VM *vm)
Format string.
static void registerFunctions(VM &vm)
static double math_sin(const std::vector< Value > &args, VM *vm)
Sine.
static Value sys_reset(const std::vector< Value > &args, VM *vm)
Reset the VM.
static std::string sb_to_string(const std::vector< Value > &args, VM *vm)
Convert string builder to string.
static int64_t sys_pid(const std::vector< Value > &args, VM *vm)
Get the current process ID.
static Value math_abs(const std::vector< Value > &args, VM *vm)
Absolute value.
static Value sys_shutdown(const std::vector< Value > &args, VM *vm)
Shutdown the VM.
static Value io_clear(const std::vector< Value > &args, VM *vm)
Clear the console.
static bool file_write_line(const std::vector< Value > &args, VM *vm)
Write a line to file.
static std::string io_putf(const std::vector< Value > &args, VM *vm)
Print formatted string with newline.
static int argc
Number of command line arguments.
Definition StdLib.hpp:48
static std::string io_printf(const std::vector< Value > &args, VM *vm)
Print formatted string.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static Value file_read_directory(const std::vector< Value > &args, VM *vm)
static int64_t str_len(const std::vector< Value > &args, VM *vm)
Get string length.
static Value sb_clear(const std::vector< Value > &args, VM *vm)
Clear string builder.
static dupenv_ret dupenv(std::string &out, const char *name)
static bool file_property_edit(const std::vector< Value > &args, VM *vm)
static std::string file_parent(const std::vector< Value > &args, VM *vm)
Get the parent of a path.
static Value std_assert(const std::vector< Value > &args, VM *vm)
static std::string str_lower(const std::vector< Value > &args, VM *vm)
Convert to lowercase.
static int64_t sys_argc(const std::vector< Value > &args, VM *vm)
Get the current number of command line arguments.
static std::string io_puts(const std::vector< Value > &args, VM *vm)
Print string with newline.
static bool file_create(const std::vector< Value > &args, VM *vm)
static bool file_move(const std::vector< Value > &args, VM *vm)
Move file.
static int64_t rand_next_range(const std::vector< Value > &args, VM *vm)
Get a random number in range.
static double math_pow(const std::vector< Value > &args, VM *vm)
Power.
static int64_t to_int(const std::vector< Value > &args, VM *vm)
Convert to integer.
static bool to_bool(const std::vector< Value > &args, VM *vm)
Convert to boolean.
static int64_t sys_fork_detached(const std::vector< Value > &args, VM *vm)
Run a native program detached.
static bool std_import(const std::vector< Value > &args, VM *vm)
static void registerMetaFunctions(VM *vm)
static void registerFileFunctions(VM *vm)
static void registerTypeConvFunctions(VM *vm)
static bool file_rename(const std::vector< Value > &args, VM *vm)
Rename file.
static std::string sys_os(const std::vector< Value > &args, VM *vm)
Get the current OS.
static Value str_substr(const std::vector< Value > &args, VM *vm)
Get substring.
static int64_t sys_fork(const std::vector< Value > &args, VM *vm)
Run a native program.
static void registerMathFunctions(VM *vm)
static bool file_write(const std::vector< Value > &args, VM *vm)
Write to file.
static std::string io_puts_error(const std::vector< Value > &args, VM *vm)
Print string with newline to error output.
static Value var_free(const std::vector< Value > &args, VM *vm)
Free a variable.
static bool file_append(const std::vector< Value > &args, VM *vm)
Append to file.
static Value file_current_directory(const std::vector< Value > &args, VM *vm)
Get/set working directory.
static std::string io_putf_error(const std::vector< Value > &args, VM *vm)
Print formatted string with newline to error output.
static bool file_remove_directory(const std::vector< Value > &args, VM *vm)
static std::string file_extension(const std::vector< Value > &args, VM *vm)
Get the extension of a path.
static double to_float(const std::vector< Value > &args, VM *vm)
Convert to float.
static Value file_read(const std::vector< Value > &args, VM *vm)
Read file.
static int64_t sb_new(const std::vector< Value > &args, VM *vm)
Create new string builder.
static std::string str_upper(const std::vector< Value > &args, VM *vm)
Convert to uppercase.
static Value std_assert(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:107
static Value meta_stack_run(const std::vector< Value > &args, VM *vm)
static std::string str_concat(const std::vector< Value > &args, VM *vm)
Concatenate strings.
static Value str_ends_with(const std::vector< Value > &args, VM *vm)
Check if string ends with.
static double math_tan(const std::vector< Value > &args, VM *vm)
Tangent.
static void registerMemoryFunctions(VM *vm)
static Value str_char_at(const std::vector< Value > &args, VM *vm)
Get character at index.
static std::string meta_get_version(const std::vector< Value > &args, VM *vm)
static int64_t file_property_get(const std::vector< Value > &args, VM *vm)
static double math_cos(const std::vector< Value > &args, VM *vm)
Cosine.
static void registerRandomFunctions(VM *vm)
static Value sys_shell(const std::vector< Value > &args, VM *vm)
Run a shell command.
static Value sys_env(const std::vector< Value > &args, VM *vm)
Get the current environment variables.
static std::string file_join_path(const std::vector< Value > &args, VM *vm)
static bool file_exists(const std::vector< Value > &args, VM *vm)
Check if file exists.
static Value sys_isatty(const std::vector< Value > &args, VM *vm)
Check if the current output is a terminal.
static double math_floor(const std::vector< Value > &args, VM *vm)
Floor.
static bool std_import(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:59
static double rand_next_float(const std::vector< Value > &args, VM *vm)
Get a random float (technically a double at a low level).
static std::string file_stem(const std::vector< Value > &args, VM *vm)
Get the stem of a path.
Virtual Machine.
Definition VM.hpp:33
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:269
void flush()
Flush stdout.
Definition Utility.cpp:281
A value in the Phasor VM.
Definition Value.hpp:58
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Definition StdLib.hpp:24