MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | // LEX.C -- creating tolkens from input string |
| 2 | // (c) 1994 by Hate@MorgenGrauen, TUBmud, NightFall |
| 3 | // -- |
| 4 | // Copy, change and distribute this bit of software as much as you like, |
| 5 | // but keep the name of the original author in the header. |
| 6 | |
| 7 | #pragma strong_types |
| 8 | |
| 9 | #include "tweak.h" |
| 10 | |
| 11 | private inherit HOME("microcode"); |
| 12 | private inherit HOME("escape"); |
| 13 | |
| 14 | private nosave mixed *tokens; |
| 15 | |
| 16 | static void create() |
| 17 | { |
| 18 | microcode::create(); |
| 19 | tokens = ({}); |
| 20 | } |
| 21 | |
| 22 | nomask private mixed make_symbol(string str) |
| 23 | { |
| 24 | mixed sym; |
| 25 | |
| 26 | if(!stringp(str)) return str; |
| 27 | if(sym = get_function(str)) // FUNCTIONS |
| 28 | return sym; |
| 29 | if(str == "list") |
| 30 | return #'({; |
| 31 | if(str[0] == '"') |
| 32 | return implode(map(regexplode(str, "\\\\."), #'unescape), ""); |
| 33 | if((str[0] >= '0' && str[0] <= '9') || // NUMBERS |
| 34 | ((str[0] == '+' || str[0] == '-') && |
| 35 | (str[1] >= '0' && str[1] <= '9'))) |
| 36 | if(member(str, '.') != -1) // FLOATS |
| 37 | return to_float(str); |
| 38 | else |
| 39 | return to_int(str); // INTEGERS |
| 40 | |
| 41 | if(str == "(" || str == ")" || str == "'") |
| 42 | return str; |
| 43 | |
| 44 | return quote(str); // SYMBOLS |
| 45 | } |
| 46 | |
| 47 | nomask private void get_tokens() |
| 48 | { |
| 49 | mixed input, tmp; |
| 50 | |
| 51 | do { |
| 52 | if(!input = get_line()) return 0; |
| 53 | // splitting the input line in string tokens and removing comments |
| 54 | tmp = regexp(regexplode(input, |
| 55 | "(\"(\\\\.|[^\\\\\"])*\")|[;].*|[() \t']"), |
| 56 | "^[^;]") |
| 57 | - ({""," ","\t"}); |
| 58 | tokens += map(tmp, #'make_symbol); |
| 59 | } |
| 60 | while(!sizeof(tokens)); |
| 61 | } |
| 62 | |
| 63 | nomask static int lex(mixed token) |
| 64 | { |
| 65 | if(!sizeof(tokens)) get_tokens(); |
| 66 | if(sizeof(tokens)) |
| 67 | { |
| 68 | token = tokens[0]; |
| 69 | tokens[0..0] = ({}); |
| 70 | return 1; |
| 71 | } |
| 72 | return 0; |
| 73 | } |