-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.c
272 lines (251 loc) · 8.23 KB
/
interpreter.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
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
/*Implementation of a custom interpreter for a small language written in C.
The interpreter supports basic arithmetic operations, string manipulation (including concatenation),
and I/O operations like scanf and printf.
It handles variable declarations for integers and strings, error reporting, and
executes statements line by line. The project showcases
a simple yet functional scripting language with basic support for integers and strings,
designed to demonstrate interpreter design principles.*/
/*Most tasks completed and solutions provided.
While some tasks are still in progress,
the majority of the work has been successfully implemented and documented.*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VARS 100
// Variable struct to store the name and value of a variable
struct Variable {
char name[100];
int intValue;
char strValue[100];
int isString; // 1 if string, 0 if integer
int isInitialized; // 1 if initialized, 0 if not
};
// Global symbol table
struct Variable symbolTable[MAX_VARS];
int symbolCount = 0;
// Function to find a variable in the symbol table
int findVariable(const char *name) {
for (int i = 0; i < symbolCount; i++) {
if (strcmp(symbolTable[i].name, name) == 0) {
return i;
}
}
return -1;
}
// Function to evaluate an arithmetic expression (only for integers)
int evaluateExpression(char *expr) {
int value = 0;
char operator= '+';
char token[100];
int idx = 0;
for (int i = 0; i <= strlen(expr); i++) {
if (isdigit(expr[i])) {
token[idx++] = expr[i];
} else if (isalpha(expr[i])) {
token[idx++] = expr[i];
} else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' ||
expr[i] == '/' || expr[i] == '\0') {
token[idx] = '\0';
if (idx > 0) {
int operand;
if (isdigit(token[0])) {
operand = atoi(token);
} else {
int varIndex = findVariable(token);
if (varIndex == -1) {
printf("Error: Undefined variable '%s'\n", token);
exit(1);
}
if (symbolTable[varIndex].isString ||
!symbolTable[varIndex].isInitialized) {
printf("Error: Cannot perform arithmetic on non-integer or "
"uninitialized variable '%s'\n",
token);
exit(1);
}
operand = symbolTable[varIndex].intValue;
}
switch (operator) {
case '+':
value += operand;
break;
case '-':
value -= operand;
break;
case '*':
value *= operand;
break;
case '/':
if (operand == 0) {
printf("Error: Division by zero\n");
exit(1);
}
value /= operand;
break;
}
}
operator= expr[i];
idx = 0;
}
}
return value;
}
// Function to evaluate a string concatenation expression
void evaluateStringConcatenation(char *result, char *expr) {
char token[100];
int idx = 0;
result[0] = '\0'; // Initialize result
for (int i = 0; i <= strlen(expr); i++) {
if (isalnum(expr[i]) || expr[i] == '_') {
token[idx++] = expr[i];
} else if (expr[i] == '+' || expr[i] == '\0') {
token[idx] = '\0';
if (idx > 0) {
int varIndex = findVariable(token);
if (varIndex == -1) {
printf("Error: Undefined variable '%s'\n", token);
exit(1);
}
if (!symbolTable[varIndex].isString ||
!symbolTable[varIndex].isInitialized) {
printf("Error: Cannot concatenate non-string or uninitialized "
"variable '%s'\n",
token);
exit(1);
}
strcat(result, symbolTable[varIndex].strValue);
}
idx = 0;
}
}
}
// Function to process a single line of code
void processLine(char *line) {
char id[100], expr[100];
// Handle integer declaration and assignment: int a = 4 or int a;
if (sscanf(line, "int %s = %[^\n]", id, expr) == 2) {
if (findVariable(id) == -1) {
strcpy(symbolTable[symbolCount].name, id);
symbolTable[symbolCount].intValue = evaluateExpression(expr);
symbolTable[symbolCount].isString = 0; // Integer variable
symbolTable[symbolCount].isInitialized = 1; // Initialized
symbolCount++;
} else {
printf("Error: Variable '%s' already declared\n", id);
exit(1);
}
} else if (sscanf(line, "int %s", id) == 1) {
if (findVariable(id) == -1) {
strcpy(symbolTable[symbolCount].name, id);
symbolTable[symbolCount].isString = 0; // Integer variable
symbolTable[symbolCount].isInitialized = 0; // Not initialized
symbolCount++;
} else {
printf("Error: Variable '%s' already declared\n", id);
exit(1);
}
}
// Handle string declaration and assignment: string s = "hello"
else if (sscanf(line, "string %s = \"%[^\"]\"", id, expr) == 2) {
if (findVariable(id) == -1) {
strcpy(symbolTable[symbolCount].name, id);
strcpy(symbolTable[symbolCount].strValue, expr);
symbolTable[symbolCount].isString = 1; // String variable
symbolTable[symbolCount].isInitialized = 1; // Initialized
symbolCount++;
} else {
printf("Error: Variable '%s' already declared\n", id);
exit(1);
}
} else if (sscanf(line, "string %s", id) == 1) {
if (findVariable(id) == -1) {
strcpy(symbolTable[symbolCount].name, id);
symbolTable[symbolCount].isString = 1; // String variable
symbolTable[symbolCount].isInitialized = 0; // Not initialized
symbolCount++;
} else {
printf("Error: Variable '%s' already declared\n", id);
exit(1);
}
}
// Handle string concatenation: string s = s1 + s2 or s = s1 + s2
else if (sscanf(line, "string %s = %[^\n]", id, expr) == 2) {
if (findVariable(id) == -1) {
strcpy(symbolTable[symbolCount].name, id);
symbolTable[symbolCount].isString = 1; // String variable
symbolTable[symbolCount].isInitialized = 1; // Initialized
evaluateStringConcatenation(symbolTable[symbolCount].strValue, expr);
symbolCount++;
} else {
printf("Error: Variable '%s' already declared\n", id);
exit(1);
}
} else if (sscanf(line, "%s = %[^\n]", id, expr) == 2) {
int varIndex = findVariable(id);
if (varIndex != -1) {
if (symbolTable[varIndex].isString) {
evaluateStringConcatenation(symbolTable[varIndex].strValue, expr);
symbolTable[varIndex].isInitialized = 1;
} else {
symbolTable[varIndex].intValue = evaluateExpression(expr);
symbolTable[varIndex].isInitialized = 1;
}
} else {
printf("Error: Undefined variable '%s'\n", id);
exit(1);
}
}
// Handle print statement: print a or print s
else if (sscanf(line, "print %[^\n]", expr) == 1) {
int varIndex = findVariable(expr);
if (varIndex != -1) {
if (!symbolTable[varIndex].isInitialized) {
printf("Error: Uninitialized variable '%s'\n", expr);
exit(1);
}
if (symbolTable[varIndex].isString) {
printf("%s\n", symbolTable[varIndex].strValue);
} else {
printf("%d\n", symbolTable[varIndex].intValue);
}
} else {
printf("Error: Undefined variable '%s'\n", expr);
exit(1);
}
}
// Handle scanf statement: scanf a
else if (sscanf(line, "scanf %s", id) == 1) {
int varIndex = findVariable(id);
if (varIndex != -1) {
if (symbolTable[varIndex].isString) {
printf("Error: Scanf does not support strings for '%s'\n", id);
exit(1);
}
printf("Enter value for %s: ", id);
scanf("%d", &symbolTable[varIndex].intValue);
symbolTable[varIndex].isInitialized = 1;
getchar(); // To consume the newline character left after scanf
} else {
printf("Error: Undefined variable '%s'\n", id);
exit(1);
}
} else {
printf("Error: Invalid syntax\n");
exit(1);
}
}
int main() {
char line[100];
// Keep taking input until user types "exit"
while (1) {
printf(">>> ");
fgets(line, sizeof(line), stdin);
line[strcspn(line, "\n")] = 0; // Remove newline character at the end
if (strcmp(line, "exit") == 0) {
break;
}
processLine(line);
}
return 0;
}