PIPS
expressions.c
Go to the documentation of this file.
1 /*
2 
3  $Id$
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 
28 /* Functions using simultaneously pipsdbm, which is based on strings,
29  and ri-util, which contains basic methods for the objects of the
30  internal representation.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 
38 #include "linear.h"
39 
40 #include "genC.h"
41 #include "misc.h"
42 #include "ri.h"
43 #include "properties.h"
44 #include "ri-util.h"
45 //#include "text-util.h"
46 #include "workspace-util.h"
47 
48 /* try to parse @p s in the context of module @p module
49  * only simple expressions are found */
51 {
53  if(entity_undefined_p(e)) {
54  /* try to find simple expression */
55  /* unary operators */
56  for(const char *iter = s ; *iter ; iter++) {
57  if(isspace(*iter)) continue;
58  if(*iter=='-') {
59  expression etmp = string_to_expression(iter+1, module);
60  if(!expression_undefined_p(etmp)) {
62  etmp);
63  }
64  }
65  }
66 
67  /*binary operators*/
69  for(int i=0; i < (int) (sizeof(seeds)/sizeof(seeds[0])); i++) {
70  char *where = strchr(s,seeds[i][0]);
71  if(where) {
72  char * head = strdup(s);
73  char * tail = head + (where -s) +1 ;
74  head[where-s]='\0';
77  free(head);
79  return MakeBinaryCall(
80  entity_intrinsic(seeds[i]),
81  e0,
82  e1
83  );
84  }
85  else {
86  free_expression(e0);
87  free_expression(e1);
88  }
89  }
90  }
91  return expression_undefined;
92  }
93  else
94  return entity_to_expression(e);
95 }
96 /* split a string using @p seed as separator
97  * and call string_to_expression on each chunk */
98 list string_to_expressions(const char * str, const char * seed, entity module) {
99  list strings = strsplit(str,seed);
100  list expressions = NIL;
101  FOREACH(STRING,s,strings) {
103  if(!expression_undefined_p(expr)) {
104  expressions = CONS(EXPRESSION,
105  expr,
106  expressions);
107  }
108  }
109  gen_map(free,strings);
110  gen_free_list(strings);
111  return gen_nreverse(expressions);
112 }
void free_expression(expression p)
Definition: ri.c:853
void const char const char const int
list string_to_expressions(const char *str, const char *seed, entity module)
split a string using seed as separator and call string_to_expression on each chunk
Definition: expressions.c:98
expression string_to_expression(const char *s, entity module)
Functions using simultaneously pipsdbm, which is based on strings, and ri-util, which contains basic ...
Definition: expressions.c:50
#define STRING(x)
Definition: genC.h:87
void free(void *)
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
void gen_map(gen_iter_func_t fp, const list l)
Definition: list.c:172
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
entity string_to_entity(const char *s, entity module)
very simple conversion from string to expression only handles entities and numeric values at the time...
Definition: naming.c:237
list strsplit(const char *, const char *)
Definition: string.c:318
static char * module
Definition: pips.c:74
#define MINUS_OPERATOR_NAME
#define PLUS_OPERATOR_NAME
#define DIVIDE_OPERATOR_NAME
#define UNARY_MINUS_OPERATOR_NAME
#define MULTIPLY_OPERATOR_NAME
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
expression entity_to_expression(entity e)
if v is a constant, returns a constant call.
Definition: expression.c:165
expression MakeBinaryCall(entity f, expression eg, expression ed)
Creates a call expression to a function with 2 arguments.
Definition: expression.c:354
expression MakeUnaryCall(entity f, expression a)
Creates a call expression to a function with one argument.
Definition: expression.c:342
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define entity_undefined_p(x)
Definition: ri.h:2762
#define expression_undefined
Definition: ri.h:1223
#define expression_undefined_p(x)
Definition: ri.h:1224
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41