diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-19 22:32:41 +0000 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-19 22:32:41 +0000 |
commit | cf2eb3dde8d9d49f28b9f91a726c865abe948109 (patch) | |
tree | deff333167b9a132df5903cf629e623f20c9a7e8 /src | |
parent | ad802305ebdc4a3956f90382f7d40a104e8c2f73 (diff) | |
download | chill-cf2eb3dde8d9d49f28b9f91a726c865abe948109.tar.gz chill-cf2eb3dde8d9d49f28b9f91a726c865abe948109.tar.bz2 chill-cf2eb3dde8d9d49f28b9f91a726c865abe948109.zip |
parseRel as lib
Diffstat (limited to 'src')
-rw-r--r-- | src/chill_run_util.cc | 120 | ||||
-rw-r--r-- | src/chillmodule.cc | 2 | ||||
-rw-r--r-- | src/parse_expr.ll | 24 | ||||
-rw-r--r-- | src/parse_expr.yy | 85 |
4 files changed, 1 insertions, 230 deletions
diff --git a/src/chill_run_util.cc b/src/chill_run_util.cc deleted file mode 100644 index 29568e7..0000000 --- a/src/chill_run_util.cc +++ /dev/null @@ -1,120 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include "chill_run_util.hh" - -static std::string to_string(int ival) { - char buffer[4]; - sprintf(buffer, "%d", ival); - return std::string(buffer); -} - -simap_vec_t* make_prog(simap_vec_t* cond) { - return cond; -} - -simap_vec_t* make_cond_gt(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - (*lhs)[it->first] -= it->second; - (*lhs)[to_string(0)] -= 1; - nvec->push_back(*lhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_vec_t* make_cond_lt(simap_t* lhs, simap_t* rhs) { - return make_cond_gt(rhs, lhs); -} - -simap_vec_t* make_cond_ge(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - (*lhs)[it->first] -= it->second; - nvec->push_back(*lhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_vec_t* make_cond_le(simap_t* lhs, simap_t* rhs) { - return make_cond_ge(rhs, lhs); -} - -simap_vec_t* make_cond_eq(simap_t* lhs, simap_t* rhs) { - simap_vec_t* nvec = new simap_vec_t(); - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] -= it->second; - nvec->push_back(*rhs); - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - it->second = -it->second; - nvec->push_back(*rhs); - delete rhs; - delete lhs; - return nvec; -} - -simap_t* make_cond_item_add(simap_t* lhs, simap_t* rhs) { - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] += it->second; - delete lhs; - return rhs; -} - -simap_t* make_cond_item_sub(simap_t* lhs, simap_t* rhs) { - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - (*rhs)[it->first] -= it->second; - delete lhs; - return rhs; -} - -simap_t* make_cond_item_mul(simap_t* lhs, simap_t* rhs) { - (*lhs)[to_string(0)] += 0; - (*rhs)[to_string(0)] += 0; - if(rhs->size() == 1) { - int t = (*rhs)[to_string(0)]; - for(simap_t::iterator it = lhs->begin(); it != lhs->end(); it++) - it->second *= t; - delete rhs; - return lhs; - } - else if(rhs->size() == 1) { - int t = (*lhs)[to_string(0)]; - for(simap_t::iterator it = rhs->begin(); it != rhs->end(); it++) - it->second *= t; - delete lhs; - return rhs; - } - else { - fprintf(stderr, "require Presburger formula"); - delete lhs; - delete rhs; - // exit(2); <-- this may be a boost feature - } -} - -simap_t* make_cond_item_neg(simap_t* expr) { - for (simap_t::iterator it = expr->begin(); it != expr->end(); it++) { - it->second = -(it->second); - } - return expr; -} - -simap_t* make_cond_item_number(int n) { - simap_t* nmap = new simap_t(); - (*nmap)[to_string(0)] = n; - return nmap; -} - -simap_t* make_cond_item_variable(const char* var) { - simap_t* nmap = new simap_t(); - (*nmap)[std::string(var)] = 1; - return nmap; -} - -simap_t* make_cond_item_level(int n) { - simap_t* nmap = new simap_t(); - (*nmap)[to_string(n)] = 1; - return nmap; -} - diff --git a/src/chillmodule.cc b/src/chillmodule.cc index 0e41f88..72f32d4 100644 --- a/src/chillmodule.cc +++ b/src/chillmodule.cc @@ -1,6 +1,6 @@ #include "chilldebug.h" -#include "chill_run_util.hh" +#include <parseRel.hh> #include <signal.h> #include <stdio.h> diff --git a/src/parse_expr.ll b/src/parse_expr.ll deleted file mode 100644 index a9b389f..0000000 --- a/src/parse_expr.ll +++ /dev/null @@ -1,24 +0,0 @@ -%{ -// some C++ code -#include "chill_run_util.hh" -#include "parse_expr.tab.hh" -%} - -%option noyywrap - -%% -[ \t]+ /*ignore*/ -\n /*ignore*/ -L[0-9]+ { yylval.val = atoi(&yytext[1]); return LEVEL; } -[0-9]+ { yylval.val = atoi(yytext); return NUMBER; } -\<\= return LE; -\>\= return GE; -\=(\=)? return EQ; -[a-zA-Z_][a-zA-Z_0-9]* { - yylval.str_val = new char[yyleng+1]; - strcpy(yylval.str_val, yytext); - return VARIABLE; - } -. return (int)yytext[0]; -%% - diff --git a/src/parse_expr.yy b/src/parse_expr.yy deleted file mode 100644 index c2943c2..0000000 --- a/src/parse_expr.yy +++ /dev/null @@ -1,85 +0,0 @@ -%{ -#include "chill_run_util.hh" -#include "parse_expr.ll.hh" - -extern int yydebug; - -void yyerror(const char*); -int yyparse(simap_vec_t** rel); - -static simap_vec_t* return_rel; // used as the return value for yyparse - -%} - -%union { - int val; - char* str_val; - simap_t* cond_item; - simap_vec_t* cond; -} - -%token <val> NUMBER -%token <val> LEVEL -%token <str_val> VARIABLE - -%left LE GE EQ '<' '>' -%left '-' '+' '*' '/' - -/*the final output from this language should be an Omega Relation object*/ -%type <cond> cond prog -%type <cond_item> expr add_expr mul_expr neg_expr - -%% -prog : cond { return_rel = make_prog($1); } -; - -cond : expr '>' expr { $$ = make_cond_gt($1, $3); } - | expr '<' expr { $$ = make_cond_lt($1, $3); } - | expr GE expr { $$ = make_cond_ge($1, $3); } - | expr LE expr { $$ = make_cond_le($1, $3); } - | expr EQ expr { $$ = make_cond_eq($1, $3); } -; - -expr : add_expr { $$ = $1; } -; - -add_expr : add_expr '+' mul_expr { $$ = make_cond_item_add($1,$3); } - | add_expr '-' mul_expr { $$ = make_cond_item_sub($1,$3); } - | mul_expr { $$ = $1; } -; - -mul_expr : mul_expr '*' neg_expr { $$ = make_cond_item_mul($1,$3); } - | neg_expr { $$ = $1; } -; - -neg_expr : '-' neg_expr { $$ = make_cond_item_neg($2); } - | '(' expr ')' { $$ = $2; } - | NUMBER { $$ = make_cond_item_number($1); } - | LEVEL { $$ = make_cond_item_level($1); } - | VARIABLE { $$ = make_cond_item_variable($1); } -; -%% - -void yyerror(const char* msg) { - fprintf(stderr, "Parse error: %s", msg); -} - -simap_vec_t* parse_relation_vector(const char* expr) { - yydebug=0; - YY_BUFFER_STATE state; - - //if(yylex_init()) { - // TODO: error out or something - //} - - state = yy_scan_string(expr); - - if(yyparse()) { - // TODO: error out or something - } - - yy_delete_buffer(state); - yylex_destroy(); - return return_rel; -} - |