aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/livingbooks_code.h
blob: 6f6297d5927d3a99abcd5cd9c27efcf0d6e6573e (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef MOHAWK_LIVINGBOOKS_CODE_H
#define MOHAWK_LIVINGBOOKS_CODE_H

#include "common/ptr.h"
#include "common/rect.h"
#include "common/stack.h"
#include "common/substream.h"

namespace Mohawk {

class MohawkEngine_LivingBooks;
class LBItem;
class LBXObject;
struct LBList;

enum LBValueType {
	kLBValueString,
	kLBValueInteger,
	kLBValueReal,
	kLBValuePoint,
	kLBValueRect,
	kLBValueItemPtr,
	kLBValueLBX,
	kLBValueList
};

struct LBValue {
	LBValue() {
		type = kLBValueInteger;
		integer = 0;
	}
	LBValue(int val) {
		type = kLBValueInteger;
		integer = val;
	}
	LBValue(const Common::String &str) {
		type = kLBValueString;
		string = str;
	}
	LBValue(const Common::Point &p) {
		type = kLBValuePoint;
		point = p;
	}
	LBValue(const Common::Rect &r) {
		type = kLBValueRect;
		rect = r;
	}
	LBValue(LBItem *itm) {
		type = kLBValueItemPtr;
		item = itm;
	}
	LBValue(Common::SharedPtr<LBXObject> l) {
		type = kLBValueLBX;
		lbx = l;
	}
	LBValue(Common::SharedPtr<LBList> l) {
		type = kLBValueList;
		list = l;
	}
	LBValue(const LBValue &val) {
		type = val.type;
		switch (type) {
		case kLBValueString:
			string = val.string;
			break;
		case kLBValueInteger:
			integer = val.integer;
			break;
		case kLBValueReal:
			real = val.real;
			break;
		case kLBValuePoint:
			point = val.point;
			break;
		case kLBValueRect:
			rect = val.rect;
			break;
		case kLBValueItemPtr:
			item = val.item;
			break;
		case kLBValueLBX:
			lbx = val.lbx;
			break;
		case kLBValueList:
			list = val.list;
			break;
		}
	}

	LBValueType type;
	Common::String string;
	int integer;
	double real;
	Common::Point point;
	Common::Rect rect;
	LBItem *item;
	Common::SharedPtr<LBXObject> lbx;
	Common::SharedPtr<LBList> list;

	bool operator==(const LBValue &x) const;
	bool operator!=(const LBValue &x) const;

	bool isNumeric() const;
	bool isZero() const;

	Common::String toString() const;
	int toInt() const;
	double toDouble() const;
	Common::Point toPoint() const;
	Common::Rect toRect() const;
};

struct LBList {
	Common::Array<LBValue> array;
};

enum {
	kLBCodeLiteralInteger = 0x1
};

enum {
	kTokenIdentifier = 0x1,
	kTokenLiteral = 0x5,
	kTokenString = 0x6,
	kTokenEndOfStatement = 0x7,
	kTokenEndOfFile = 0x8,
	kTokenConcat = 0xb,
	kTokenSingleQuote = 0xc, // ??
	kTokenDoubleQuote = 0xd, // ??
	kTokenMultiply = 0xe,
	kTokenOpenBracket = 0xf,
	kTokenCloseBracket = 0x10,
	kTokenMinus = 0x11,
	kTokenMinusMinus = 0x12,
	kTokenPlusEquals = 0x13,
	kTokenPlus = 0x14,
	kTokenPlusPlus = 0x15,
	kTokenEquals = 0x16,
	kTokenMinusEquals = 0x17,
	kTokenMultiplyEquals = 0x18,
	kTokenDivideEquals = 0x19,
	kTokenListStart = 0x1a,
	kTokenListEnd = 0x1b,
	kTokenColon = 0x1c, // ??
	kTokenLessThan = 0x1d,
	kTokenGreaterThan = 0x1e,
	kTokenAndEquals = 0x1f,
	kTokenDotOperator = 0x20,
	kTokenDivide = 0x21,
	kTokenAssign = 0x22,
	kTokenLessThanEq = 0x23,
	kTokenGreaterThanEq = 0x24,
	kTokenNotEq = 0x25,
	kTokenQuote = 0x27, // ??
	kTokenAnd = 0x2a,
	kTokenComma = 0x2c,
	kTokenConstMode = 0x31,
	kTokenIntDivide = 0x32,
	kTokenModulo = 0x34,
	kTokenNot = 0x35,
	kTokenOr = 0x37,
	kTokenTrue = 0x39,
	kTokenFalse = 0x3a,
	kTokenConstDataType = 0x3b, // ??
	kTokenConstItemType = 0x3c, // ??
	kTokenConstEventId = 0x42,
	kTokenConstScriptOpcode = 0x43, // ??
	kTokenConstScriptParam = 0x44, // ??
	kTokenEval = 0x4b,
	kTokenGeneralCommand = 0x4d,
	kTokenItemCommand = 0x4e,
	kTokenNotifyCommand = 0x4f,
	// 0x5e?!
	kTokenKeycode = 0x5f,

	// v5 only:
	kTokenLocal = 0x61,
	kTokenPropListCommand = 0x70,
	kTokenRectCommand = 0x71
};

class LBCode {
public:
	LBCode(MohawkEngine_LivingBooks *vm, uint16 baseId);
	~LBCode();

	LBValue runCode(LBItem *src, uint32 offset);
	uint parseCode(const Common::String &source);

protected:
	MohawkEngine_LivingBooks *_vm;

	uint32 _size;
	byte *_data;
	Common::HashMap<uint16, Common::String> _strings;

	uint32 _currOffset;
	LBItem *_currSource;

	Common::Stack<LBValue> _stack;
	byte _currToken;
	LBValue _currValue;

	void nextToken();

	LBValue runCode(byte terminator);
	void parseStatement();
	void parseComparisons();
	void parseConcat();
	void parseArithmetic1();
	void parseArithmetic2();
	void parseMain();

	LBValue *getIndexedVar(Common::String varname, const Common::Array<LBValue> &index);
	LBItem *resolveItem(const LBValue &value);
	Common::Array<LBValue> readParams();
	Common::Rect getRectFromParams(const Common::Array<LBValue> &params);

	void runGeneralCommand();
	void runItemCommand();
	void runNotifyCommand();

	uint nextFreeString();
	bool parseCodeSymbol(Common::String name, uint &pos, Common::Array<byte> &code, bool useAllAliases);

public:
	void cmdUnimplemented(const Common::Array<LBValue> &params);
	void cmdEval(const Common::Array<LBValue> &params);
	void cmdRandom(const Common::Array<LBValue> &params);
	void cmdStringLen(const Common::Array<LBValue> &params);
	void cmdSubstring(const Common::Array<LBValue> &params);
	void cmdMax(const Common::Array<LBValue> &params);
	void cmdMin(const Common::Array<LBValue> &params);
	void cmdAbs(const Common::Array<LBValue> &params);
	void cmdGetRect(const Common::Array<LBValue> &params);
	void cmdMakePoint(const Common::Array<LBValue> &params);
	void cmdTopLeft(const Common::Array<LBValue> &params);
	void cmdBottomRight(const Common::Array<LBValue> &params);
	void cmdMousePos(const Common::Array<LBValue> &params);
	void cmdTop(const Common::Array<LBValue> &params);
	void cmdLeft(const Common::Array<LBValue> &params);
	void cmdBottom(const Common::Array<LBValue> &params);
	void cmdRight(const Common::Array<LBValue> &params);
	void cmdXPos(const Common::Array<LBValue> &params);
	void cmdYPos(const Common::Array<LBValue> &params);
	void cmdWidth(const Common::Array<LBValue> &params);
	void cmdHeight(const Common::Array<LBValue> &params);
	void cmdMove(const Common::Array<LBValue> &params);
	void cmdSetDragParams(const Common::Array<LBValue> &params);
	void cmdNewList(const Common::Array<LBValue> &params);
	void cmdAdd(const Common::Array<LBValue> &params);
	void cmdAddAt(const Common::Array<LBValue> &params);
	void cmdSetAt(const Common::Array<LBValue> &params);
	void cmdListLen(const Common::Array<LBValue> &params);
	void cmdDeleteAt(const Common::Array<LBValue> &params);
	void cmdSetProperty(const Common::Array<LBValue> &params);
	void cmdGetProperty(const Common::Array<LBValue> &params);
	void cmdDeleteVar(const Common::Array<LBValue> &params);
	void cmdExec(const Common::Array<LBValue> &params);
	void cmdReturn(const Common::Array<LBValue> &params);
	void cmdSetPlayParams(const Common::Array<LBValue> &params);
	void cmdSetKeyEvent(const Common::Array<LBValue> &params);
	void cmdSetHitTest(const Common::Array<LBValue> &params);
	void cmdLBXCreate(const Common::Array<LBValue> &params);
	void cmdLBXFunc(const Common::Array<LBValue> &params);
	void cmdKey(const Common::Array<LBValue> &params);

	void itemClone(const Common::Array<LBValue> &params);
	void itemIsPlaying(const Common::Array<LBValue> &params);
	void itemIsLoaded(const Common::Array<LBValue> &params);
	void itemMoveTo(const Common::Array<LBValue> &params);
	void itemSeek(const Common::Array<LBValue> &params);
	void itemSeekToFrame(const Common::Array<LBValue> &params);
	void itemSetParent(const Common::Array<LBValue> &params);
};

} // End of namespace Mohawk

#endif