1 |
dpavlin |
1 |
/* |
2 |
|
|
* PearPC |
3 |
|
|
* forth.h |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
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 version 2 as |
9 |
|
|
* published by the Free Software Foundation. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifndef __IO_FORTH_H__ |
22 |
|
|
#define __IO_FORTH_H__ |
23 |
|
|
|
24 |
|
|
#include "system/types.h" |
25 |
|
|
#include "tools/data.h" |
26 |
|
|
#include "tools/stream.h" |
27 |
|
|
#include "tools/except.h" |
28 |
|
|
|
29 |
|
|
enum ForthPosMode { |
30 |
|
|
fpmOffset, |
31 |
|
|
fpmLinePos, |
32 |
|
|
}; |
33 |
|
|
|
34 |
|
|
class ForthPos: public Object { |
35 |
|
|
uint32 mOffset; |
36 |
|
|
int mLine, mPos; |
37 |
|
|
ForthPosMode mFpm; |
38 |
|
|
public: |
39 |
|
|
ForthPos(); |
40 |
|
|
virtual int toString(char *buf, int buflen) const; |
41 |
|
|
void copy(ForthPos &p); |
42 |
|
|
void setMode(ForthPosMode fpm); |
43 |
|
|
void setLinePos(int line, int pos); |
44 |
|
|
void clearPos(); |
45 |
|
|
void setOffset(uint32 offset); |
46 |
|
|
void inc(); |
47 |
|
|
void inc(int n); |
48 |
|
|
void incLine(); |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
class ForthException: public MsgException { |
52 |
|
|
public: |
53 |
|
|
ForthException(); |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
class ForthInterpreterException: public ForthException { |
57 |
|
|
public: |
58 |
|
|
ForthInterpreterException(ForthPos &pos, const char *err, ...); |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
class ForthRunException: public ForthException { |
62 |
|
|
public: |
63 |
|
|
ForthRunException(ForthPos &pos, const char *err, ...); |
64 |
|
|
}; |
65 |
|
|
|
66 |
|
|
enum ForthVMMode { |
67 |
|
|
fmInterprete, |
68 |
|
|
fmCompile, |
69 |
|
|
}; |
70 |
|
|
|
71 |
|
|
class ForthVM; |
72 |
|
|
|
73 |
|
|
typedef void (*FCodeFunction)(ForthVM &vm); |
74 |
|
|
|
75 |
|
|
class ForthVM: public Object { |
76 |
|
|
private: |
77 |
|
|
Stack *datastack; |
78 |
|
|
Stack *codestack; |
79 |
|
|
Container *mGlobalVocalbulary; |
80 |
|
|
public: |
81 |
|
|
char currentChar; |
82 |
|
|
char mCurToken[50]; |
83 |
|
|
ForthPos mPos; |
84 |
|
|
ForthPos mErrorPos; |
85 |
|
|
Stream *input, *output; |
86 |
|
|
|
87 |
|
|
ForthVMMode mMode; |
88 |
|
|
|
89 |
|
|
int mStringBufferIdx; |
90 |
|
|
char *mStringBuffer[2]; |
91 |
|
|
uint32 mStringBufferEA[2]; |
92 |
|
|
|
93 |
|
|
int mFCodeBufferIdx; |
94 |
|
|
String *mFCodeBuffer; |
95 |
|
|
|
96 |
|
|
FCodeFunction mFCodes[0xfff]; |
97 |
|
|
|
98 |
|
|
ForthVM(); |
99 |
|
|
~ForthVM(); |
100 |
|
|
|
101 |
|
|
void interprete(Stream &input, Stream &output); |
102 |
|
|
|
103 |
|
|
// compile |
104 |
|
|
void emitFCode(uint32 fcode); |
105 |
|
|
void emitFCodeByte(byte b); |
106 |
|
|
byte getFCodeByte(); |
107 |
|
|
uint32 getFCode(); |
108 |
|
|
|
109 |
|
|
// data stack |
110 |
|
|
void dataPush(uint32 value); |
111 |
|
|
uint32 dataPop(); |
112 |
|
|
bool dataEmpty(); |
113 |
|
|
uint32 dataGet(uint n=0); |
114 |
|
|
void dataClear(); |
115 |
|
|
uint32 dataDepth(); |
116 |
|
|
void * dataStr(uint32 u, bool exc); |
117 |
|
|
|
118 |
|
|
// code stack |
119 |
|
|
void codePush(uint32 value); |
120 |
|
|
uint32 codePop(); |
121 |
|
|
bool codeEmpty(); |
122 |
|
|
uint32 codeGet(uint n=0); |
123 |
|
|
void codeClear(); |
124 |
|
|
uint32 codeDepth(); |
125 |
|
|
|
126 |
|
|
// io |
127 |
|
|
int outf(const char *m, ...); |
128 |
|
|
bool getChar(); |
129 |
|
|
bool consumeSpace(bool except); |
130 |
|
|
String &getToken(const String &delimiters); |
131 |
|
|
bool skipWhite(); |
132 |
|
|
bool skipWhiteCR(); |
133 |
|
|
|
134 |
|
|
// memory |
135 |
|
|
void promMalloc(uint32 size, uint32 &ea, void **p); |
136 |
|
|
}; |
137 |
|
|
|
138 |
|
|
class ForthWord: public Object { |
139 |
|
|
char *mName; |
140 |
|
|
public: |
141 |
|
|
ForthWord(const char *name); |
142 |
|
|
~ForthWord(); |
143 |
|
|
|
144 |
|
|
virtual int compareTo(const Object *obj) const; |
145 |
|
|
virtual void compile(ForthVM &vm); |
146 |
|
|
virtual uint32 getExecToken(ForthVM &vm); |
147 |
|
|
virtual void interprete(ForthVM &vm); |
148 |
|
|
virtual int toString(char *buf, int buflen) const; |
149 |
|
|
}; |
150 |
|
|
|
151 |
|
|
class ForthVar: public ForthWord { |
152 |
|
|
public: |
153 |
|
|
ForthVar(const char *name, uint32 address); |
154 |
|
|
virtual void compile(ForthVM &vm); |
155 |
|
|
virtual uint32 getExecToken(ForthVM &vm); |
156 |
|
|
virtual void interprete(ForthVM &vm); |
157 |
|
|
}; |
158 |
|
|
|
159 |
|
|
class ForthValue: public ForthWord { |
160 |
|
|
public: |
161 |
|
|
ForthValue(const char *name, uint32 address); |
162 |
|
|
virtual void compile(ForthVM &vm); |
163 |
|
|
virtual uint32 getExecToken(ForthVM &vm); |
164 |
|
|
virtual void interprete(ForthVM &vm); |
165 |
|
|
}; |
166 |
|
|
|
167 |
|
|
class ForthWordBuildIn: public ForthWord { |
168 |
|
|
uint32 mFCode; |
169 |
|
|
FCodeFunction mFunc; |
170 |
|
|
public: |
171 |
|
|
ForthWordBuildIn(const char *name, uint32 fcode, FCodeFunction func); |
172 |
|
|
virtual void compile(ForthVM &vm); |
173 |
|
|
virtual uint32 getExecToken(ForthVM &vm); |
174 |
|
|
virtual void interprete(ForthVM &vm); |
175 |
|
|
}; |
176 |
|
|
|
177 |
|
|
class ForthWordAlias: public ForthWord { |
178 |
|
|
int mNumFCodes; |
179 |
|
|
uint16 *mFCodes; |
180 |
|
|
public: |
181 |
|
|
ForthWordAlias(const char *name, int n, ...); |
182 |
|
|
virtual void compile(ForthVM &vm); |
183 |
|
|
virtual void interprete(ForthVM &vm); |
184 |
|
|
}; |
185 |
|
|
|
186 |
|
|
enum ForthWordStringType { |
187 |
|
|
fwstString, |
188 |
|
|
fwstStringWithHex, |
189 |
|
|
fwstStringPrint, |
190 |
|
|
fwstStringPrintBracket, |
191 |
|
|
}; |
192 |
|
|
|
193 |
|
|
class ForthWordString: public ForthWord { |
194 |
|
|
ForthWordStringType mFwst; |
195 |
|
|
public: |
196 |
|
|
ForthWordString(const char *name, ForthWordStringType fwst); |
197 |
|
|
virtual void compile(ForthVM &vm); |
198 |
|
|
String &get(ForthVM &vm, String &s); |
199 |
|
|
virtual void interprete(ForthVM &vm); |
200 |
|
|
}; |
201 |
|
|
|
202 |
|
|
#endif |
203 |
|
|
|