Example #1
0
/**
 * example of packing three values into an expression of the form:
 *
 * (tag (v1 v2 v3))
 *
 * COMMENTS SHOW THE STATE OF SX AS IT IS CONSTRUCTED
 */
sexp_t *pack(char *tag, float v1, float v2, float v3) {
  sexp_t *sx = new_sexp_list(new_sexp_atom(tag,strlen(tag),SEXP_BASIC));
  /* sx = (tag) */
  char sbuf[32];
  sexp_t *vlist,*vptr;
  

  vlist = new_sexp_list(NULL); /* vlist = () */
  sx->list->next = vlist; /* sx = (tag ()) */

  sprintf(sbuf,"%f",v1);
  vlist->list = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1) */
  vptr = vlist->list;

  /* sx = (tag (v1)) */

  sprintf(sbuf,"%f",v2);
  vptr->next = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1 v2) */
  vptr = vptr->next;

  /* sx = (tag (v1 v2)) */

  sprintf(sbuf,"%f",v3);
  vptr->next = new_sexp_atom(sbuf,strlen(sbuf),SEXP_BASIC); /* vlist = (v1 v2 v3) */

  /* sx = (tag (v1 v2 v3))  ---- done, return. */

  return sx;
}
Example #2
0
int ml_eval(char **input, char resp[BUFSIZ], dict_t *env) {
	size_t len = strlen(*input);
	sexp_t *sx = parse_sexp(*input, len);
	char *v;

	memset(resp, 0, sizeof(*resp));

	if (sx->ty != SEXP_LIST || sx->list->ty != SEXP_VALUE) {
		print_sexp(resp, BUFSIZ, sx);
		return 0;
	}

	v = sx->list->val;

	if (strcmp(v, "define") == 0) {
		insert(sx->list->next->val, sx->list->next->next, env);
		print_sexp(resp, BUFSIZ, sx->list->next->next);
		sx->list->next->next = NULL;
	} else if (strcmp(v, "env") == 0) {
		sexp_t *out, *vlist, *vptr;
		out = new_sexp_list(NULL);

		dict_t *_env = env;
		dict_t *next;

		char r2[BUFSIZ];
		memset(r2, 0, BUFSIZ);
		while (_env != NULL) {
			next  = _env->next;

			vlist = new_sexp_list(NULL);

			if (out->list == NULL) {
				out->list = vptr = vlist;
			} else {
				vptr->next = vlist;
				vptr = vlist;
			}

			vlist->list = new_sexp_atom(_env->varname, strlen(_env->varname), SEXP_BASIC);
			vlist->list->next = _env->valexp;

			_env = next;
		}


		print_sexp(resp, BUFSIZ, out);
	} else {
		sexp_t *ret = NULL;

		lookup(v, env, &ret);
		print_sexp(resp, BUFSIZ, ret);
	}

	return 0;
}