-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Language.Semantic.Twee.pas
277 lines (217 loc) · 6.72 KB
/
GS.Language.Semantic.Twee.pas
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
273
274
275
276
277
unit GS.Language.Semantic.Twee;
interface
uses classes,
SysUtils,
GS.Language.Compiler,
Generics.Collections;
Type
TTweeBook = class; //Project owner.
TTweeStructure = class;
TTweePassage = class; //Passage Data.
TTweeMemory = class; //Momory bank, variable, object, array and so on. Shared.
TTweeCode = class; //code related.
TTweeStep = class; //Step, code chunck in a passage.
TTweeStepDisplayText = class; //Pure Display Code instruction : To be render later.
TTweeStepVarAssign = class; //Variable update/assign (set:...)
TTweeStepFlow = class; //code flow directive.
TTweeStepFlowGoto = class; //if...
// TTweeStepFlowConditional = class; //if...
// TTweeStepFlowDisplay = class; //(display: ...) (got and run a passage, and display it in current passage.
// TTweeStepFlowClickToPassage = class; //Solid link to a passage, user must click. (i.e. [click!->[clickMe]]
TTweeStructure = class
public
constructor Create; virtual;
destructor destroy; override;
end;
TTweeStructureList = class(TObjectList<TTweeStructure>) //Memory keeper. All other list are pointer only.
end;
TTweeCode = Class
End;
TTweeStep = class(TTweeCode)
end;
TTweeMemory = class(TTweeStructure)
private
fName: String;
public
constructor Create(memName : string); reintroduce;
property Name : String read fName;
end;
TTweeStepList = class(TObjectList<TTweeStep>)
end;
TTweeMemoryList = class(TObjectDictionary<string,TTweeMemory>)
end;
TTweePassageList = class(TObjectDictionary<string,TTweePassage>)
end;
TTweePassage = Class(TTweeStructure)
protected
fbook : TTweeBook;
fPassageName : String;
fCodes : TTweeStepList; //ordered code description.
public
LocalMemory : TTweeMemoryList;
constructor Create(_book : TTweeBook; _passageName : String); reintroduce;
destructor destroy; override;
function addCode(_code : TTweeStep) : TTweeStep;
property PassageName : String read fPassageName;
property Book : TTweeBook read fBook;
property Code : TTweeStepList read fCodes;
End;
TTweeStepDisplayText = class(TTweeStep)
private
fFormula: string;
public
Constructor Create(_formula : string); reintroduce;
property formula : string read fFormula;
end;
TTweeStepVarAssign = class(TTweeStep)
private
fFormula: string;
fVarname: string;
public
Constructor Create(_varName : string; _formula : string); reintroduce;
property varname : string read fVarname;
property formula : string read fFormula;
end;
TTweeStepFlow = class(TTweeStep)
private
ftarget: string;
public
Constructor Create(_target : string); reintroduce;
property target : string read ftarget write ftarget;
end;
TTweeStepFlowGoto = class(TTweeStepFlow)
end;
TTweeStepFlowDisplay = class(TTweeStepFlow)
end;
TTweeBook = class
private
fPassages: TTweePassageList;
fStoryTitle: String;
//fStoryData : TTweeStoryMetaData (struct); //Todo.
fStructure: TTweeStructureList;
fGlobalMemory : TTweeMemoryList; //global var $MyVar and other global structure.
public
Constructor Create; virtual;
Destructor Destroy; override;
function addObject(struct : TTweeStructure) : TTweeStructure;
property StoryTitle : String read fStoryTitle;
property passages : TTweePassageList read fPassages;
property Memory : TTweeMemoryList read fGlobalMemory;
property AllObjects : TTweeStructureList read fStructure;
end;
TTweeSemanticProcessor = Class(TSemanticProcessor)
private
fBook: TTweeBook;
public
constructor Create(_root : TGSCRoot); virtual;
destructor Destroy; override;
property Book : TTweeBook read fBook;
End;
implementation
{ TTweeBook }
function TTweeBook.addObject(struct: TTweeStructure): TTweeStructure;
begin
result := struct;
fStructure.Add(struct);
if struct is TTweePassage then
begin
if Not fPassages.ContainsKey(TTweePassage(struct).PassageName) then
begin
fPassages.Add(TTweePassage(struct).PassageName,TTweePassage(struct))
end
else
raise Exception.Create('TTweeBook.addObject : Error : try to add already existing Passage. ("'+TTweePassage(struct).PassageName+'")');
end
else
if struct is TTweeMemory then
fGlobalMemory.Add(TTweeMemory(struct).Name, TTweeMemory(struct));
end;
constructor TTweeBook.Create;
begin
fStructure := TTweeStructureList.Create(True);
fPassages := TTweePassageList.Create([]);
fGlobalMemory := TTweeMemoryList.Create([]);
end;
destructor TTweeBook.Destroy;
begin
FreeAndNil(fGlobalMemory);
FreeAndNil(fPassages);
FreeAndNil(fStructure);
inherited;
end;
{ TTweeStructure }
constructor TTweeStructure.Create;
begin
inherited; //here for syntax (general override for inherited stuffs)
end;
destructor TTweeStructure.destroy;
begin
inherited; //here for syntax (general override for inherited stuffs)
end;
{ TTweePassage }
function TTweePassage.addCode(_code: TTweeStep): TTweeStep;
begin
assert(assigned(_code));
result := _code;
fCodes.Add(_code);
end;
constructor TTweePassage.Create(_book : TTweeBook; _passageName : String);
var pass : string;
begin
assert(assigned(_book));
fbook := _book;
pass := (trim(_passageName));
assert(pass<>'');
assert(Not Book.passages.ContainsKey(pass),'Passage "'+pass+'" already exists');
inherited Create;
fPassageName := pass;
fCodes := TTweeStepList.Create(true); //ordered code description.
LocalMemory := TTweeMemoryList.Create([doOwnsValues]); //var _myVar.
end;
destructor TTweePassage.destroy;
begin
FreeAndNil(fcodes);
FreeAndNil(LocalMemory);
inherited;
end;
{ TTweeMemory }
constructor TTweeMemory.Create(memName: string);
begin
inherited create;
fName := memName;
end;
{ TTweeSemanticProcessor }
constructor TTweeSemanticProcessor.Create(_root: TGSCRoot);
begin
inherited Create(_root);
fBook := TTweeBook.Create;
end;
destructor TTweeSemanticProcessor.Destroy;
begin
FreeAndNil(fBook);
inherited;
end;
{ TTweeStepVarAssign }
constructor TTweeStepVarAssign.Create(_varName, _formula: string);
begin
inherited Create;
assert(length(_varName)>0);
assert(length(_formula)>0);
fVarname := _varName;
fFormula := _formula;
end;
{ TTweeStepFlow }
constructor TTweeStepFlow.Create(_target: string);
begin
inherited create;
assert(length(_target)>0);
ftarget := _target;
end;
{ TTweeStepDisplayText }
constructor TTweeStepDisplayText.Create(_formula: string);
begin
inherited Create;
assert(length(_formula)>0);
fFormula := _formula;
end;
end.