annotate lwbasic/lexer.c @ 100:7ce01324e391

Added tag lwtools-4.2 for changeset 1cb23a4e1e7d
author lost@l-w.ca
date Sat, 06 Aug 2011 10:51:33 -0600
parents 5325b640424d
children
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 },
36
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
75 { "not", token_op_not },
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
76 { "bnot", token_op_bnot },
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
77 { NULL }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
78 };
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
79
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
80 static char *lexer_token_names[] =
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
81 {
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
82 "SUB",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
83 "FUNCTION",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
84 "AS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
85 "PUBLIC",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
86 "PRIVATE",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
87 "PARAMS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
88 "RETURNS",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
89 "INTEGER",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
90 "ENDSUB",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
91 "ENDFUNCTION",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
92 "DIM",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
93 "<assignment>",
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
94 "<equality>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
95 "<greater>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
96 "<less>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
97 "<greaterequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
98 "<lessequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
99 "<notequal>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
100 "<and>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
101 "<or>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
102 "<xor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
103 "<bitwiseand>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
104 "<bitwiseor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
105 "<bitwisexor>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
106 "<plus>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
107 "<minus>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
108 "<times>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
109 "<divide>",
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
110 "<modulus>",
36
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
111 "<openparen>",
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
112 "<closeparen>",
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
113 "<not>",
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
114 "<bitwisenot>",
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
115 "<identifier>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
116 "<char>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
117 "<uint>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
118 "<int>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
119 "<eol>",
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
120 "<eof>"
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
121 };
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 char *lexer_token_name(int token)
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
124 {
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
125 if (token > token_eol)
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
126 return "???";
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
127 return lexer_token_names[token];
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
128 }
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
129
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
130 static int lexer_getchar(cstate *state)
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 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
133 c = input_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
134 if (c == -2)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
135 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
136 lwb_error("Error reading input stream.");
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
137 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
138 return c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
139 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
140
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
141 static void lexer_nextchar(cstate *state)
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 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
144 if (state -> lexer_curchar == state -> lexer_ignorechar)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
145 state -> lexer_curchar = lexer_getchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
146 state -> lexer_ignorechar = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
147 }
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 static int lexer_curchar(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
150 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
151 if (state -> lexer_curchar == -1)
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 lexer_nextchar(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
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
156 return state -> lexer_curchar;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
157 }
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 static void lexer_skip_white(cstate *state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
160 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
161 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
162
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
163 for (;;)
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 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
166 if (!(c == 0 || c == ' ' || c == '\t'))
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
167 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
168 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
169 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
170 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
171
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
172 /* must not be called unless the word will be non-zero length */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
173 static void lexer_word(cstate *state)
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 int wordlen = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
176 int wordpos = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
177 char *word = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
178 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
179 struct token_list *tok = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
180
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
181 for (;;) {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
182 c = lexer_curchar(state);
26
26aa76da75ad Additional parsing in function/sub; emission of prolog/epilog code
lost@l-w.ca
parents: 25
diff changeset
183 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
184 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
185 /* character is part of word */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
186 if (wordpos >= wordlen)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
187 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
188 word = lw_realloc(word, wordlen + 32);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
189 wordlen += 32;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
190 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
191 word[wordpos++] = c;
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 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
194 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
195
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
196 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
197 }
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 word[wordpos] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
200 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
201 state -> lexer_token_string = lw_strdup(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
202
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
203 switch (state -> parser_state)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
204 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
205 default:
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
206 tok = lexer_global_tokens;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
207 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
208
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
209 if (state -> expression)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
210 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
211 tok = lexer_expr_tokens;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
212 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
213
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
214 /* check for tokens if appropriate */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
215 /* force uppercase */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
216 if (tok)
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 for (c = 0; word[c]; c++)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
219 if (word[c] >= 'A' && word[c] <= 'Z')
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
220 word[c] = word[c] + 0x20;
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 while (tok -> string)
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 if (strcmp(tok -> string, word) == 0)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
225 break;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
226 tok++;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
227 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
228 }
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 lw_free(word);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
231 if (tok && tok -> string)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
232 state -> lexer_token = tok -> token;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
233 else
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
234 state -> lexer_token = token_identifier;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
235 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
236
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
237 static void lexer_parse_number(cstate *state, int neg)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
238 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
239 unsigned long tint = 0;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
240 int c;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
241
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
242 for (;;)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
243 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
244 c = lexer_curchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
245 if (c >= '0' && c <= '9')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
246 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
247 tint *= 10 + (c - '0');
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
248 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
249 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
250 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
251 /* end of the number here */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
252 if (neg)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
253 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
254 if (tint > 0x80000000)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
255 lwb_error("Integer overflow\n");
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
256 state -> lexer_token_number.integer = -tint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
257 state -> lexer_token = token_int;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
258 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
259 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
260 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
261 state -> lexer_token = token_uint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
262 state -> lexer_token_number.uinteger = tint;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
263 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
264 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
265 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
266 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
267 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
268 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
269
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
270 static void lexer_empty_token(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 lw_free(state -> lexer_token_string);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
273 state -> lexer_token_string = NULL;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
274 }
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 void lexer(cstate *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 int c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
279
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
280 lexer_skip_white(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
281
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
282 lexer_empty_token(state);
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 c = lexer_curchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
285 if (c == -1)
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 state -> lexer_token = token_eof;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
288 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
289 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
290
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
291 if (c == '\n')
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 /* LF */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
294 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
295 state -> lexer_ignorechar = '\r';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
296 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
297 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
298 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
299
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
300 if (c == '\r')
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 /* CR */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
303 lexer_nextchar(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
304 state -> lexer_ignorechar = '\n';
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
305 state -> lexer_token = token_eol;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
306 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
307 }
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
308
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
309 if (c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 0x80)
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
310 {
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
311 /* we have a word here; identifier, keyword, etc. */
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
312 lexer_word(state);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
313 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
314 }
35
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 if (state -> expression && c >= '0' && c <= '9')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
317 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
318 /* we have a number */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
319 lexer_parse_number(state, 0);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
320 return;
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
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
323 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
324 if (state -> expression)
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 == '-' && lexer_curchar(state) >= '0' && lexer_curchar(state) <= '9')
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 /* we have a negative number here */
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
329 lexer_parse_number(state, 1);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
330 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
331 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
332 if (c == '=')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
333 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
334 state -> lexer_token = token_op_equality;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
335 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
336 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
337 if (c == '<')
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_lessequal;
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 if (lexer_curchar(state) == '>')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
346 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
347 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
348 state -> lexer_token = token_op_notequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
349 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
350 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
351 state -> lexer_token = token_op_less;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
352 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
353 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
354 if (c == '>')
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 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
359 state -> lexer_token = token_op_greaterequal;
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 if (lexer_curchar(state) == '<')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
363 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
364 state -> lexer_token = token_op_notequal;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
365 lexer_nextchar(state);
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
366 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
367 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
368 state -> lexer_token = token_op_greater;
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 switch(c)
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
372 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
373 case '+':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
374 state -> lexer_token = token_op_plus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
375 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
376
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
377 case '-':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
378 state -> lexer_token = token_op_minus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
379 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
380
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
381 case '/':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
382 state -> lexer_token = token_op_divide;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
383 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
384
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
385 case '*':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
386 state -> lexer_token = token_op_times;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
387 return;
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 case '%':
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
390 state -> lexer_token = token_op_modulus;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
391 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
392
36
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
393 case '(':
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
394 state -> lexer_token = token_op_oparen;
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
395 return;
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
396
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
397 case ')':
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
398 state -> lexer_token = token_op_cparen;
5325b640424d First pass expression parsing
Lost Wizard (lost@starbug3)
parents: 35
diff changeset
399 return;
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
400
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
401 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
402 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
403 else
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
404 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
405 if (c == '=')
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
406 {
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
407 state -> lexer_token = token_op_assignment;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
408 return;
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
409 }
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
410 }
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
411
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
412 /* return the character if all else fails */
35
cdb0175e1063 More work on expressions
Lost Wizard (lost@starbug3)
parents: 34
diff changeset
413 state -> lexer_token = token_char;
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
414 state -> lexer_token_string = lw_realloc(state -> lexer_token_string, 2);
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
415 state -> lexer_token_string[0] = c;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
416 state -> lexer_token_string[1] = 0;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
417 return;
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents:
diff changeset
418 }
31
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
419
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
420 char *lexer_return_token(cstate *state)
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 static char *buffer = NULL;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
423 static int buflen = 0;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
424 int l;
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 if (buflen == 0)
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
427 {
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
428 buffer = lw_alloc(128);
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
429 buflen = 128;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
430 }
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
431
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
432 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
433 if (l >= buflen)
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
434 {
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
435 buffer = lw_realloc(buffer, l + 1);
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
436 buflen = l + 1;
34
bfea77812e64 Start of assignment code
Lost Wizard (lost@starbug3)
parents: 33
diff changeset
437 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
438 }
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
439 return buffer;
574931d87abd Created a function to prettyprint the current lexer token
lost@l-w.ca
parents: 26
diff changeset
440 }