Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
meta.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <version.h>
3
4namespace Phasor
5{
6
8{
9#ifndef SANDBOXED
12#endif
14}
15
16#ifndef SANDBOXED
17int64_t StdLib::meta_operation(const std::vector<Value> &args, VM *vm)
18{
19 checkArgCount(args, 1, "phs_op");
20 if (args.size() > 4)
21 throw std::runtime_error("Function 'phs_op' expects at most 4 arguments, but got " +
22 std::to_string(args.size()));
23 if (!args[0].isInt())
24 throw std::runtime_error("Function 'phs_op' expects an OpCode (int) as the first argument");
25
26 auto ret = vm->operation(static_cast<Phasor::OpCode>(args[0].asInt()),
27 args.size() > 1 ? static_cast<int>(args[1].asInt()) : 0,
28 args.size() > 2 ? static_cast<int>(args[2].asInt()) : 0,
29 args.size() > 3 ? static_cast<int>(args[3].asInt()) : 0);
30 return ret.asInt();
31}
32
33Value StdLib::meta_stack_run(const std::vector<Value> &args, VM *vm)
34{
35 checkArgCount(args, 1, "phs_stack_run");
36 if (!args[0].isInt())
37 throw std::runtime_error("Function 'phs_stack_run' expects an OpCode (int) as the first argument");
38 auto opcode = static_cast<Phasor::OpCode>(args[0].asInt());
39
40 for (size_t i = args.size(); i-- > 1;)
41 vm->push(args[i]);
42
43 vm->operation(opcode);
44 return vm->pop();
45}
46#endif
47
48std::string StdLib::meta_get_version(const std::vector<Value> &args, VM *)
49{
50 checkArgCount(args, 0, "phs_version");
51 return PHASOR_VERSION_STRING;
52}
53
54} // namespace Phasor
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static Value meta_stack_run(const std::vector< Value > &args, VM *vm)
Definition meta.cpp:33
static void registerMetaFunctions(VM *vm)
Definition meta.cpp:7
static std::string meta_get_version(const std::vector< Value > &args, VM *vm)
Definition meta.cpp:48
static int64_t meta_operation(const std::vector< Value > &args, VM *vm)
Definition meta.cpp:17
Virtual Machine.
Definition VM.hpp:33
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0)
Execute a single operation.
Definition Operations.cpp:8
Value pop()
Pop a value from the stack.
Definition Stack.cpp:17
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
void push(const Value &value)
Push a value onto the stack.
Definition Stack.cpp:8
A value in the Phasor VM.
Definition Value.hpp:58
int64_t asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:159
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
OpCode
Definition ISA.hpp:12