20 const size_t key_len = strlen(name);
22 const char *val = NULL;
23 for (
size_t i = 0; argp[i]; i++)
25 const char *entry = argp[i];
26 if (strncmp(entry, name, key_len) == 0 && entry[key_len] ==
'=')
28 val = entry + key_len + 1;
38 out = std::string(val);
45 bool allowMoreArguments)
47 if (args.size() < minimumArguments)
49 throw std::runtime_error(
"Function '" + name +
"' expects at least " + std::to_string(minimumArguments) +
50 " arguments, but got " + std::to_string(args.size()));
52 if (!allowMoreArguments && args.size() > minimumArguments)
54 throw std::runtime_error(
"Function '" + name +
"' expects exactly " + std::to_string(minimumArguments) +
55 " arguments, but got " + std::to_string(args.size()));
65 for (
const auto &arg : args)
69 throw std::runtime_error(
"All arguments to 'using' must be strings");
72 auto moduleName = arg.asString();
74 if (moduleName ==
"stdio") [[likely]]
76 else if (moduleName ==
"stdsys") [[likely]]
78 else if (moduleName ==
"stdmath")
80 else if (moduleName ==
"stdstr")
82 else if (moduleName ==
"stdtype")
85 else if (moduleName ==
"stdfile")
88 else if (moduleName ==
"std*") [[unlikely]]
101 throw std::runtime_error(
"Unknown standard library module: " + moduleName);
116 vm->
log(std::format(
"StdLib::{}({:T})\n", __func__, args[0]));
118 vm->
log(std::format(
"StdLib::{}({:T}): Assertion skipped (NDEBUG)\n", __func__, args[0]));
124 if (!args[0].isTruthy())
126 vm->
logerr(std::format(
"StdLib::{}({:T}): Assertion failed!\n", __func__, args[0]));
129 assert(args[0].isTruthy());
static void registerStringFunctions(VM *vm)
static void registerFileFunctions(VM *vm)
static char ** argv
Command line arguments.
static void registerIOFunctions(VM *vm)
static void registerTypeConvFunctions(VM *vm)
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static void registerMathFunctions(VM *vm)
static int argc
Number of command line arguments.
static Value std_import(const std::vector< Value > &args, VM *vm)
static void registerSysFunctions(VM *vm)
static Value std_assert(const std::vector< Value > &args, VM *vm)
static char ** envp
Environment variables.
static int dupenv(std::string &out, const char *name, char *const argp[])
void logerr(const Value &msg)
Log a Value to stderr.
void log(const Value &msg)
Log a Value to stdout.
void flush()
Flush stdout.
void flusherr()
Flush stderr.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.