ScummVM API documentation
expression.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  *
21  * This file is dual-licensed.
22  * In addition to the GPLv3 license mentioned above, this code is also
23  * licensed under LGPL 2.1. See LICENSES/COPYING.LGPL file for the
24  * full text of the license.
25  *
26  */
27 
28 #ifndef GOB_EXPRESSION_H
29 #define GOB_EXPRESSION_H
30 
31 #include "common/scummsys.h"
32 
33 namespace Gob {
34 
35 class GobEngine;
36 
37 enum {
38  OP_NEG = 1,
39  OP_ADD = 2,
40  OP_SUB = 3,
41  OP_BITOR = 4,
42  OP_MUL = 5,
43  OP_DIV = 6,
44  OP_MOD = 7,
45  OP_BITAND = 8,
46  OP_BEGIN_EXPR = 9,
47  OP_END_EXPR = 10,
48  OP_NOT = 11,
49 
50  OP_END_MARKER = 12, // Marks end of an array or string
51 
52 
53  OP_ARRAY_INT8 = 16,
54  OP_LOAD_VAR_INT16 = 17,
55  OP_LOAD_VAR_INT8 = 18,
56  OP_LOAD_IMM_INT32 = 19,
57  OP_LOAD_IMM_INT16 = 20,
58  OP_LOAD_IMM_INT8 = 21,
59  OP_LOAD_IMM_STR = 22,
60  OP_LOAD_VAR_INT32 = 23,
61  OP_LOAD_VAR_INT32_AS_INT16 = 24,
62  OP_LOAD_VAR_STR = 25,
63  OP_ARRAY_INT32 = 26,
64  OP_ARRAY_INT16 = 27,
65  OP_ARRAY_STR = 28,
66 
67  OP_FUNC = 29,
68 
69  OP_OR = 30, // Logical OR
70  OP_AND = 31, // Logical AND
71  OP_LESS = 32,
72  OP_LEQ = 33,
73  OP_GREATER = 34,
74  OP_GEQ = 35,
75  OP_EQ = 36,
76  OP_NEQ = 37
77 };
78 
79 enum {
80  FUNC_SQRT1 = 0,
81  FUNC_SQRT2 = 1,
82  FUNC_SQRT3 = 6,
83 
84  FUNC_SQR = 5,
85  FUNC_ABS = 7,
86  FUNC_RAND = 10
87 };
88 
89 enum {
90  TYPE_IMM_INT8 = OP_LOAD_IMM_INT8, // 21
91  TYPE_IMM_INT32 = OP_LOAD_IMM_INT32, // 19
92  TYPE_IMM_INT16 = OP_LOAD_IMM_INT16, // 20
93  TYPE_IMM_STR = OP_LOAD_IMM_STR, // 22
94  TYPE_VAR_INT8 = OP_LOAD_VAR_INT8, // 18
95  TYPE_VAR_INT16 = OP_LOAD_VAR_INT16, // 17
96  TYPE_VAR_INT32 = OP_LOAD_VAR_INT32, // 23
97  TYPE_VAR_STR = OP_LOAD_VAR_STR, // 25
98  TYPE_ARRAY_INT8 = OP_ARRAY_INT8, // 16
99  TYPE_ARRAY_INT16 = OP_ARRAY_INT16, // 27
100  TYPE_ARRAY_INT32 = OP_ARRAY_INT32, // 26
101  TYPE_ARRAY_STR = OP_ARRAY_STR, // 28
102  TYPE_VAR_INT32_AS_INT16 = OP_LOAD_VAR_INT32_AS_INT16 // 24
103 };
104 
105 enum {
106  // FIXME: The following two 'truth values' are stored inside the list
107  // of "operators". So they somehow coincide with OP_LOAD_VAR_INT32
108  // and OP_LOAD_VAR_INT32_AS_INT16. I haven't yet quite understood
109  // how, resp. what that means. You have been warned.
110  GOB_TRUE = 24,
111  GOB_FALSE = 23
112 };
113 
114 class Expression {
115 public:
116  Expression(GobEngine *vm);
117  virtual ~Expression() {}
118 
119  void skipExpr(char stopToken);
120  void printExpr(char stopToken);
121  void printVarIndex();
122 
123  uint16 parseVarIndex(uint16 *size = 0, uint16 *type = 0);
124  int16 parseValExpr(byte stopToken = 99);
125  int16 parseExpr(byte stopToken, byte *type);
126 
127  int32 getResultInt();
128  char *getResultStr();
129 
130 private:
131  class Stack {
132  public:
133  byte *opers;
134  int32 *values;
135 
136  Stack(size_t size = 20);
137  ~Stack();
138  };
139  class StackFrame {
140  public:
141  byte *opers;
142  int32 *values;
143  int16 pos;
144 
145  StackFrame(const Stack &stack);
146 
147  void push(int count = 1);
148  void pop(int count = 1);
149  };
150 
151  enum PointerType {
152  kExecPtr = 0,
153  kInterVar = 1,
154  kResStr = 2
155  };
156 
157  GobEngine *_vm;
158 
159  int32 _resultInt;
160  char _resultStr[200];
161 
162  int32 encodePtr(byte *ptr, int type);
163  byte *decodePtr(int32 n);
164 
165  void printExpr_internal(char stopToken);
166 
167  bool getVarBase(uint32 &varBase, bool mindStop = false,
168  uint16 *size = 0, uint16 *type = 0);
169  int cmpHelper(const StackFrame &stackFrame);
170  void loadValue(byte operation, uint32 varBase, const StackFrame &stackFrame);
171 
172  void simpleArithmetic1(StackFrame &stackFrame);
173  void simpleArithmetic2(StackFrame &stackFrame);
174  bool complexArithmetic(Stack &stack, StackFrame &stackFrame, int16 brackStart);
175  void getResult(byte operation, int32 value, byte *type);
176 };
177 
178 } // End of namespace Gob
179 
180 #endif // GOB_EXPRESSION_H
Definition: gob.h:163
Definition: expression.h:114
Definition: anifile.h:40