forked from falltergeist/int2ssl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFalloutScript.h
147 lines (115 loc) · 4.45 KB
/
FalloutScript.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
*
* Copyright (c) 2005-2009 Anchorite (TeamX), <anchorite2001@yandex.ru>
* Copyright (c) 2014-2015 Nirran, phobos2077
* Copyright (c) 2015 alexeevdv <mail@alexeevdv.ru>
* Distributed under the GNU GPL v3. For full terms see the file license.txt
*
*/
#ifndef FALLOUT_SCRIPT_H
#define FALLOUT_SCRIPT_H
// C++ standard includes
#include <cstring> // for std::memcpy() / int2ssl::bit_cast<>
#include <memory> // for std::addressof() / int2ssl::bit_cast<>
#include <vector>
// int2ssl includes
#include "StartupCode.h"
#include "ProcTable.h"
#include "Namespace.h"
#include "Node.h"
// Third party includes
class CFalloutScript
{
public:
CFalloutScript();
virtual ~CFalloutScript();
public:
virtual void Serialize();
void Dump();
void InitDefinitions();
void ProcessCode();
void StoreSource();
void StoreTree();
private:
enum Assoc {
NON_ASSOC,
LEFT_ASSOC,
RIGHT_ASSOC,
};
private:
void ExtractCodeElements(COpcodeArray& Source, COpcodeArray& Destination, uint16_t wDelimeter, uint32_t nSizeOfCodeItem, const char* lpszErrorMessage, bool (CFalloutScript::*pCheckFunc)(uint16_t, int32_t));
bool CheckExportVarCode(uint16_t wOperator, int32_t nIndex);
bool CheckSetExportedVarValueCode(uint16_t wOperator, int32_t nIndex);
bool CheckExportProcCode(uint16_t wOperator, int32_t nIndex);
int32_t GetIndexOfProc(const char* lpszName);
int32_t GetIndexOfProc(uint32_t ulNameOffset);
int32_t GetIndexOfExportedVariable(uint32_t ulNameOffset);
void SetExternalVariable(uint32_t ulNameOffset);
void TryRenameGlobalVariables();
void TryRenameImportedVariables();
int32_t NextNodeIndex( CNodeArray& NodeArray, int32_t nCurrentIndex, int32_t nSteep);
bool CheckSequenceOfNodes(CNodeArray& NodeArray, int32_t nStartIndex, const uint16_t wSequence[], int32_t nSequenceLen);
bool RemoveSequenceOfNodes(CNodeArray& NodeArray,int32_t nStartIndex, int32_t nCount, const uint16_t wSequence[], int32_t nSequenceLen);
void InitialReduce();
void BuildTree(CNodeArray& NodeArray);
void ExtractAndReduceCondition(CNodeArray& Source, CNodeArray& Destination, int32_t nStartIndex);
void SetBordersOfBlocks(CNodeArray& NodeArray);
uint32_t BuildTreeBranch(CNodeArray& NodeArray, uint32_t nStartIndex, uint32_t ulEndOffset);
bool IsOmittetArgsAllowed(uint16_t wOpcode);
void StoreDefinitions();
void StoreDeclarations();
std::string GetSource( CNode& node, bool bLabel, uint32_t ulNumArgs);
std::string GetSource( CNode& node, uint32_t ulNumArgs, uint32_t aulProcArg[], uint32_t ulProcArgCount);
bool ArgNeedParens(const CNode& node, const CNode& argument, CFalloutScript::Assoc assoc = CFalloutScript::NON_ASSOC);
std::string GetIndentString(int32_t nLevel);
int GetPriority(uint16_t wOperator);
Assoc GetAssociation(uint16_t wOperator);
private:
class CDefObject
{
public:
enum ObjectType {
OBJECT_VARIABLE,
OBJECT_PROCEDURE
};
public:
CDefObject(ObjectType type = OBJECT_VARIABLE, uint32_t ulAttributes = 0, uint32_t ulObjectData = 0);
public:
ObjectType m_ObjectType;
uint32_t m_ulAttributes;
union {
uint32_t m_ulVarValue;
uint32_t m_ulProcIndex;
};
};
private:
// CMapuint32_tToDefObject
typedef CMap<uint32_t, uint32_t, CDefObject, CDefObject&> CMapuint32_tToDefObject;
CStartupCode m_StartupCode;
CProcTable m_ProcTable;
CNamespace m_Namespace;
CNamespace m_Stringspace;
COpcodeArray m_GlobalVar;
COpcodeArray m_ExportedVar;
COpcodeArray m_ExportedVarValue;
COpcodeArray m_ExportedProc;
CArrayOfNodeArray m_ProcBodies;
CArrayOfNodeArray m_Conditions;
CMapuint32_tToDefObject m_Definitions;
std::vector<std::string> m_GlobalVarsNames;
};
namespace int2ssl
{
// https://en.cppreference.com/w/cpp/numeric/bit_cast
template<class T, class F>
T bit_cast(const F& f)
{
static_assert(sizeof(T) == sizeof(F), "Types must match sizes");
static_assert(std::is_pod<F>::value, "Requires POD input");
static_assert(std::is_pod<T>::value, "Requires POD output");
T t;
std::memcpy(std::addressof(t), std::addressof(f), sizeof(T));
return t;
}
}
#endif //FALLOUT_SCRIPT_H