annotate lwbasic/lexer.c @ 35:cdb0175e1063

More work on expressions
author Lost Wizard (lost@starbug3)
date Sat, 05 Feb 2011 14:22:54 -0700
parents bfea77812e64
children 5325b640424d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
1 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
2 lexer.c
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
3
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2011 William Astle
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
5
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
7
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
11 version.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
12
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
16 more details.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
17
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
20 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
21
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
22 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
23 This handles the gritty details of parsing tokens
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
24 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
26 #include <stdlib.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
27 #include <stdio.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
28 #include <string.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
29
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
30 #include <lw_alloc.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
31 #include <lw_string.h>
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
32
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
33 #define __lexer_c_seen__
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
34 #include "lwbasic.h"
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
35
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
36 /*
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
37 A token idenfier is returned by lexer(). The actual string value
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
38 is found in state->lexer_lexer_token_string; if the token as an integer value,
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
39 it will be found in state->lexer_token_number in the appropriate "value"
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
40 slot.
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
41 */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
42
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
43 struct token_list
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
44 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
45 char *string;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
46 int token;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
47 };
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
48
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
49 /* keywords that appear as part of normal expressions */
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
50 static struct token_list lexer_global_tokens[] =
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
51 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
52 { "function", token_kw_function },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
53 { "sub", token_kw_sub },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
54 { "public", token_kw_public },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
55 { "private", token_kw_private },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
56 { "as", token_kw_as },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
57 { "params", token_kw_params },
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
58 { "returns", token_kw_returns },
26
26aa76da75ad Additional parsing in function/sub; emission of prolog/epilog code
lost@l-w.ca
parents: 25
diff changeset
59 { "integer", token_kw_integer },
26aa76da75ad Additional parsing in function/sub; emission of prolog/epilog code
lost@l-w.ca
parents: 25
diff changeset
60 { "endsub", token_kw_endsub },
26aa76da75ad Additional parsing in function/sub; emission of prolog/epilog code
lost@l-w.ca
parents: 25
diff changeset
61 { "endfunction", token_kw_endfunction },
33
890a8f688889 Basic parsing of local variable decls
lost@l-w.ca
parents: 31
diff changeset
62 { "dim", token_kw_dim },
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
63 { NULL }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
64 };
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
65
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
66 /* contains "built in" function names */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
67 static struct token_list lexer_expr_tokens[] =
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
68 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
69 { "and", token_op_and },
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
70 { "or", token_op_or },
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
71 { "band", token_op_band },
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
72 { "bor", token_op_bor },
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
73 { "bxor", token_op_bxor },
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
74 { "xor", token_op_xor },
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
75 { NULL }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
76 };
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
77
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
78 static char *lexer_token_names[] =
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
79 {
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
80 "SUB",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
81 "FUNCTION",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
82 "AS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
83 "PUBLIC",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
84 "PRIVATE",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
85 "PARAMS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
86 "RETURNS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
87 "INTEGER",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
88 "ENDSUB",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
89 "ENDFUNCTION",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
90 "DIM",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
91 "<assignment>",
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
92 "<equality>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
93 "<greater>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
94 "<less>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
95 "<greaterequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
96 "<lessequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
97 "<notequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
98 "<and>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
99 "<or>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
100 "<xor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
101 "<bitwiseand>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
102 "<bitwiseor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
103 "<bitwisexor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
104 "<plus>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
105 "<minus>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
106 "<times>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
107 "<divide>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
108 "<modulus>",
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
109 "<identifier>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
110 "<char>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
111 "<uint>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
112 "<int>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
113 "<eol>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
114 "<eof>"
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
115 };
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
116
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
117 char *lexer_token_name(int token)
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
118 {
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
119 if (token > token_eol)
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
120 return "???";
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
121 return lexer_token_names[token];
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
122 }
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
123
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
124 static int lexer_getchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
125 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
126 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
127 c = input_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
128 if (c == -2)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
129 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
130 lwb_error("Error reading input stream.");
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
131 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
132 return c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
133 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
134
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
135 static void lexer_nextchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
136 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
137 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
138 if (state -> lexer_curchar == state -> lexer_ignorechar)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
139 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
140 state -> lexer_ignorechar = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
141 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
142
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
143 static int lexer_curchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
144 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
145 if (state -> lexer_curchar == -1)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
146 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
147 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
148 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
149
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
150 return state -> lexer_curchar;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
151 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
152
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
153 static void lexer_skip_white(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
154 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
155 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
156
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
157 for (;;)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
158 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
159 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
160 if (!(c == 0 || c == ' ' || c == '\t'))
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
161 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
162 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
163 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
164 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
165
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
166 /* must not be called unless the word will be non-zero length */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
167 static void lexer_word(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
168 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
169 int wordlen = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
170 int wordpos = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
171 char *word = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
172 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
173 struct token_list *tok = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
174
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
175 for (;;) {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
176 c = lexer_curchar(state);
26
26aa76da75ad Additional parsing in function/sub; emission of prolog/epilog code
lost@l-w.ca
parents: 25
diff changeset
177 if (c == '_' || (c >= '0' && c <= '9' ) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80)
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
178 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
179 /* character is part of word */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
180 if (wordpos >= wordlen)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
181 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
182 word = lw_realloc(word, wordlen + 32);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
183 wordlen += 32;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
184 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
185 word[wordpos++] = c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
186 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
187 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
188 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
189
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
190 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
191 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
192
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
193 word[wordpos] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
194 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
195 state -> lexer_token_string = lw_strdup(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
196
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
197 switch (state -> parser_state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
198 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
199 default:
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
200 tok = lexer_global_tokens;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
201 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
202
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
203 if (state -> expression)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
204 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
205 tok = lexer_expr_tokens;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
206 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
207
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
208 /* check for tokens if appropriate */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
209 /* force uppercase */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
210 if (tok)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
211 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
212 for (c = 0; word[c]; c++)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
213 if (word[c] >= 'A' && word[c] <= 'Z')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
214 word[c] = word[c] + 0x20;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
215
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
216 while (tok -> string)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
217 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
218 if (strcmp(tok -> string, word) == 0)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
219 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
220 tok++;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
221 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
222 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
223
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
224 lw_free(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
225 if (tok && tok -> string)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
226 state -> lexer_token = tok -> token;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
227 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
228 state -> lexer_token = token_identifier;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
229 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
230
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
231 static void lexer_parse_number(cstate *state, int neg)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
232 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
233 unsigned long tint = 0;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
234 int c;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
235
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
236 for (;;)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
237 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
238 c = lexer_curchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
239 if (c >= '0' && c <= '9')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
240 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
241 tint *= 10 + (c - '0');
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
242 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
243 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
244 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
245 /* end of the number here */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
246 if (neg)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
247 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
248 if (tint > 0x80000000)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
249 lwb_error("Integer overflow\n");
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
250 state -> lexer_token_number.integer = -tint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
251 state -> lexer_token = token_int;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
252 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
253 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
254 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
255 state -> lexer_token = token_uint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
256 state -> lexer_token_number.uinteger = tint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
257 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
258 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
259 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
260 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
261 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
262 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
263
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
264 static void lexer_empty_token(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
265 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
266 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
267 state -> lexer_token_string = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
268 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
269
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
270 void lexer(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
271 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
272 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
273
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
274 lexer_skip_white(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
275
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
276 lexer_empty_token(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
277
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
278 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
279 if (c == -1)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
280 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
281 state -> lexer_token = token_eof;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
282 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
283 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
284
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
285 if (c == '\n')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
286 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
287 /* LF */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
288 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
289 state -> lexer_ignorechar = '\r';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
290 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
291 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
292 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
293
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
294 if (c == '\r')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
295 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
296 /* CR */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
297 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
298 state -> lexer_ignorechar = '\n';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
299 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
300 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
301 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
302
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
303 if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
304 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
305 /* we have a word here; identifier, keyword, etc. */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
306 lexer_word(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
307 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
308 }
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
309
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
310 if (state -> expression && c >= '0' && c <= '9')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
311 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
312 /* we have a number */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
313 lexer_parse_number(state, 0);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
314 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
315 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
316
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
317 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
318 if (state -> expression)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
319 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
320 if (c == '-' && lexer_curchar(state) >= '0' && lexer_curchar(state) <= '9')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
321 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
322 /* we have a negative number here */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
323 lexer_parse_number(state, 1);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
324 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
325 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
326 if (c == '=')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
327 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
328 state -> lexer_token = token_op_equality;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
329 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
330 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
331 if (c == '<')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
332 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
333 if (lexer_curchar(state) == '=')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
334 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
335 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
336 state -> lexer_token = token_op_lessequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
337 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
338 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
339 if (lexer_curchar(state) == '>')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
340 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
341 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
342 state -> lexer_token = token_op_notequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
343 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
344 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
345 state -> lexer_token = token_op_less;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
346 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
347 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
348 if (c == '>')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
349 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
350 if (lexer_curchar(state) == '>')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
351 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
352 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
353 state -> lexer_token = token_op_greaterequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
354 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
355 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
356 if (lexer_curchar(state) == '<')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
357 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
358 state -> lexer_token = token_op_notequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
359 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
360 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
361 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
362 state -> lexer_token = token_op_greater;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
363 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
364 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
365 switch(c)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
366 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
367 case '+':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
368 state -> lexer_token = token_op_plus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
369 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
370
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
371 case '-':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
372 state -> lexer_token = token_op_minus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
373 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
374
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
375 case '/':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
376 state -> lexer_token = token_op_divide;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
377 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
378
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
379 case '*':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
380 state -> lexer_token = token_op_times;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
381 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
382
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
383 case '%':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
384 state -> lexer_token = token_op_modulus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
385 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
386
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
387
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
388 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
389 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
390 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
391 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
392 if (c == '=')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
393 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
394 state -> lexer_token = token_op_assignment;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
395 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
396 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
397 }
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
398
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
399 /* return the character if all else fails */
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
400 state -> lexer_token = token_char;
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
401 state -> lexer_token_string = lw_realloc(state -> lexer_token_string, 2);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
402 state -> lexer_token_string[0] = c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
403 state -> lexer_token_string[1] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
404 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
405 }
31
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
406
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
407 char *lexer_return_token(cstate *state)
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
408 {
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
409 static char *buffer = NULL;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
410 static int buflen = 0;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
411 int l;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
412
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
413 if (buflen == 0)
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
414 {
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
415 buffer = lw_alloc(128);
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
416 buflen = 128;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
417 }
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
418
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
419 l = snprintf(buffer, buflen, "%s (%s)", state -> lexer_token_string, lexer_token_name(state -> lexer_token));
31
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
420 if (l >= buflen)
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
421 {
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
422 buffer = lw_realloc(buffer, l + 1);
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
423 buflen = l + 1;
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
424 snprintf(buffer, buflen, "%s (%s)", state -> lexer_token_string, lexer_token_name(state -> lexer_token));
31
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
425 }
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
426 return buffer;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
427 }