Skip to content

Commit

Permalink
Started work on a class for variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtahu committed Apr 26, 2013
1 parent aa8cb43 commit a3aaf34
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
2 changes: 2 additions & 0 deletions LangS.h
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion LangS.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CXX=distcc g++
CXX=distcc clang++
#CFLAGS= -O2 -pipe -march=native
CFLAGS=-ggdb
CXXFLAGS=$(CFLAGS)
SRC= $(wildcard *.cpp)
OBJ= $(SRC:.cpp=.o) LangS.tab.o LangS.yy.o


all: LangS test

LangS: $(OBJ)
$(CXX) -o $@ $^

Expand Down
14 changes: 9 additions & 5 deletions Scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#include <iostream>
#include <map>

#include "Variable.h"

using namespace std;

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

map<string, string> varTable;
map<string, Variable*> varTable;

/**
* Converts a string into a double.
Expand Down Expand Up @@ -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

Expand All @@ -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 \
Expand All @@ -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("-")){
Expand All @@ -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[]) {
Expand Down
34 changes: 34 additions & 0 deletions Variable.cpp
Original file line number Diff line number Diff line change
@@ -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
}

28 changes: 28 additions & 0 deletions Variable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Variable.h
*
* Created on: Apr 25, 2013
* Author: jimtahu
*/

#ifndef VARIABLE_H_
#define VARIABLE_H_

#include <string>

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_ */

0 comments on commit a3aaf34

Please sign in to comment.