-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnush.c
143 lines (124 loc) · 3.15 KB
/
nush.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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/wait.h>
#include "tokens.h"
#include "ast.h"
#include "parse.h"
#include "list.h"
//LINE 13 to 84 ARE TAKEN FROM NAT TUCK LECTURE WITH SOME SLIGHT MODIFICATIONS
char*
read_number(const char* text,long ii){
int nn= 0;
while (isalnum(text[ii+nn]) || 45 == text[ii+nn] || 46 == text[ii+nn] || 47 == text[ii+nn] || 95 == text[ii+nn]){
++nn;
}
char* num = malloc(nn + 1);
memcpy(num, text + ii, nn);
num[nn] = 0;
return num;
}
list* tokenize(const char* text){
list* xs = 0;
int nn =strlen(text);
int ii = 0;
while (ii< nn){
if (isblank(text[ii])){
++ii;
continue;
}
if(isalnum(text[ii]) || 45 == text[ii] || 46 == text[ii] || 47 == text[ii] || 95 == text[ii]){
char* num = read_number(text, ii);
xs = cons(num, xs);
ii += strlen(num);
free(num);
continue;
}
if(text[ii]==38){
if (text[ii+1] == 38) {
char op[4] = "&&";
xs = cons(op, xs);
ii += 2;
continue;
} else {
char op[4] = "&";
xs = cons(op, xs);
ii +=1;
continue;
}
}
if(text[ii]==124){
if (text[ii+1] == 124) {
char op[4] = "||";
xs = cons(op, xs);
ii += 2;
continue;
} else {
char op[4] = "|";
xs = cons(op, xs);
ii +=1;
continue;
}
}
//else, operator
char op[4] = "x";//two bytes: 'x', '\0' x and null byte
op[0] = text[ii];
xs = cons(op, xs);
++ii;
}
return rev_free(xs);
}
//LECTURE CODE ENDS HERE
void
chomp(char* text)
{
int ln = strlen(text) -1;
if (*text && text[ln] == '\n'){
text[ln] = '\0';
}
}
//STARTER CODE WITH SOME MODIFICATIONS, THE FORMAT OF THIS CODE IS VERY SIMILAR AND HEAVILY RELIANT ON CACULATOR CODE FROM LECTURE 5
int
main(int argc, char* argv[])
{
char cmd[256];
int out = 0;
if(argc == 1){
while(1){
printf("nush$ ");
fflush(stdout);
char* rv = fgets(cmd, 256, stdin);
if (!rv){
break;
}
chomp(cmd);
list* tokens = tokenize(cmd);
//parse and eval
calc_ast* ast = parse(tokens);
out = ast_eval(ast);
free_ast(ast);
free_list(tokens);
}
} else {
FILE* f= fopen(argv[1], "r");
if (f == NULL){
printf("File does not exists \n");
exit(0);
}
while(fgets(cmd, 256, f)){
chomp(cmd);
list* tokens = tokenize(cmd);
//parse
calc_ast* ast = parse(tokens);
//eval
out = ast_eval(ast);
free_ast(ast);
free_list(tokens);
}
fclose(f);
}
return out;
}