This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
127 lines (99 loc) · 2.47 KB
/
token.c
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
/* sexc - s-expression calculator
* Copyright (C) 2011 Barret Rennie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "token.h"
#include "function.h"
#define IS_SIGN_PREFIX(c) (c == '+' || c == '-')
#define IS_WHITESPACE(c) (c == ' ' || c == '\t' || c == '\n')
#define IS_SPECIAL(c) (c == '(' || c == ')' || IS_WHITESPACE(c) || c == EOF)
extern Token lookahead;
void die(const char *message)
{
fprintf(stderr, "Error: %s.\n", message);
exit(1);
}
int fpeek(FILE *stream)
{
int c = fgetc(stream);
if (ungetc(c, stream) == EOF && c != EOF)
die("Stream error");
return c;
}
void nextToken(FILE *input)
{
char buffer[FN_NAME_MAX + 1];
int c;
for (c = fgetc(input); IS_WHITESPACE(c); c = fgetc(input));
if (c == EOF)
{
#ifdef DEBUG
printf("Got EOF\n");
#endif
die("EOF Reached");
}
if (c == '(')
{
lookahead.type = BEGIN;
#ifdef DEBUG
printf("Got (\n");
#endif
}
else if (c == ')')
{
lookahead.type = END;
#ifdef DEBUG
printf("Got )\n");
#endif
}
else if (IS_SIGN_PREFIX(c) && isdigit(fpeek(input)) || isdigit(c))
{
ungetc(c, input);
lookahead.type = VALUE;
fscanf(input, "%lf", &lookahead.value.number);
#ifdef DEBUG
printf("Got number %f\n", lookahead.value.number);
#endif
}
else
{
unsigned pos = 1;
int index;
lookahead.type = FUNCTION;
buffer[0] = c & 0xFF;
memset(buffer + 1, '\0', FN_NAME_MAX);
c = fgetc(input);
while (!IS_SPECIAL(c) && pos < FN_NAME_MAX)
{
buffer[pos++] = c & 0xFF;
c = fgetc(input);
}
if (pos == FN_NAME_MAX && !IS_SPECIAL(c))
die("token too long");
ungetc(c, input);
index = lookupFunction(buffer);
if (index < 0)
die("function not found");
else
lookahead.value.fnIndex = index;
#ifdef DEBUG
printf("Got function: %d:%s\n", lookahead.value.fnIndex, buffer);
#endif
}
}