summaryrefslogtreecommitdiff
path: root/miniasm/scan.c
blob: 00f9f4e9b2ec83dd4d5e4052762b260e16d29454 (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
#include "const.h"
#include "scan.h"
#include "ctype.h"
#include "string.h"
#include "utils.h"
#include "io.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 = 1;
	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;
		
	return c;
}

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

void scanner_skip_line( Scanner *s )
{
	while( s->peek != '\0' && s->peek != '\n' ) {
		s->col++;
		get_char( s );
	}
	
	if( s->peek == '\n' ) {
		s->col++;
	}
}

Symbol get_int( Scanner *s )
{
	Symbol sym;

	sym.sym = S_number;
	sym.data.n = 0;

	do {
		sym.data.n = sym.data.n * 10 + ( s->peek - '0' );
		get_char( s );
	} while( s->peek != '\0' && isdigit( s->peek ) );

	return sym;
}

Symbol get_ident_or_label( Scanner *s )
{
	Symbol newSym;
	char *p;
	
	newSym.sym = S_ident;
	newSym.data.s[0] = '\0';
	p = newSym.data.s;

	do {
		*p++ = s->peek;
		get_char( s );
	} while( ( isalnum( s->peek ) || s->peek == '_' ) && !isspace( s->peek ) && ( s->peek != '\0' ) && ( s->peek != ':' ) && ( p - newSym.data.s < MAX_IDENT_LEN ) );

	if( s->peek == ':' ) {
		newSym.sym = S_label;
		get_char( s );
	}
	
	*p = '\0';

	return newSym;
}

Symbol scanner_scan( Scanner *s )
{
	Symbol sym;

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

	skip_whitespace( s );

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

	if( isdigit( s->peek ) ) {
		sym = get_int( s );
		if( s->debug ) {
			char buf[MAX_TMP_LEN];
			char buf2[MAX_TMP_LEN];
			buf[0] = '\0';
			strcat( buf, "SCANNER(NUMBER," );
			inttohex( sym.data.n, buf2 );
			strcat( buf, buf2 );
			strcat( buf, ")" );
			print( buf );
		}
		return sym;
	} else if( isalpha( s->peek ) ) {
		sym = get_ident_or_label( s );
		if( s->debug ) {
			char buf[MAX_TMP_LEN];
			buf[0] = '\0';
			if( sym.sym == S_ident ) {
				strcat( buf, "SCANNER(IDENT," );
			} else if( sym.sym == S_label ) {
				strcat( buf, "SCANNER(LABEL," );
			} else {
				strcat( buf, "SCANNER(<unknown>," );
			}
			strcat( buf, sym.data.s );
			strcat( buf, ")" );
			print( buf );
		}
		return sym;	
	} else if( s->peek == '\n' ) {
		sym.sym = S_newline;	
		if( s->debug ) {
			print( "SCANNER(NEWLINE)" );
		}
		s->peek = ' ';
		return sym;
	} else if( s->peek == ';' ) {
		sym.sym = S_comment;
		if( s->debug ) {
			print( "SCANNER(COMMENT)" );
		}
		s->peek = ' ';
		return sym;		
	} else {
		sym.sym = S_token;
		sym.tag = s->peek;
		s->peek = ' ';
		if( s->debug ) {
			char buf[MAX_TMP_LEN];
			char buf2[MAX_TMP_LEN];
			buf[0] = '\0';
			strcat( buf, "SCANNER(TOKEN," );
			inttohex( sym.tag, buf2 );
			strcat( buf, buf2 );
			strcat( buf, ")" );
			print( buf );
		}
		return sym;
	}
	
	return sym;
}

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