1// This file is part of SmallBASIC
2//
3// SmallBASIC, pseudo-compiler structures & globals
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8// Copyright(C) 2000 Nicholas Christopoulos
9
10/**
11 * @defgroup scan pseudo-compiler
12 */
13
14#if !defined(_sb_scan_h)
15#define _sb_scan_h
16
17#include "common/sys.h"
18
19#define KEYWORD_PADDING 4
20
21#if defined(__cplusplus)
22extern "C" {
23#endif
24
25/**
26 * @ingroup scan
27 * @struct keyword_s
28 *
29 * Generic keywords (basic bc-types & oldest code)
30 */
31struct keyword_s {
32 const char name[16]; /**< keyword name */
33 code_t code; /**< byte-code */
34};
35
36/**
37 * @ingroup scan
38 * @struct opr_keyword_s
39 *
40 * Operators (not the symbols)
41 */
42struct opr_keyword_s {
43 const char name[16]; /**< keyword name */
44 code_t code; /**< keyword code */
45 code_t opr; /**< operator code */
46};
47
48/**
49 * @ingroup scan
50 * @struct spopr_keyword_s
51 *
52 * Special operators
53 */
54struct spopr_keyword_s {
55 const char name[16]; /**< keyword name */
56 code_t code; /**< keyword code */
57};
58
59/**
60 * @ingroup scan
61 * @struct func_keyword_s
62 *
63 * Buildin-functions
64 */
65struct func_keyword_s {
66 const char name[16]; /**< keyword name */
67 bid_t fcode; /**< keyword code */
68};
69
70/**
71 * @ingroup scan
72 * struct proc_keyword_s
73 *
74 * Buildin procedures
75 */
76struct proc_keyword_s {
77 const char name[16]; /**< keyword name */
78 bid_t pcode; /**< keyword code */
79};
80
81/**
82 * @ingroup scan
83 * @typedef ext_proc_node_t
84 *
85 * External procedure (Modules)
86 */
87typedef struct {
88 char name[SB_KEYWORD_SIZE + KEYWORD_PADDING]; /**< keyword name */
89 int lib_id; /**< library id */
90 bid_t pcode; /**< keyword code */
91 int symbol_index; /**< symbol index on symbol-table */
92} ext_proc_node_t;
93
94/**
95 * @ingroup scan
96 * @typedef ext_func_node_t
97 *
98 * External functions (Modules)
99 */
100typedef struct {
101 char name[SB_KEYWORD_SIZE + KEYWORD_PADDING]; /**< keyword name */
102 int lib_id; /**< library id */
103 bid_t fcode; /**< keyword code */
104 int symbol_index; /**< symbol index on symbol-table */
105} ext_func_node_t;
106
107/**
108 * @ingroup scan
109 * @typedef comp_var_t
110 *
111 * compiler's variable node
112 */
113struct comp_var_s {
114 char *name; /**< variable's name @ingroup scan */
115 int lib_id; /**< library id (if it is an external variable; otherwise -1) @ingroup scan */
116 int symbol_index; /**< symbol index on symbol-table */
117 int local_id;
118 int local_proc_level;
119 byte dolar_sup; /**< used on system variables (so, COMMAND and COMMAND$ to be the same) @ingroup scan */
120};
121
122typedef struct comp_var_s comp_var_t;
123
124/**
125 * @ingroup scan
126 * @typedef comp_label_t
127 *
128 * compiler's label node
129 */
130struct comp_label_s {
131 char name[SB_KEYWORD_SIZE + KEYWORD_PADDING]; /**< label name @ingroup scan */
132 bcip_t ip; /**< address in BC @ingroup scan */
133 bid_t block_id; /**< block_id (FOR-NEXT,IF-FI,etc) used for GOTOs @ingroup scan */
134 bcip_t dp; /**< data pointer @ingroup scan */
135 byte level; /**< block level (used for GOTOs) @ingroup scan */
136};
137
138typedef struct comp_label_s comp_label_t;
139
140typedef struct {
141 int count;
142 int size;
143 comp_label_t **elem;
144} comp_label_table_t;
145
146/**
147 * @ingroup scan
148 * @typedef comp_proc_t
149 *
150 * compiler's user-defined procedure/function node
151 */
152struct comp_proc_s {
153 char *name; /**< procedure/function name @ingroup scan */
154 bcip_t ip; /**< address in BC @ingroup scan */
155 bid_t vid; /**< variable index (return variable-id; for functions) @ingroup scan */
156 bid_t block_id; /**< block_id (FOR-NEXT,IF-FI,etc) used for GOTOs @ingroup scan */
157 int pline; /**< source code line number @ingroup scan */
158 byte level; /**< block level (used for GOTOs) @ingroup scan */
159};
160
161typedef struct comp_proc_s comp_udp_t;
162
163/*
164 * @ingroup scan
165 * @typedef comp_pass_node_t
166 *
167 * compiler's pass-2 stack node
168 */
169struct comp_pass_node_s {
170 char sec[SB_KEYWORD_SIZE + KEYWORD_PADDING]; /**< section-name (PalmOS) @ingroup scan */
171 bcip_t pos; /**< address in BC @ingroup scan */
172 int line; /**< source code line number @ingroup scan */
173 bid_t block_id; /**< block ID @ingroup scan */
174 byte level; /**< block level @ingroup scan */
175};
176
177typedef struct comp_pass_node_s comp_pass_node_t;
178
179typedef struct {
180 int count;
181 int size;
182 comp_pass_node_t **elem;
183} comp_pass_node_table_t;
184
185#if !defined(SCAN_MODULE) // actually static data
186extern struct keyword_s keyword_table[]; /**< basic keywords @ingroup scan */
187extern struct opr_keyword_s opr_table[]; /**< operators table @ingroup scan */
188extern struct spopr_keyword_s spopr_table[]; /**< special operators table @ingroup scan */
189extern struct func_keyword_s func_table[]; /**< buildin functions table @ingroup scan */
190extern struct proc_keyword_s proc_table[]; /**< buildin procedures table @ingroup scan */
191#endif
192
193/**
194 * @ingroup scan
195 *
196 * clears all external-func/proc/var entries
197 */
198void comp_reset_externals(void);
199
200/**
201 * @ingroup scan
202 *
203 * adds a new external procedure to the compiler
204 *
205 * @param proc_name is the procedure name
206 * @param lib_id is the ID of the library
207 * @return the ID which will used in the compiler (and later on BC)
208 */
209int comp_add_external_proc(const char *proc_name, int lib_id);
210
211/**
212 * @ingroup scan
213 *
214 * adds a new external function to the compiler
215 *
216 * @param func_name is the function name
217 * @param lib_id is the ID of the library
218 * @return the ID which will used in the compiler (and later on BC)
219 */
220int comp_add_external_func(const char *func_name, int lib_id);
221
222/**
223 * @ingroup scan
224 *
225 * adds a new external variable to the compiler
226 *
227 * @param name is the variable name
228 * @param lib_id is the ID of the library
229 * @return the ID which will used in the compiler (and later on BC)
230 */
231int comp_add_external_var(const char *name, int lib_id);
232
233/**
234 * @ingroup scan
235 *
236 * returns true if the 'name' is a registered external procedure
237 *
238 * @param name is the procedure name
239 * @return non-zero if found
240 */
241int comp_is_external_proc(const char *name);
242
243/**
244 * @ingroup scan
245 *
246 * returns true if the 'name' is a registered external function
247 *
248 * @param name is the function name
249 * @return non-zero if found
250 */
251int comp_is_external_func(const char *name);
252
253/**
254 * @ingroup scan
255 *
256 * returns true if the 'name' is a base-level keyword (see kw)
257 *
258 * @param name is the keyword name
259 * @return non-zero if found
260 */
261int comp_is_keyword(const char *name);
262
263/**
264 * @ingroup scan
265 *
266 * returns true if the 'name' is a build-in function
267 *
268 * @param name is the function name
269 * @return non-zero if found
270 */
271bid_t comp_is_func(const char *name);
272
273/**
274 * @ingroup scan
275 *
276 * returns true if the 'name' is a build-in procedure
277 *
278 * @param name is the procedure name
279 * @return non-zero if found
280 */
281bid_t comp_is_proc(const char *name);
282
283/**
284 * @ingroup scan
285 *
286 * returns true if the 'name' is a special operator
287 *
288 * @param name is the string
289 * @return non-zero if found
290 */
291int comp_is_special_operator(const char *name);
292
293/**
294 * @ingroup scan
295 *
296 * returns true if the 'name' is an operator
297 *
298 * @param name is the string
299 * @return non-zero if found
300 */
301int comp_is_operator(const char *name);
302
303/**
304 * @ingroup scan
305 *
306 * compiles a SB file... actually the only function that you'll need.
307 *
308 * related options: opt_nosave, opt_checksyntax
309 *
310 * @param sb_file_name the SB source file-name
311 * @return non-zero on success
312 */
313int comp_compile(const char *sb_file_name);
314
315/**
316 * compiler - main
317 *
318 * @param source buffer
319 * @return non-zero on success
320 */
321int comp_compile_buffer(const char *source);
322
323/**
324 * @ingroup scan
325 *
326 * loads a file for compile
327 *
328 * @param fileName the file
329 * @return the text (newly allocated string)
330 */
331char *comp_load(const char *sb_file_name);
332
333/**
334 * @ingroup scan
335 *
336 * initialize compiler
337 */
338void comp_init();
339
340/**
341 * @ingroup scan
342 *
343 * clean-up compiler
344 */
345void comp_close();
346
347/**
348 * @ingroup scan
349 *
350 * compiles a program
351 *
352 * @param section is the section's name (use NULL for "main")
353 * @param text is the text of the file
354 */
355int comp_pass1(const char *section, const char *text);
356
357/**
358 * @ingroup scan
359 *
360 * PASS-2
361 */
362int comp_pass2(void);
363
364/**
365 * @ingroup scan
366 *
367 * setup prefered graphics mode (global variables)
368 *
369 * @param source is a string of form "WIDTHxHEIGHT[xBPP]"
370 */
371void comp_preproc_grmode(const char *source);
372
373#if defined(__cplusplus)
374}
375#endif
376#endif
377
378