aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/archetype/misc.h
blob: 0799077a8585bbf743954fe3d0a476e5484a29b6 (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
/* 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 ARCHETYPE_MISC
#define ARCHETYPE_MISC

#include "common/file.h"
#include "glk/archetype/crypt.h"
#include "glk/archetype/string.h"

namespace Glk {
namespace Archetype {

#define MAX_STRING 255
#define NULL_CH '\0'
#define NEWLINE_CH '\r'

enum {
	DEBUG_BYTES = 0x01,
	DEBUG_MSGS = 0x02,
	DEBUG_EXPR = 0x04,
	DEBUG_STMT = 0x08
};

enum AclType {
	RESERVED, IDENT, MESSAGE, OPER, TEXT_LIT, QUOTE_LIT, NUMERIC, PUNCTUATION,
	STR_PTR, ATTR_PTR, BAD_TOKEN, NEWLINE
};

/**
 * Source program file/accounting structure.With such a file, it is important to keep
 * not only the file pointer, but also fields to keep track of position in the source file
 * andthe compiler state, or the context of the tokenizer
 */
class progfile {
private:
	Common::File _file;	// The physical file
public:
	String filename;					// to do with error tracking
	String line_buffer;
	int file_line;
	int line_pos;

	bool newlines;						// having to do with the tokenizer context
	char last_ch;
	bool consumed;
	AclType ttype;
	int tnum;
public:
	/**
	 * Constructor
	 */
	progfile() : file_line(0), line_pos(0), newlines(false), last_ch(NULL_CH),
		consumed(false), ttype(RESERVED), tnum(0) {}

	/**
	 * Opens an Archetype program source file.
	 * @param name			Filename
	 */
	bool open(const String &name);

	/**
	 * Closes an Archetype program source code file.
	 */
	void close();

	/**
	 * Reads a single character from the given progfile, performing all appropriate housekeeping.
	 *
	 * It appends an internal newline to the end of every line taken from the file; it is up to
	 * the tokenizer as to whether to consider it white space or a token.
	 * @param c				The output character
	 * @returns				True if the character was safely read from the file
	 */
	bool readChar(char &ch);

	/**
	 * Has the effect of putting a character back on the data stream.
	 * Closely cooperates with read_char above.
	 * @param ch			Character to un-read
	 */
	void unreadChar(char ch);

	/**
	 * Writes out the current position in the source file nicely for error messages
	 * and so forth.It will, however, only write this out once per execution of the program.
	 * This is to prevent messages scrolling uncontrollably off the screen.
	 */
	void sourcePos();
};

enum ClassifyType { TYPE_ID, OBJECT_ID, ATTRIBUTE_ID, ENUMERATE_ID, UNDEFINED_ID };

extern const char *const VERSION;
extern const char *const VERSION_STUB;
extern const double VERSION_NUM;
extern size_t Bytes;		// Bytes consumed by allocated memory
extern int Debug;
extern bool KeepLooking;
extern bool AllErrors;

/**
 * Performs initialization of fields local to the file
 */
extern void misc_init();

/**
 * Provides a method of keeping track of the size, in allocation, of the used part of the heap.
 * @param delta		if positive, the number allocated; if negative, the number deallocated.
 */
extern void add_bytes(int delta);

/**
 * Given a name andextension, tacks on the given extension if none given.
 * of <name> are ".<ext>", or else tacks those four characters on.
 * @param name			Filename
 * @param ext			Extension
 * @param replace		Whether to replace existing extension
 */
extern String formatFilename(const String &name, const String &ext, bool replace);

/**
 * Given an input stream, reads in a Pascal style string with the first byte being the length.
 * Secondarily, it also decrypts the string
 * @param fIn			Input file
 * @param the_string	Output string
 */
extern void load_string(Common::ReadStream *fIn, String &the_string);

/**
 * Given an untyped file variable anda string variable, writes to the file first the length
 * of the string andthen the string itself.
 * @param fOut			Output file
 * @param the_string	The string to output
 */
extern void dump_string(Common::WriteStream *fOut, const String &the_string);

/**
 * Used for allocating string space that is not expected to be disposed of
 * until the end of the program andis never expected to change.
 * Only the very minimum space necessary to store the string is used;
 * thereby using minimal space andincurring no fragmentation
 */
extern StringPtr NewConstStr(const String &s);

/**
 * Frees a const string
 */
extern void FreeConstStr(StringPtr &sp);

/**
 * Dynamic strings were originally strings where we need speed and yet we need to allocate
 * only the string space necessary. These days, we can simply just allocate a new string
 */
extern StringPtr NewDynStr(const String &s);

/**
 * Frees a dynamic string
 */
extern void FreeDynStr(StringPtr &sp);

} // End of namespace Archetype
} // End of namespace Glk

#endif