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#include <phsint.hpp>
4
5namespace Phasor
6{
7
9{
10#ifndef SANDBOXED
13#endif
15}
16
17#ifndef SANDBOXED
18i64 StdLib::meta_operation(const std::vector<Value> &args, VM *vm)
19{
20 checkArgCount(args, 1, "phs_op");
21 if (args.size() > 4)
22 throw std::runtime_error("Function 'phs_op' expects at most 4 arguments, but got " +
23 std::to_string(args.size()));
24 if (!args[0].isInt())
25 throw std::runtime_error("Function 'phs_op' expects an OpCode (int) as the first argument");
26
27 auto ret = vm->operation(static_cast<Phasor::OpCode>(args[0].asInt()),
28 args.size() > 1 ? static_cast<int>(args[1].asInt()) : 0,
29 args.size() > 2 ? static_cast<int>(args[2].asInt()) : 0,
30 args.size() > 3 ? static_cast<int>(args[3].asInt()) : 0);
31 return ret.asInt();
32}
33
34Value StdLib::meta_stack_run(const std::vector<Value> &args, VM *vm)
35{
36 checkArgCount(args, 1, "phs_stack_run");
37 if (!args[0].isInt())
38 throw std::runtime_error("Function 'phs_stack_run' expects an OpCode (int) as the first argument");
39 auto opcode = static_cast<Phasor::OpCode>(args[0].asInt());
40
41 for (size_t i = args.size(); i-- > 1;)
42 vm->push(args[i]);
43
44 vm->operation(opcode);
45 return vm->pop();
46}
47#endif
48
49PhsString StdLib::meta_get_version(const std::vector<Value> &args, VM *)
50{
51 checkArgCount(args, 0, "phs_version");
52 return PHASOR_VERSION_STRING;
53}
54
55} // namespace Phasor
static i64 meta_operation(const std::vector< Value > &args, VM *vm)
Definition meta.cpp:18
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:34
static void registerMetaFunctions(VM *vm)
Definition meta.cpp:8
static PhsString meta_get_version(const std::vector< Value > &args, VM *vm)
Definition meta.cpp:49
Virtual Machine.
Definition VM.hpp:36
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0)
Execute a single operation.
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:59
i64 asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:164
The Phasor Programming Language and Runtime.
Definition AST.hpp:13
int64_t i64
Definition phsint.hpp:16
OpCode
Definition ISA.hpp:12