1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
* Variable.h
*
* Created on: 6 Mar 2021
* Author: David
*/
#ifndef SRC_GCODES_VARIABLE_H_
#define SRC_GCODES_VARIABLE_H_
#include <RepRapFirmware.h>
#include <General/FreelistManager.h>
#include <Platform/Heap.h>
#include <ObjectModel/ObjectModel.h>
#include <General/function_ref.h>
// Class to represent a variable having a name and a value
class Variable
{
public:
Variable(const char *_ecv_array str, ExpressionValue pVal, int8_t pScope) noexcept;
~Variable();
ReadLockedPointer<const char> GetName() const noexcept { return name.Get(); }
ExpressionValue GetValue() const noexcept { return val; }
int8_t GetScope() const noexcept { return scope; }
void Assign(ExpressionValue ev) noexcept { val = ev; }
private:
StringHandle name;
ExpressionValue val;
int8_t scope; // -1 for a parameter, else the block nesting level when it was created
};
// Class to represent a collection of variables.
// For now this is just a linked list, but it could be changed to a hash table for faster lookup and insertion.
class VariableSet
{
public:
VariableSet() noexcept : root(nullptr) { }
~VariableSet();
VariableSet(const VariableSet&) = delete;
VariableSet& operator=(const VariableSet& other) = delete;
void AssignFrom(VariableSet& other) noexcept;
Variable *Lookup(const char *_ecv_array str) noexcept;
const Variable *Lookup(const char *_ecv_array str) const noexcept;
void InsertNew(const char *str, ExpressionValue pVal, int8_t pScope) noexcept;
void EndScope(uint8_t blockNesting) noexcept;
void Delete(const char *str) noexcept;
void Clear() noexcept;
void IterateWhile(function_ref<bool(unsigned int index, const Variable& v) /*noexcept*/ > func) const noexcept;
private:
struct LinkedVariable
{
void* operator new(size_t sz) noexcept { return FreelistManager::Allocate<LinkedVariable>(); }
void operator delete(void* p) noexcept { FreelistManager::Release<LinkedVariable>(p); }
LinkedVariable(const char *_ecv_array str, ExpressionValue pVal, int8_t pScope, LinkedVariable *p_next) : next(p_next), v(str, pVal, pScope) {}
LinkedVariable * null next;
Variable v;
};
LinkedVariable * null root;
};
#endif /* SRC_GCODES_VARIABLE_H_ */
|