Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
StdLib.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <cassert>
3#ifdef _WIN32
4#include <stdlib.h>
5#else
6#include <cstdlib>
7#endif
8
9namespace Phasor
10{
11
12char **StdLib::argv = nullptr;
13int StdLib::argc = 0;
14
15StdLib::dupenv_ret StdLib::dupenv(std::string &out, const char *name)
16{
17 if (!name || name[0] == '\0')
18 {
20 }
21
22#ifdef _WIN32
23 char *buffer = nullptr;
24 size_t len = 0;
25 if (_dupenv_s(&buffer, &len, name) == 0 && buffer != nullptr)
26 {
27 out = buffer;
28 free(buffer);
30 }
31#else
32 const char *val = std::getenv(name);
33 if (val)
34 {
35 out = val;
37 }
38#endif
39
40 out.clear();
42}
43
44void StdLib::checkArgCount(const std::vector<Value> &args, size_t minimumArguments, const std::string &name,
45 bool allowMoreArguments)
46{
47 if (args.size() < minimumArguments)
48 {
49 throw std::runtime_error("Function '" + name + "' expects at least " + std::to_string(minimumArguments) +
50 " arguments, but got " + std::to_string(args.size()));
51 }
52 if (!allowMoreArguments && args.size() > minimumArguments)
53 {
54 throw std::runtime_error("Function '" + name + "' expects exactly " + std::to_string(minimumArguments) +
55 " arguments, but got " + std::to_string(args.size()));
56 }
57}
58
59bool StdLib::std_import(const std::vector<Value> &args, VM *vm)
60{
61 checkArgCount(args, 1, "using", true);
62
63 std::unordered_map<std::string, std::function<void(Phasor::VM *)>> modules{
64 {"stdio", registerIOFunctions},
65 {"stdsys", registerSysFunctions},
66 {"stdmath", registerMathFunctions},
67 {"stdstr", registerStringFunctions},
68 {"stdtype", registerTypeConvFunctions},
69 {"stdmeta", registerMetaFunctions},
70 {"stdmem", registerMemoryFunctions},
71 {"stdrand", registerRandomFunctions},
72#ifndef SANDBOXED
73 {"stdfile", registerFileFunctions},
74#endif
75 {"std*",
76 [](Phasor::VM *vm) {
85#ifndef SANDBOXED
87#endif
88 }},
89 };
90
91 for (const auto &arg : args)
92 {
93 auto it = modules.find(arg.asString());
94 if (it != modules.end())
95 {
96 it->second(vm);
97 }
98 else
99 {
100 throw std::runtime_error("Unknown module: " + arg.asString());
101 }
102 }
103 return true;
104}
105
106#ifndef SANDBOXED
107Value StdLib::std_assert(const std::vector<Value> &args, VM *vm)
108{
109 checkArgCount(args, 1, "assert");
110
111#ifdef TRACING
112#ifndef NDEBUG
113 vm->log(std::format("StdLib::{}({:T})\n", __func__, args[0]));
114#else
115 vm->log(std::format("StdLib::{}({:T}): Assertion skipped (NDEBUG)\n", __func__, args[0]));
116#endif
117 vm->flush();
118#endif
119
120#ifndef NDEBUG
121 if (!args[0].isTruthy())
122 {
123 vm->logerr(std::format("StdLib::{}({:T}): Assertion failed!\n", __func__, args[0]));
124 vm->flusherr();
125 }
126 assert(args[0].isTruthy());
127#endif
128 return Value();
129}
130#endif
131
132} // namespace Phasor
static void registerStringFunctions(VM *vm)
Definition string.cpp:7
static void registerMemoryFunctions(VM *vm)
Definition memory.cpp:6
static char ** argv
Command line arguments.
Definition StdLib.hpp:47
static void registerFileFunctions(VM *vm)
Definition file.cpp:10
static void registerIOFunctions(VM *vm)
Definition io.cpp:13
static void registerTypeConvFunctions(VM *vm)
Definition typeconv.cpp:6
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static void registerMathFunctions(VM *vm)
Definition math.cpp:6
static int argc
Number of command line arguments.
Definition StdLib.hpp:48
static void registerSysFunctions(VM *vm)
Definition system.cpp:27
static void registerRandomFunctions(VM *vm)
Definition random.cpp:7
static void registerMetaFunctions(VM *vm)
Definition meta.cpp:7
static dupenv_ret dupenv(std::string &out, const char *name)
Definition StdLib.cpp:15
static Value std_assert(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:107
static bool std_import(const std::vector< Value > &args, VM *vm)
Definition StdLib.cpp:59
Virtual Machine.
Definition VM.hpp:33
void logerr(const Value &msg)
Log a Value to stderr.
Definition Utility.cpp:275
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:269
void flush()
Flush stdout.
Definition Utility.cpp:281
void flusherr()
Flush stderr.
Definition Utility.cpp:286
A value in the Phasor VM.
Definition Value.hpp:58
The Phasor Programming Language and Runtime.
Definition AST.hpp:12