root/branches/mcstas-1.x/cexp.c

Revision 2699, 5.2 KB (checked in by pkwi, 2 years ago)

Added files from mcstas-1.12a

Line 
1/*******************************************************************************
2*
3* McStas, neutron ray-tracing package
4*         Copyright 1997-2002, All rights reserved
5*         Risoe National Laboratory, Roskilde, Denmark
6*         Institut Laue Langevin, Grenoble, France
7*
8* Kernel: cexp.c
9*
10* %Identification
11* Written by: K.N.
12* Date: Aug  7, 1997
13* Origin: Risoe
14* Release: McStas 1.6
15* Version: $Revision: 1.19 $
16*
17* Handle expressions used as arguments to components etc.
18*
19*       $Id: cexp.c,v 1.19 2006-04-19 13:06:25 farhi Exp $
20*
21*       $Log: cexp.c,v $
22*       Revision 1.19  2006-04-19 13:06:25  farhi
23*       * Updated Release, Version and Origin fields in headers
24*       * Improved setversion to update all McStasx.y occurencies into current release
25*       * Added 'string' type for DEFINITION parameters to be handled as this type so that auto-quoting occurs in mcgui
26*       * Added possibility to save log of the session to a file (appended) in mcgui
27*       * Made Scilab use either TCL_EvalStr or TK_EvalStr
28*
29*       Revision 1.18  2003/02/11 12:28:45  farhi
30*       Variouxs bug fixes after tests in the lib directory
31*       mcstas_r  : disable output with --no-out.. flag. Fix 1D McStas output
32*       read_table:corrected MC_SYS_DIR -> MCSTAS define
33*       monitor_nd-lib: fix Log(signal) log(coord)
34*       HOPG.trm: reduce 4000 points -> 400 which is enough and faster to resample
35*       Progress_bar: precent -> percent parameter
36*       CS: ----------------------------------------------------------------------
37*
38*       Revision 1.6  2000/07/27 09:04:59  kn
39*       Support full C expressions. Now stores source line numbers within
40*       expression representation, and distinguishes between values and compound
41*       expressions.
42*
43*       Revision 1.5  2000/07/05 13:32:10  kn
44*       Properly quote constant string expressions.
45*
46*       Revision 1.4  1998/10/02 08:35:04  kn
47*       Fixed header comment.
48*
49*       Revision 1.3  1998/10/01 11:44:07  kn
50*       Added support for string expressions.
51*
52*       Revision 1.2  1997/09/07 17:54:28  kn
53*       Snapshot with (untested) code generation complete.
54*
55*       Revision 1.1  1997/08/13 09:11:24  kn
56*       Initial revision
57*
58*******************************************************************************/
59#include <stdarg.h>
60#include <stdio.h>
61
62#include "mcstas.h"
63
64/* The internal structure implementing a C expression. */
65struct cexp
66  {
67    char *s;                /* String representation */
68    int isvalue;            /* True if identifier or string/number constant */
69    int lineno;             /* Starting line number, or zero */
70  };
71
72/* Create an expression from a string representing a value (either identifier,
73   constant number, or constant string). */
74static CExp
75mkvalueexp(char *s)
76{
77  CExp e;
78  palloc(e);
79  e->s = s;
80  e->isvalue = 1;
81  e->lineno = 0;                /* Initially no line number set */
82  return e;
83}
84
85/* Create an expression from a string not representing a value. */
86static CExp
87mknonvalueexp(char *s)
88{
89  CExp e;
90  palloc(e);
91  e->s = s;
92  e->isvalue = 0;
93  e->lineno = 0;                /* Initially no line number set */
94  return e;
95}
96
97/*******************************************************************************
98* Handle identifiers used as arguments to components.
99* There are two types of identifiers: normal and extern. Normal identifiers
100* refer to formal arguments of the instrument, and are always of type
101* double. These can be varied eg. to perform a scan. Extern identifiers refer
102* to user-supplied C identifiers that is typically put in the declaration and
103* initialization sections of the instrument definition.
104*
105* The final version will distinguish (using a union) between these two types,
106* and will maintain a mapping from formal parameters of the instrument to
107* generated names in the generated simulation (the extern names must be
108* copied unchanged). But in this first version a simpler scheme is used: all
109* identifier expressions are simply strings, and all normal identifiers refer
110* to instrument parameters (which have ID_PRE and "ip" prepended).
111*******************************************************************************/
112
113CExp
114exp_id(char *id)
115{
116  return mkvalueexp(str_cat(ID_PRE, "ip", id, NULL));
117}
118
119CExp
120exp_extern_id(char *id)
121{
122  return mkvalueexp(str_dup(id));
123}
124
125CExp
126exp_number(char *n)
127{
128  return mkvalueexp(str_dup(n));
129}
130
131CExp
132exp_string(char *s)
133{
134  char *quoted, *result;
135  quoted = str_quote(s);
136  result =  str_cat("\"", s, "\"", NULL);
137  str_free(quoted);
138  return mkvalueexp(result);
139}
140
141CExp
142exp_ctoken(char *s)
143{
144  return mknonvalueexp(str_dup(s));
145}
146
147CExp
148exp_compound(int n, ...)
149{
150  char *result, *new;
151  CExp e;
152  va_list ap;
153  char *separator = "";         /* Token separator, initially empty */
154
155  va_start(ap, n);
156  result = str_dup("");
157  while(n-- > 0)
158  {
159    e = va_arg(ap, CExp);
160    new = str_cat(result, separator, e->s, NULL);
161    str_free(result);
162    result = new;
163    separator = " ";            /* Now use space separator for rest. */
164  }
165  return mknonvalueexp(result);
166}
167
168void
169exp_free(CExp e)
170{
171  str_free(e->s);
172  memfree(e);
173}
174
175char *
176exp_tostring(CExp e)
177{
178  char *s = e->s;
179  if(s == NULL)
180  {
181    s = "";
182    debugn((DEBUG_HIGH, "exp_tostring(): NULL cexp received.\n"));
183  }
184  return str_dup(s);
185}
186
187void
188exp_fprint(FILE *f, CExp e)
189{
190  fputs(e->s, f);
191}
192
193int
194exp_isvalue(CExp e)
195{
196  return e->isvalue;
197}
198
199void
200exp_setlineno(CExp e, int n)
201{
202  e->lineno = n;
203}
204
205int
206exp_getlineno(CExp e)
207{
208  return e->lineno;
209}
Note: See TracBrowser for help on using the browser.