This repository was archived by the owner on Oct 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (64 loc) · 2.19 KB
/
main.cpp
File metadata and controls
80 lines (64 loc) · 2.19 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
//
// Created by Freeman on 21/03/2016.
//
#include <stdio.h>
#include "y.tab.h"
#include "ast/ast.hpp"
#include "grammar.tab.h"
#include "lex.yy.h"
#include <stdlib.h>
#include <cstdarg>
#include <cstdio>
#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <string>
extern FILE *yyin;
int yyparse(void);
extern ScriptBody *root;
extern int global_var;
ESObject* globalObj;
extern std::map<int, vector<std::string> > codeScope; // this really should be named something better...?
extern int codeScopeDepth;
extern std::vector<std::string> functionDefinitions;
extern unsigned int getNewRegister();
// int Node::registerIndex = 0;
int main(int argc, char* argv[]) {
int global_var=0;
globalObj = new ESObject();
codeScopeDepth = 0;
yyin = fopen(argv[1], "r");
// 'compiled' c file name
char* inputFile = argv[1];
char* outputFilename = (char*)malloc(strlen(inputFile) + 3);
sprintf(outputFilename, "%s.c", inputFile);
FILE* outputFile = fopen(outputFilename, "w");
yyparse();
fprintf(outputFile, "#include \"./runtime/core.hpp\"\n");
fprintf(outputFile, "#include \"./runtime/console.hpp\"\n");
fprintf(outputFile, "#include \"./scope/reference.hpp\"\n");
fprintf(outputFile, "\n");
fprintf(outputFile, "ESObject* globalObj = new ESObject();\n\n");
if (root != NULL) {
root->dump(0);
root->genCode();
// printf("printing scoped IR:\n");
for (std::vector<std::string>::iterator iter = functionDefinitions.begin(); iter != functionDefinitions.end(); ++iter) {
std::string s = (*iter);
const char* c = s.c_str();
fprintf(outputFile, "%s\n", c);
}
for (std::vector<std::string>::iterator iter = codeScope[codeScopeDepth].begin(); iter != codeScope[codeScopeDepth].end(); ++iter) {
std::string s = (*iter);
const char* c = s.c_str();
fprintf(outputFile, "%s\n", c);
}
fprintf(outputFile, "\n");
}
return 0;
}
char* substring(const char* str, size_t begin, size_t len) {
if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin+len))
return 0;
return strndup(str + begin, len);
}