-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (86 loc) · 1.69 KB
/
main.cpp
File metadata and controls
92 lines (86 loc) · 1.69 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
//
// Project: CMiniLang
// Author: bajdcc
//
#include <cstdio>
#include <fstream>
#include <iostream>
#include "cparser.h"
#include "cvm.h"
extern int g_argc;
extern char** g_argv;
int main(int argc, char **argv)
{
g_argc = argc;
g_argv = argv;
#if 0
string_t txt = R"(
int fibonacci(int i) {
if (i <= 1)
return 1;
return fibonacci(i - 1) + fibonacci(i - 2);
}
void move(char x, char y)
{
printf("%c --> %c\n", x, y);
}
void hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
move(a, c);
}
else
{
hanoi(n-1, a, c, b);
move(a, c);
hanoi(n-1, b, a, c);
}
}
int main()
{
int i;
i = 0;
printf("##### fibonacci #####\n");
while (i <= 10) {
printf("fibonacci(%2d) = %d\n", i, fibonacci(i));
i++;
}
printf("##### hanoi #####\n");
hanoi(3, 'A', 'B', 'C');
return 0;
})";
try {
clib::cparser p(txt);
auto root = p.parse();
clib::cast::print(root, 0, std::cout);
clib::cgen gen(root);
gen.eval();
} catch (const std::exception& e) {
printf("ERROR: %s\n", e.what());
}
#else
g_argc--;
g_argv++;
if (g_argc < 1) {
printf("Usage: CMiniLang file ...\n");
return -1;
}
std::ifstream in(*g_argv);
std::istreambuf_iterator<char> beg(in), end;
std::string str(beg, end);
if (str.empty()) {
exit(-1);
}
try {
clib::cparser p(str);
auto root = p.parse();
//clib::cast::print(root, 0, std::cout);
clib::cgen gen(root);
gen.eval();
} catch (const std::exception& e) {
printf("ERROR: %s\n", e.what());
}
#endif
return 0;
}