-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.c
142 lines (127 loc) · 2.94 KB
/
parser.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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "slothvm.h"
int progLen(FILE* f){
char c;
int count = 0;
for (c = getc(f); c != EOF; c = getc(f)){
if (c == '\n'){
count = count + 1;
}
}
fseek(f, 0, SEEK_SET);
return count * 3;
}
void freeProgram(struct sloth_program* P){
free(P->codes);
free(P);
}
//Read a line delimited by \n from file
//Allocates a null terminated buffer to hold the line.
//Caller is responsible for freeing it.
//If the line is blank result will be empty
//If end of file is reached while reading a line, the last non null character
//in the buffer will be -1.
char *readline(FILE *file){
//Default length of 100 characters - feel free to use a different value
const size_t default_len = 100;
char *line = malloc(sizeof(char) * default_len);
size_t len = default_len;
size_t currentChar = 0;
char c = '\0';
while(c != -1){
c = fgetc(file);
//Get more memory to hold the line if needed
if(currentChar == len){
len *= 2;
line = realloc(line, len);
}
if(c == '\n'){
break;
}
line[currentChar] = c;
currentChar++;
}
//If at end of file put EOF in buffer
if(c == -1){
line[currentChar] = -1;
currentChar++;
}
line[currentChar] = '\0';
return line;
}
struct sloth_program* parse(char* filepath){
FILE* sFile;
sFile = fopen(filepath, "r");
if(sFile == NULL){
fprintf(stderr, "[ERROR] File could not be opened.");
exit(1);
}
int numCodes = progLen(sFile);
ubyte* byteCode = malloc(sizeof(char) * numCodes);
char c = '\0';
size_t count = 0;//Current position in line
char *line = 0x0;
size_t len = 0;
size_t codeNum = 0;
unsigned char currentCode = 0;
char hadToken = 0;
while(1){
count = 0;
currentCode = 0;
line = readline(sFile);
len = strlen(line);
//Make sure the line is okay
if(line[0] == -1){
free(line);
break;
}
if(len == 0){
free(line);
continue;
}
c = line[0];
while(c != 0 && c != -1 && c != '#' && count <= len){
//Check keywords
if(strncmp("slothy", line + count, 6) == 0){
byteCode[codeNum] = 0x1;
codeNum++;
count += 6;
hadToken = 1;
} else if(strncmp("sloth", line + count, 5) == 0){
currentCode++;
count += 5;
hadToken = 1;
}else if(strncmp("and", line + count, 3) == 0){
byteCode[codeNum] = currentCode;
codeNum++;
currentCode = 0;
count += 3;
hadToken = 1;
}else if(strncmp("nap", line + count, 3) == 0){
byteCode[codeNum] = 0x0;
codeNum++;
count += 3;
hadToken = 1;
}else{
//Ignore any other characters
count++;
}
c = line[count];
}
if(hadToken){
byteCode[codeNum] = currentCode;
codeNum++;
hadToken = 0;
}
free(line);
}
//for(int i = 0; i < numCodes; i++){
//printf("%x\n", byteCode[i]);
// }
struct sloth_program* S = malloc(sizeof(struct sloth_program));
S->codes = byteCode;
return S;
}