loom

Форк
0
/
VariableStack.cpp 
114 строк · 3.6 Кб
1
/*
2
MIT License
3

4
Copyright (c) 2022 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
5

6
https://bmstu.codes/lsx/simodo/loom
7
*/
8

9
#include "simodo/variable/VariableStack.h"
10
#include "simodo/bormental/DrBormental.h"
11
#include "simodo/inout/format/fmt.h"
12

13
namespace simodo::variable
14
{
15
    VariableStack::VariableStack(size_t stack_size)
16
    {
17
        _stack.reserve(stack_size);
18
    }
19

20
    void VariableStack::push(Variable v)
21
    {
22
        size_t size     = _stack.size();
23
        size_t capacity = _stack.capacity();
24

25
        if (size >= capacity)
26
            throw bormental::DrBormental("VariableStack::push", 
27
                            inout::fmt("SBL variable stack overflow occurred (size: %1)")
28
                            .arg(size));
29

30
        _stack.push_back(v);
31
    }
32

33
    Variable VariableStack::pop() 
34
    {
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"));
38

39
        Variable v = _stack.back();
40

41
        _stack.pop_back();
42

43
        return v;
44
    }
45

46
    void VariableStack::popAmount(size_t n)
47
    {
48
        if (n > _stack.size())
49
            throw bormental::DrBormental("VariableStack::popAmount", 
50
                            inout::fmt("Attempt to pop too many elements (%1) from the SBL variable stack (size: %2)")
51
                            .arg(n)
52
                            .arg(_stack.size()));
53

54
        while(n--)
55
            _stack.pop_back();
56
    }
57

58
    const Variable & VariableStack::top(size_t shift_from_top) const 
59
    {
60
        if (shift_from_top >= _stack.size())
61
            throw bormental::DrBormental("VariableStack::top", 
62
                            inout::fmt("Attempt to handle invalid offset (%1) in SBL variable stack (size: %2)")
63
                            .arg(shift_from_top)
64
                            .arg(_stack.size()));
65

66
        return _stack[_stack.size()-shift_from_top-1];
67
    }
68

69
    Variable & VariableStack::top(size_t shift_from_top)
70
    {
71
        if (shift_from_top >= _stack.size())
72
            throw bormental::DrBormental("VariableStack::top", 
73
                            inout::fmt("Attempt to handle invalid offset (%1) in SBL variable stack (size: %2)")
74
                            .arg(shift_from_top)
75
                            .arg(_stack.size()));
76

77
        return _stack[_stack.size()-shift_from_top-1];
78
    }
79

80
    const Variable & VariableStack::at(size_t index) const
81
    {
82
        if (index > _stack.size())
83
            throw bormental::DrBormental("VariableStack::at", 
84
                            inout::fmt("Attempt to handle invalid offset (%1) in SBL variable stack (size: %2)")
85
                            .arg(index)
86
                            .arg(_stack.size()));
87

88
        return _stack[index];
89
    }
90

91
    Variable & VariableStack::at(size_t index)
92
    {
93
        if (index > _stack.size())
94
            throw bormental::DrBormental("VariableStack::at", 
95
                            inout::fmt("Attempt to handle invalid offset (%1) in SBL variable stack (size: %2)")
96
                            .arg(index)
97
                            .arg(_stack.size()));
98

99
        return _stack[index];
100
    }
101

102
    std::shared_ptr<variable::Object> VariableStack::convertToObject(size_t stack_start_position)
103
    {
104
        std::shared_ptr<variable::Object> ret = std::make_shared<variable::Object>();
105

106
        for(size_t i=stack_start_position; i < stack().size(); ++i)
107
            ret->variables().push_back(stack().at(i).copyVariable());
108

109
        popAmount(stack().size() - stack_start_position);
110
        
111
        return ret;
112
    }
113

114
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.