parameter-test.c 8.3 KB
Newer Older
Erick's avatar
Erick committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/*
 * parameter.c  -- Parameter Objects (SRFI-39)
 *
 * Copyright © 2003-2018 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 *           Author: Erick Gallesio [eg@essi.fr]
 *    Creation date:  1-Jul-2003 11:38 (eg)
 * Last file update: 18-Jul-2018 19:41 (eg)
 */


#include "stklos.h"
#include "vm.h"


struct parameter_obj {
  stk_header header;
  SCM value;
  SCM converter;
  SCM dynenv;     /* an A-list ((thr1 . val) ...) to look before returning value */
  int C_type;     /* 0: parameter is expressed in Scheme
                     1: Converter is expressed in C rather than in Scheme
                     2: idem and value is a procedure to call to get value
                     3: A Scheme parameter whose value is a thunk to be called
                  */
};

#define PARAMETERP(o)           (BOXED_TYPE_EQ((o), tc_parameter))
#define PARAMETER_VALUE(p)      (((struct parameter_obj *) (p))->value)
#define PARAMETER_CONV(p)       (((struct parameter_obj *) (p))->converter)
#define PARAMETER_C_TYPE(p)     (((struct parameter_obj *) (p))->C_type)
#define PARAMETER_DYNENV(p)     (((struct parameter_obj *) (p))->dynenv)

/*===========================================================================*\
 *
 *      Utilities
 *
\*===========================================================================*/

static void error_bad_parameter(SCM obj)
{
  STk_error("bad parameter ~S", obj);
}


SCM STk_get_parameter(SCM param)
{
  SCM val, tmp;

  if (!PARAMETERP(param)) error_bad_parameter(param);

  tmp = STk_int_assq(STk_current_thread(), PARAMETER_DYNENV(param));
  val =  (tmp != STk_false) ? CDR(tmp) : PARAMETER_VALUE(param);

  switch (PARAMETER_C_TYPE(param)) {
    case 2:  return ((SCM (*)(void)) val)();
    case 3:  return STk_C_apply(val, 0);
    default: return val;
  }
}

SCM STk_set_parameter(SCM param, SCM value)
{
  SCM conv, tmp, new;

  if (!PARAMETERP(param)) error_bad_parameter(param);

  conv = PARAMETER_CONV(param);

  switch (PARAMETER_C_TYPE(param)) {
    case 0:
    case 3:
      new = (conv != STk_false) ? STk_C_apply(conv,1,value): value;
      break;
    default:
      new = (conv != STk_false) ? ((SCM (*) (SCM))conv)(value): value;
      break;
  }
  
        


//    
//  if (PARAMETER_C_TYPE(param)) {
//    /* We have a C converter */
//    new = (conv != STk_false) ? ((SCM (*) (SCM))conv)(value): value;
//  } else {
//    /* We have a Scheme converter */
//    new = (conv != STk_false) ? STk_C_apply(conv,1,value): value;
//  }

  tmp  = STk_int_assq(STk_current_thread(), PARAMETER_DYNENV(param));
  if (tmp != STk_false)
    CDR(tmp) = new;
  else {
    if (PARAMETER_C_TYPE(param) != 2)
      PARAMETER_VALUE(param) = new;
  }

  return STk_void;
}

SCM STk_make_C_parameter(SCM symbol, SCM value, SCM (*proc)(SCM new_value),
                         SCM module)
{
  SCM z;

  /* Define the parameter */
  NEWCELL(z, parameter);
  PARAMETER_VALUE(z)  = value;
  PARAMETER_CONV(z)   = (SCM) proc;
  PARAMETER_C_TYPE(z) = 1;
  PARAMETER_DYNENV(z) = STk_nil;

  /* Bind it to the given symbol */
  STk_define_variable(STk_intern(symbol), z, module);

  return z;
}

SCM STk_make_C_parameter2(SCM symbol, SCM (*value)(void), SCM (*proc)(SCM new_value),
                          SCM module)
{
  SCM z = STk_make_C_parameter(symbol, (SCM) value, proc, module);

  PARAMETER_C_TYPE(z) = 2;
  return z;
}



/*===========================================================================*\
 *
 *      Primitives
 *
\*===========================================================================*/

