Skip to content

Commit

Permalink
Starting work on vector type (they now are added to tree but cause se…
Browse files Browse the repository at this point in the history
…gfault while cleaning).
  • Loading branch information
jimtahu committed May 15, 2013
1 parent 88fde55 commit c347acb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
3 changes: 1 addition & 2 deletions LangS.l
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ print {return Print; }
return STRING;
}
[[:digit:]]+ {yylval = new ParseTree::StringValue(yytext); return STRING; }
$[[:alpha:]]+ {yylval = new ParseTree::Identifier(yytext); return Scalar; }
%[[:alpha:]]+ {yylval = new ParseTree::Identifier(yytext); return Vector; }
$[[:alpha:]]+ {yylval = new ParseTree::Identifier(yytext); return Ident; }
[-] {yylval = new ParseTree::Identifier("-"); return BinOP; }
[+] {yylval = new ParseTree::Identifier("+"); return BinOP; }
[\*] {yylval = new ParseTree::Identifier("*"); return BinOP; }
Expand Down
17 changes: 8 additions & 9 deletions LangS.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Program *theProg;

%}

%token Scalar
%token Vector
%token COMMENT
%token Ident
%token STRING
%token BinOP
%token Print
Expand All @@ -33,20 +33,19 @@ Prog: Stm ';' Prog { theProg->add((Statement *)$1); }
| Stm ';' { theProg->add((Statement *)$1); }

Stm: Exit { $$ = new ExitStatement();}
| Scalar '=' Value { $$ = new AssingmentStatement((Identifier*)$1,(Value*)$3); }
| Vector '=' List { $$ = new AssingmentStatement((Identifier*)$1,new StringValue("#list#")); }
| Ident '=' Value { $$ = new AssingmentStatement((Identifier*)$1,(Value*)$3); }
| Value { }
| Print Value { $$ = new PrintStatement((Value*)$2); }

Value: STRING { $$ = $1; }
| Vector { $$ = $1; }
| Scalar { $$ = $1; }
| Ident { $$ = $1; }
| Items { $$ = $1; }
| Value BinOP Value { $$ = new BinOpValue(((Identifier*)$2)->getName(),(Value*)$1,(Value*)$3); }

List: '[' Items ']'
Items: '[' List ']' { $$ = $1; }

Items: Items ',' Value
| Value
List: List ',' Value { ((ListValue *)$1)->add((Value*)$3); $$ = $1; }
| Value { $$ = new ListValue((Value*)$1); }

%%

Expand Down
31 changes: 31 additions & 0 deletions ListValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ListValue.cpp
*
* Created on: May 9, 2013
* Author: jimtahu
*/

#include "ListValue.h"

namespace ParseTree {

using namespace std;

ListValue::ListValue(Value *head) {
items.insert(items.begin(),head);
}

void ListValue::add(Value *item){
items.insert(items.end(),item);
}

string ListValue::getValue(){
return "%LIST%";
}

ListValue::~ListValue() {
for(std::vector<Value *>::iterator i=items.begin(); i!=items.end(); i++)
delete *i;
}

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

#ifndef LISTVALUE_H_
#define LISTVALUE_H_

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

namespace ParseTree {

class ListValue: public Value {
private:
std::vector<Value *> items;
public:
ListValue(Value *head);
void add(Value *item);
std::string getValue();
virtual ~ListValue();
};

} /* namespace ParseTree */
#endif /* LISTVALUE_H_ */
1 change: 1 addition & 0 deletions ParseTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Value.h"
#include "Identifier.h"
#include "StringValue.h"
#include "ListValue.h"
#include "BinOpValue.h"
#include "Statement.h"
#include "AssingmentStatement.h"
Expand Down

0 comments on commit c347acb

Please sign in to comment.