Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Variables.cpp
Go to the documentation of this file.
1#ifndef CMAKE
2#include "VM.hpp"
3#endif
4
5namespace Phasor
6{
7
8size_t VM::addVariable(const Value &value)
9{
10#ifdef TRACING
11 log(std::format("VM::{}({:T})\n", __func__, value));
12 flush();
13#endif
14 variables.push_back(value);
15 return variables.size() - 1;
16}
17
18void VM::freeVariable(const size_t index)
19{
20#ifdef TRACING
21 log(std::format("VM::{}({})\n", __func__, index));
22 flush();
23#endif
24 if (index < variables.size())
25 {
26 variables[index] = Value();
27 }
28}
29
30void VM::setVariable(const size_t index, const Value &value)
31{
32#ifdef TRACING
33 log(std::format("VM::{}({}, {:T})\n", __func__, index, value));
34 flush();
35#endif
36 if (index >= variables.size())
37 {
38 throw std::runtime_error("Invalid variable index");
39 }
40 variables[index] = value;
41}
42
43Value VM::getVariable(const size_t index)
44{
45 if (index >= variables.size())
46 {
47#ifdef TRACING
48 log(std::format("VM::{}({}) -> <invalid index>\n", __func__, index));
49 flush();
50#endif
51 throw std::runtime_error("Invalid variable index");
52 }
53#ifdef TRACING
54 log(std::format("VM::{}({}) -> {:T}\n", __func__, index, variables[index]));
55 flush();
56#endif
57 return variables[index];
58}
59
61{
62#ifdef TRACING
63 log(std::format("VM::{}()\n", __func__));
64 flush();
65#endif
66 return variables.size();
67}
68
69} // namespace Phasor
std::vector< Value > variables
Variable storage indexed by variable index.
Definition VM.hpp:270
Value getVariable(size_t index)
Get a variable from the VM.
Definition Variables.cpp:43
size_t getVariableCount()
Get the number of variables in the VM.
Definition Variables.cpp:60
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:176
size_t addVariable(const Value &value)
Add a variable to the VM.
Definition Variables.cpp:8
void flush()
Flush stdout.
Definition Utility.cpp:188
void setVariable(size_t index, const Value &value)
Set a variable in the VM.
Definition Variables.cpp:30
void freeVariable(size_t index)
Free a variable in the VM.
Definition Variables.cpp:18
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11