-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
89 lines (73 loc) · 1.99 KB
/
test.cpp
File metadata and controls
89 lines (73 loc) · 1.99 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
#include <mpc.h>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <string>
using namespace std;
ifstream inFile;
char* fileContents;
static mpc_val_t *print_token(mpc_val_t *x)
{
printf("Token: '%s'\n", (char*)x);
return x;
}
int main(int argn, char* argv[])
{
// open lua file
inFile.open(argv[1]);
// error handling
if(!inFile)
{
cerr << "cant open file\n";
exit(1);
}
// get path and size
filesystem::path p{argv[1]};
cout << filesystem::file_size(p) << " bytes\n";
//alocate and read in data
fileContents = (char*)malloc(sizeof(char)*filesystem::file_size(p));
int inputLen = sizeof(char)*filesystem::file_size(p);
inFile.read(fileContents, inputLen);
inFile.close();
// replace ; with \n
for(int i = 0; i < inputLen; i++)
{
if(fileContents[i] == ';')
fileContents[i] = '\n';
}
// remove all comments
for(int i = 0; i < inputLen; i++)
{
if(fileContents[i] == '-')
if(fileContents[i+1] == '-')
{ // --?
fileContents[i] = ' ';
fileContents[i+1] = ' ';
i += 2;
while(fileContents[i] != '\n' && (i < inputLen))
{
fileContents[i] = ' ';
i ++;
}
}
}
mpc_parser_t* Tokens = mpc_many(
mpcf_all_free,
mpc_apply(
mpc_strip(
mpc_re("\\s*([0-9]+|[a-zA-Z_]+|[(){}-=+*\\[\\].,/&%$#!<>~^]+|['\"]+)")
//mpc_re("\\s*([a-zA-Z_]+|[0-9]+|,|\\.|:)")
),
print_token)
);
mpc_result_t r;
mpc_parse("input", fileContents, Tokens, &r);
// bracket parsing:
/// first { after function, delete it
/// first { after "for" replace with "do"
/// every } replace with "end"
// give a warning if a { does not match a }
mpc_delete(Tokens);
free(fileContents);
return 0;
}