Exemplo n.º 1
0
Arquivo: forms.c Projeto: bieber/col
/*** if
 * Conditional form.  Accepts exactly three arguments.  First feeds its input
 * to the first argument.  If the result is boolean True, it feeds the input to
 * its second argument, if False then it feeds it to its third argument,
 * otherwise it just returns Bottom.
 *
 * if{ f, g, h } : x = if f : x then g : x else h : x
 */
struct value *iff(struct list *args, struct value *in)
{
    struct value *test = value_copy(in);
    struct value *out = NULL;

    // Checking for correct number of arguments
    if(args->count != 3)
    {
        value_delete(test);
        value_delete(in);
        return value_new();
    }

    // Testing input with first argument
    test = function_exec(list_get(args, 0), test);

    if(test->type == BOOL_VAL)
    {
        if(test->data.bool_val)
        {
            value_delete(test);
            return function_exec(list_get(args, 1), in);
        }
        else
        {
            value_delete(test);
            return function_exec(list_get(args, 2), in);
        }
    }
    else
    {
        value_delete(in);
        return value_new();
    }
}
Exemplo n.º 2
0
Arquivo: forms.c Projeto: bieber/col
/*** map
 * Mapping functional form.  Accepts a single function argument.  Input to the
 * form should always be in the form of a list, and the return value will be
 * the result of applying the argument function to each element in the list.
 *
 * map{ f } : < x, y, z > = < f : x, f : y, f : z >
 */
struct value *map(struct list *args, struct value *in)
{

    struct value *out = NULL;
    struct function *f = list_get(args, 0);
    struct list *l = NULL;
    struct cursor *c;

    // First ensure valid input
    if(args->count != 1 || in->type != SEQ_VAL)
    {
        value_delete(in);
        return value_new();
    }

    // Otherwise create an output list by applying f to each element of in
    out = value_new();
    out->type = SEQ_VAL;
    out->data.seq_val = list_new();

    l = in->data.seq_val;

    for(c = cursor_new_front(l); cursor_valid(c); cursor_next(c))
        list_push_back(out->data.seq_val,
                       function_exec(f, value_copy(cursor_get(c))));

    value_delete(in);
    cursor_delete(c);
    return out;

}
Exemplo n.º 3
0
void			read_client(t_serv *serv, int cs)
{
  char			str[4096];
  pid_t			pid;

  pid = fork();
  if (pid == 0)
    {
      while (1)
	{
	  memset(str, 0, 4096);
	  if (read(cs, str, 4096) != 0)
	    {
	      if (strcmp(EXIT, str) == 0)
		client_disconnect(serv, cs);
	      if (strcmp(str, GET) == 0)
		send_file(cs);
	      else if (strcmp(str, PUT) == 0)
		recv_file(cs);
	      else
		function_exec(serv, str, cs);
	    }
	}
    }
  return ;
}
Exemplo n.º 4
0
Arquivo: forms.c Projeto: bieber/col
/*** construct
 * Sequence construction.  Feeds its input to each of its argument functions,
 * and generate a sequence where each element is the output of one of the
 * argument functions.
 *
 * construct{ f, g } : x  = < f : x, g : x >
 */
struct value *construct(struct list *args, struct value *in)
{
    struct value *out = value_new();
    struct cursor *c = NULL;

    out->type = SEQ_VAL;
    out->data.seq_val = list_new();

    for(c = cursor_new_front(args); cursor_valid(c); cursor_next(c))
    {
        list_push_back(out->data.seq_val,
                       function_exec(cursor_get(c), value_copy(in)));
    }
    value_delete(in);
    cursor_delete(c);
    return out;
}
Exemplo n.º 5
0
Arquivo: forms.c Projeto: bieber/col
/*** compose
 * Function composition.  Feeds its input to the last function in its argument
 * list, then feeds that function's output to the second-to-last, and so on,
 * eventually returning the output of the last function in the list as the
 * combined function's output.
 *
 * compose{ f, g } : x = f : (g : x)
 */
struct value *compose(struct list *args, struct value *in)
{
    struct value *current = in;
    struct value *last = in;
    struct function *f = NULL;
    struct cursor *c = NULL;

    // Stepping backwards through the list of arguments and feeding
    // input to successive functions, deleting intermediate values
    for(c = cursor_new_back(args); cursor_valid(c); cursor_prev(c))
    {
        f = cursor_get(c);
        last = current;
        current = function_exec(f, current);
    }
    cursor_delete(c);

    return current;
}
Exemplo n.º 6
0
Arquivo: forms.c Projeto: bieber/col
/*** reduce
 * Reducing functional form.  Accepts a single function argument.  Expects
 * input in the form of a list, return value is the result of first applying
 * the argument function to a pair formed from the first two elements of the
 * input list, then forming a new pair from that result and the next
 * right-most element, and so on until the list is exhausted.
 *
 * reduce{ f } : < x, y, z > = f : < f : < x, y>, z >
 */
struct value *reduce(struct list *args, struct value *in)
{
    struct value *out = value_new();
    struct value *v = NULL;
    struct function *f = list_get(args, 0);
    int i = 0;

    // Check for valid input
    if (args->count != 1 || in->type != SEQ_VAL || in->data.seq_val->count < 2)
    {
        value_delete(in);
        return out;
    }

    // Setting up initial pair
    out->type = SEQ_VAL;
    out->data.seq_val = list_new();
    list_push_back(out->data.seq_val,
                   value_copy(list_get(in->data.seq_val, 0)));
    list_push_back(out->data.seq_val,
                   value_copy(list_get(in->data.seq_val, 1)));
    // Pairing up elements and feeding them to f
    for (i = 2; i <= in->data.seq_val->count; i++)
    {
        out = function_exec(f, out);
        if (i < in->data.seq_val->count)
        {
            v = value_new();
            v->type = SEQ_VAL;
            v->data.seq_val = list_new();
            list_push_back(v->data.seq_val, out);
            list_push_back(v->data.seq_val,
                           value_copy(list_get(in->data.seq_val, i)));
            out = v;
        }
    }

    value_delete(in);
    return out;
}