Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (101 loc) · 3.5 KB
/
Copy pathmain.cpp
File metadata and controls
116 lines (101 loc) · 3.5 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
106
107
108
109
110
111
112
113
114
115
116
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "python.tab.h"
#include <vector>
#include "ProgramTree.hpp"
#include "EvalMachine.hpp"
int yylex();
void lnumber();
vector<ProgramTree*> getStatmentList();
void yylex_destroy();
extern FILE* yyin;
void delstringsflex();
void delstringsbison();
//creates a single tree to evaluate from a vector of trees
void buildParseTree(vector<ProgramTree*> pList,int vectPos,ProgramTree* currentParent){
//adds element of vector to apropriate part of tree
//uses indent levels and type to tell if it should be added above the current
//statment that is excepting children
if(currentParent->getType()==program){
currentParent->addChild(pList[vectPos]);
}
else if(currentParent->getType()==ifStatments&&pList[vectPos]->getType()==elseStatments){
if(currentParent->getIndent()>=pList[vectPos]->getIndent()){
while(currentParent->getIndent()>=pList[vectPos]->getIndent() && currentParent->getType()!=program&&
!(currentParent->getType()==ifElSt&¤tParent->getIndent()==pList[vectPos]->getIndent())){
currentParent=currentParent->getParent();
}
}
currentParent->addChild(pList[vectPos]);
}
else if(currentParent->getType()==functionStatments||currentParent->getType()==elseStatments||currentParent->getType()==ifStatments){
if(currentParent->getIndent()>=pList[vectPos]->getIndent()){
while(currentParent->getIndent()>=pList[vectPos]->getIndent() && currentParent->getType()!=program){
currentParent=currentParent->getParent();
}
}
currentParent->addChild(pList[vectPos]);
}
//changes statment that future statments should be added to as children
//example if just added a function definition future elements should be children of the function (changes statment excepting children)
if(pList[vectPos]->getType()==ifElSt){
ProgramTree* theIfStatments=new ProgramTree(pList[vectPos]->getIndent(),ifStatments);
pList[vectPos]->addChild(theIfStatments);
currentParent=theIfStatments;
}
if(pList[vectPos]->getType()==functionDecleration){
ProgramTree* theFunctionStatments=new ProgramTree(pList[vectPos]->getIndent(),functionStatments);
pList[vectPos]->addChild(theFunctionStatments);
currentParent=theFunctionStatments;
}
if(pList[vectPos]->getType()==elseStatments){
currentParent=pList[vectPos];
}
vectPos++;
if(vectPos<pList.size()){
buildParseTree(pList,vectPos,currentParent);
}
}
int main(int argc, char *argv[]){
//yyparse();
//while(yylex());
//cout<<"TEST"<<endl;
//cout<<argv[1]<<endl;
//read file
FILE* pythonFile= fopen(argv[1],"r");
yyin=pythonFile;
//parse file
yyparse();
//lnumber();
//vector of parsed lines of the program
vector<ProgramTree*> theList=getStatmentList();
//root of tree to be built
ProgramTree* theProgram=new ProgramTree(0,program);
//creates a tree using indentation of statments
buildParseTree(theList,0,theProgram);
//theProgram->print();
//to keep track of functions and variables
vector<Variable> vars;
vector<Function> funcs;
//create object for evaluating
EvalMachine theMachine=EvalMachine(vars,funcs);
try{
theMachine.evaluate(theProgram);
}
catch(string e){
//for undefined functions and variables
cout<<e<<endl;
}
//clean up
yylex_destroy();
delete theProgram;
//delete strings created during lexing
delstringsflex();
//delete strings made during parsing
delstringsbison();
fclose(pythonFile);
return 0;
}