-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTinyScript.cpp
More file actions
executable file
·105 lines (92 loc) · 2.26 KB
/
TinyScript.cpp
File metadata and controls
executable file
·105 lines (92 loc) · 2.26 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
#include <iostream>
#include <memory.h>
#include "Parser/Program.hpp"
#include "CodeGen/TinyVMCode.hpp"
#include "CodeGen/CCode.hpp"
#include "flags.h"
extern "C"
{
#include "vm.h"
#include "std.h"
#include "bytecode.h"
}
using namespace TinyScript;
void import_std(NodeProgram &prog)
{
prog.add_src("../std/io.tiny");
}
int run_bin(string path)
{
FILE *file = fopen(path.c_str(), "rb");
fseek(file, 0L, SEEK_END);
int len = ftell(file);
rewind(file);
int main_func;
char *code = (char*)malloc(len - sizeof(int));
fread(&main_func, 1, sizeof(int), file);
fread(code, 1, len - sizeof(int), file);
vm_init();
register_std();
vm_run(code, main_func, NULL);
free(code);
return 0;
}
int main(int argc, char *argv[])
{
C::Code code("c_code");
NodeProgram prog;
string output = "";
string bin = "";
//import_std(prog);
// Include all files parsed into compiler
for (int i = 1; i < argc; i++)
{
string arg = argv[i];
if (arg == "-o" || arg == "--output")
{
if (i >= argc - 1)
Logger::link_error("Expected output file");
else
output = argv[++i];
}
else if (arg == "-b" || arg == "--bin")
{
if (i >= argc - 1)
Logger::link_error("Expected bin file");
else
return run_bin(argv[++i]);
}
else
{
prog.add_src(argv[i]);
}
}
prog.parse();
code.compile_program(prog);
/*
vector<char> bytecode = code.link();
int main_func = code.find_funcion("test.main");
char *raw_code = (char*)malloc(bytecode.size());
memcpy(raw_code, &bytecode[0], bytecode.size());
#if DEBUG_ASSEMBLY
printf("\nDisassembly: ");
disassemble(raw_code, bytecode.size());
printf("\n");
#endif
if (!Logger::has_error())
{
if (output != "")
{
FILE *file = fopen(output.c_str(), "wb");
fwrite(&main_func, 1, sizeof(int), file);
fwrite(raw_code, 1, bytecode.size(), file);
fclose(file);
return 0;
}
vm_init();
register_std();
vm_run(raw_code, main_func, NULL);
}
free(raw_code);
*/
}