-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.c
154 lines (124 loc) · 2.8 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
// Muhammad Huzaifa Elahi
// 260726386
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "interpreter.h"
#include "shellmemory.h"
#include "shell.h"
// Constants
const int MAX_LINE_LENGTH = 1000;
int recursionCount = 0;
const char runCmd[] = "run";
const char printCmd[] = "print";
const char setCmd[] = "set";
const char quitCmd[] = "quit";
const char helpCmd[] = "help";
int interpreter(char *words[]){
if(strcmp(words[0], runCmd) == 0)
return run(words[1]);
else if(strcmp(words[0], printCmd) == 0)
return print(words[1]);
else if (strcmp(words[0], setCmd) == 0)
return set(words);
else if (strcmp(words[0], quitCmd) == 0)
return quit();
else if (strcmp(words[0], helpCmd) == 0)
return help();
else
return unknown();
return 0;
}
int script(FILE *filePtr){
int errCode = 0;
char *line = calloc(MAX_LINE_LENGTH, sizeof(char));
while(!feof(filePtr)){
if(fgets(line, MAX_LINE_LENGTH, filePtr) == NULL){
printf("Unable to retrieve file input, please try again\n");
return 0;
}
while(line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n'){
line[strlen(line)-1] = '\0';
}
errCode = parse(line);
// If quit encountered, exit only script, not
if(errCode == 2){
errCode = -1;
}
if(errCode != 0){
free(line);
fclose(filePtr);
return errCode;
}
}
free(line);
fclose(filePtr);
return errCode;
}
int unknown(){
printf("Unknown command\n");
return -1;
}
int run(char *file){
if(file == NULL){
printf("Please enter a script\n");
return -1;
}
FILE *filePtr = fopen(file, "r");
if(filePtr == NULL){
printf("Script not found\n");
return -1;
}
if(recursionCount > 10000){
recursionCount = 0;
printf("Script contained recursion!\n");
return -1;
}
recursionCount++;
return script(filePtr);
}
int print(char *var){
if(var == NULL){
printf("Please enter a variable\n");
return -1;
}
char* value = getValue(var);
if(value == NULL){
printf("Variable does not exist\n");
return -1;
}
printf("%s\n", value);
return 0;
}
int set(char*words[]){
int i = 3;
int errCode = 0;
// Check var
if(words[1] == NULL){
printf("Please enter a variable\n");
return -1;
}
// Check string
if(words[2] == NULL){
printf("Please enter a string\n");
return -1;
}
errCode = addNode(words[1], words[2]);
if(errCode){
printf("Shell memory full\n");
}
return errCode;
}
int quit(){
printf("Bye!\n");
return 2;
}
int help(){
printf("Command (case sensitive) Description\n");
printf(" help Displays all the commands\n");
printf(" quit Exits / terminates the shell with \"Bye!\"\n");
printf("set VAR STRING Assigns a value to shell memory\n");
printf(" print VAR Displays the STRING assigned to VAR\n");
printf("run SCRIPT.TXT Executes the file SCRIPT.TXT\n");
return 0;
}