From a3aaf340fd985d52dd6d690ce295b842935a1f92 Mon Sep 17 00:00:00 2001 From: jimtahu Date: Thu, 25 Apr 2013 19:55:41 -0500 Subject: [PATCH 1/3] Started work on a class for variables. --- LangS.h | 2 ++ LangS.ypp | 2 +- Makefile | 4 +++- Scalar.cpp | 14 +++++++++----- Variable.cpp | 34 ++++++++++++++++++++++++++++++++++ Variable.h | 28 ++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 Variable.cpp create mode 100644 Variable.h diff --git a/LangS.h b/LangS.h index 8e08fed..88b7850 100644 --- a/LangS.h +++ b/LangS.h @@ -1,6 +1,8 @@ #ifndef LANGS_H #define LANGS_H +#include "Variable.h" + std::string setValue(std::string name, std::string value); std::string getValue(std::string name); std::string binOP(std::string op, std::string a, std::string b); diff --git a/LangS.ypp b/LangS.ypp index 2deb8e7..be517f3 100644 --- a/LangS.ypp +++ b/LangS.ypp @@ -29,7 +29,7 @@ int yylex (void); Prog: Stm ';' Prog | Stm ';' -Stm: Exit { } +Stm: Exit { exit(0); } | Scalar '=' Value { setValue($1, $3); } | Vector '=' List { } | Value { } diff --git a/Makefile b/Makefile index 48de3db..4462e07 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CXX=distcc g++ +CXX=distcc clang++ #CFLAGS= -O2 -pipe -march=native CFLAGS=-ggdb CXXFLAGS=$(CFLAGS) @@ -6,6 +6,8 @@ SRC= $(wildcard *.cpp) OBJ= $(SRC:.cpp=.o) LangS.tab.o LangS.yy.o +all: LangS test + LangS: $(OBJ) $(CXX) -o $@ $^ diff --git a/Scalar.cpp b/Scalar.cpp index 73eac5f..ba36025 100644 --- a/Scalar.cpp +++ b/Scalar.cpp @@ -2,12 +2,14 @@ #include #include +#include "Variable.h" + using namespace std; void yyerror(std::string s); int yyparse (void); -map varTable; +map varTable; /** * Converts a string into a double. @@ -40,7 +42,8 @@ string num2str(double val){ * @return value, for convienace. */ string setValue(string name, string value){ - varTable[name]=value; + if(varTable.count(name)==0)varTable[name]=new Variable(name); + varTable[name]->SetValue(value); return value; }//end setValue @@ -51,14 +54,14 @@ string setValue(string name, string value){ * Fetching a variable which has not been stored is undefined. */ string getValue(string name){ - return varTable[name]; + return varTable[name]->GetValue(); }//end getValue #define TRY_NUM(OP) try{ \ double x=str2num(a); \ double y=str2num(b); \ return num2str(x OP y); \ - }catch(string tx){ \ + }catch(string &tx){ \ return "NaN"; \ }//end try \ @@ -75,7 +78,7 @@ string binOP(string op, string a, string b){ double x=str2num(a); double y=str2num(b); return num2str(x+y); - }catch(string tx){ + }catch(string &tx){ return a+b; }//end try }else if(!op.compare("-")){ @@ -85,6 +88,7 @@ string binOP(string op, string a, string b){ }else if(!op.compare("/")){ TRY_NUM(/); }else return a+b; + return "MAJICS_VALUE"; }//end binOP int main(int argc, char *argv[]) { diff --git a/Variable.cpp b/Variable.cpp new file mode 100644 index 0000000..6ad6801 --- /dev/null +++ b/Variable.cpp @@ -0,0 +1,34 @@ +/* + * Variable.cpp + * + * Created on: Apr 25, 2013 + * Author: jimtahu + */ + +#include "Variable.h" + +Variable::Variable(string name){ + this->Name=name; +} + +Variable::Variable(Variable &other){ + this->Name=other.GetName(); + this->Value=other.GetValue(); +} + +string Variable::GetName(void){ + return this->Name; +} + +string Variable::GetValue(void){ + return this->Value; +} + +void Variable::SetValue(string value){ + this->Value=value; +} + +Variable::~Variable(){ + // TODO Auto-generated destructor stub +} + diff --git a/Variable.h b/Variable.h new file mode 100644 index 0000000..d39bd77 --- /dev/null +++ b/Variable.h @@ -0,0 +1,28 @@ +/* + * Variable.h + * + * Created on: Apr 25, 2013 + * Author: jimtahu + */ + +#ifndef VARIABLE_H_ +#define VARIABLE_H_ + +#include + +using namespace std; + +class Variable { +private: + string Name; + string Value; +public: + Variable(string name); + Variable(Variable &other); + string GetName(); + string GetValue(); + void SetValue(string value); + virtual ~Variable(); +}; + +#endif /* VARIABLE_H_ */ From cc969008b7f77a172ff6461b7fecc7962092e42f Mon Sep 17 00:00:00 2001 From: jimtahu Date: Fri, 26 Apr 2013 12:44:33 -0500 Subject: [PATCH 2/3] Added class for List type --- ListVariable.cpp | 28 ++++++++++++++++++++++++++++ ListVariable.h | 28 ++++++++++++++++++++++++++++ Variable.h | 6 +++--- 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 ListVariable.cpp create mode 100644 ListVariable.h diff --git a/ListVariable.cpp b/ListVariable.cpp new file mode 100644 index 0000000..4e395cf --- /dev/null +++ b/ListVariable.cpp @@ -0,0 +1,28 @@ +/* + * List.cpp + * + * Created on: Apr 25, 2013 + * Author: jimtahu + */ + +#include "ListVariable.h" + +ListVarable::ListVarable(string name):Variable(name) { + this->head=NULL; +} + +void ListVarable::add(string value){ + ListItem *item = new ListItem(); + item->value=value; + item->next=this->head; + this->head=item; +} + +ListVarable::~ListVarable() { + ListItem *item = this->head; + while(this->head != NULL){ + this->head=item->next; + delete item; + item=this->head; + }//end while list items +} diff --git a/ListVariable.h b/ListVariable.h new file mode 100644 index 0000000..631a72d --- /dev/null +++ b/ListVariable.h @@ -0,0 +1,28 @@ +/* + * List.h + * + * Created on: Apr 25, 2013 + * Author: jimtahu + */ + +#ifndef LIST_H_ +#define LIST_H_ + +#include "Variable.h" + +class ListItem { +public: + string value; + ListItem *next; +public: +}; + +class ListVarable: public Variable { + ListItem *head; +public: + ListVarable(string name); + void add(string value); + virtual ~ListVarable(); +}; + +#endif /* LIST_H_ */ diff --git a/Variable.h b/Variable.h index d39bd77..4083d14 100644 --- a/Variable.h +++ b/Variable.h @@ -19,9 +19,9 @@ class Variable { public: Variable(string name); Variable(Variable &other); - string GetName(); - string GetValue(); - void SetValue(string value); + virtual string GetName(); + virtual string GetValue(); + virtual void SetValue(string value); virtual ~Variable(); }; From 4a7636fabd73892fd2095d07d68c4baf7259ae5e Mon Sep 17 00:00:00 2001 From: jimtahu Date: Fri, 26 Apr 2013 16:38:49 -0500 Subject: [PATCH 3/3] Moved Scalar Variable to its own class --- Scalar.cpp | 6 +++--- ScalarVariable.cpp | 29 +++++++++++++++++++++++++++++ ScalarVariable.h | 23 +++++++++++++++++++++++ Variable.cpp | 9 --------- Variable.h | 3 --- 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 ScalarVariable.cpp create mode 100644 ScalarVariable.h diff --git a/Scalar.cpp b/Scalar.cpp index ba36025..cde6b10 100644 --- a/Scalar.cpp +++ b/Scalar.cpp @@ -2,14 +2,14 @@ #include #include -#include "Variable.h" +#include "ScalarVariable.h" using namespace std; void yyerror(std::string s); int yyparse (void); -map varTable; +map varTable; /** * Converts a string into a double. @@ -42,7 +42,7 @@ string num2str(double val){ * @return value, for convienace. */ string setValue(string name, string value){ - if(varTable.count(name)==0)varTable[name]=new Variable(name); + if(varTable.count(name)==0)varTable[name]=new ScalarVariable(name); varTable[name]->SetValue(value); return value; }//end setValue diff --git a/ScalarVariable.cpp b/ScalarVariable.cpp new file mode 100644 index 0000000..0a65c92 --- /dev/null +++ b/ScalarVariable.cpp @@ -0,0 +1,29 @@ +/* + * ScalarVariable.cpp + * + * Created on: Apr 26, 2013 + * Author: jimtahu + */ + +#include "ScalarVariable.h" + +ScalarVariable::ScalarVariable(string name):Variable(name){ + this->Value=""; +} + +ScalarVariable::ScalarVariable(ScalarVariable &other):Variable(other){ + this->Value=other.GetValue(); +} + +string ScalarVariable::GetValue(void){ + return this->Value; +} + +void ScalarVariable::SetValue(string value){ + this->Value=value; +} + +ScalarVariable::~ScalarVariable() { + // TODO Auto-generated destructor stub +} + diff --git a/ScalarVariable.h b/ScalarVariable.h new file mode 100644 index 0000000..0f906fb --- /dev/null +++ b/ScalarVariable.h @@ -0,0 +1,23 @@ +/* + * ScalarVariable.h + * + * Created on: Apr 26, 2013 + * Author: jimtahu + */ + +#ifndef SCALARVARIABLE_H_ +#define SCALARVARIABLE_H_ + +#include "Variable.h" + +class ScalarVariable: public Variable { + string Value; +public: + ScalarVariable(string name); + ScalarVariable(ScalarVariable &other); + string GetValue(); + void SetValue(string value); + virtual ~ScalarVariable(); +}; + +#endif /* SCALARVARIABLE_H_ */ diff --git a/Variable.cpp b/Variable.cpp index 6ad6801..1f6cf8f 100644 --- a/Variable.cpp +++ b/Variable.cpp @@ -13,21 +13,12 @@ Variable::Variable(string name){ Variable::Variable(Variable &other){ this->Name=other.GetName(); - this->Value=other.GetValue(); } string Variable::GetName(void){ return this->Name; } -string Variable::GetValue(void){ - return this->Value; -} - -void Variable::SetValue(string value){ - this->Value=value; -} - Variable::~Variable(){ // TODO Auto-generated destructor stub } diff --git a/Variable.h b/Variable.h index 4083d14..37564c6 100644 --- a/Variable.h +++ b/Variable.h @@ -15,13 +15,10 @@ using namespace std; class Variable { private: string Name; - string Value; public: Variable(string name); Variable(Variable &other); virtual string GetName(); - virtual string GetValue(); - virtual void SetValue(string value); virtual ~Variable(); };