Skip to content

Commit

Permalink
Now creates a parse tree off of the input
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtahu committed Apr 27, 2013
1 parent 875b272 commit db2b21c
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
6 changes: 4 additions & 2 deletions LangS.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ std::ofstream out;
void yyerror(std::string s);
int yylex (void);

Program *theProg;

%}

%token Scalar
Expand All @@ -27,8 +29,8 @@ int yylex (void);

%%

Prog: Stm ';' Prog
| Stm ';'
Prog: Stm ';' Prog { theProg->add((Statement *)$1); }
| Stm ';' { theProg->add((Statement *)$1); }

Stm: Exit { $$ = new ExitStatement();}
| Scalar '=' Value { $$ = new AssingmentStatement((Identifier*)$1,(Value*)$3); }
Expand Down
2 changes: 2 additions & 0 deletions ParseTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
#include "ExitStatement.h"
#include "PrintStatement.h"

#include "Program.h"

#endif /* PARSETREE_H_ */
31 changes: 31 additions & 0 deletions Program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Program.cpp
*
* Created on: Apr 26, 2013
* Author: jimtahu
*/

#include "Program.h"

namespace ParseTree {

Program::Program() {

}

void Program::add(Statement *stmt){
this->code.push_back(stmt);
}

void Program::run(){
for(std::vector<Statement *>::iterator i=code.begin(); i!=code.end(); i++){
(*i)->Execute();
}
return;
}

Program::~Program() {

}

} /* namespace ParseTree */
27 changes: 27 additions & 0 deletions Program.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Program.h
*
* Created on: Apr 26, 2013
* Author: jimtahu
*/

#ifndef PROGRAM_H_
#define PROGRAM_H_

#include <vector>
#include "Statement.h"

namespace ParseTree {

class Program {
private:
std::vector<Statement *> code;
public:
Program();
void add(Statement *stmt);
void run();
virtual ~Program();
};

} /* namespace ParseTree */
#endif /* PROGRAM_H_ */
46 changes: 46 additions & 0 deletions Util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <sstream>
#include <iostream>
#include <map>

#include "ParseTree.h"

using namespace std;

void yyerror(std::string s);
int yyparse (void);


/**
* Converts a string into a double.
* @return The double repersented by the string.
* @param text The string to convert.
* Uses stringstream for the conversion, so 7k.89 is 7.
*/
double str2num(string text){
stringstream ss(text);
double ret;
ss>>ret ? ret : throw text;
return ret;
}//end str2num

/**
* Converts a double to a string.
* @param val The double to convert.
* @return The string repersenting the double.
*/
string num2str(double val){
stringstream ss;
ss<<val;
return ss.str();
}//end num2str

extern ParseTree::Program *theProg;

int main(int argc, char *argv[]) {
theProg = new ParseTree::Program();
yyparse();
theProg->run();
delete theProg;
return 0;
}//end main

0 comments on commit db2b21c

Please sign in to comment.