From d3ad6ff0e99b180113eb126f58a9a29303c8f82b Mon Sep 17 00:00:00 2001 From: jimtahu Date: Fri, 26 Apr 2013 22:31:46 -0500 Subject: [PATCH 1/2] Adding handeling for variables --- AssingmentStatement.cpp | 5 +++++ AssingmentStatement.h | 1 + Identifier.cpp | 5 +++++ Identifier.h | 2 +- Program.cpp | 2 +- Variable.cpp | 31 +++++++++++++++++++++++++++++++ Variable.h | 3 +++ 7 files changed, 47 insertions(+), 2 deletions(-) diff --git a/AssingmentStatement.cpp b/AssingmentStatement.cpp index 363eccc..b596d4d 100644 --- a/AssingmentStatement.cpp +++ b/AssingmentStatement.cpp @@ -6,6 +6,7 @@ */ #include "AssingmentStatement.h" +#include "Variable.h" namespace ParseTree { @@ -14,6 +15,10 @@ AssingmentStatement::AssingmentStatement(Identifier *id, Value *value) { this->value=value; } +void AssingmentStatement::Execute(){ + setScalar(this->id->getName(),this->value->getValue()); +} + AssingmentStatement::~AssingmentStatement() { // TODO Auto-generated destructor stub } diff --git a/AssingmentStatement.h b/AssingmentStatement.h index 84229e8..3523810 100644 --- a/AssingmentStatement.h +++ b/AssingmentStatement.h @@ -19,6 +19,7 @@ class AssingmentStatement: public ParseTree::Statement { Value *value; public: AssingmentStatement(Identifier *id, Value *value); + void Execute(); virtual ~AssingmentStatement(); }; diff --git a/Identifier.cpp b/Identifier.cpp index 3c3a8bb..c5aa3a5 100644 --- a/Identifier.cpp +++ b/Identifier.cpp @@ -6,6 +6,7 @@ */ #include "Identifier.h" +#include "Variable.h" namespace ParseTree { @@ -19,6 +20,10 @@ string Identifier::getName(){ return this->Name; } +string Identifier::getValue(){ + return getScalar(this->Name); +} + Identifier::~Identifier() { // TODO Auto-generated destructor stub } diff --git a/Identifier.h b/Identifier.h index aa13094..3f6304f 100644 --- a/Identifier.h +++ b/Identifier.h @@ -18,7 +18,7 @@ class Identifier: public ParseTree::Value { public: Identifier(std::string name); std::string getName(); - void setValue(std::string value); + std::string getValue(); virtual ~Identifier(); }; diff --git a/Program.cpp b/Program.cpp index f52026c..876a831 100644 --- a/Program.cpp +++ b/Program.cpp @@ -14,7 +14,7 @@ Program::Program() { } void Program::add(Statement *stmt){ - this->code.push_back(stmt); + code.insert(code.begin(),stmt); } void Program::run(){ diff --git a/Variable.cpp b/Variable.cpp index 1f6cf8f..2eda14a 100644 --- a/Variable.cpp +++ b/Variable.cpp @@ -5,7 +5,38 @@ * Author: jimtahu */ +#include + #include "Variable.h" +#include "ScalarVariable.h" + +using namespace std; + +map varTable; + +/** +* Places a varable into the table. +* @param name The lable for the varable. +* @param value The value the varable now holds. +* @return value, for convienace. +*/ +string setScalar(string name, string value){ + if(varTable.count(name)==0)varTable[name]=new ScalarVariable(name); + varTable[name]->SetValue(value); + return value; +}//end setValue + +/** +* Fetches the value for a varable. +* @param name The lable of the varable to fetch. +* @return the value. +* Fetching a variable which has not been stored is undefined. +*/ +string getScalar(string name){ + if(varTable.count(name)) + return varTable[name]->GetValue(); + else return "EMPTY"; +}//end getValue Variable::Variable(string name){ this->Name=name; diff --git a/Variable.h b/Variable.h index 37564c6..66a31bf 100644 --- a/Variable.h +++ b/Variable.h @@ -12,6 +12,9 @@ using namespace std; +string setScalar(string name, string value); +string getScalar(string name); + class Variable { private: string Name; From 83aec26230a7ba54cc0e04a541c1ddc9d9bbde7b Mon Sep 17 00:00:00 2001 From: jimtahu Date: Sat, 27 Apr 2013 13:32:11 -0500 Subject: [PATCH 2/2] we now can assign and fetch variables --- PrintStatement.cpp | 8 ++++++++ PrintStatement.h | 1 + 2 files changed, 9 insertions(+) diff --git a/PrintStatement.cpp b/PrintStatement.cpp index 542a7bf..6d8bb11 100644 --- a/PrintStatement.cpp +++ b/PrintStatement.cpp @@ -5,14 +5,22 @@ * Author: jimtahu */ +#include + #include "PrintStatement.h" namespace ParseTree { +using namespace std; + PrintStatement::PrintStatement(Value *output) { this->value=output; } +void PrintStatement::Execute(){ + cout<value->getValue()<