OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
scriptcompiler.h
1/*
2===========================================================================
3Copyright (C) 2008 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// compiler.h: Script Compiler
24
25#pragma once
26
27#include "../script/scriptopcodes.h"
28#include "../fgame/gamescript.h"
29#include "../parser/parsetree.h"
30
31class ScriptVariable;
32
33typedef void (*ScriptDisplayTokenFunc)(const char *type, const char *name);
34
35typedef struct scriptmacro {
36 str name;
37 str parameters;
38} scriptmacro_t;
39
40#define BREAK_JUMP_LOCATION_COUNT 100
41#define CONTINUE_JUMP_LOCATION_COUNT 100
42
43class ScriptCompiler
44{
45public:
46 unsigned char *code_pos;
47 unsigned char *code_ptr;
48 unsigned char *prog_ptr;
49 unsigned char *prog_end_ptr;
50
51 GameScript *script;
52 StateScript *stateScript;
53
54 bool bCanBreak;
55 bool bCanContinue;
56
57 opcode_info_t prev_opcodes[100];
58 unsigned int prev_opcode_pos;
59
60 int m_iVarStackOffset;
61 int m_iInternalMaxVarStackOffset;
62 int m_iMaxExternalVarStackOffset;
63 int m_iMaxCallStackOffset;
64 int m_iHasExternal;
65
66 unsigned char *apucBreakJumpLocations[BREAK_JUMP_LOCATION_COUNT];
67 int iBreakJumpLocCount;
68 unsigned char *apucContinueJumpLocations[CONTINUE_JUMP_LOCATION_COUNT];
69 int iContinueJumpLocCount;
70
71 bool compileSuccess;
72
73 static int current_label;
74
75public:
76 ScriptCompiler();
77 void Reset();
78
79 unsigned char PrevOpcode();
80 signed char PrevVarStackOffset();
81 void AbsorbPrevOpcode();
82 void ClearPrevOpcode();
83 void AccumulatePrevOpcode(int opcode, int iVarStackOffset);
84
85 void AddBreakJumpLocation(unsigned char *pos);
86 void AddContinueJumpLocation(unsigned char *pos);
87 void AddJumpLocation(unsigned char *pos);
88 void AddJumpBackLocation(unsigned char *pos);
89 void AddJumpToLocation(unsigned char *pos);
90
91 bool BuiltinReadVariable(unsigned int sourcePos, int type, int eventnum);
92 bool BuiltinWriteVariable(unsigned int sourcePos, int type, int eventnum);
93
94 void EmitAssignmentStatement(sval_t lhs, unsigned int sourcePos);
95
96 void EmitBoolJumpFalse(unsigned int sourcePos);
97 void EmitBoolJumpTrue(unsigned int sourcePos);
98 void EmitBoolNot(unsigned int sourcePos);
99 void EmitBoolToVar(unsigned int sourcePos);
100
101 void EmitBreak(unsigned int sourcePos);
102 void EmitCatch(sval_t val, unsigned char *try_begin_code_pos, unsigned int sourcePos);
103 void EmitConstArray(sval_t lhs, sval_t rhs, unsigned int sourcePos);
104 void EmitConstArrayOpcode(int iCount);
105 void EmitContinue(unsigned int sourcePos);
106 void EmitDoWhileJump(sval_t while_stmt, sval_t while_expr, unsigned int sourcePos);
107 void EmitEof(unsigned int sourcePos);
108 void EmitField(sval_t listener_val, sval_t field_val, unsigned int sourcePos);
109 void EmitFloat(float value, unsigned int sourcePos);
110 void EmitFunc1(int opcode, unsigned int sourcePos);
111 //void EmitFunction(int iParamCount, sval_t val, unsigned int sourcePos);
112 void EmitIfElseJump(sval_t if_stmt, sval_t else_stmt, unsigned int sourcePos);
113 void EmitIfJump(sval_t if_stmt, unsigned int sourcePos);
114 void EmitInteger(unsigned int value, unsigned int sourcePos);
115 void EmitJump(unsigned char *pos, unsigned int sourcePos);
116 void EmitJumpBack(unsigned char *pos, unsigned int sourcePos);
117 void EmitLabel(const char *name, unsigned int sourcePos);
118 void EmitLabel(int name, unsigned int sourcePos);
119 void EmitLabelParameterList(sval_t parameter_list, unsigned int sourcePos);
120 void EmitLabelPrivate(const char *name, unsigned int sourcePos);
121 void EmitAndJump(sval_t logic_stmt, unsigned int sourcePos);
122 void EmitOrJump(sval_t logic_stmt, unsigned int sourcePos);
123 void EmitMakeArray(sval_t val);
124 void EmitMethodExpression(int iParamCount, int eventnum, unsigned int sourcePos);
125 void EmitNil(unsigned int sourcePos);
126 void EmitNop();
127 int EmitNot(unsigned int sourcePos);
128 void EmitOpcode(int opcode, unsigned int sourcePos);
129 void EmitParameter(sval_u lhs, unsigned int sourcePos);
130 int EmitParameterList(sval_t event_parameter_list);
131 void EmitRef(sval_t val, unsigned int sourcePos);
132 void EmitStatementList(sval_t val);
133 void EmitString(str value, unsigned int sourcePos);
134 void EmitSwitch(sval_t val, unsigned int sourcePos);
135 void EmitValue(sval_t val);
136 void EmitValue(ScriptVariable& var, unsigned int sourcePos);
137 void EmitVarToBool(unsigned int sourcePos);
138 void EmitWhileJump(sval_t while_expr, sval_t while_stmt, sval_t inc_stmt, unsigned int sourcePos);
139
140 bool EvalPrevValue(ScriptVariable& var);
141
142 void ProcessBreakJumpLocations(int iStartBreakJumpLocCount);
143 void ProcessContinueJumpLocations(int iStartContinueJumpLocCount);
144
145 unsigned char *GetPosition();
146
147 // compile
148 void CompileError(unsigned int sourcePos, const char *format, ...);
149
150 scriptmacro_t *GetMacro(char *sourceLine);
151
152 char *Preprocess(char *sourceBuffer);
153 void Preclean(char *processedBuffer);
154 bool Parse(GameScript *m_GameScript, char *sourceBuffer, const char *type, size_t& outLength);
155 bool Compile(GameScript *m_GameScript, unsigned char *progBuffer, size_t& outLength);
156
157 static str GetLine(str content, int line);
158
159private:
160 template<typename Value>
161 void EmitOpcodeValue(const Value& value, size_t size);
162
163 template<typename Value>
164 void EmitOpcodeValue(const Value& value);
165
166 template<typename Value>
167 void EmitAt(unsigned char *location, const Value& value, size_t size);
168
169 template<typename Value>
170 void SetOpcodeValue(const Value& value);
171
172 template<typename Value>
173 Value GetOpcodeValue(size_t size) const;
174
175 template<typename Value>
176 Value GetOpcodeValue(size_t offset, size_t size) const;
177};
178
179extern ScriptCompiler Compiler;
180
181void CompileAssemble(const char *filename, const char *outputfile);
182bool GetCompiledScript(GameScript *scr);
Definition gamescript.h:118
Definition scriptcompiler.h:44
Definition scriptvariable.h:75
Definition gamescript.h:83
Definition str.h:77
Definition scriptopcodes.h:51
Definition scriptcompiler.h:35
Definition parsetree.h:77