-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime.h
273 lines (210 loc) · 7.41 KB
/
runtime.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef BASIC_INT_RUNTIME_H
#define BASIC_INT_RUNTIME_H
#include <string>
#include <map>
#include <deque>
#include <unordered_map>
#include <sstream>
#include "value.h"
namespace runtime
{
struct line_begin_tag;
struct runtime_tag;
struct parse_mode_tag;
enum class ParseMode
{
Normal,
ParseStatementSkipElse,
SkipStatementParseElse,
ParseElse,
SkipElse
};
class Runtime
{
public:
void Store( std::string name, value_t val );
value_t Load( std::string name ) const;
void AddLine( linenum_t line, std::string_view str );
void AppendToPrevLine( std::string_view str );
void UpdateCurParseLine( linenum_t line );
std::tuple<const std::string*, linenum_t, unsigned> GetNextLine();
void Dim( std::string baseVarName, const std::vector<int_t> &dimentions );
void Goto( linenum_t line );
void GotoNextLine()
{
GotoImpl( { mProgramCounter.line + 1, 0 } );
}
void Gosub( linenum_t line, unsigned currentLineOffset )
{
const ProgramCounter pc{ mProgramCounter.line, currentLineOffset };
mGosubStack.push_back( pc );
Goto( line );
}
void Return()
{
if( mGosubStack.empty() )
throw std::runtime_error( "Mismatched GOSUB/RETURN statement" );
GotoImpl( mGosubStack.back() );
mGosubStack.pop_back();
}
void ForLoop( std::string varName, value_t initVal, value_t targetVal, value_t stepVal, unsigned currentLineOffset );
void Next( std::vector<std::string> varNames );
void DefineFuntion( std::string fncName, std::string varName, std::string exprStr );
value_t CallFuntion( std::string fncName, value_t arg ) const;
void Print( int_t val ) const;
void Print( float_t val ) const;
void Print( const str_t &val ) const;
void Input( const std::string& prompt, const std::string& name );
value_t Inkey();
void AddFakeInput( std::string str )
{
mFakeInput.push_back( std::move( str ) );
}
void AddData( int v )
{
AddDataImpl( value_t{ static_cast<int_t>(v) } );
}
void AddData( float v )
{
AddDataImpl( value_t{ float_t{v} } );
}
void AddData( str_t v )
{
AddDataImpl( value_t{ std::move( v ) } );
}
void Read( std::string name );
void Restore();
void Restore( linenum_t line );
void Randomize( unsigned int n );
void ClearProgram();
void Clear();
void Start();
bool IsExpectedToContinueLineExecution() const
{
return mProgramCounter.lineOffset == ProgramCounter::ContinueExecution;
}
void PrintVars( std::ostream &os ) const;
static value_t GetDefaultValue( std::string_view name );
private:
struct ProgramCounter
{
static constexpr unsigned LineOffsetBits = 16;
static constexpr unsigned ContinueExecution = ~(~unsigned(0) << LineOffsetBits);
linenum_t line: sizeof(linenum_t) * CHAR_BIT - LineOffsetBits;
linenum_t lineOffset: LineOffsetBits;
};
static_assert( sizeof(ProgramCounter) == sizeof(linenum_t) );
struct ForLoopItem
{
std::string varName;
value_t targetVal;
value_t stepVal;
ProgramCounter startBodyPC;
};
struct FunctionInfo
{
std::string varName;
std::string exprStr;
};
void GotoImpl( ProgramCounter pc )
{
mProgramCounter = pc;
}
bool NextImpl( std::string varName );
void AddDataImpl( value_t value );
static ValueType DetectVarType( std::string_view name );
static bool IsArrayVar( std::string_view name );
template<class T>
void PrintNumberImpl( T val ) const;
private:
std::unordered_map<std::string, value_t> mVars;
std::map<std::string, FunctionInfo, std::less<>> mFunctions;
std::map<linenum_t, std::string> mProgram;
std::unordered_map<linenum_t, size_t> mLineToDataPos;
std::vector<ForLoopItem> mForLoopStack;
std::vector<ProgramCounter> mGosubStack;
std::deque<std::string> mFakeInput;
ProgramCounter mProgramCounter = {};
std::vector<value_t> mData;
size_t mCurDataIdx = 0;
};
class FunctionRuntime
{
public:
static value_t Calculate( const Runtime& rootRuntime, std::string_view exprStr, std::string_view varName, value_t varValue );
value_t Load( std::string name ) const;
value_t CallFuntion( std::string fncName, value_t arg ) const
{
return mRootRuntime.CallFuntion( std::move(fncName), std::move(arg) );
}
value_t Inkey()
{
return value_t{};
}
bool IsExpectedToContinueLineExecution() const
{
return true;
}
private:
FunctionRuntime( const Runtime &rootRuntime, std::string_view varName, value_t varValue ) :
mRootRuntime{ rootRuntime }, mVarName{ varName }, mVarValue{ varValue } {}
private:
const Runtime& mRootRuntime;
std::string_view mVarName;
value_t mVarValue;
};
class SkipStatementRuntime
{
using TStrArg = const std::string&;
using TValueArg = const value_t&;
public:
void Store( TStrArg name, TValueArg val ) { /*Nothing*/ }
value_t Load( TStrArg name ) const { return Runtime::GetDefaultValue(name); }
void Dim( TStrArg baseVarName, const std::vector<int_t>& dimentions ) { /*Nothing*/ }
void Goto( linenum_t line ) { /*Nothing*/ }
void GotoNextLine() { /*Nothing*/ }
void Gosub( linenum_t line, unsigned currentLineOffset ) { /*Nothing*/ }
void Return() { /*Nothing*/ }
void ForLoop( TStrArg varName, TValueArg initVal, TValueArg targetVal, TValueArg stepVal, unsigned currentLineOffset ) { /*Nothing*/ }
void Next( const std::vector<std::string> &varNames ) { /*Nothing*/ }
void DefineFuntion( TStrArg fncName, TStrArg varName, TStrArg exprStr ) { /*Nothing*/ }
value_t CallFuntion( TStrArg fncName, TValueArg arg ) const { return value_t{}; }
template<class T> void Print( T&& val ) const { /*Nothing*/ }
void Input( TStrArg prompt, TStrArg name ) { /*Nothing*/ }
value_t Inkey() { return value_t{}; }
void Read( TStrArg name ) { /*Nothing*/ }
void Restore() { /*Nothing*/ }
void Restore( linenum_t line ) { /*Nothing*/ }
void Randomize( unsigned int n ) { /*Nothing*/ }
};
class TestRuntime : public runtime::Runtime
{
public:
template<class T>
void Print( T&& val )
{
mStrOut << val;
}
void Clear()
{
Runtime::Clear();
ClearOutput();
}
void Start()
{
Runtime::Start();
ClearOutput();
}
std::string GetOutput() const
{
return mStrOut.str();
}
private:
void ClearOutput()
{
mStrOut.str( "" );
}
std::ostringstream mStrOut;
};
}
#endif // BASIC_INT_RUNTIME_H