Repository navigation
Expand file tree
/
Copy pathinter.c
More file actions
187 lines (135 loc) · 3.21 KB
/
Copy pathinter.c
File metadata and controls
187 lines (135 loc) · 3.21 KB
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define input_len 1000
#define INT 0
#define OP 1
#define err -1
#define INT_STR "INT"
#define OPERATOR_STRING "OPERATOR"
typedef struct
{
int type;//num or op 2+2+2
char operator_value;
int number_value;
}Token;
size_t current_pos=0;
Token current_token;
bool is_digit(char c)
{
return c>='0'&&c<='9';
}
bool is_operator(char c)
{
return c=='+'||c=='-';
}
bool is_wight_space(char c)
{
return c==' '|| c == '\n' || c == '\r' || c == '\t';
}
void get_next_token(char *text){
char c = text[current_pos];
while (is_wight_space(c))
{
current_pos ++;
c = text[current_pos];
}
if (is_digit(c))
{
int current_number=c-'0';
current_pos ++;
while (is_digit(text[current_pos]))
{
current_number *= 10;
current_number+=(text[current_pos] - '0');
current_pos ++;
}
current_token.type=INT;
current_token.number_value=current_number;
current_token.operator_value=' ';
}
else if(is_operator(c))
{
current_token.type=OP;
current_token.operator_value=c;
current_token.number_value=0;
current_pos ++ ;
}
else
{
// current_token.type = err;
// current_token.number_value=0;
printf("invalid token at pos %ld : %c\n",current_pos,c);
exit(-1);
}
}
void token_str(Token token, char *s) // token as string in string s
{
// char *type = OPERATOR_STRING;
// if (token.type==INT)
// {
// type = INT_STR;
// }
// char *value = token.number_value;
// if (token.type==OP)
// {
// value = &token.operator_value;
// }
sprintf(s,"Token (type=%s, numper_value=%d,operator_vlaue=%c)", token.type==INT? INT_STR:OPERATOR_STRING,token.number_value,token.operator_value);
}
void parse(int type){
if (current_token.type!=type)
{
char s[100];
token_str(current_token, s);
printf("syntax error at pos %ld: Expected token of type %s,but received tosken%s", current_pos,type==INT ? INT_STR : OPERATOR_STRING,s);
exit(-1);
}
}
int interpret(char *text)
{
int result;
get_next_token(text);
parse(INT);
result = current_token.number_value;
while (is_wight_space(text[current_pos]))
{
current_pos++;
}
while (text[current_pos] != '\0')
{
get_next_token(text);
parse(OP);
int operator = current_token.operator_value;
get_next_token(text);
parse(INT);
int operand = current_token.number_value;
if (operator == '+')
{
result += operand;
}
else
{
result -= operand;
}
while (is_wight_space(text[current_pos]))
{
current_pos++;
}
}
return result;
}
int main() {
char s[input_len];
printf("Interpreter is running......");
int result;
while (true)
{
current_pos=0;
printf(">>>");
fgets(s,input_len,stdin);
result =interpret(s);
printf("%d\n",result);
}
}