summaryrefslogtreecommitdiff
path: root/src/user/app/kbasic/lex.c
blob: 5d3487be62f1bc78ecbfab45de7614648f3bccf9 (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "basic.h"

struct commands { /* keyword lookup table */
	char command[20];
	char tok;
} table[] = { /* Commands must be entered lowercase */
	{"print", PRINT}, /* in this table. */
	{"input", INPUT},
	{"color", COLOR},

	{"if", IF},
	{"then", THEN},
	{"goto", GOTO},
	{"for", FOR},
	{"next", NEXT},
	{"to", TO},
	{"gosub", GOSUB},
	{"return", RETURN},

	{"end", END},
	{"run", RUN},
	{"load", LOAD},
	{"list", LIST},
	{"exec", EXEC},

	{"rnd", RND},
	{"", END}  /* mark end of table */
};

char token[TOK_LEN];
int token_type, tok;

int look_up(char *s);	// lookup keyword
int iswhite(char c);
int isdelim(char c);


/* Load a file into a buffer. */
char* load_file(char *fname) {
	FILE *fp;
	char *p;
	int size;

	if(!(fp=fopen(fname, "rb"))) return 0;

	// Get file size
	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	p = (char*)malloc(size + 2);
	fseek(fp, 0, SEEK_SET);

	// Read file contents
	fread(p, size, 1, fp);
	fclose(fp);
	
	/* null terminate the program */
	if (p[size-1] != '\n') p[size++] = '\n';
	p[size] = 0;

	return p;
}


/* Find the start of the next line. */
void find_eol()
{
	while(*prog_ip!='\n'  && *prog_ip!='\0') ++prog_ip;
	if(*prog_ip) prog_ip++;
}

/* Get a token. */
int get_token()
{

	register char *temp;

	token_type=0; tok=0;
	temp=token;

	if(*prog_ip=='\0') { /* end of file */
		*token=0;
		tok = FINISHED;
		token_type=DELIMITER;
		goto get_tok_end;
	}

	while(iswhite(*prog_ip)) ++prog_ip;  /* skip over white space */

	if(*prog_ip=='\r') { /* crlf */
		++prog_ip; ++prog_ip;
		tok = EOL; *token='\r';
		token[1]='\n'; token[2]=0;
		token_type = DELIMITER;
		goto get_tok_end;
	}
	if (*prog_ip=='\n') {	/* lf (unix newline) */
		++prog_ip;
		tok = EOL; *token='\n'; token[1] = 0;
		token_type = DELIMITER;
		goto get_tok_end;
	}

	if(strchr("+-*^/%=;(),><", *prog_ip)){ /* delimiter */
		*temp=*prog_ip;
		prog_ip++; /* advance to next position */
		temp++;
		*temp=0; 
		token_type=DELIMITER;
		goto get_tok_end;
	}

	if(*prog_ip=='"') { /* quoted string */
		prog_ip++;
		while(*prog_ip!='"'&& *prog_ip!='\r' && *prog_ip!='\n') {
			if (temp - token < TOK_LEN - 1) *temp++=*prog_ip;
			prog_ip++;
		}
		if(*prog_ip=='\r' || *prog_ip=='\n') serror(1);
		prog_ip++;*temp=0;
		token_type=QUOTE;
		goto get_tok_end;
	}

	if(isdigit(*prog_ip)) { /* number */
		while(!isdelim(*prog_ip)) {
			if (temp - token < TOK_LEN - 1) *temp++=*prog_ip;
			prog_ip++;
		}
		*temp = '\0';
		token_type = NUMBER;
		goto get_tok_end;
	}

	if(isalpha(*prog_ip)) { /* var or command */
		while(!isdelim(*prog_ip)) {
			if (temp - token < TOK_LEN - 1) *temp++=*prog_ip;
			prog_ip++;
		}
		token_type=STRING;
	}

	*temp = '\0';

	/* see if a string is a command or a variable */
	if(token_type==STRING) {
		tok=look_up(token); /* convert to internal rep */
		if(!tok) token_type = VARIABLE;
		else token_type = COMMAND; /* is a command */
	}

get_tok_end:
	//printf("[%d.%d.%s]", token_type, tok, token);

	return token_type;
}



/* Return a token to input stream. */
void putback() 
{

	char *t; 

	t = token; 
	for(; *t; t++) prog_ip--; 
}

/* Look up a a token's internal representation in the
   token table.
 */
int look_up(char *s)
{
	register int i;
	char *p;

	/* convert to lowercase */
	p = s;
	while(*p){ *p = tolower(*p); p++; }

	/* see if token is in table */
	for(i=0; *table[i].command; i++)
		if(!strcmp(table[i].command, s)) return table[i].tok;
	return 0; /* unknown command */
}

/* Return true if c is a delimiter. */
int isdelim(char c)
{
	if(strchr(" ;,+-<>/*%^=()", c) || c==9 || c=='\r' || c=='\n' || c==0) 
		return 1;  
	return 0;
}

/* Return 1 if c is space or tab. */
int iswhite(char c)
{
	if(c==' ' || c=='\t') return 1;
	else return 0;
}