1 /******************************************************
8 * Copyright (c) 2006, 2007 Charles R. Childers
10 * Permission to use, copy, modify, and distribute this
11 * software for any purpose with or without fee is hereby
12 * granted, provided that the above copyright notice and
13 * this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR
16 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
19 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
21 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 ******************************************************/
35 extern VM_STACK data
, address
, alternate
;
40 /******************************************************
43 *|F| Current file stream to parse from. Setup as
44 *|F| an array of 8 inputs.
47 *|F| Pointer to the most recent input source in the array
50 *|F| Holds the current numeric base
53 *|F| When ON (TRUE), system parsing words will parse. When
54 *|F| OFF (FALSE), they will take a string from the stack.
57 *|F| When ON (TRUE), escape sequences will be handled
58 *|F| by the parser. When OFF (FALSE), they will be ignored.
61 *|G| base ( -a ) Variable containg the current
63 *|G| parser ( -a ) Variable holding current parser
65 *|G| escape-sequences ( -a) Variable determining if
66 *|G| escape sequences are used.
67 ******************************************************/
75 /******************************************************
76 *|G| >number ( a-nf ) Attempt to convert a string
80 *|F| Attempt to convert a string (on TOS) to a number.
81 *|F| This accepts a format of:
82 *|F| [prefix][-]number
83 *|F| If successful, it leaves the number and a flag of
84 *|F| TRUE on the stack. Otherwise, it leaves the original
85 *|F| string, and a flag of FALSE.
87 ******************************************************/
93 s
= (char *)TOS
; DROP
;
143 vm_push(strtol(s
, (char **)NULL
, base
));
153 /******************************************************
154 *|G| >string ( n-a ) Convert a number to a string
157 *|F| Convert a number (on TOS) to a string.
159 ******************************************************/
163 s
= gc_alloc(128, sizeof(char), GC_MEM
);
166 case 8: snprintf(s
, 128, "%lo", TOS
); break;
167 case 16: snprintf(s
, 128, "%lx", TOS
); break;
168 default: snprintf(s
, 128, "%li", TOS
); break;
175 /******************************************************
176 *|G| parse ( d-a ) Parse until the character
177 *|G| represented by 'd' is found.
178 *|G| Return a pointer to the string
181 *|F| Parse the input buffer until the character passed
182 *|F| on TOS is found, or until the end of the line is
183 *|F| encountered. Return a pointer to the resulting
184 *|F| string on the stack.
186 ******************************************************/
193 s
= gc_alloc(4096, sizeof(char), GC_MEM
);
199 /******************************************************
200 *|F| get_token(char *s, long delim)
201 *|F| Return a string (in "s") up to the specified
202 *|F| delimiter. This also puts the resulting string
205 ******************************************************/
206 void get_token(char *s
, long delim
)
215 if ((c
= getc(input
[isp
])) == EOF
&& input
[isp
] != stdin
)
221 if (c
== EOF
&& input
[isp
] == stdin
)
226 if (c
== '\\' && escapes
== TRUE
)
228 c
= getc(input
[isp
]);
251 if (delim
== 10 || delim
== 32)
253 if (c
== 10 || c
== 13)
261 if (c
>= 32 || c
== 10 || c
== 13)
270 /******************************************************
271 *|F| long include_file(char *s)
272 *|F| Attempt to open a file ("s") and add it to the
273 *|F| top of the input stack.
275 ******************************************************/
276 long include_file(char *s
)
281 file
= fopen(s
, "r");
296 /******************************************************
297 *|G| include ( "- ) Attempt to open a file and
298 *|G| add it to the input stack.
299 *|G| ( $- ) Non-parsing form
302 *|F| Take a filename off the stack, attempt to open
303 *|F| it and add it to the input stream if successful.
305 ******************************************************/
312 s
= gc_alloc(256, sizeof(char), GC_TEMP
);
313 get_token(s
, 32); DROP
;
317 s
= (char *)TOS
; DROP
;
325 /******************************************************
326 *|G| needs ( "- ) Attempt to include a file
327 *|G| from the library (normally
328 *|G| /usr/share/toka/library)
329 *|G| ( $- ) Non-parsing form
332 *|F| Take a filename off the stack. Attempt to open it
333 *|F| from the library, and add it to the input stream
336 ******************************************************/
342 d
= gc_alloc(384, sizeof(char), GC_TEMP
);
347 s
= gc_alloc(256, sizeof(char), GC_TEMP
);
348 get_token(s
, 32); DROP
;
352 s
= (char *)TOS
; DROP
;
360 /******************************************************
361 *|G| end. ( - ) Remove the current file from
365 *|F| Remove the current file from the input stack. This
366 *|F| can be used to abort an include.
368 ******************************************************/
378 error(ERROR_CLOSE_STDIN
);