ScummVM API documentation
handler.h
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef LINGODEC_HANDLER_H
8 #define LINGODEC_HANDLER_H
9 
10 #include "common/array.h"
11 #include "common/stablemap.h"
12 #include "common/str.h"
13 #include "./enums.h"
14 
15 namespace Common {
16 class SeekableReadStream;
17 }
18 
19 namespace LingoDec {
20 
21 struct AST;
22 struct Bytecode;
23 class CodeWriterVisitor;
24 struct Node;
25 struct Script;
26 
27 /* Handler */
28 
29 struct Handler {
30  int16 nameID = 0;
31  uint16 vectorPos = 0;
32  uint32 compiledLen = 0;
33  uint32 compiledOffset = 0;
34  uint16 argumentCount = 0;
35  uint32 argumentOffset = 0;
36  uint16 localsCount = 0;
37  uint32 localsOffset = 0;
38  uint16 globalsCount = 0;
39  uint32 globalsOffset = 0;
40  uint32 unknown1 = 0;
41  uint16 unknown2 = 0;
42  uint16 lineCount = 0;
43  uint32 lineOffset = 0;
44  uint32 stackHeight = 0;
45 
46  Common::Array<int16> argumentNameIDs;
47  Common::Array<int16> localNameIDs;
48  Common::Array<int16> globalNameIDs;
49 
50  Script *script = nullptr;
51  Common::Array<Bytecode> bytecodeArray;
52  Common::StableMap<uint32, size_t> bytecodePosMap;
53  Common::Array<Common::String> argumentNames;
56  Common::String name;
57 
59  AST ast;
60 
61  bool isGenericEvent = false;
62 
63  Handler(): ast(0, this) {}
64 
65  void setScript(Script *s) {
66  script = s;
67  }
68 
69  void readRecord(Common::SeekableReadStream &stream);
70  void readData(Common::SeekableReadStream &stream);
71  Common::Array<int16> readVarnamesTable(Common::SeekableReadStream &stream, uint16 count, uint32 offset);
72  void readNames();
73  bool validName(int id) const;
74  Common::String getName(int id) const;
75  Common::String getArgumentName(int id) const;
76  Common::String getLocalName(int id) const;
78  int variableMultiplier();
79  Common::SharedPtr<Node> readVar(int varType);
80  Common::String getVarNameFromSet(const Bytecode &bytecode);
81  Common::SharedPtr<Node> readV4Property(uint32 offset, int propertyType, int propertyID);
82  Common::SharedPtr<Node> readChunkRef(uint32 offset, Common::SharedPtr<Node> string);
83  void tagLoops();
84  bool isRepeatWithIn(uint32 startIndex, uint32 endIndex);
85  BytecodeTag identifyLoop(uint32 startIndex, uint32 endIndex);
86  void parse();
87  uint32 translateBytecode(Bytecode &bytecode, uint32 index);
88  void writeBytecodeText(CodeWriterVisitor &code) const;
89 };
90 
91 /* Bytecode */
92 
93 struct Bytecode {
94  byte opID;
95  OpCode opcode;
96  int32 obj;
97  uint32 pos;
98  BytecodeTag tag;
99  uint32 ownerLoop;
100  Common::SharedPtr<Node> translation;
101 
102  Bytecode(byte op, int32 o, uint32 p)
103  : opID(op), obj(o), pos(p), tag(kTagNone), ownerLoop(0xffffffff) {
104  opcode = static_cast<OpCode>(op >= 0x40 ? 0x40 + op % 0x40 : op);
105  }
106 };
107 
108 }
109 
110 #endif // LINGODEC_HANDLER_H
Definition: str.h:59
Definition: script.h:38
Definition: handler.h:93
Definition: cast.h:33
Definition: stream.h:745
Definition: codewritervisitor.h:14
Definition: algorithm.h:29
Definition: ptr.h:159
Definition: handler.h:29
Definition: lobject.h:332
Definition: ast.h:848