-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
89 lines (75 loc) · 1.88 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char keywords[] = {"PRINT"};
char tokens[][16] = {"START"};
//**********************************************//
// Retreving File Contents //
//**********************************************//
char* file(char* path) {
char* buf = 0;
int fileLen;
long len;
// File initiation
FILE* fp = fopen(path, "rb");
// Allocate memory for file sizes and contents
if (fp) {
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
buf = malloc(len);
if (buf) {
fread(buf, 1, len, fp);
}
}
// Close program file
fclose(fp);
if (buf) {
// Return file contents
return buf;
}
}
void strAppend(char targetArray[2048], char obj) {
int length;
length = strlen(targetArray);
targetArray[length] = obj;
targetArray[length + 1] = 0;
}
//**********************************************//
// Lexical Analysis //
//**********************************************//
char* lex(char contents[2048]) {
int i;
int len;
int newLine = 1;
char currTok[2048];
char* tokPointer = currTok;
char curr; // Current character that is being read
for (i = 0; i < strlen(contents); i++) {
// Remove spaces from program contents
if (contents[i] == ' ') {
memmove(&contents[i], &contents[i + 1], strlen(contents) - i);
}
if (contents[i] == '\n') { contents[i] = 0; }
len = strlen(currTok);
currTok[len] = contents[i];
currTok[len + 1] = 0;
currTok[0] = ':';
//printf("%s\n", currTok);
if (!strcmp(currTok, ":PRINT")) {
//strAppend(tokens[length + 1], 'h');
//strcpy(tokens, ":PRINT");
printf("PRINT detected\n");
}
}
printf("%s\n", contents);
}
//**********************************************//
// Initializer Method //
//**********************************************//
int main(int argv, char* argc[]) {
char* cont = file(argc[1]);
lex(cont);
printf("%s\n", tokens[0]);
return 0;
}