-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasapa.c
More file actions
506 lines (454 loc) · 9.65 KB
/
asapa.c
File metadata and controls
506 lines (454 loc) · 9.65 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "node.h"
node *START = NULL, *STOP = NULL;
node2 *STACK = NULL;
node* data_type(char word[])
{
node *p = START;
while(p != NULL && strcmp(p->var_name, word))
{
p = p->next;
}
return p;
}
void string_copy(char *a, char *b, int i)
{
char temp[100];
temp[0] = b[i];
strcpy(a,temp);
}
int operate_value(char line[], int length)
{
return 1;
}
int private_functions(FILE *fp, char line[], int length)
{
fgets(line, length, fp);
if(STACK == NULL)
{
STACK-> start_ptr = START;
START = NULL;
}
else
{
node2* temp;
//malloc
temp -> start_ptr = START;
temp -> prev_node = STACK;
STACK = temp;
START = NULL;
}
char *arguments = (char*) malloc(sizeof(char) * (length));
int i = 0, j = 0;
while(i < length && line[i] == '(')
i++;
while(line[i] != ')')
arguments[j++] = line[i++];
while(line != '}')
{
if( // come across the same function,
// Call this function again
// call identify line ..
}
node2* temp = STACK;
STACK = STACK -> prev_node;
free(temp);
return 1;
}
int assign_value(char line[], int length)
{
char *vals = (char*) malloc(sizeof(char) * (length));
char *name = (char*) malloc(sizeof(char) * (150));;
int i = 0;
while(i < length && line[i] != ' ' && line[i] != '=' )
name[i] = line[i++];
name[i] = '\0';
while(line[i] == ' ')
i++;
if(line[i] == '=')
{
i++;
while(line[i] == ' ')
i++;
}
node *p = data_type(name);
if( line[i] == '\"' )
{
int g = 0;
i++;
while(i < length && line[i] != '\"')
vals[g++] = line[i++];
vals[g] = '\0';
if(p == NULL)
p = newStringNode(name, strlen(name), vals);
else
strcpy(p->sval, vals);
}
else
{
int j = i, fl = 0;
while(j < length)
{
if(line[j] == '.')
fl = 1;
j++;
}
j = 0;
while(i < length)
{
vals[j++] = line[i++];
}
vals[j] = '\0';
if(fl == 0)
{
if(p == NULL)
p = newIntegerNode(name, strlen(name), atoi(vals));
else
p->ival = atoi(vals);
}
else
{
if(p == NULL)
p = newRealNode(name, strlen(name), atof(vals));
else
p->rval = atof(vals);
}
}
p->next = NULL;
if(START == NULL)
{
START = p;
STOP = START;
}
else
{
STOP -> next = p;
STOP = STOP -> next;
}
return 1;
}
void delete(node *data)
{
node* temp = NULL;
while(data != NULL)
{
if(data->next != NULL)
printf("lp\n");
temp = data->next;
if(data != NULL)
free(data);
data = temp;
}
//free(temp);
}
void memory_of_variables(char line[], int length)
{
node *p, *temp1;
char c;
char *word;
word = (char *) malloc(sizeof(char) * 100);
int i = 3, count1 = 0;
temp1 = START;
while(temp1->next != NULL)
temp1 = temp1 -> next;
while( i < length)
{
c = line[i];
if( c != ',' && c != '\n')
word[count1++] = c;
else
{
if( line[0] == 'i' && line[1] == 'n')
p = newIntegerNode(word, count1, 0);
else if( line[0] == 'f' && line[1] == 'l' )
p = newRealNode(word, count1, 0.0);
else if( line[0] == 'c' && line[1] == 'h' )
p = newStringNode(word, count1,"ok");
else if( line[0] == '(' && line[1] == ')' )
p = newFunctionNode(word, count1);
if(START == NULL)
{
START = p;
STOP = START;
}
else
{
STOP -> next = p;
STOP = STOP -> next;
}
count1 = 0;
free(word);
word = (char *) malloc(sizeof(char) * 100);
}
i++;
}
/* if(c == '}')
{
store_START->child = START;
START = store_START;
store_START = NULL;
starting = 0;
open = 0;
}
if(c == '{')
{
store_START = START;
START = NULL;
starting = 1;
open = 1;
}*/
// printf("\n");
return ;
}
int input_line(char line[], int length)
{
char *name = (char *) malloc(sizeof(char) * (strlen(line))); ;
int i = 3;
while(i < (strlen(line) - 1) && line[i] != '\n')
{
name[i-3] = line[i];
++i;
}
char input[400];
int len;
scanf("%s",input,400);
len=strlen(input);
char *new_str = (char *) malloc(sizeof(char) * len);
node *rgh;
rgh = data_type(name);
if(input[0] == '\"')
{
rgh->sval = (char *) malloc(sizeof(char) * (len - 1));
for (i=1;i<len-1;i++)
{
rgh->sval[i-1] = input[i];
}
rgh->sval[i] = '\0';
}
else
{
strcpy(new_str, input);
int floating = 0;
for (i=0;i<strlen(input);i++)
if(input[i] == '.')
floating = 1;
if(floating == 0)
{
int val = atoi( new_str );
rgh->ival = val;
}
else
{
float val = atof( new_str );
rgh->rval = val;
}
}
//assign_value(rgh->var_name);
free(new_str);
return 1;
}
int output_line(char line[], int length)
{
// The correct print syntax is :
// <- Hello welcome to ASAPA. Value of a is \: @ (a is already defined): a
//Equivalent C syntax is (assuming a is integer) :
// printf("Hello welcome to ASAPA. Value of a is : %d (a is already defined)",a);
int i, count = 0, j;
char *words = (char*) malloc(sizeof(char) * (length));
i = 3;
//Loop to position i at the place where the variables to be printed are written
while( i < length-1)
{
if(line[i] == ':' && line[i-1] != '\\')
break;
i++;
}
i++;
j = 0;
//Loop to write the variables to be written in a list of strings
for(;i < length-1; ++i)
{
if(line[i] == '\n')
{
words[j] = '\0';
j = 0;
}
else
words[j++] = line[i];
}
count = 0;
for(i = 3; (i < length) ; ++i)
{
if(line[i] == '@' && line[i-1] != '\\') //printing a variable
{
node *c = data_type(words);
print(c);
}
else if(( line[i] == ':' ) && (line[i-1] != '\\'))//Coming out of the loop
{
//printf("\n"); // you temme : do you want a print statement to end in \n automatically ??
break;
}
else if(( line[i] == ':' ) && (line[i-1] == '\\'))
{
printf("%c",line[i]);
}
else if(( line[i] == 'n' ) && (line[i-1] == '\\'))
{
printf("\n");
}
else if(( line[i] == 't' ) && (line[i-1] == '\\'))
{
printf("\t");
}
else if(line[i] != '\\')
printf("%c",line[i]);
}
free(words);
return 1;
}
int identify_line(char line[], int length, char *filename)
{
char word[500];
int i = 0, j = 0, eq = 0, op = 0;
while(i < 500 && line[i] != ' ')
{
if(line[i] == '=')
eq = 1;
else if(line[i] == '+' || line[i] == '*' || line[i] == '-' || line[i] == '/')
op = 1;
word[i] = line[i++];
}
j = i;
while(j < length && eq == 0)
{
if(line[j] == '=')
eq = 1;
else if(line[j] == '+' || line[j] == '*' || line[j] == '-' || line[j] == '/')
op = 1;
j++;
}
word[i] = '\0';
if(strcmp(word,"in") == 0 || strcmp(word,"ch") == 0 || strcmp(word,"fl") == 0)
{
memory_of_variables(line, length);
return 1;
}
else if(line[0] == '<' && line[1] == '-' && line[2] == ' ')
{
if(output_line(line, length) == 1)
return 1;
}
else if(line[0] == '-' && line[1] == '>'&& line[2] == ' ')
{
if(input_line(line, length) == 1)
return 1;
}
else if(eq == 1 && op == 0)
{
if(assign_value(line, length) == 1)
return 1;
}
else if(op == 0)
{
if(operate_value(line, length) == 1)
return 1;
}
else if(evaluate_functions(filename, word))
{
return 1;
}
return 0;
}
int execute_program(char filename[])
{
int line_length = 500, line_count = 0;
FILE *fp;
fp = fopen(filename,"r"); // read mode
if( fp == NULL )
{
printf("Error while opening the file.\n");
return 0;
}
char *line;
line = (char*) malloc(sizeof(char) * line_length);
int j = 0;
fgets(line, line_length, fp);
while (!feof(fp))
{
//if(line[0] == '{')
// while(line[0] == '}')
// fgets(line, line_length, fp);
//fgets(line, line_length, fp);
if(identify_line(line, strlen(line), filename) == 0)
{
printf("ERROR: Unable to identify line : %d\n",line_count+1);
//return 0;
}
else
line_count++;
++j;
fgets(line, line_length, fp);
}
delete(START);
return 1;
}
int evaluate_functions(char filename[], char fn_name[])
{
/*
This is to evaluate functions in our asapa. It works like this
When you come across a function name, you store the function name in fn_name. Then you reopen the entire code and then start reading from the beginning till the line which starts with def.
If the word that follows the "def" is same as the function name, what you do is call a function called execute_line or execute_loop ( take a look at the pre-defined function execute program, to get an idea of how it should be done.
Do this from the start of the open-parenthesis "{" till the close-parenthesis "}".
Put that in a loop.
Test your code by you yourself writing a main function and checking whether it is executing the lines. write normal print statements in your function.
I will take care of the return statement as well as assigning values to the arguments.
Just see to it that it identifies all the lines defined within braces.
I will take care of managing the argument values and the return value.
Identify all the lines till you encounter a close brace.
*/
int line_length=300;
char *line;
line = (char*) malloc(sizeof(char) * line_length);
char a[1000],b[1000];
FILE * inp;
inp=fopen(filename,"r");
fgets(a, 1000, inp);
while(!(feof(inp)))
{
if(feof(inp))
break;
else
{
char str[] = "def";
strcat(str," ");
strcat(str,fn_name);
if(!strcmp(a,str))
{
fscanf(inp,"%s",b);//scans "{" and keeps it apart.
fgets(a, 1000, inp);
while(strcmp(a,"}"))
{
identify_line(a, strlen(line), filename);
fgets(a, 1000, inp);
if(strcmp(a,"}"))
break;
}
}
else
fscanf(inp,"%s",a);
}
}
}
int main(int argc, char*argv[])
{
if(argc != 2)
{
printf("ERROR: Invalid command line arguments\n");
return -1;
}
execute_program(argv[1]);
return 1;
}