4
Copyright (c) 2022 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/variable/VariableStack.h"
10
#include "simodo/bormental/DrBormental.h"
11
#include "simodo/inout/format/fmt.h"
13
namespace simodo::variable
15
VariableStack::VariableStack(size_t stack_size)
17
_stack.reserve(stack_size);
20
void VariableStack::push(Variable v)
22
size_t size = _stack.size();
23
size_t capacity = _stack.capacity();
26
throw bormental::DrBormental("VariableStack::push",
27
inout::fmt("SBL variable stack overflow occurred (size: %1)")
33
Variable VariableStack::pop()
35
if (_stack.size() == 0)
36
throw bormental::DrBormental("VariableStack::pop",
37
inout::fmt("Attempt to pop an element from the empty SBL variable stack"));
39
Variable v = _stack.back();
46
void VariableStack::popAmount(size_t n)
48
if (n > _stack.size())
49
throw bormental::DrBormental("VariableStack::popAmount",
50
inout::fmt("Attempt to pop too many elements (%1) from variable stack (size: %2)")
58
const Variable & VariableStack::top(size_t shift_from_top) const
60
if (shift_from_top >= _stack.size())
61
throw bormental::DrBormental("VariableStack::top",
62
inout::fmt("Attempt to handle invalid offset (%1) in variable stack (size: %2)")
66
return _stack[_stack.size()-shift_from_top-1];
69
Variable & VariableStack::top(size_t shift_from_top)
71
if (shift_from_top >= _stack.size())
72
throw bormental::DrBormental("VariableStack::top",
73
inout::fmt("Attempt to handle invalid offset (%1) in variable stack (size: %2)")
77
return _stack[_stack.size()-shift_from_top-1];
80
const Variable & VariableStack::at(size_t index) const
82
if (index > _stack.size())
83
throw bormental::DrBormental("VariableStack::at",
84
inout::fmt("Attempt to handle invalid offset (%1) in variable stack (size: %2)")
88
return _stack[index]; // -V557
91
Variable & VariableStack::at(size_t index)
93
if (index > _stack.size())
94
throw bormental::DrBormental("VariableStack::at",
95
inout::fmt("Attempt to handle invalid offset (%1) in variable stack (size: %2)")
99
return _stack[index]; // -V557
102
std::shared_ptr<variable::Object> VariableStack::convertToObject(size_t stack_start_position)
104
std::shared_ptr<variable::Object> ret = std::make_shared<variable::Object>();
106
for(size_t i=stack_start_position; i < stack().size(); ++i)
107
ret->add(stack().at(i).copyVariable());
109
popAmount(stack().size() - stack_start_position);