summaryrefslogtreecommitdiff
path: root/minic/scan.c
blob: d008f4cb9fb59d76a57ffd72c68344f74169e2ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "scan.h"
#include "io.h"
#include "minilib.h"
#include "const.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"

void scanner_init( Scanner *s, char *src )
{
	s->src = src;
	scanner_reset( s );
}

void scanner_reset( Scanner *s )
{
	s->peek = ' ';
	s->row = 1;
	s->col = 0;
	s->pos = s->src;
}

void scanner_done( Scanner *s )
{
}

static char get_char( Scanner *s )
{
	char c;

	c = *s->pos;
	s->pos++;
	s->peek = c;
	s->col++;
	if( c == '\n' ) {
		s->col = 1;
		s->row++;
	}

	return c;
}

static char peek_char( Scanner *s, int seek )
{
	char *pos = s->pos;
	char c = *pos;

	while( c != '\0' && seek > 0 ) {
		c = *pos;
		pos++;
		seek--;
	}

	return c;
}

static void skip_char( Scanner *s, int seek )
{
	while( seek > 0 ) {
		get_char( s );
		seek--;
	}
}

static void skip_whitespace( Scanner *s )
{
	for( ; ; get_char( s ) ) {
		if( s->peek == ' ' || s->peek == '\t' || s->peek == '\n' ) {
			continue;
		} else if( s->peek == '\0' ) {
			return;
		} else {
			break;
		}
	}
}

static void error( Scanner *s, char *msg )
{
	char buf[MAX_TMP_LEN];
	char buf2[12];
	buf[0] = '\0';

	itoa( s->row, buf2, 10 );
	strcat( buf, "ERROR in row " );
	strcat( buf, buf2 );
	itoa( s->col, buf2, 10 );
	strcat( buf, ", col " );
	strcat( buf, buf2 );
	strcat( buf, ": " );
	strcat( buf, msg );

	print( buf );
	halt( );
}

static void unexpected_char( Scanner *s, int c )
{
	char msg[MAX_TMP_LEN];
	msg[0] = '\0';

	strcat( msg, "unexpected character '" );
	strcat_c( msg, c );
	strcat( msg, "'" );

	error( s, msg );
}

static void skip_comment( Scanner *s )
{
	get_char( s );
	get_char( s );
	while( s->peek != '*' ) {
		get_char( s );
	}
	get_char( s );
	if( s->peek != '/' ) {
		error( s, "unclosed comment" );
	}
	get_char( s );
}

static scanner_Symbol parse_ident( Scanner *s )
{
	scanner_Symbol sym;
	int len = 0;

	sym.sym = S_IDENT;
	sym.data.s[0] = '\0';
	
	while( len < MAX_IDENT_LEN && ( isalnum( s->peek ) || s->peek == '_' ) ) {
		strcat_c( sym.data.s, s->peek );
		len++;
		get_char( s );
	}

	return sym;
}

static scanner_Symbol parse_number( Scanner *s )
{
	scanner_Symbol sym;
	int len = 0;
	
	sym.sym = S_INT_CONST;
	sym.data.i = 0;
	
	/* TODO: unsigned, signed, overflow handling */
	while( len < MAX_NUMBER_LEN && isdigit( s->peek ) ) {
		int digit = s->peek - '0';
		sym.data.i = sym.data.i * 10 + digit;
		len++;
		get_char( s );
	}
	
	return sym;
}

scanner_Symbol scanner_scan( Scanner *s )
{
	scanner_Symbol sym;
	int c;

	for( ;; ) {
		skip_whitespace( s );	
		switch( s->peek ) {
			case '\0':
				sym.sym = S_eof;
				if( s->debug ) {
					print( "SCANNER(EOF)" );
				}
				return sym;

			case '/':
				c = peek_char( s, 1 );
				if( c == '*' ) {
					if( s->debug ) {
						print( "SCANNER(COMMENT_START)" );
					}
					skip_comment( s );
					if( s->debug ) {
						print( "SCANNER(COMMENT_END)" );
					}
					continue;
				} else {
					get_char( s );
					sym.sym = S_DIV;
					return sym;
				}
				break;

			case 'i':
				switch( peek_char( s, 1 ) ) {
					case 'n':
						switch( peek_char( s, 2 ) ) {
							case 't':
								skip_char( s, 3 );
								sym.sym = S_INT;
								return sym;
						}
				}
				goto id;
			
			case 'v':
				switch( peek_char( s, 1 ) ) {
					case 'o':
						switch( peek_char( s, 2 ) ) {
							case 'i':
								switch( peek_char( s, 3 ) ) {
									case 'd':
										skip_char( s, 4 );
										sym.sym = S_VOID;
										return sym;
								}
						}
				}
				goto id;

			case 'r':
				switch( peek_char( s, 1 ) ) {
					case 'e':
						switch( peek_char( s, 2 ) ) {
							case 't':
								switch( peek_char( s, 3 ) ) {
									case 'u':
										switch( peek_char( s, 4 ) ) {
											case 'r':
												switch( peek_char( s, 5 ) ) {
													case 'n':
														skip_char( s, 6 );
														sym.sym = S_RETURN;
														return sym;
												}
										}
								}
						}
				}
				goto id;
			
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				sym = parse_number( s );
				return sym;
				
			id:
			case 'm': case 'a': case 'n':
				sym = parse_ident( s );
				return sym;
			
			case ';':
				get_char( s );
				sym.sym = S_SEMICOLON;
				return sym;
			
			case '(':
				get_char( s );
				sym.sym = S_LPARENT;
				return sym;

			case ')':
				get_char( s );
				sym.sym = S_RPARENT;
				return sym;

			case '{':
				get_char( s );
				sym.sym = S_LCURL;
				return sym;
			
			case '}':
				get_char( s );
				sym.sym = S_RCURL;
				return sym;
				
			default:
				unexpected_char( s, s->peek );
				sym.sym = S_eof;
				return sym;
		}
	}
}

void scanner_debug( Scanner *s, int enable )
{
	s->debug = enable;
	
	if( s->debug ) {
		print( "SCANNER DEBUGGING ENABLED" );
	}
}