From c347acb12b9f1c00da7cef2c49d8911b7617fe80 Mon Sep 17 00:00:00 2001 From: jimtahu Date: Tue, 14 May 2013 23:25:27 -0400 Subject: [PATCH] Starting work on vector type (they now are added to tree but cause segfault while cleaning). --- LangS.l | 3 +-- LangS.ypp | 17 ++++++++--------- ListValue.cpp | 31 +++++++++++++++++++++++++++++++ ListValue.h | 27 +++++++++++++++++++++++++++ ParseTree.h | 1 + 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 ListValue.cpp create mode 100644 ListValue.h diff --git a/LangS.l b/LangS.l index 170d365..10b3ce6 100644 --- a/LangS.l +++ b/LangS.l @@ -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; } diff --git a/LangS.ypp b/LangS.ypp index 3827213..dc1034d 100644 --- a/LangS.ypp +++ b/LangS.ypp @@ -18,8 +18,8 @@ Program *theProg; %} -%token Scalar -%token Vector +%token COMMENT +%token Ident %token STRING %token BinOP %token Print @@ -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); } %% diff --git a/ListValue.cpp b/ListValue.cpp new file mode 100644 index 0000000..ce1884d --- /dev/null +++ b/ListValue.cpp @@ -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::iterator i=items.begin(); i!=items.end(); i++) + delete *i; +} + +} /* namespace ParseTree */ diff --git a/ListValue.h b/ListValue.h new file mode 100644 index 0000000..1b9ea3c --- /dev/null +++ b/ListValue.h @@ -0,0 +1,27 @@ +/* + * ListValue.h + * + * Created on: May 9, 2013 + * Author: jimtahu + */ + +#ifndef LISTVALUE_H_ +#define LISTVALUE_H_ + +#include +#include "Value.h" + +namespace ParseTree { + +class ListValue: public Value { +private: + std::vector items; +public: + ListValue(Value *head); + void add(Value *item); + std::string getValue(); + virtual ~ListValue(); +}; + +} /* namespace ParseTree */ +#endif /* LISTVALUE_H_ */ diff --git a/ParseTree.h b/ParseTree.h index 6f0441c..59154ae 100644 --- a/ParseTree.h +++ b/ParseTree.h @@ -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"