Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Stack.cpp
Go to the documentation of this file.
1#ifndef CMAKE
2#include "VM.hpp"
3#endif
4
5namespace Phasor
6{
7
8void VM::push(const Value &value)
9{
10#ifdef TRACING_STACK
11 log(std::format("VM::{}({:T})\n", __func__, value));
12 flush();
13#endif
14 stack.push_back(value);
15}
16
18{
19 if (stack.empty())
20 {
21#ifdef TRACING_STACK
22 log(std::format("VM::{}() -> <empty stack>\n", __func__));
23 flush();
24#endif
25 std::string msg = "Stack underflow at pc=" + std::to_string(pc);
26 throw std::runtime_error(msg);
27 return Value();
28 }
29#ifdef TRACING_STACK
30 log(std::format("VM::{}() -> {:T}\n", __func__, stack.back()));
31 flush();
32#endif
33 Value value = stack.back();
34 stack.pop_back();
35 return value;
36}
37
39{
40 if (stack.empty())
41 {
42#ifdef TRACING_STACK
43 log(std::format("VM::{}() -> <empty stack>\n", __func__));
44 flush();
45#endif
46 std::string msg = "Stack is empty at pc=" + std::to_string(pc);
47 throw std::runtime_error(msg);
48 return Value();
49 }
50#ifdef TRACING_STACK
51 log(std::format("VM::{}() -> {:T}\n", __func__, stack.back()));
52 flush();
53#endif
54 return stack.back();
55}
56
57} // namespace Phasor
Value pop()
Pop a value from the stack.
Definition Stack.cpp:17
std::vector< Value > stack
Stack for function calls.
Definition VM.hpp:264
void log(const Value &msg)
Log a Value to stdout.
Definition Utility.cpp:176
void flush()
Flush stdout.
Definition Utility.cpp:188
size_t pc
Program counter.
Definition VM.hpp:276
void push(const Value &value)
Push a value onto the stack.
Definition Stack.cpp:8
Value peek()
Peek at the top value on the stack.
Definition Stack.cpp:38
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11