/*
<doc EXT make-parameter
 * (make-parameter init)
 * (make-parameter init converter)
 *
 * Returns a new parameter object which is bound in the global dynamic
 * environment to a cell containing the value returned by the call
 * |(converter init)|. If the conversion procedure |converter| is not
 * specified the identity function is used instead.
 * @l
 * The parameter object is a procedure which accepts zero or one
 * argument. When it is called with no argument, the content of the
 * cell bound to this parameter object in the current dynamic
 * environment is returned. When it is called with one argument, the
 * content of the cell bound to this parameter object in the current
 * dynamic environment is set to the result of the call
 * |(converter arg)|, where |arg| is the argument passed to the
 * parameter object, and
 * an unspecified value is returned.
 *
 * @lisp
 * (define radix
 *     (make-parameter 10))
 *
 * (define write-shared
 *    (make-parameter
 *       #f
 *       (lambda (x)
 *         (if (boolean? x)
 *             x
 *             (error 'write-shared "bad boolean ~S" x)))))
 *
 *  (radix)           =>  10
 *  (radix 2)
 *  (radix)           =>  2
 *  (write-shared 0)  => error
 *
 *  (define prompt
 *    (make-parameter
 *      123
 *      (lambda (x)
 *        (if (string? x)
 *            x
 *            (with-output-to-string (lambda () (write x)))))))
 *
 *  (prompt)       =>  "123"
 *  (prompt ">")
 *  (prompt)       =>  ">"
 * @end lisp
doc>
*/
DEFINE_PRIMITIVE("make-parameter", make_parameter, subr12, (SCM value, SCM conv))
{
  SCM z, v;

  if (conv && STk_procedurep(conv) == STk_false)
    STk_error("bad conversion function ~S", conv);

  /* initialize v with (conv value) */
  v = (conv) ? STk_C_apply(conv, 1, value): value;

  NEWCELL(z, parameter);
  PARAMETER_VALUE(z)  = v;
  PARAMETER_CONV(z)   = (conv) ? conv : STk_false;
  PARAMETER_C_TYPE(z) = 0;
  PARAMETER_DYNENV(z) = STk_nil;

  return z;
}

DEFINE_PRIMITIVE("make-thunk-parameter", make_thunk_parameter, subr12,
                 (SCM value, SCM conv))
{
  SCM z;

  if (conv && STk_procedurep(conv) == STk_false)
    STk_error("bad conversion function ~S", conv);

  if (STk_procedurep(value) != STk_true)
    STk_error("bad thunk ~S", value);

  NEWCELL(z, parameter);
  PARAMETER_VALUE(z)  = value;
  PARAMETER_CONV(z)   = (conv) ? conv : STk_false;
  PARAMETER_C_TYPE(z) = 3;
  PARAMETER_DYNENV(z) = STk_nil;
  return z;
}

/*
<doc EXT parameter?
 * (parameter? obj)
 *
 *  Returns |#t| if |obj| is a parameter object, otherwise returns |#f|.
doc>
 */
DEFINE_PRIMITIVE("parameter?", parameterp, subr1, (SCM obj))
{
  return MAKE_BOOLEAN(PARAMETERP(obj));
}



DEFINE_PRIMITIVE("%parameter-dynenv-push!", parameter_dynenv_push, subr1, (SCM param))
{
  if (!PARAMETERP(param)) error_bad_parameter(param);

  PARAMETER_DYNENV(param) = STk_cons(STk_cons(STk_current_thread(), STk_void),
                                     PARAMETER_DYNENV(param));
  return STk_void;
}

DEFINE_PRIMITIVE("%parameter-dynenv-pop!", parameter_dynenv_pop, subr1, (SCM param))
{
  SCM prev, tmp, thr;

  if (!PARAMETERP(param)) error_bad_parameter(param);

  thr = STk_current_thread();
  STk_debug("IΝDYN = ~S th = ~S ~S param = ~S\n", PARAMETER_DYNENV(param), thr, param);
  for (prev = tmp = PARAMETER_DYNENV(param); !NULLP(tmp); prev=tmp, tmp=CDR(tmp)) {
    if (CAR(CAR(tmp)) == thr) {
      if (tmp ==  prev) PARAMETER_DYNENV(param) = CDR(tmp);
      else CDR(prev) = CDR(tmp);
      break;
    }
  }
  STk_debug("OUT DYN = ~S th = ~S ~S \n", PARAMETER_DYNENV(param), thr), param;
  return STk_void;
}



/*===========================================================================*\
 *
 *      Initialization code
 *
\*===========================================================================*/

static struct extended_type_descr xtype_parameter = { "parameter", NULL };

int STk_init_parameter(void)
{
  /* Define type for parameters */
  DEFINE_XTYPE(parameter, &xtype_parameter);

  /* Define primitives */
  ADD_PRIMITIVE(make_parameter);
  ADD_PRIMITIVE(make_thunk_parameter);
  ADD_PRIMITIVE(parameterp);
  ADD_PRIMITIVE(parameter_dynenv_push);
  ADD_PRIMITIVE(parameter_dynenv_pop);

  return TRUE;
}