diff --git a/LangS.ypp b/LangS.ypp index 122821a..e019009 100644 --- a/LangS.ypp +++ b/LangS.ypp @@ -14,6 +14,8 @@ std::ofstream out; void yyerror(std::string s); int yylex (void); +Program *theProg; + %} %token Scalar @@ -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); } diff --git a/ParseTree.h b/ParseTree.h index 7918b6f..6f0441c 100644 --- a/ParseTree.h +++ b/ParseTree.h @@ -18,4 +18,6 @@ #include "ExitStatement.h" #include "PrintStatement.h" +#include "Program.h" + #endif /* PARSETREE_H_ */ diff --git a/Program.cpp b/Program.cpp new file mode 100644 index 0000000..f52026c --- /dev/null +++ b/Program.cpp @@ -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::iterator i=code.begin(); i!=code.end(); i++){ + (*i)->Execute(); + } + return; +} + +Program::~Program() { + +} + +} /* namespace ParseTree */ diff --git a/Program.h b/Program.h new file mode 100644 index 0000000..1eb4520 --- /dev/null +++ b/Program.h @@ -0,0 +1,27 @@ +/* + * Program.h + * + * Created on: Apr 26, 2013 + * Author: jimtahu + */ + +#ifndef PROGRAM_H_ +#define PROGRAM_H_ + +#include +#include "Statement.h" + +namespace ParseTree { + +class Program { +private: + std::vector code; +public: + Program(); + void add(Statement *stmt); + void run(); + virtual ~Program(); +}; + +} /* namespace ParseTree */ +#endif /* PROGRAM_H_ */ diff --git a/Util.cpp b/Util.cpp new file mode 100644 index 0000000..32f662b --- /dev/null +++ b/Util.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +#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<run(); + delete theProg; + return 0; +}//end main +