aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/livingbooks_code.h
blob: 79c9af94f754054dd9edff75ac978d355f92beed (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
/* 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/rect.h"
#include "common/stack.h"
#include "common/substream.h"

namespace Mohawk {

class MohawkEngine_LivingBooks;
class LBItem;

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

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(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;
		}
	}

	LBValueType type;
	Common::String string;
	int integer;
	double real;
	Common::Point point;
	Common::Rect rect;
	LBItem *item;

	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;
};

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, // ??
	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();

	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(const Common::String &name, uint &pos, Common::Array<byte> &code);

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 cmdGetRect(const Common::Array<LBValue> &params);
	void cmdTopLeft(const Common::Array<LBValue> &params);
	void cmdBottomRight(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 cmdSetDragParams(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 cmdKey(const Common::Array<LBValue> &params);

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

} // End of namespace Mohawk

#endif