Initial commit of newLISP.
[newlisp.git] / newlisp.h
blobda320e73b72ac7072abf76eb9f588acbeb003bb4
1 /* newlisp.h - header file for newLISP
3 Copyright (C) 2008 Lutz Mueller
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /*
21 starting with version 9.1.9 MINGW is the default flavor for WIN_32
22 and the MINGW label as been eliminated
25 #ifdef TRU64
26 #define strtoll strtol
27 #define strtoull strtoul
28 #endif
30 #if defined(SOLARIS) || defined(TRU64)
31 #define MY_RAND_MAX 2147483647
32 #else
33 #define MY_RAND_MAX RAND_MAX
34 #endif
36 /*
37 This is for 64bit large file support (LFS),
39 #define LFS
40 #ifdef LFS
41 #ifdef SOLARIS
42 #define _LARGEFILE64_SOURCE 1
43 #endif
44 #define _FILE_OFFSET_BITS 64
45 #endif
47 #include <signal.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <locale.h>
52 #include <setjmp.h>
53 #include <stdarg.h>
54 #include <dirent.h>
55 #include <limits.h>
57 /* some Linux do UTF-8 but do not have wcsftime()
58 buggy in some GCC, i.e. MinGW and Solaris
61 #ifdef SUPPORT_UTF8
62 #ifdef LINUX
63 #include <wchar.h>
64 #define WCSFTIME
65 #endif
66 #ifdef WIN_32
67 #include <wchar.h>
68 #endif
69 #endif
71 #ifdef WIN_32
72 #include <windef.h>
73 #include <winbase.h>
74 #else
75 #include <termios.h>
76 #include <sys/wait.h>
77 #endif
79 #include <unistd.h>
80 #include <sys/time.h>
81 #include <math.h>
82 #include <float.h>
83 #include <string.h>
84 #include <ctype.h>
85 #include <fcntl.h>
86 #include <stdarg.h>
87 #include <time.h>
88 #include <sys/stat.h>
89 #include <sys/timeb.h>
90 #include <sys/types.h>
92 #ifndef SOLARIS
93 #ifndef _BSD
94 #ifndef MAC_OSX
95 #include <malloc.h>
96 #endif
97 #endif
98 #else
99 #include <alloca.h>
100 #endif
102 #ifdef OS2
103 #define vasprintf my_vasprintf
104 #define MY_VASPRINTF
105 #endif
107 #ifdef MAC_OSX
108 #define vasprintf my_vasprintf
109 #define MY_VASPRINTF
110 #endif
112 #ifdef SOLARIS
113 #define vasprintf my_vasprintf
114 #define setenv my_setenv
115 #define MY_VASPRINTF
116 #define MY_SETENV
117 #define MY_RAND_MAX 2147483647
118 #endif
120 #ifdef WIN_32
121 #define LITTLE_ENDIAN
122 #define MY_VASPRINTF
123 #define MY_SETENV
124 #define LINE_FEED "\r\n"
125 #define isSocketStream(A) ((A)->_flag == 0xFFFF)
126 #define getSocket(A) ((A)->_file)
127 #define vasprintf my_vasprintf
128 #define setenv my_setenv
129 #define random rand
130 #define srandom srand
131 #define ioctl ioctlsocket
132 #define off_t off64_t
133 #define lseek lseek64
134 #define ftell ftello64
136 #ifndef SUPPORT_UTF8
137 #define mkdir _mkdir
138 #define rmdir _rmdir
139 #define lstat stat
140 #endif
142 #define realpath win32_realpath
144 /* WIN_32 UTF16 support for file paths */
145 #ifdef SUPPORT_UTF8
146 #define USE_WIN_UTF16PATH
147 #define rename rename_utf16
148 #define open open_utf16
149 #define mkdir mkdir_utf16
150 #define rmdir rmdir_utf16
151 #define unlink unlink_utf16
152 #define chdir chdir_utf16
153 #define opendir opendir_utf16
155 #define DIR _WDIR
156 #define lstat _wstat
157 #define dirent _wdirent
158 #define readdir _wreaddir
159 #define closedir _wclosedir
160 #endif /* SUPPORT_UTF8 */
162 #endif /* WIN_32 */
165 #ifndef WIN_32
166 #define LINE_FEED "\n"
167 #define NET_PING
168 #define NANOSLEEP
169 #endif
171 #ifndef O_BINARY
172 #define O_BINARY 0
173 #endif
175 #define UTF8_MAX_BYTES 6
177 #define UINT unsigned long
179 #define INT16 short int
180 #ifndef NEWLISP64
181 #define MAX_LONG 0x7FFFFFFF
182 #else
183 #define MAX_LONG 0x7FFFFFFFFFFFFFFFLL
184 #endif
186 #ifndef NEWLISP64
187 #ifdef TRU64
188 #define INT64 long
189 #define UINT64 unsigned long
190 #else
191 #define INT64 long long int
192 #define UINT64 unsigned long long int
193 #endif
194 #else
195 #define INT64 long
196 #define UINT64 unsigned long
197 #endif
199 #define pushEnvironment(A) (*(envStack + envStackIdx++) = (UINT)(A))
200 #define popEnvironment() (*(envStack + --envStackIdx))
202 #define pushResult(A) (*(resultStack + resultStackIdx++) = (UINT)(A))
203 #define popResult() ((CELL *)*(resultStack + --resultStackIdx))
205 #define freeMemory free
207 #define INT32_MIN_AS_INT64 (((long long int)0xFFFFFFFF << 32) | 0x80000000)
208 #define MY_INT64_MAX (((long long int)0x7FFFFFFF << 32) | 0xFFFFFFFF)
209 #define TRUE 1
210 #define FALSE 0
212 #define MAX_STRING 2048
213 #define MAX_LINE 255
214 #define MAX_SYMBOL 255
216 #define MAX_READ_LEN 0x4000
217 #define MAX_PRINT_LEN 0x4000
218 #define MAX_LOAD_BUFFER 0x4000
219 #define MAX_FILE_BUFFER 0x4000
220 #define MAX_BLOCK 1023
221 #define MAX_URL_LEN 256
223 #define MAX_REGEX_EXP 16
225 /* token types */
226 #define TKN_ERROR -1
227 #define TKN_EMPTY 0
228 #define TKN_CHARACTER 1
229 #define TKN_HEX 2
230 #define TKN_OCTAL 3
231 #define TKN_DECIMAL 4
232 #define TKN_FLOAT 5
233 #define TKN_STRING 6
234 #define TKN_SYMBOL 7
235 #define TKN_CONTEXT 8
236 #define TKN_LEFT_PAR '('
237 #define TKN_RIGHT_PAR ')'
238 #define TKN_QUOTE '\''
240 /* symbol flags types and masks */
241 #define PRINT_TYPE_MASK 0x0F
242 #define SYMBOL_PROTECTED 0x10
243 #define SYMBOL_GLOBAL 0x20
244 #define SYMBOL_BUILTIN 0x40
245 #define SYMBOL_PRIMITIVE 0x80
247 /* cell masks */
248 #define RAW_TYPE_MASK 0x0FFF
249 #define COMPARE_TYPE_MASK 0x000F
250 #define ENVELOPE_TYPE_MASK 0x0010
251 #define LIST_TYPE_MASK 0x0020
252 #define SYMBOL_TYPE_MASK 0x0040
253 #define NUMBER_TYPE_MASK 0x0080
254 #define EVAL_SELF_TYPE_MASK 0x0100
255 #define INT64_MASK 0x0200
257 /* cell types */
258 #define CELL_NIL (0 | EVAL_SELF_TYPE_MASK)
259 #define CELL_TRUE (1 | EVAL_SELF_TYPE_MASK)
260 #define CELL_INT 2 /* any INT */
261 #define CELL_LONG (2 | EVAL_SELF_TYPE_MASK | NUMBER_TYPE_MASK)
262 #define CELL_INT64 (2 | EVAL_SELF_TYPE_MASK | NUMBER_TYPE_MASK | INT64_MASK)
263 #define CELL_FLOAT (3 | EVAL_SELF_TYPE_MASK | NUMBER_TYPE_MASK)
264 #define CELL_STRING (4 | EVAL_SELF_TYPE_MASK)
265 #define CELL_SYMBOL (5 | SYMBOL_TYPE_MASK)
266 #define CELL_CONTEXT 6
267 #define CELL_PRIMITIVE (7 | EVAL_SELF_TYPE_MASK)
268 #define CELL_IMPORT_CDECL (8 | EVAL_SELF_TYPE_MASK)
269 #define CELL_IMPORT_DLL (9 | EVAL_SELF_TYPE_MASK)
270 #define CELL_QUOTE (10 | ENVELOPE_TYPE_MASK)
271 #define CELL_EXPRESSION (11 | ENVELOPE_TYPE_MASK | LIST_TYPE_MASK)
272 #define CELL_LAMBDA (12 | ENVELOPE_TYPE_MASK | LIST_TYPE_MASK | EVAL_SELF_TYPE_MASK)
273 #define CELL_MACRO (13 | ENVELOPE_TYPE_MASK | LIST_TYPE_MASK | EVAL_SELF_TYPE_MASK)
274 #define CELL_ARRAY (14 | ENVELOPE_TYPE_MASK | EVAL_SELF_TYPE_MASK)
275 #define CELL_DYN_SYMBOL (15 | SYMBOL_TYPE_MASK)
276 #define CELL_FREE 0xFF
278 #define isEnvelope(A) ((A) & ENVELOPE_TYPE_MASK)
279 #define isList(A) ((A) & LIST_TYPE_MASK)
280 #define isArray(A) ((A) == CELL_ARRAY)
281 #define isString(A) ((A) == CELL_STRING)
282 #define isNumber(A) ((A) & NUMBER_TYPE_MASK)
283 #define isSymbol(A) ((A) & SYMBOL_TYPE_MASK)
284 #define isSelfEval(A) ((A) & EVAL_SELF_TYPE_MASK)
285 #define isProtected(A) ((A) & SYMBOL_PROTECTED)
286 #define isGlobal(A) ((A) & SYMBOL_GLOBAL)
287 #define isDigit(A) isdigit((int)(A))
289 #define isNil(A) ((A)->type == CELL_NIL || ((A)->type == CELL_SYMBOL && (A)->contents == (UINT)nilSymbol))
290 #define isTrue(A) ((A)->type == CELL_TRUE || ((A)->type == CELL_SYMBOL && (A)->contents == (UINT)trueSymbol))
291 #define isEmpty(A) ((A)->type == CELL_EXPRESSION && (A)->contents == (UINT)nilCell)
293 #define symbolType(A) ((CELL*)(A)->contents)->type
295 /* redefine some functions */
296 #ifdef NEWLISP64
297 #define stuffInteger64 stuffInteger
298 #endif
301 /* RED BLACK binary balanced tree: nl-symbol.c */
302 #define BLACK 0
303 #define RED 1
304 #define NIL_SYM &sentinel
305 #define LOOKUP_ONLY 0 /* symbol lookup only, if not found return NULL */
306 #define FORCE_CREATION 1 /* if symbol does not exist, create it */
309 /* traceFlag */
310 #define TRACE_TRUE 0x0001
311 #define TRACE_IN_ENTRY 0x0002
312 #define TRACE_IN_EXIT 0x0004
313 #define TRACE_IN_DEBUG 0x0008
314 #define TRACE_DEBUG_PENDING 0x0010
315 #define TRACE_DEBUG_EVAL 0x0020
316 #define TRACE_DEBUG_STEP 0x0040
317 #define TRACE_DEBUG_NEXT 0x0080
318 #define TRACE_SIGINT 0x1000
319 #define TRACE_TIMER 0x2000
320 #define TRACE_SIGNAL 0x4000
322 /* error handling */
324 #define ERR_NOT_ENOUGH_MEMORY 1
325 #define ERR_OUT_OF_ENV_STACK 2
326 #define ERR_OUT_OF_CALL_STACK 3
327 #define ERR_ACCESSING_FILE 4
328 #define ERR_EXPRESSION 5
329 #define ERR_MISSING_PAR 6
330 #define ERR_STRING_TOO_LONG 7
331 #define ERR_MISSING_ARGUMENT 8
332 #define ERR_NUMBER_OR_STRING_EXPECTED 9
333 #define ERR_NUMBER_EXPECTED 10
334 #define ERR_STRING_EXPECTED 11
335 #define ERR_SYMBOL_EXPECTED 12
336 #define ERR_CONTEXT_EXPECTED 13
337 #define ERR_SYMBOL_OR_CONTEXT_EXPECTED 14
338 #define ERR_LIST_EXPECTED 15
339 #define ERR_LIST_OR_ARRAY_EXPECTED 16
340 #define ERR_LIST_OR_SYMBOL_EXPECTED 17
341 #define ERR_LIST_OR_STRING_EXPECTED 18
342 #define ERR_LIST_OR_NUMBER_EXPECTED 19
343 #define ERR_ARRAY_EXPECTED 20
344 #define ERR_ARRAY_LIST_OR_STRING_EXPECTED 21
345 #define ERR_LAMBDA_EXPECTED 22
346 #define ERR_MACRO_EXPECTED 23
347 #define ERR_INVALID_FUNCTION 24
348 #define ERR_INVALID_LAMBDA 25
349 #define ERR_INVALID_MACRO 26
350 #define ERR_INVALID_LET 27
351 #define ERR_SAVING_FILE 28
352 #define ERR_MATH 29
353 #define ERR_NOT_MATRIX 30
354 #define ERR_WRONG_DIMENSIONS 31
355 #define ERR_SINGULAR 32
356 #define ERR_REGULAR_EXPRESSION 33
357 #define ERR_THROW_WO_CATCH 34
358 #define ERR_IMPORT_LIB_NOT_FOUND 35
359 #define ERR_IMPORT_FUNC_NOT_FOUND 36
360 #define ERR_SYMBOL_PROTECTED 37
361 #define ERR_NUMBER_OUT_OF_RANGE 38
362 #define ERR_REGEX 39
363 #define ERR_MISSING_TEXT_END 40
364 #define ERR_FORMAT_NUM_ARGS 41
365 #define ERR_FORMAT_STRING 42
366 #define ERR_FORMAT_DATA_TYPE 43
367 #define ERR_INVALID_PARAMETER 44
368 #define ERR_INVALID_PARAMETER_0 45
369 #define ERR_INVALID_PARAMETER_NAN 46
370 #define ERR_ILLEGAL_TYPE 47
371 #define ERR_NOT_IN_MAIN 48
372 #define ERR_NOT_CURRENT_CONTEXT 49
373 #define ERR_TARGET_NO_MAIN 50
374 #define ERR_LIST_INDEX_OUTOF_BOUNDS 51
375 #define ERR_ARRAY_INDEX_OUTOF_BOUNDS 52
376 #define ERR_STRING_INDEX_OUTOF_BOUNDS 53
377 #define ERR_NESTING_TOO_DEEP 54
378 #define ERR_SYNTAX_WRONG 55
379 #define ERR_USER_ERROR 56
380 #define ERR_USER_RESET 57
381 #define ERR_SIGINT 58
382 #define ERR_NOT_REENTRANT 59
383 #define MAX_ERROR_NUMBER 59
385 /* I/O routines */
387 #define OUT_NULL 0
388 #define OUT_DEVICE 1
389 #define OUT_CONSOLE 2
390 #define OUT_LOG 3
392 /* HTTP in nl-web.c */
393 #define HTTP_GET_URL 0
394 #define HTTP_GET_HEAD 1
395 #define HTTP_PUT_URL 2
396 #define HTTP_PUT_APPEND_URL 3
397 #define HTTP_POST_URL 4
398 #define HTTP_DELETE_URL 5
400 /* additional regex option */
401 #define REPLACE_ONCE 0x8000
403 extern int vasprintf (char **, const char *, va_list);
405 /* ---------------------------- standard types ------------------------- */
407 typedef struct
409 int handle;
410 char *ptr;
411 char *buffer;
412 size_t position;
413 size_t size;
414 } STREAM;
416 typedef struct tagSYMBOL
418 short int flags;
419 short int color;
420 char * name;
421 UINT contents;
422 struct tagSYMBOL * context;
423 struct tagSYMBOL * parent;
424 struct tagSYMBOL * left;
425 struct tagSYMBOL * right;
426 } SYMBOL;
428 typedef struct
430 UINT type;
431 void * next;
432 UINT aux;
433 UINT contents;
434 } CELL;
436 typedef struct
438 char * name;
439 CELL * (*function)(CELL *);
440 short int prettyPrint;
441 } PRIMITIVE;
443 /* --------------------------- globals -------------------------------- */
445 extern char startupDir[];
446 extern FILE * IOchannel;
447 extern int MAX_CPU_STACK;
448 extern long MAX_CELL_COUNT;
449 extern int version;
450 extern int opsys;
451 extern char ostype[];
452 extern size_t cellCount;
453 extern size_t symbolCount;
454 extern int recursionCount;
455 extern UINT printDevice;
456 extern UINT * resultStack;
457 extern UINT * envStack;
458 extern int resultStackIdx;
459 extern int envStackIdx;
460 extern CELL * trueCell;
461 extern CELL * nilCell;
462 extern STREAM strStream;
463 extern SYMBOL * nilSymbol;
464 extern SYMBOL * trueSymbol;
465 extern SYMBOL * startSymbol;
466 extern SYMBOL * questionSymbol;
467 extern SYMBOL * atSymbol;
468 extern SYMBOL * mainContext;
469 extern SYMBOL * currentContext;
470 extern SYMBOL * errorEvent;
471 extern SYMBOL sentinel;
472 extern int commandLineFlag;
473 extern int traceFlag;
474 extern int errorReg;
475 extern char * errorMessage[];
476 extern jmp_buf errorJump;
477 extern int pushResultFlag;
478 extern int prettyPrintFlags;
479 #define PRETTYPRINT_DOUBLE 1
480 #define PRETTYPRINT_STRING 2
481 extern char lc_decimal_point;
482 /* end of file */