Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
StdLib.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2
3namespace Phasor
4{
5
6char **StdLib::argv = nullptr;
7int StdLib::argc = 0;
8char **StdLib::envp = nullptr;
9
20
21int StdLib::dupenv(std::string &out, const char *name, char *const argp[])
22{
23 if (!name || !argp)
24 {
25 return 1;
26 }
27
28 const size_t key_len = strlen(name);
29
30 const char *val = NULL;
31 for (size_t i = 0; argp[i]; i++)
32 {
33 const char *entry = argp[i];
34 if (strncmp(entry, name, key_len) == 0 && entry[key_len] == '=')
35 {
36 val = entry + key_len + 1;
37 break;
38 }
39 }
40 if (!val)
41 {
42 out.clear();
43 return 2;
44 }
45
46 out = std::string(val);
47 return 0;
48}
49
50void StdLib::checkArgCount(const std::vector<Value> &args, size_t minimumArguments, const std::string &name,
51 bool allowMoreArguments)
52{
53 if (args.size() < minimumArguments)
54 {
55 throw std::runtime_error("Function '" + name + "' expects at least " + std::to_string(minimumArguments) +
56 " arguments, but got " + std::to_string(args.size()));
57 }
58 if (!allowMoreArguments && args.size() > minimumArguments)
59 {
60 throw std::runtime_error("Function '" + name + "' expects exactly " + std::to_string(minimumArguments) +
61 " arguments, but got " + std::to_string(args.size()));
62 }
63}
64
65} // namespace Phasor
static Value registerIOFunctions(const std::vector< Value > &args, VM *vm)
Definition io.cpp:6
static void registerFunctions(VM &vm)
Register all standard library functions.
Definition StdLib.cpp:10
static char ** argv
Command line arguments.
Definition StdLib.hpp:30
static Value registerMathFunctions(const std::vector< Value > &args, VM *vm)
Definition math.cpp:7
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:50
static int argc
Number of command line arguments.
Definition StdLib.hpp:31
static Value registerTypeConvFunctions(const std::vector< Value > &args, VM *vm)
Definition typeconv.cpp:6
static Value registerSysFunctions(const std::vector< Value > &args, VM *vm)
Definition system.cpp:20
static char ** envp
Environment variables.
Definition StdLib.hpp:32
static Value registerStringFunctions(const std::vector< Value > &args, VM *vm)
Definition string.cpp:7
static int dupenv(std::string &out, const char *name, char *const argp[])
Definition StdLib.cpp:21
static Value registerRegexFunctions(const std::vector< Value > &args, VM *vm)
Definition regex.cpp:8
static Value registerFileFunctions(const std::vector< Value > &args, VM *vm)
Definition file.cpp:10
Virtual Machine.
Definition VM.hpp:18
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition VM.cpp:869
The Phasor Programming Language and Runtime.
Definition AST.hpp:8