Blame src/alisp/alisp.c

Packit 4a16fb
/*
Packit 4a16fb
 *  ALSA lisp implementation
Packit 4a16fb
 *  Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *  Based on work of Sandro Sigala (slisp-1.2)
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <assert.h>
Packit 4a16fb
Packit 4a16fb
#include <limits.h>
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <ctype.h>
Packit 4a16fb
#include <math.h>
Packit 4a16fb
#include <err.h>
Packit 4a16fb
Packit 4a16fb
#define alisp_seq_iterator alisp_object
Packit 4a16fb
Packit 4a16fb
#include "local.h"
Packit 4a16fb
#include "alisp.h"
Packit 4a16fb
#include "alisp_local.h"
Packit 4a16fb
Packit 4a16fb
struct alisp_object alsa_lisp_nil;
Packit 4a16fb
struct alisp_object alsa_lisp_t;
Packit 4a16fb
Packit 4a16fb
/* parser prototypes */
Packit 4a16fb
static struct alisp_object * parse_object(struct alisp_instance *instance, int havetoken);
Packit 4a16fb
static void princ_cons(snd_output_t *out, struct alisp_object * p);
Packit 4a16fb
static void princ_object(snd_output_t *out, struct alisp_object * p);
Packit 4a16fb
static struct alisp_object * eval(struct alisp_instance *instance, struct alisp_object * p);
Packit 4a16fb
Packit 4a16fb
/* functions */
Packit 4a16fb
static struct alisp_object *F_eval(struct alisp_instance *instance, struct alisp_object *);
Packit 4a16fb
static struct alisp_object *F_progn(struct alisp_instance *instance, struct alisp_object *);
Packit 4a16fb
static struct alisp_object *F_funcall(struct alisp_instance *instance, struct alisp_object *);
Packit 4a16fb
Packit 4a16fb
/* others */
Packit 4a16fb
static int alisp_include_file(struct alisp_instance *instance, const char *filename);
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  object handling
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static int get_string_hash(const char *s)
Packit 4a16fb
{
Packit 4a16fb
	int val = 0;
Packit 4a16fb
	if (s == NULL)
Packit 4a16fb
		return val;
Packit 4a16fb
	while (*s)
Packit 4a16fb
		val += *s++;
Packit 4a16fb
	return val & ALISP_OBJ_PAIR_HASH_MASK;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void nomem(void)
Packit 4a16fb
{
Packit 4a16fb
	SNDERR("alisp: no enough memory");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void lisp_verbose(struct alisp_instance *instance, const char *fmt, ...)
Packit 4a16fb
{
Packit 4a16fb
	va_list ap;
Packit 4a16fb
Packit 4a16fb
	if (!instance->verbose)
Packit 4a16fb
		return;
Packit 4a16fb
	va_start(ap, fmt);
Packit 4a16fb
	snd_output_printf(instance->vout, "alisp: ");
Packit 4a16fb
	snd_output_vprintf(instance->vout, fmt, ap);
Packit 4a16fb
	snd_output_putc(instance->vout, '\n');
Packit 4a16fb
	va_end(ap);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void lisp_error(struct alisp_instance *instance, const char *fmt, ...)
Packit 4a16fb
{
Packit 4a16fb
	va_list ap;
Packit 4a16fb
Packit 4a16fb
	if (!instance->warning)
Packit 4a16fb
		return;
Packit 4a16fb
	va_start(ap, fmt);
Packit 4a16fb
	snd_output_printf(instance->eout, "alisp error: ");
Packit 4a16fb
	snd_output_vprintf(instance->eout, fmt, ap);
Packit 4a16fb
	snd_output_putc(instance->eout, '\n');
Packit 4a16fb
	va_end(ap);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void lisp_warn(struct alisp_instance *instance, const char *fmt, ...)
Packit 4a16fb
{
Packit 4a16fb
	va_list ap;
Packit 4a16fb
Packit 4a16fb
	if (!instance->warning)
Packit 4a16fb
		return;
Packit 4a16fb
	va_start(ap, fmt);
Packit 4a16fb
	snd_output_printf(instance->wout, "alisp warning: ");
Packit 4a16fb
	snd_output_vprintf(instance->wout, fmt, ap);
Packit 4a16fb
	snd_output_putc(instance->wout, '\n');
Packit 4a16fb
	va_end(ap);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void lisp_debug(struct alisp_instance *instance, const char *fmt, ...)
Packit 4a16fb
{
Packit 4a16fb
	va_list ap;
Packit 4a16fb
Packit 4a16fb
	if (!instance->debug)
Packit 4a16fb
		return;
Packit 4a16fb
	va_start(ap, fmt);
Packit 4a16fb
	snd_output_printf(instance->dout, "alisp debug: ");
Packit 4a16fb
	snd_output_vprintf(instance->dout, fmt, ap);
Packit 4a16fb
	snd_output_putc(instance->dout, '\n');
Packit 4a16fb
	va_end(ap);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_object(struct alisp_instance *instance, int type)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	if (list_empty(&instance->free_objs_list)) {
Packit 4a16fb
		p = (struct alisp_object *)malloc(sizeof(struct alisp_object));
Packit 4a16fb
		if (p == NULL) {
Packit 4a16fb
			nomem();
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		lisp_debug(instance, "allocating cons %p", p);
Packit 4a16fb
	} else {
Packit 4a16fb
		p = (struct alisp_object *)instance->free_objs_list.next;
Packit 4a16fb
		list_del(&p->list);
Packit 4a16fb
		instance->free_objs--;
Packit 4a16fb
		lisp_debug(instance, "recycling cons %p", p);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	instance->used_objs++;
Packit 4a16fb
Packit 4a16fb
	alisp_set_type(p, type);
Packit 4a16fb
	alisp_set_refs(p, 1);
Packit 4a16fb
	if (type == ALISP_OBJ_CONS) {
Packit 4a16fb
		p->value.c.car = &alsa_lisp_nil;
Packit 4a16fb
		p->value.c.cdr = &alsa_lisp_nil;
Packit 4a16fb
		list_add(&p->list, &instance->used_objs_list[0][ALISP_OBJ_CONS]);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (instance->used_objs + instance->free_objs > instance->max_objs)
Packit 4a16fb
		instance->max_objs = instance->used_objs + instance->free_objs;
Packit 4a16fb
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void free_object(struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	switch (alisp_get_type(p)) {
Packit 4a16fb
	case ALISP_OBJ_STRING:
Packit 4a16fb
	case ALISP_OBJ_IDENTIFIER:
Packit 4a16fb
		free(p->value.s);
Packit 4a16fb
		alisp_set_type(p, ALISP_OBJ_INTEGER);
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void delete_object(struct alisp_instance *instance, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (p == NULL || p == &alsa_lisp_nil || p == &alsa_lisp_t)
Packit 4a16fb
		return;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_NIL) ||
Packit 4a16fb
	    alisp_compare_type(p, ALISP_OBJ_T))
Packit 4a16fb
		return;
Packit 4a16fb
	assert(alisp_get_refs(p) > 0);
Packit 4a16fb
	lisp_debug(instance, "delete cons %p (type = %i, refs = %i) (s = '%s')", p, alisp_get_type(p), alisp_get_refs(p),
Packit 4a16fb
			alisp_compare_type(p, ALISP_OBJ_STRING) ||
Packit 4a16fb
			alisp_compare_type(p, ALISP_OBJ_IDENTIFIER) ? p->value.s : "???");
Packit 4a16fb
	if (alisp_dec_refs(p))
Packit 4a16fb
		return;
Packit 4a16fb
	list_del(&p->list);
Packit 4a16fb
	instance->used_objs--;
Packit 4a16fb
	free_object(p);
Packit 4a16fb
	if (instance->free_objs >= ALISP_FREE_OBJ_POOL) {
Packit 4a16fb
		lisp_debug(instance, "freed cons %p", p);
Packit 4a16fb
		free(p);
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
	lisp_debug(instance, "moved cons %p to free list", p);
Packit 4a16fb
	list_add(&p->list, &instance->free_objs_list);
Packit 4a16fb
	instance->free_objs++;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void delete_tree(struct alisp_instance *instance, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (p == NULL)
Packit 4a16fb
		return;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_CONS)) {
Packit 4a16fb
		delete_tree(instance, p->value.c.car);
Packit 4a16fb
		delete_tree(instance, p->value.c.cdr);
Packit 4a16fb
	}
Packit 4a16fb
	delete_object(instance, p);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * incref_object(struct alisp_instance *instance ATTRIBUTE_UNUSED, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (p == NULL || p == &alsa_lisp_nil || p == &alsa_lisp_t)
Packit 4a16fb
		return p;
Packit 4a16fb
	if (alisp_get_refs(p) == ALISP_MAX_REFS) {
Packit 4a16fb
		assert(0);
Packit 4a16fb
		fprintf(stderr, "OOPS: alsa lisp: incref fatal error\n");
Packit 4a16fb
		exit(EXIT_FAILURE);
Packit 4a16fb
	}
Packit 4a16fb
	alisp_inc_refs(p);
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * incref_tree(struct alisp_instance *instance, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (p == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_CONS)) {
Packit 4a16fb
		incref_tree(instance, p->value.c.car);
Packit 4a16fb
		incref_tree(instance, p->value.c.cdr);
Packit 4a16fb
	}
Packit 4a16fb
	return incref_object(instance, p);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* Function not used yet. Leave it commented out until we actually use it to
Packit 4a16fb
 * avoid compiler complaints */
Packit 4a16fb
#if 0
Packit 4a16fb
static struct alisp_object * incref_tree_explicit(struct alisp_instance *instance, struct alisp_object * p, struct alisp_object * e)
Packit 4a16fb
{
Packit 4a16fb
	if (p == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_CONS)) {
Packit 4a16fb
		if (e == p) {
Packit 4a16fb
			incref_tree(instance, p->value.c.car);
Packit 4a16fb
			incref_tree(instance, p->value.c.cdr);
Packit 4a16fb
		} else {
Packit 4a16fb
			incref_tree_explicit(instance, p->value.c.car, e);
Packit 4a16fb
			incref_tree_explicit(instance, p->value.c.cdr, e);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (e == p)
Packit 4a16fb
		return incref_object(instance, p);
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static void free_objects(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos, *pos1;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
	struct alisp_object_pair * pair;
Packit 4a16fb
	int i, j;
Packit 4a16fb
Packit 4a16fb
	for (i = 0; i < ALISP_OBJ_PAIR_HASH_SIZE; i++) {
Packit 4a16fb
		list_for_each_safe(pos, pos1, &instance->setobjs_list[i]) {
Packit 4a16fb
			pair = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
			lisp_debug(instance, "freeing pair: '%s' -> %p", pair->name, pair->value);
Packit 4a16fb
			delete_tree(instance, pair->value);
Packit 4a16fb
			free((void *)pair->name);
Packit 4a16fb
			free(pair);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	for (i = 0; i < ALISP_OBJ_PAIR_HASH_SIZE; i++)
Packit 4a16fb
		for (j = 0; j <= ALISP_OBJ_LAST_SEARCH; j++) {
Packit 4a16fb
			list_for_each_safe(pos, pos1, &instance->used_objs_list[i][j]) {
Packit 4a16fb
				p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
				lisp_warn(instance, "object %p is still referenced %i times!", p, alisp_get_refs(p));
Packit 4a16fb
#if 0
Packit 4a16fb
				snd_output_printf(instance->wout, ">>>> ");
Packit 4a16fb
				princ_object(instance->wout, p);
Packit 4a16fb
				snd_output_printf(instance->wout, " <<<<\n");
Packit 4a16fb
#endif
Packit 4a16fb
				if (alisp_get_refs(p) > 0)
Packit 4a16fb
					alisp_set_refs(p, 1);
Packit 4a16fb
				delete_object(instance, p);
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	list_for_each_safe(pos, pos1, &instance->free_objs_list) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		list_del(&p->list);
Packit 4a16fb
		free(p);
Packit 4a16fb
		lisp_debug(instance, "freed (all) cons %p", p);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * search_object_identifier(struct alisp_instance *instance, const char *s)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head * pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->used_objs_list[get_string_hash(s)][ALISP_OBJ_IDENTIFIER]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		if (alisp_get_refs(p) > ALISP_MAX_REFS_LIMIT)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (!strcmp(p->value.s, s))
Packit 4a16fb
			return incref_object(instance, p);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * search_object_string(struct alisp_instance *instance, const char *s)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head * pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->used_objs_list[get_string_hash(s)][ALISP_OBJ_STRING]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		if (!strcmp(p->value.s, s)) {
Packit 4a16fb
			if (alisp_get_refs(p) > ALISP_MAX_REFS_LIMIT)
Packit 4a16fb
				continue;
Packit 4a16fb
			return incref_object(instance, p);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * search_object_integer(struct alisp_instance *instance, long in)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head * pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->used_objs_list[in & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_INTEGER]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		if (p->value.i == in) {
Packit 4a16fb
			if (alisp_get_refs(p) > ALISP_MAX_REFS_LIMIT)
Packit 4a16fb
				continue;
Packit 4a16fb
			return incref_object(instance, p);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * search_object_float(struct alisp_instance *instance, double in)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head * pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->used_objs_list[(long)in & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_FLOAT]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		if (p->value.i == in) {
Packit 4a16fb
			if (alisp_get_refs(p) > ALISP_MAX_REFS_LIMIT)
Packit 4a16fb
				continue;
Packit 4a16fb
			return incref_object(instance, p);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * search_object_pointer(struct alisp_instance *instance, const void *ptr)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head * pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->used_objs_list[(long)ptr & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_POINTER]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		if (p->value.ptr == ptr) {
Packit 4a16fb
			if (alisp_get_refs(p) > ALISP_MAX_REFS_LIMIT)
Packit 4a16fb
				continue;
Packit 4a16fb
			return incref_object(instance, p);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_integer(struct alisp_instance *instance, long value)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * obj;
Packit 4a16fb
	
Packit 4a16fb
	obj = search_object_integer(instance, value);
Packit 4a16fb
	if (obj != NULL)
Packit 4a16fb
		return obj;
Packit 4a16fb
	obj = new_object(instance, ALISP_OBJ_INTEGER);
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		list_add(&obj->list, &instance->used_objs_list[value & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_INTEGER]);
Packit 4a16fb
		obj->value.i = value;
Packit 4a16fb
	}
Packit 4a16fb
	return obj;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_float(struct alisp_instance *instance, double value)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * obj;
Packit 4a16fb
	
Packit 4a16fb
	obj = search_object_float(instance, value);
Packit 4a16fb
	if (obj != NULL)
Packit 4a16fb
		return obj;
Packit 4a16fb
	obj = new_object(instance, ALISP_OBJ_FLOAT);
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		list_add(&obj->list, &instance->used_objs_list[(long)value & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_FLOAT]);
Packit 4a16fb
		obj->value.f = value;
Packit 4a16fb
	}
Packit 4a16fb
	return obj;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_string(struct alisp_instance *instance, const char *str)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * obj;
Packit 4a16fb
	
Packit 4a16fb
	obj = search_object_string(instance, str);
Packit 4a16fb
	if (obj != NULL)
Packit 4a16fb
		return obj;
Packit 4a16fb
	obj = new_object(instance, ALISP_OBJ_STRING);
Packit 4a16fb
	if (obj)
Packit 4a16fb
		list_add(&obj->list, &instance->used_objs_list[get_string_hash(str)][ALISP_OBJ_STRING]);
Packit 4a16fb
	if (obj && (obj->value.s = strdup(str)) == NULL) {
Packit 4a16fb
		delete_object(instance, obj);
Packit 4a16fb
		nomem();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	return obj;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_identifier(struct alisp_instance *instance, const char *id)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * obj;
Packit 4a16fb
	
Packit 4a16fb
	obj = search_object_identifier(instance, id);
Packit 4a16fb
	if (obj != NULL)
Packit 4a16fb
		return obj;
Packit 4a16fb
	obj = new_object(instance, ALISP_OBJ_IDENTIFIER);
Packit 4a16fb
	if (obj)
Packit 4a16fb
		list_add(&obj->list, &instance->used_objs_list[get_string_hash(id)][ALISP_OBJ_IDENTIFIER]);
Packit 4a16fb
	if (obj && (obj->value.s = strdup(id)) == NULL) {
Packit 4a16fb
		delete_object(instance, obj);
Packit 4a16fb
		nomem();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	return obj;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_pointer(struct alisp_instance *instance, const void *ptr)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * obj;
Packit 4a16fb
	
Packit 4a16fb
	obj = search_object_pointer(instance, ptr);
Packit 4a16fb
	if (obj != NULL)
Packit 4a16fb
		return obj;
Packit 4a16fb
	obj = new_object(instance, ALISP_OBJ_POINTER);
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		list_add(&obj->list, &instance->used_objs_list[(long)ptr & ALISP_OBJ_PAIR_HASH_MASK][ALISP_OBJ_POINTER]);
Packit 4a16fb
		obj->value.ptr = ptr;
Packit 4a16fb
	}
Packit 4a16fb
	return obj;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * new_cons_pointer(struct alisp_instance * instance, const char *ptr_id, void *ptr)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * lexpr;
Packit 4a16fb
Packit 4a16fb
	if (ptr == NULL)
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	lexpr = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
	if (lexpr == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	lexpr->value.c.car = new_string(instance, ptr_id);
Packit 4a16fb
	if (lexpr->value.c.car == NULL)
Packit 4a16fb
		goto __end;
Packit 4a16fb
	lexpr->value.c.cdr = new_pointer(instance, ptr);
Packit 4a16fb
	if (lexpr->value.c.cdr == NULL) {
Packit 4a16fb
		delete_object(instance, lexpr->value.c.car);
Packit 4a16fb
	      __end:
Packit 4a16fb
		delete_object(instance, lexpr);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	return lexpr;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void alsa_lisp_init_objects(void) __attribute__ ((constructor));
Packit 4a16fb
Packit 4a16fb
void alsa_lisp_init_objects(void)
Packit 4a16fb
{
Packit 4a16fb
	memset(&alsa_lisp_nil, 0, sizeof(alsa_lisp_nil));
Packit 4a16fb
	alisp_set_type(&alsa_lisp_nil, ALISP_OBJ_NIL);
Packit 4a16fb
	INIT_LIST_HEAD(&alsa_lisp_nil.list);
Packit 4a16fb
	memset(&alsa_lisp_t, 0, sizeof(alsa_lisp_t));
Packit 4a16fb
	alisp_set_type(&alsa_lisp_t, ALISP_OBJ_T);
Packit 4a16fb
	INIT_LIST_HEAD(&alsa_lisp_t.list);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * lexer
Packit 4a16fb
 */ 
Packit 4a16fb
Packit 4a16fb
static int xgetc(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	instance->charno++;
Packit 4a16fb
	if (instance->lex_bufp > instance->lex_buf)
Packit 4a16fb
		return *--(instance->lex_bufp);
Packit 4a16fb
	return snd_input_getc(instance->in);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline void xungetc(struct alisp_instance *instance, int c)
Packit 4a16fb
{
Packit 4a16fb
	*(instance->lex_bufp)++ = c;
Packit 4a16fb
	instance->charno--;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int init_lex(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	instance->charno = instance->lineno = 1;
Packit 4a16fb
	instance->token_buffer_max = 10;
Packit 4a16fb
	if ((instance->token_buffer = (char *)malloc(instance->token_buffer_max)) == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	instance->lex_bufp = instance->lex_buf;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void done_lex(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	free(instance->token_buffer);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char * extend_buf(struct alisp_instance *instance, char *p)
Packit 4a16fb
{
Packit 4a16fb
	int off = p - instance->token_buffer;
Packit 4a16fb
Packit 4a16fb
	instance->token_buffer_max += 10;
Packit 4a16fb
	instance->token_buffer = (char *)realloc(instance->token_buffer, instance->token_buffer_max);
Packit 4a16fb
	if (instance->token_buffer == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return instance->token_buffer + off;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int gettoken(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	char *p;
Packit 4a16fb
	int c;
Packit 4a16fb
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		c = xgetc(instance);
Packit 4a16fb
		switch (c) {
Packit 4a16fb
		case '\n':
Packit 4a16fb
			++instance->lineno;
Packit 4a16fb
			break;
Packit 4a16fb
Packit 4a16fb
		case ' ': case '\f': case '\t': case '\v': case '\r':
Packit 4a16fb
			break;
Packit 4a16fb
Packit 4a16fb
		case ';':
Packit 4a16fb
			/* Comment: ";".*"\n" */
Packit 4a16fb
			while ((c = xgetc(instance)) != '\n' && c != EOF)
Packit 4a16fb
				;
Packit 4a16fb
			if (c != EOF)
Packit 4a16fb
				++instance->lineno;
Packit 4a16fb
			break;
Packit 4a16fb
Packit 4a16fb
		case '?':
Packit 4a16fb
			/* Character: "?". */
Packit 4a16fb
			c = xgetc(instance);
Packit 4a16fb
			sprintf(instance->token_buffer, "%d", c);
Packit 4a16fb
			return instance->thistoken = ALISP_INTEGER;
Packit 4a16fb
Packit 4a16fb
		case '-':
Packit 4a16fb
			/* Minus sign: "-". */
Packit 4a16fb
			c = xgetc(instance);
Packit 4a16fb
			if (!isdigit(c)) {
Packit 4a16fb
				xungetc(instance, c);
Packit 4a16fb
				c = '-';
Packit 4a16fb
				goto got_id;
Packit 4a16fb
			}
Packit 4a16fb
			xungetc(instance, c);
Packit 4a16fb
			c = '-';
Packit 4a16fb
			/* FALLTRHU */
Packit 4a16fb
Packit 4a16fb
		case '0':
Packit 4a16fb
		case '1': case '2': case '3':
Packit 4a16fb
		case '4': case '5': case '6':
Packit 4a16fb
		case '7': case '8': case '9':
Packit 4a16fb
			/* Integer: [0-9]+ */
Packit 4a16fb
			p = instance->token_buffer;
Packit 4a16fb
			instance->thistoken = ALISP_INTEGER;
Packit 4a16fb
			do {
Packit 4a16fb
			      __ok:
Packit 4a16fb
				if (p - instance->token_buffer >= instance->token_buffer_max - 1) {
Packit 4a16fb
					p = extend_buf(instance, p);
Packit 4a16fb
					if (p == NULL)
Packit 4a16fb
						return instance->thistoken = EOF;
Packit 4a16fb
				}
Packit 4a16fb
				*p++ = c;
Packit 4a16fb
				c = xgetc(instance);
Packit 4a16fb
				if (c == '.' && instance->thistoken == ALISP_INTEGER) {
Packit 4a16fb
					c = xgetc(instance);
Packit 4a16fb
					xungetc(instance, c);
Packit 4a16fb
					if (isdigit(c)) {
Packit 4a16fb
						instance->thistoken = ALISP_FLOAT;
Packit 4a16fb
						c = '.';
Packit 4a16fb
						goto __ok;
Packit 4a16fb
					} else {
Packit 4a16fb
						c = '.';
Packit 4a16fb
					}
Packit 4a16fb
				} else if (c == 'e' && instance->thistoken == ALISP_FLOAT) {
Packit 4a16fb
					c = xgetc(instance);
Packit 4a16fb
					if (isdigit(c)) {
Packit 4a16fb
						instance->thistoken = ALISP_FLOATE;
Packit 4a16fb
						goto __ok;
Packit 4a16fb
					}
Packit 4a16fb
				}
Packit 4a16fb
			} while (isdigit(c));
Packit 4a16fb
			xungetc(instance, c);
Packit 4a16fb
			*p = '\0';
Packit 4a16fb
			return instance->thistoken;
Packit 4a16fb
Packit 4a16fb
		got_id:
Packit 4a16fb
		case '!': case '_': case '+': case '*': case '/': case '%':
Packit 4a16fb
		case '<': case '>': case '=': case '&':
Packit 4a16fb
		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
Packit 4a16fb
		case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
Packit 4a16fb
		case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
Packit 4a16fb
		case 's': case 't': case 'u': case 'v': case 'w': case 'x':
Packit 4a16fb
		case 'y': case 'z':
Packit 4a16fb
		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
Packit 4a16fb
		case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
Packit 4a16fb
		case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
Packit 4a16fb
		case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
Packit 4a16fb
		case 'Y': case 'Z':
Packit 4a16fb
			/* Identifier: [!-/+*%<>=&a-zA-Z_][-/+*%<>=&a-zA-Z_0-9]* */
Packit 4a16fb
			p = instance->token_buffer;
Packit 4a16fb
			do {
Packit 4a16fb
				if (p - instance->token_buffer >= instance->token_buffer_max - 1) {
Packit 4a16fb
					p = extend_buf(instance, p);
Packit 4a16fb
					if (p == NULL)
Packit 4a16fb
						return instance->thistoken = EOF;
Packit 4a16fb
				}
Packit 4a16fb
				*p++ = c;
Packit 4a16fb
				c = xgetc(instance);
Packit 4a16fb
			} while (isalnum(c) || strchr("!_-+*/%<>=&", c) != NULL);
Packit 4a16fb
			xungetc(instance, c);
Packit 4a16fb
			*p = '\0';
Packit 4a16fb
			return instance->thistoken = ALISP_IDENTIFIER;
Packit 4a16fb
Packit 4a16fb
		case '"':
Packit 4a16fb
			/* String: "\""([^"]|"\\".)*"\"" */
Packit 4a16fb
			p = instance->token_buffer;
Packit 4a16fb
			while ((c = xgetc(instance)) != '"' && c != EOF) {
Packit 4a16fb
				if (p - instance->token_buffer >= instance->token_buffer_max - 1) {
Packit 4a16fb
					p = extend_buf(instance, p);
Packit 4a16fb
					if (p == NULL)
Packit 4a16fb
						return instance->thistoken = EOF;
Packit 4a16fb
				}
Packit 4a16fb
				if (c == '\\') {
Packit 4a16fb
					c = xgetc(instance);
Packit 4a16fb
					switch (c) {
Packit 4a16fb
					case '\n': ++instance->lineno; break;
Packit 4a16fb
					case 'a': *p++ = '\a'; break;
Packit 4a16fb
					case 'b': *p++ = '\b'; break;
Packit 4a16fb
					case 'f': *p++ = '\f'; break;
Packit 4a16fb
					case 'n': *p++ = '\n'; break;
Packit 4a16fb
					case 'r': *p++ = '\r'; break;
Packit 4a16fb
					case 't': *p++ = '\t'; break;
Packit 4a16fb
					case 'v': *p++ = '\v'; break;
Packit 4a16fb
					default: *p++ = c;
Packit 4a16fb
					}
Packit 4a16fb
				} else {
Packit 4a16fb
					if (c == '\n')
Packit 4a16fb
						++instance->lineno;
Packit 4a16fb
					*p++ = c;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
			*p = '\0';
Packit 4a16fb
			return instance->thistoken = ALISP_STRING;
Packit 4a16fb
Packit 4a16fb
		default:
Packit 4a16fb
			return instance->thistoken = c;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  parser
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * parse_form(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	int thistoken;
Packit 4a16fb
	struct alisp_object * p, * first = NULL, * prev = NULL;
Packit 4a16fb
Packit 4a16fb
	while ((thistoken = gettoken(instance)) != ')' && thistoken != EOF) {
Packit 4a16fb
		/*
Packit 4a16fb
		 * Parse a dotted pair notation.
Packit 4a16fb
		 */
Packit 4a16fb
		if (thistoken == '.') {
Packit 4a16fb
			gettoken(instance);
Packit 4a16fb
			if (prev == NULL) {
Packit 4a16fb
				lisp_error(instance, "unexpected '.'");
Packit 4a16fb
			      __err:
Packit 4a16fb
				delete_tree(instance, first);
Packit 4a16fb
				return NULL;
Packit 4a16fb
			}
Packit 4a16fb
			prev->value.c.cdr = parse_object(instance, 1);
Packit 4a16fb
			if (prev->value.c.cdr == NULL)
Packit 4a16fb
				goto __err;
Packit 4a16fb
			if ((thistoken = gettoken(instance)) != ')') {
Packit 4a16fb
				lisp_error(instance, "expected ')'");
Packit 4a16fb
				goto __err;
Packit 4a16fb
			}
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		p = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
		if (p == NULL)
Packit 4a16fb
			goto __err;
Packit 4a16fb
Packit 4a16fb
		if (first == NULL)
Packit 4a16fb
			first = p;
Packit 4a16fb
		if (prev != NULL)
Packit 4a16fb
			prev->value.c.cdr = p;
Packit 4a16fb
Packit 4a16fb
		p->value.c.car = parse_object(instance, 1);
Packit 4a16fb
		if (p->value.c.car == NULL)
Packit 4a16fb
			goto __err;
Packit 4a16fb
Packit 4a16fb
		prev = p;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (first == NULL)
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	else
Packit 4a16fb
		return first;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * quote_object(struct alisp_instance *instance, struct alisp_object * obj)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	if (obj == NULL)
Packit 4a16fb
		goto __end1;
Packit 4a16fb
Packit 4a16fb
	p = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
	if (p == NULL)
Packit 4a16fb
		goto __end1;
Packit 4a16fb
Packit 4a16fb
	p->value.c.car = new_identifier(instance, "quote");
Packit 4a16fb
	if (p->value.c.car == NULL)
Packit 4a16fb
		goto __end;
Packit 4a16fb
	p->value.c.cdr = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
	if (p->value.c.cdr == NULL) {
Packit 4a16fb
		delete_object(instance, p->value.c.car);
Packit 4a16fb
	      __end:
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
	      __end1:
Packit 4a16fb
		delete_tree(instance, obj);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	p->value.c.cdr->value.c.car = obj;
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline struct alisp_object * parse_quote(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	return quote_object(instance, parse_object(instance, 0));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * parse_object(struct alisp_instance *instance, int havetoken)
Packit 4a16fb
{
Packit 4a16fb
	int thistoken;
Packit 4a16fb
	struct alisp_object * p = NULL;
Packit 4a16fb
Packit 4a16fb
	if (!havetoken)
Packit 4a16fb
		thistoken = gettoken(instance);
Packit 4a16fb
	else
Packit 4a16fb
		thistoken = instance->thistoken;
Packit 4a16fb
Packit 4a16fb
	switch (thistoken) {
Packit 4a16fb
	case EOF:
Packit 4a16fb
		break;
Packit 4a16fb
	case '(':
Packit 4a16fb
		p = parse_form(instance);
Packit 4a16fb
		break;
Packit 4a16fb
	case '\'':
Packit 4a16fb
		p = parse_quote(instance);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_IDENTIFIER:
Packit 4a16fb
		if (!strcmp(instance->token_buffer, "t"))
Packit 4a16fb
			p = &alsa_lisp_t;
Packit 4a16fb
		else if (!strcmp(instance->token_buffer, "nil"))
Packit 4a16fb
			p = &alsa_lisp_nil;
Packit 4a16fb
		else {
Packit 4a16fb
			p = new_identifier(instance, instance->token_buffer);
Packit 4a16fb
		}
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_INTEGER: {
Packit 4a16fb
		p = new_integer(instance, atol(instance->token_buffer));
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case ALISP_FLOAT:
Packit 4a16fb
	case ALISP_FLOATE: {
Packit 4a16fb
		p = new_float(instance, atof(instance->token_buffer));
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	case ALISP_STRING:
Packit 4a16fb
		p = new_string(instance, instance->token_buffer);
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		lisp_warn(instance, "%d:%d: unexpected character `%c'", instance->lineno, instance->charno, thistoken);
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  object manipulation
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static struct alisp_object_pair * set_object_direct(struct alisp_instance *instance, struct alisp_object * name, struct alisp_object * value)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	const char *id;
Packit 4a16fb
Packit 4a16fb
	id = name->value.s;
Packit 4a16fb
	p = (struct alisp_object_pair *)malloc(sizeof(struct alisp_object_pair));
Packit 4a16fb
	if (p == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	p->name = strdup(id);
Packit 4a16fb
	if (p->name == NULL) {
Packit 4a16fb
		delete_tree(instance, value);
Packit 4a16fb
		free(p);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	list_add(&p->list, &instance->setobjs_list[get_string_hash(id)]);
Packit 4a16fb
	p->value = value;
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int check_set_object(struct alisp_instance * instance, struct alisp_object * name)
Packit 4a16fb
{
Packit 4a16fb
	if (name == &alsa_lisp_nil) {
Packit 4a16fb
		lisp_warn(instance, "setting the value of a nil object");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	if (name == &alsa_lisp_t) {
Packit 4a16fb
		lisp_warn(instance, "setting the value of a t object");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	if (!alisp_compare_type(name, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !alisp_compare_type(name, ALISP_OBJ_STRING)) {
Packit 4a16fb
		lisp_warn(instance, "setting the value of an object with non-indentifier");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object_pair * set_object(struct alisp_instance *instance, struct alisp_object * name, struct alisp_object * value)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	const char *id;
Packit 4a16fb
Packit 4a16fb
	if (name == NULL || value == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
Packit 4a16fb
	id = name->value.s;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->setobjs_list[get_string_hash(id)]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
		if (!strcmp(p->name, id)) {
Packit 4a16fb
			delete_tree(instance, p->value);
Packit 4a16fb
			p->value = value;
Packit 4a16fb
			return p;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	p = (struct alisp_object_pair *)malloc(sizeof(struct alisp_object_pair));
Packit 4a16fb
	if (p == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	p->name = strdup(id);
Packit 4a16fb
	if (p->name == NULL) {
Packit 4a16fb
		delete_tree(instance, value);
Packit 4a16fb
		free(p);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	list_add(&p->list, &instance->setobjs_list[get_string_hash(id)]);
Packit 4a16fb
	p->value = value;
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * unset_object(struct alisp_instance *instance, struct alisp_object * name)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	struct alisp_object *res;
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	const char *id;
Packit 4a16fb
	
Packit 4a16fb
	if (!alisp_compare_type(name, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !alisp_compare_type(name, ALISP_OBJ_STRING)) {
Packit 4a16fb
	    	lisp_warn(instance, "unset object with a non-indentifier");
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	id = name->value.s;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->setobjs_list[get_string_hash(id)]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
		if (!strcmp(p->name, id)) {
Packit 4a16fb
			list_del(&p->list);
Packit 4a16fb
			res = p->value;
Packit 4a16fb
			free((void *)p->name);
Packit 4a16fb
			free(p);
Packit 4a16fb
			return res;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * get_object1(struct alisp_instance *instance, const char *id)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &instance->setobjs_list[get_string_hash(id)]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
		if (!strcmp(p->name, id))
Packit 4a16fb
			return p->value;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * get_object(struct alisp_instance *instance, struct alisp_object * name)
Packit 4a16fb
{
Packit 4a16fb
	if (!alisp_compare_type(name, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !alisp_compare_type(name, ALISP_OBJ_STRING)) {
Packit 4a16fb
	    	delete_tree(instance, name);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	return get_object1(instance, name->value.s);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * replace_object(struct alisp_instance *instance, struct alisp_object * name, struct alisp_object * onew)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	struct alisp_object *r;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	const char *id;
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(name, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !alisp_compare_type(name, ALISP_OBJ_STRING)) {
Packit 4a16fb
	    	delete_tree(instance, name);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	id = name->value.s;
Packit 4a16fb
	list_for_each(pos, &instance->setobjs_list[get_string_hash(id)]) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
		if (!strcmp(p->name, id)) {
Packit 4a16fb
			r = p->value;
Packit 4a16fb
			p->value = onew;
Packit 4a16fb
			return r;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void dump_objects(struct alisp_instance *instance, const char *fname)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object_pair *p;
Packit 4a16fb
	snd_output_t *out;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	int i, err;
Packit 4a16fb
Packit 4a16fb
	if (!strcmp(fname, "-"))
Packit 4a16fb
		err = snd_output_stdio_attach(&out, stdout, 0);
Packit 4a16fb
	else
Packit 4a16fb
		err = snd_output_stdio_open(&out, fname, "w+");
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		SNDERR("alisp: cannot open file '%s' for writing (%s)", fname, snd_strerror(errno));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	for (i = 0; i < ALISP_OBJ_PAIR_HASH_SIZE; i++) {
Packit 4a16fb
		list_for_each(pos, &instance->setobjs_list[i]) {
Packit 4a16fb
			p = list_entry(pos, struct alisp_object_pair, list);
Packit 4a16fb
			if (alisp_compare_type(p->value, ALISP_OBJ_CONS) &&
Packit 4a16fb
			    alisp_compare_type(p->value->value.c.car, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
			    !strcmp(p->value->value.c.car->value.s, "lambda")) {
Packit 4a16fb
			    	snd_output_printf(out, "(defun %s ", p->name);
Packit 4a16fb
			    	princ_cons(out, p->value->value.c.cdr);
Packit 4a16fb
			    	snd_output_printf(out, ")\n");
Packit 4a16fb
			    	continue;
Packit 4a16fb
			}
Packit 4a16fb
			snd_output_printf(out, "(setq %s '", p->name);
Packit 4a16fb
 			princ_object(out, p->value);
Packit 4a16fb
			snd_output_printf(out, ")\n");
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	snd_output_close(out);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const char *obj_type_str(struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	switch (alisp_get_type(p)) {
Packit 4a16fb
	case ALISP_OBJ_NIL: return "nil";
Packit 4a16fb
	case ALISP_OBJ_T: return "t";
Packit 4a16fb
	case ALISP_OBJ_INTEGER: return "integer";
Packit 4a16fb
	case ALISP_OBJ_FLOAT: return "float";
Packit 4a16fb
	case ALISP_OBJ_IDENTIFIER: return "identifier";
Packit 4a16fb
	case ALISP_OBJ_STRING: return "string";
Packit 4a16fb
	case ALISP_OBJ_POINTER: return "pointer";
Packit 4a16fb
	case ALISP_OBJ_CONS: return "cons";
Packit 4a16fb
	default: assert(0);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void print_obj_lists(struct alisp_instance *instance, snd_output_t *out)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
	int i, j;
Packit 4a16fb
Packit 4a16fb
	snd_output_printf(out, "** used objects\n");
Packit 4a16fb
	for (i = 0; i < ALISP_OBJ_PAIR_HASH_SIZE; i++)
Packit 4a16fb
		for (j = 0; j <= ALISP_OBJ_LAST_SEARCH; j++)
Packit 4a16fb
			list_for_each(pos, &instance->used_objs_list[i][j]) {
Packit 4a16fb
				p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
				snd_output_printf(out, "**   %p (%s) (", p, obj_type_str(p));
Packit 4a16fb
				if (!alisp_compare_type(p, ALISP_OBJ_CONS))
Packit 4a16fb
					princ_object(out, p);
Packit 4a16fb
				else
Packit 4a16fb
					snd_output_printf(out, "cons");
Packit 4a16fb
				snd_output_printf(out, ") refs=%i\n", alisp_get_refs(p));
Packit 4a16fb
			}
Packit 4a16fb
	snd_output_printf(out, "** free objects\n");
Packit 4a16fb
	list_for_each(pos, &instance->free_objs_list) {
Packit 4a16fb
		p = list_entry(pos, struct alisp_object, list);
Packit 4a16fb
		snd_output_printf(out, "**   %p\n", p);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void dump_obj_lists(struct alisp_instance *instance, const char *fname)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_t *out;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (!strcmp(fname, "-"))
Packit 4a16fb
		err = snd_output_stdio_attach(&out, stdout, 0);
Packit 4a16fb
	else
Packit 4a16fb
		err = snd_output_stdio_open(&out, fname, "w+");
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		SNDERR("alisp: cannot open file '%s' for writing (%s)", fname, snd_strerror(errno));
Packit 4a16fb
		return;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	print_obj_lists(instance, out);
Packit 4a16fb
Packit 4a16fb
	snd_output_close(out);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  functions
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static int count_list(struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	int i = 0;
Packit 4a16fb
Packit 4a16fb
	while (p != &alsa_lisp_nil && alisp_compare_type(p, ALISP_OBJ_CONS)) {
Packit 4a16fb
		p = p->value.c.cdr;
Packit 4a16fb
		++i;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return i;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline struct alisp_object * car(struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_CONS))
Packit 4a16fb
		return p->value.c.car;
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline struct alisp_object * cdr(struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_CONS))
Packit 4a16fb
		return p->value.c.cdr;
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (car expr)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_car(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object *p1 = car(args), *p2;
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	p1 = eval(instance, p1);
Packit 4a16fb
	delete_tree(instance, cdr(p1));
Packit 4a16fb
	p2 = car(p1);
Packit 4a16fb
	delete_object(instance, p1);
Packit 4a16fb
	return p2;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (cdr expr)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_cdr(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object *p1 = car(args), *p2;
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	p1 = eval(instance, p1);
Packit 4a16fb
	delete_tree(instance, car(p1));
Packit 4a16fb
	p2 = cdr(p1);
Packit 4a16fb
	delete_object(instance, p1);
Packit 4a16fb
	return p2;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (+ expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_add(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * n;
Packit 4a16fb
	long v = 0;
Packit 4a16fb
	double f = 0;
Packit 4a16fb
	int type = ALISP_OBJ_INTEGER;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(p));
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
			if (type == ALISP_OBJ_FLOAT)
Packit 4a16fb
				f += p1->value.i;
Packit 4a16fb
			else
Packit 4a16fb
				v += p1->value.i;
Packit 4a16fb
		} else if (alisp_compare_type(p1, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
			f += p1->value.f + v;
Packit 4a16fb
			v = 0;
Packit 4a16fb
			type = ALISP_OBJ_FLOAT;
Packit 4a16fb
		} else {
Packit 4a16fb
			lisp_warn(instance, "sum with a non integer or float operand");
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		p = cdr(n = p);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
		if (p == &alsa_lisp_nil)
Packit 4a16fb
			break;
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
	}
Packit 4a16fb
	if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
		return new_integer(instance, v);
Packit 4a16fb
	} else {
Packit 4a16fb
		return new_float(instance, f);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (concat expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_concat(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * n;
Packit 4a16fb
	char *str = NULL, *str1;
Packit 4a16fb
	
Packit 4a16fb
	p1 = eval(instance, car(p));
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_STRING)) {
Packit 4a16fb
			str1 = realloc(str, (str ? strlen(str) : 0) + strlen(p1->value.s) + 1);
Packit 4a16fb
			if (str1 == NULL) {
Packit 4a16fb
				nomem();
Packit 4a16fb
				free(str);
Packit 4a16fb
				return NULL;
Packit 4a16fb
			}
Packit 4a16fb
			if (str == NULL)
Packit 4a16fb
				strcpy(str1, p1->value.s);
Packit 4a16fb
			else
Packit 4a16fb
				strcat(str1, p1->value.s);
Packit 4a16fb
			str = str1;
Packit 4a16fb
		} else {
Packit 4a16fb
			lisp_warn(instance, "concat with a non string or identifier operand");
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		p = cdr(n = p);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
		if (p == &alsa_lisp_nil)
Packit 4a16fb
			break;
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
	}
Packit 4a16fb
	if (str) {
Packit 4a16fb
		p = new_string(instance, str);
Packit 4a16fb
		free(str);
Packit 4a16fb
	} else {
Packit 4a16fb
		p = &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (- expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_sub(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * n;
Packit 4a16fb
	long v = 0;
Packit 4a16fb
	double f = 0;
Packit 4a16fb
	int type = ALISP_OBJ_INTEGER;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
			if (p == args && cdr(p) != &alsa_lisp_nil) {
Packit 4a16fb
				v = p1->value.i;
Packit 4a16fb
			} else {
Packit 4a16fb
				if (type == ALISP_OBJ_FLOAT)
Packit 4a16fb
					f -= p1->value.i;
Packit 4a16fb
				else
Packit 4a16fb
					v -= p1->value.i;
Packit 4a16fb
			}
Packit 4a16fb
		} else if (alisp_compare_type(p1, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
			if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
				f = v;
Packit 4a16fb
				type = ALISP_OBJ_FLOAT;
Packit 4a16fb
			}
Packit 4a16fb
			if (p == args && cdr(p) != &alsa_lisp_nil)
Packit 4a16fb
				f = p1->value.f;
Packit 4a16fb
			else {
Packit 4a16fb
				f -= p1->value.f;
Packit 4a16fb
			}
Packit 4a16fb
		} else
Packit 4a16fb
			lisp_warn(instance, "difference with a non integer or float operand");
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		n = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
		return new_integer(instance, v);
Packit 4a16fb
	} else {
Packit 4a16fb
		return new_float(instance, f);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (* expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_mul(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * n;
Packit 4a16fb
	long v = 1;
Packit 4a16fb
	double f = 1;
Packit 4a16fb
	int type = ALISP_OBJ_INTEGER;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
			if (type == ALISP_OBJ_FLOAT)
Packit 4a16fb
				f *= p1->value.i;
Packit 4a16fb
			else
Packit 4a16fb
				v *= p1->value.i;
Packit 4a16fb
		} else if (alisp_compare_type(p1, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
			f *= p1->value.f * v; v = 1;
Packit 4a16fb
			type = ALISP_OBJ_FLOAT;
Packit 4a16fb
		} else {
Packit 4a16fb
			lisp_warn(instance, "product with a non integer or float operand");
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		n = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
		return new_integer(instance, v);
Packit 4a16fb
	} else {
Packit 4a16fb
		return new_float(instance, f);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (/ expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_div(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * n;
Packit 4a16fb
	long v = 0;
Packit 4a16fb
	double f = 0;
Packit 4a16fb
	int type = ALISP_OBJ_INTEGER;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
			if (p == args && cdr(p) != &alsa_lisp_nil) {
Packit 4a16fb
				v = p1->value.i;
Packit 4a16fb
			} else {
Packit 4a16fb
				if (p1->value.i == 0) {
Packit 4a16fb
					lisp_warn(instance, "division by zero");
Packit 4a16fb
					v = 0;
Packit 4a16fb
					f = 0;
Packit 4a16fb
					break;
Packit 4a16fb
				} else {
Packit 4a16fb
					if (type == ALISP_OBJ_FLOAT)
Packit 4a16fb
						f /= p1->value.i;
Packit 4a16fb
					else
Packit 4a16fb
						v /= p1->value.i;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
		} else if (alisp_compare_type(p1, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
			if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
				f = v;
Packit 4a16fb
				type = ALISP_OBJ_FLOAT;
Packit 4a16fb
			}
Packit 4a16fb
			if (p == args && cdr(p) != &alsa_lisp_nil) {
Packit 4a16fb
				f = p1->value.f;
Packit 4a16fb
			} else {
Packit 4a16fb
				if (p1->value.f == 0) {
Packit 4a16fb
					lisp_warn(instance, "division by zero");
Packit 4a16fb
					f = 0;
Packit 4a16fb
					break;
Packit 4a16fb
				} else {
Packit 4a16fb
					f /= p1->value.i;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
		} else
Packit 4a16fb
			lisp_warn(instance, "quotient with a non integer or float operand");
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		n = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	if (type == ALISP_OBJ_INTEGER) {
Packit 4a16fb
		return new_integer(instance, v);
Packit 4a16fb
	} else {
Packit 4a16fb
		return new_float(instance, f);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (% expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_mod(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * p3;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p2->value.i == 0) {
Packit 4a16fb
			lisp_warn(instance, "module by zero");
Packit 4a16fb
			p3 = new_integer(instance, 0);
Packit 4a16fb
		} else {
Packit 4a16fb
			p3 = new_integer(instance, p1->value.i % p2->value.i);
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) || 
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		f1 = fmod(f1, f2);
Packit 4a16fb
		if (f1 == EDOM) {
Packit 4a16fb
			lisp_warn(instance, "module by zero");
Packit 4a16fb
			p3 = new_float(instance, 0);
Packit 4a16fb
		} else {
Packit 4a16fb
			p3 = new_float(instance, f1);
Packit 4a16fb
		}
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "module with a non integer or float operand");
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return p3;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (< expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_lt(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p1->value.i < p2->value.i) {
Packit 4a16fb
		      __true:
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		if (f1 < f2)
Packit 4a16fb
			goto __true;
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "comparison with a non integer or float operand");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (> expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_gt(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p1->value.i > p2->value.i) {
Packit 4a16fb
		      __true:
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		if (f1 > f2)
Packit 4a16fb
			goto __true;
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "comparison with a non integer or float operand");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (<= expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_le(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p1->value.i <= p2->value.i) {
Packit 4a16fb
		      __true:
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		if (f1 <= f2)
Packit 4a16fb
			goto __true;
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "comparison with a non integer or float operand");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (>= expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_ge(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p1->value.i >= p2->value.i) {
Packit 4a16fb
		      __true:
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		if (f1 >= f2)
Packit 4a16fb
			goto __true;
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "comparison with a non integer or float operand");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (= expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_numeq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    alisp_compare_type(p2, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		if (p1->value.i == p2->value.i) {
Packit 4a16fb
		      __true:
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		}
Packit 4a16fb
	} else if ((alisp_compare_type(p1, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
	            alisp_compare_type(p1, ALISP_OBJ_FLOAT)) &&
Packit 4a16fb
		   (alisp_compare_type(p2, ALISP_OBJ_INTEGER) ||
Packit 4a16fb
		    alisp_compare_type(p2, ALISP_OBJ_FLOAT))) {
Packit 4a16fb
		double f1, f2;
Packit 4a16fb
		f1 = alisp_compare_type(p1, ALISP_OBJ_INTEGER) ? p1->value.i : p1->value.f;
Packit 4a16fb
		f2 = alisp_compare_type(p2, ALISP_OBJ_INTEGER) ? p2->value.i : p2->value.f;
Packit 4a16fb
		if (f1 == f2)
Packit 4a16fb
			goto __true;
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "comparison with a non integer or float operand");
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (!= expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_numneq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
	
Packit 4a16fb
	p = F_numeq(instance, args);
Packit 4a16fb
	if (p == &alsa_lisp_nil)
Packit 4a16fb
		return &alsa_lisp_t;
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (exfun name)
Packit 4a16fb
 * Test, if a function exists
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_exfun(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	p2 = get_object(instance, p1);
Packit 4a16fb
	if (p2 == &alsa_lisp_nil) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	p2 = car(p2);
Packit 4a16fb
	if (alisp_compare_type(p2, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !strcmp(p2->value.s, "lambda")) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		return &alsa_lisp_t;
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void princ_string(snd_output_t *out, char *s)
Packit 4a16fb
{
Packit 4a16fb
	char *p;
Packit 4a16fb
Packit 4a16fb
	snd_output_putc(out, '"');
Packit 4a16fb
	for (p = s; *p != '\0'; ++p)
Packit 4a16fb
		switch (*p) {
Packit 4a16fb
		case '\a': snd_output_putc(out, '\\'); snd_output_putc(out, 'a'); break;
Packit 4a16fb
		case '\b': snd_output_putc(out, '\\'); snd_output_putc(out, 'b'); break;
Packit 4a16fb
		case '\f': snd_output_putc(out, '\\'); snd_output_putc(out, 'f'); break;
Packit 4a16fb
		case '\n': snd_output_putc(out, '\\'); snd_output_putc(out, 'n'); break;
Packit 4a16fb
		case '\r': snd_output_putc(out, '\\'); snd_output_putc(out, 'r'); break;
Packit 4a16fb
		case '\t': snd_output_putc(out, '\\'); snd_output_putc(out, 't'); break;
Packit 4a16fb
		case '\v': snd_output_putc(out, '\\'); snd_output_putc(out, 'v'); break;
Packit 4a16fb
		case '"': snd_output_putc(out, '\\'); snd_output_putc(out, '"'); break;
Packit 4a16fb
		default: snd_output_putc(out, *p);
Packit 4a16fb
		}
Packit 4a16fb
	snd_output_putc(out, '"');
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void princ_cons(snd_output_t *out, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	do {
Packit 4a16fb
		princ_object(out, p->value.c.car);
Packit 4a16fb
		p = p->value.c.cdr;
Packit 4a16fb
		if (p != &alsa_lisp_nil) {
Packit 4a16fb
			snd_output_putc(out, ' ');
Packit 4a16fb
			if (!alisp_compare_type(p, ALISP_OBJ_CONS)) {
Packit 4a16fb
				snd_output_printf(out, ". ");
Packit 4a16fb
				princ_object(out, p);
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	} while (p != &alsa_lisp_nil && alisp_compare_type(p, ALISP_OBJ_CONS));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void princ_object(snd_output_t *out, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	switch (alisp_get_type(p)) {
Packit 4a16fb
	case ALISP_OBJ_NIL:
Packit 4a16fb
		snd_output_printf(out, "nil");
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_T:
Packit 4a16fb
		snd_output_putc(out, 't');
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_IDENTIFIER:
Packit 4a16fb
		snd_output_printf(out, "%s", p->value.s);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_STRING:
Packit 4a16fb
		princ_string(out, p->value.s);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_INTEGER:
Packit 4a16fb
		snd_output_printf(out, "%ld", p->value.i);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_FLOAT:
Packit 4a16fb
		snd_output_printf(out, "%f", p->value.f);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_POINTER:
Packit 4a16fb
		snd_output_printf(out, "<%p>", p->value.ptr);
Packit 4a16fb
		break;
Packit 4a16fb
	case ALISP_OBJ_CONS:
Packit 4a16fb
		snd_output_putc(out, '(');
Packit 4a16fb
		princ_cons(out, p);
Packit 4a16fb
		snd_output_putc(out, ')');
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (princ expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_princ(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1 = NULL, * n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (p1)
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_STRING))
Packit 4a16fb
			snd_output_printf(instance->out, "%s", p1->value.s);
Packit 4a16fb
		else
Packit 4a16fb
			princ_object(instance->out, p1);
Packit 4a16fb
		n = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (atom expr)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_atom(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	p = eval(instance, car(args));
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (p == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
Packit 4a16fb
	switch (alisp_get_type(p)) {
Packit 4a16fb
	case ALISP_OBJ_T:
Packit 4a16fb
	case ALISP_OBJ_NIL:
Packit 4a16fb
	case ALISP_OBJ_INTEGER:
Packit 4a16fb
	case ALISP_OBJ_FLOAT:
Packit 4a16fb
	case ALISP_OBJ_STRING:
Packit 4a16fb
	case ALISP_OBJ_IDENTIFIER:
Packit 4a16fb
	case ALISP_OBJ_POINTER:
Packit 4a16fb
		delete_tree(instance, p);
Packit 4a16fb
		return &alsa_lisp_t;
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (cons expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_cons(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p;
Packit 4a16fb
Packit 4a16fb
	p = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
	if (p) {
Packit 4a16fb
		p->value.c.car = eval(instance, car(args));
Packit 4a16fb
		p->value.c.cdr = eval(instance, car(cdr(args)));
Packit 4a16fb
		delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
		delete_object(instance, cdr(args));
Packit 4a16fb
		delete_object(instance, args);
Packit 4a16fb
	} else {
Packit 4a16fb
		delete_tree(instance, args);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (list expr1...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_list(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * first = NULL, * prev = NULL, * p1;
Packit 4a16fb
Packit 4a16fb
	if (p == &alsa_lisp_nil)
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
		if (p1 == NULL) {
Packit 4a16fb
			delete_tree(instance, p);
Packit 4a16fb
			delete_tree(instance, first);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		p1->value.c.car = eval(instance, car(p));
Packit 4a16fb
		if (p1->value.c.car == NULL) {
Packit 4a16fb
			delete_tree(instance, first);
Packit 4a16fb
			delete_tree(instance, cdr(p));
Packit 4a16fb
			delete_object(instance, p);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		if (first == NULL)
Packit 4a16fb
			first = p1;
Packit 4a16fb
		if (prev != NULL)
Packit 4a16fb
			prev->value.c.cdr = p1;
Packit 4a16fb
		prev = p1;
Packit 4a16fb
		p = cdr(p1 = p);
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return first;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline int eq(struct alisp_object * p1, struct alisp_object * p2)
Packit 4a16fb
{
Packit 4a16fb
	return p1 == p2;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int equal(struct alisp_object * p1, struct alisp_object * p2)
Packit 4a16fb
{
Packit 4a16fb
	int type1, type2;
Packit 4a16fb
Packit 4a16fb
	if (eq(p1, p2))
Packit 4a16fb
		return 1;
Packit 4a16fb
Packit 4a16fb
	type1 = alisp_get_type(p1);
Packit 4a16fb
	type2 = alisp_get_type(p2);
Packit 4a16fb
Packit 4a16fb
	if (type1 == ALISP_OBJ_CONS || type2 == ALISP_OBJ_CONS)
Packit 4a16fb
		return 0;
Packit 4a16fb
Packit 4a16fb
	if (type1 == type2) {
Packit 4a16fb
		switch (type1) {
Packit 4a16fb
		case ALISP_OBJ_STRING:
Packit 4a16fb
			return !strcmp(p1->value.s, p2->value.s);
Packit 4a16fb
		case ALISP_OBJ_INTEGER:
Packit 4a16fb
			return p1->value.i == p2->value.i;
Packit 4a16fb
		case ALISP_OBJ_FLOAT:
Packit 4a16fb
			return p1->value.i == p2->value.i;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (eq expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_eq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (eq(p1, p2)) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		return &alsa_lisp_t;
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (equal expr1 expr2)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_equal(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (equal(p1, p2)) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		return &alsa_lisp_t;
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (quote expr)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_quote(struct alisp_instance *instance ATTRIBUTE_UNUSED, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object *p = car(args);
Packit 4a16fb
	
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (and expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_and(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1 = NULL, * n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (p1)
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (p1 == &alsa_lisp_nil) {
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p));
Packit 4a16fb
			delete_object(instance, p);
Packit 4a16fb
			return &alsa_lisp_nil;
Packit 4a16fb
		}
Packit 4a16fb
		p = cdr(n = p);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (or expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_or(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1 = NULL, * n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (p1)
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (p1 != &alsa_lisp_nil) {
Packit 4a16fb
			delete_tree(instance, cdr(p));
Packit 4a16fb
			delete_object(instance, p);
Packit 4a16fb
			return p1;
Packit 4a16fb
		}
Packit 4a16fb
		p = cdr(n = p);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (not expr)
Packit 4a16fb
 * Syntax: (null expr)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_not(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = eval(instance, car(args));
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (p != &alsa_lisp_nil) {
Packit 4a16fb
		delete_tree(instance, p);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p);
Packit 4a16fb
	return &alsa_lisp_t;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (cond (expr1 [expr2])...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_cond(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * p2, * p3;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = car(p);
Packit 4a16fb
		if ((p2 = eval(instance, car(p1))) != &alsa_lisp_nil) {
Packit 4a16fb
			p3 = cdr(p1);
Packit 4a16fb
			delete_object(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p));
Packit 4a16fb
			delete_object(instance, p);
Packit 4a16fb
			if (p3 != &alsa_lisp_nil) {
Packit 4a16fb
				delete_tree(instance, p2);
Packit 4a16fb
				return F_progn(instance, p3);
Packit 4a16fb
			} else {
Packit 4a16fb
				delete_tree(instance, p3);
Packit 4a16fb
				return p2;
Packit 4a16fb
			}
Packit 4a16fb
		} else {
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			delete_tree(instance, cdr(p1));
Packit 4a16fb
			delete_object(instance, p1);
Packit 4a16fb
		}
Packit 4a16fb
		p = cdr(p2 = p);
Packit 4a16fb
		delete_object(instance, p2);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (if expr then-expr else-expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_if(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * p3;
Packit 4a16fb
Packit 4a16fb
	p1 = car(args);
Packit 4a16fb
	p2 = car(cdr(args));
Packit 4a16fb
	p3 = cdr(cdr(args));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, p1);
Packit 4a16fb
	if (p1 != &alsa_lisp_nil) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p3);
Packit 4a16fb
		return eval(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return F_progn(instance, p3);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (when expr then-expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_when(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = car(args);
Packit 4a16fb
	p2 = cdr(args);
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if ((p1 = eval(instance, p1)) != &alsa_lisp_nil) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		return F_progn(instance, p2);
Packit 4a16fb
	} else {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (unless expr else-expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_unless(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2;
Packit 4a16fb
Packit 4a16fb
	p1 = car(args);
Packit 4a16fb
	p2 = cdr(args);
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if ((p1 = eval(instance, p1)) == &alsa_lisp_nil) {
Packit 4a16fb
		return F_progn(instance, p2);
Packit 4a16fb
	} else {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (while expr exprs...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_while(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * p3;
Packit 4a16fb
Packit 4a16fb
	p1 = car(args);
Packit 4a16fb
	p2 = cdr(args);
Packit 4a16fb
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	while (1) {
Packit 4a16fb
		incref_tree(instance, p1);
Packit 4a16fb
		if ((p3 = eval(instance, p1)) == &alsa_lisp_nil)
Packit 4a16fb
			break;
Packit 4a16fb
		delete_tree(instance, p3);
Packit 4a16fb
		incref_tree(instance, p2);
Packit 4a16fb
		delete_tree(instance, F_progn(instance, p2));
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	delete_tree(instance, p2);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (progn expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_progn(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1 = NULL, * n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (p1)
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		n = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (prog1 expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_prog1(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * first = NULL, * p1;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (first == NULL)
Packit 4a16fb
			first = p1;
Packit 4a16fb
		else
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = p1;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	if (first == NULL)
Packit 4a16fb
		first = &alsa_lisp_nil;
Packit 4a16fb
Packit 4a16fb
	return first;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (prog2 expr...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_prog2(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * second = NULL, * p1;
Packit 4a16fb
	int i = 0;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		++i;
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (i == 2)
Packit 4a16fb
			second = p1;
Packit 4a16fb
		else
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		p = p1;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	if (second == NULL)
Packit 4a16fb
		second = &alsa_lisp_nil;
Packit 4a16fb
Packit 4a16fb
	return second;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (set name value)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_set(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1 = eval(instance, car(args)),
Packit 4a16fb
			    * p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (!check_set_object(instance, p1)) {
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		p2 = &alsa_lisp_nil;
Packit 4a16fb
	} else {
Packit 4a16fb
		if (set_object(instance, p1, p2) == NULL) {
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return incref_tree(instance, p2);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (unset name)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_unset(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1 = eval(instance, car(args));
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, unset_object(instance, p1));
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (setq name value...)
Packit 4a16fb
 * Syntax: (setf name value...)
Packit 4a16fb
 * `name' is not evalled
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_setq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1, * p2 = NULL, *n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = car(p);
Packit 4a16fb
		p2 = eval(instance, car(cdr(p)));
Packit 4a16fb
		n = cdr(cdr(p));
Packit 4a16fb
		delete_object(instance, cdr(p));
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		if (!check_set_object(instance, p1)) {
Packit 4a16fb
			delete_tree(instance, p2);
Packit 4a16fb
			p2 = &alsa_lisp_nil;
Packit 4a16fb
		} else {
Packit 4a16fb
			if (set_object(instance, p1, p2) == NULL) {
Packit 4a16fb
				delete_tree(instance, p1);
Packit 4a16fb
				delete_tree(instance, p2);
Packit 4a16fb
				return NULL;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		p = n;
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return incref_tree(instance, p2);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (unsetq name...)
Packit 4a16fb
 * Syntax: (unsetf name...)
Packit 4a16fb
 * `name' is not evalled
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_unsetq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1 = NULL, * n;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (p1)
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
		p1 = unset_object(instance, car(p));
Packit 4a16fb
		delete_tree(instance, car(p));
Packit 4a16fb
		p = cdr(n = p);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (defun name arglist expr...)
Packit 4a16fb
 * `name' is not evalled
Packit 4a16fb
 * `arglist' is not evalled
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_defun(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1 = car(args),
Packit 4a16fb
			    * p2 = car(cdr(args)),
Packit 4a16fb
			    * p3 = cdr(cdr(args));
Packit 4a16fb
	struct alisp_object * lexpr;
Packit 4a16fb
Packit 4a16fb
	lexpr = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
	if (lexpr) {
Packit 4a16fb
		lexpr->value.c.car = new_identifier(instance, "lambda");
Packit 4a16fb
		if (lexpr->value.c.car == NULL) {
Packit 4a16fb
			delete_object(instance, lexpr);
Packit 4a16fb
			delete_tree(instance, args);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		if ((lexpr->value.c.cdr = new_object(instance, ALISP_OBJ_CONS)) == NULL) {
Packit 4a16fb
			delete_object(instance, lexpr->value.c.car);
Packit 4a16fb
			delete_object(instance, lexpr);
Packit 4a16fb
			delete_tree(instance, args);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		lexpr->value.c.cdr->value.c.car = p2;
Packit 4a16fb
		lexpr->value.c.cdr->value.c.cdr = p3;
Packit 4a16fb
		delete_object(instance, cdr(args));
Packit 4a16fb
		delete_object(instance, args);
Packit 4a16fb
		if (set_object(instance, p1, lexpr) == NULL) {
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, lexpr);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
	} else {
Packit 4a16fb
		delete_tree(instance, args);
Packit 4a16fb
	}
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * eval_func(struct alisp_instance *instance, struct alisp_object * p, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * p3, * p4;
Packit 4a16fb
	struct alisp_object ** eval_objs, ** save_objs;
Packit 4a16fb
	int i;
Packit 4a16fb
Packit 4a16fb
	p1 = car(p);
Packit 4a16fb
	if (alisp_compare_type(p1, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !strcmp(p1->value.s, "lambda")) {
Packit 4a16fb
		p2 = car(cdr(p));
Packit 4a16fb
		p3 = args;
Packit 4a16fb
Packit 4a16fb
		if ((i = count_list(p2)) != count_list(p3)) {
Packit 4a16fb
			lisp_warn(instance, "wrong number of parameters");
Packit 4a16fb
			goto _delete;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		eval_objs = malloc(2 * i * sizeof(struct alisp_object *));
Packit 4a16fb
		if (eval_objs == NULL) {
Packit 4a16fb
			nomem();
Packit 4a16fb
			goto _delete;
Packit 4a16fb
		}
Packit 4a16fb
		save_objs = eval_objs + i;
Packit 4a16fb
		
Packit 4a16fb
		/*
Packit 4a16fb
		 * Save the new variable values.
Packit 4a16fb
		 */
Packit 4a16fb
		i = 0;
Packit 4a16fb
		while (p3 != &alsa_lisp_nil) {
Packit 4a16fb
			eval_objs[i++] = eval(instance, car(p3));
Packit 4a16fb
			p3 = cdr(p4 = p3);
Packit 4a16fb
			delete_object(instance, p4);
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		/*
Packit 4a16fb
		 * Save the old variable values and set the new ones.
Packit 4a16fb
		 */
Packit 4a16fb
		i = 0;
Packit 4a16fb
		while (p2 != &alsa_lisp_nil) {
Packit 4a16fb
			p3 = car(p2);
Packit 4a16fb
			save_objs[i] = replace_object(instance, p3, eval_objs[i]);
Packit 4a16fb
			if (save_objs[i] == NULL &&
Packit 4a16fb
			    set_object_direct(instance, p3, eval_objs[i]) == NULL) {
Packit 4a16fb
			    	p4 = NULL;
Packit 4a16fb
				goto _end;
Packit 4a16fb
			}
Packit 4a16fb
			p2 = cdr(p2);
Packit 4a16fb
			++i;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		p4 = F_progn(instance, cdr(incref_tree(instance, p3 = cdr(p))));
Packit 4a16fb
Packit 4a16fb
		/*
Packit 4a16fb
		 * Restore the old variable values.
Packit 4a16fb
		 */
Packit 4a16fb
		p2 = car(p3);
Packit 4a16fb
		delete_object(instance, p3);
Packit 4a16fb
		i = 0;
Packit 4a16fb
		while (p2 != &alsa_lisp_nil) {
Packit 4a16fb
			p3 = car(p2);
Packit 4a16fb
			if (save_objs[i] == NULL) {
Packit 4a16fb
				p3 = unset_object(instance, p3);
Packit 4a16fb
			} else {
Packit 4a16fb
				p3 = replace_object(instance, p3, save_objs[i]);
Packit 4a16fb
			}
Packit 4a16fb
			i++;
Packit 4a16fb
			delete_tree(instance, p3);
Packit 4a16fb
			delete_tree(instance, car(p2));
Packit 4a16fb
			p2 = cdr(p3 = p2);
Packit 4a16fb
			delete_object(instance, p3);
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
               _end:
Packit 4a16fb
		free(eval_objs);
Packit 4a16fb
Packit 4a16fb
		return p4;
Packit 4a16fb
	} else {
Packit 4a16fb
	       _delete:
Packit 4a16fb
		delete_tree(instance, args);
Packit 4a16fb
	}
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
struct alisp_object * F_gc(struct alisp_instance *instance ATTRIBUTE_UNUSED, struct alisp_object * args ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	/* improved: no more traditional gc */
Packit 4a16fb
	return &alsa_lisp_t;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (path what)
Packit 4a16fb
 * what is string ('data')
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_path(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (!alisp_compare_type(p1, ALISP_OBJ_STRING)) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	if (!strcmp(p1->value.s, "data")) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		return new_string(instance, snd_config_topdir());
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (include filename...)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_include(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = args, * p1;
Packit 4a16fb
	int res = -ENOENT;
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		p1 = eval(instance, car(p));
Packit 4a16fb
		if (alisp_compare_type(p1, ALISP_OBJ_STRING))
Packit 4a16fb
			res = alisp_include_file(instance, p1->value.s);
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		p = cdr(p1 = p);
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
	} while (p != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	return new_integer(instance, res);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (string-to-integer value)
Packit 4a16fb
 * 'value' can be integer or float type
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_string_to_integer(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = eval(instance, car(args)), * p1;
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_INTEGER))
Packit 4a16fb
		return p;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
		p1 = new_integer(instance, floor(p->value.f));
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "expected an integer or float for integer conversion");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p);
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (string-to-float value)
Packit 4a16fb
 * 'value' can be integer or float type
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_string_to_float(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = eval(instance, car(args)), * p1;
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_FLOAT))
Packit 4a16fb
		return p;
Packit 4a16fb
	if (alisp_compare_type(p, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		p1 = new_float(instance, p->value.i);
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "expected an integer or float for integer conversion");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p);
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int append_to_string(char **s, int *len, char *from, int size)
Packit 4a16fb
{
Packit 4a16fb
	if (*len == 0) {
Packit 4a16fb
		*s = malloc(*len = size + 1);
Packit 4a16fb
		if (*s == NULL) {
Packit 4a16fb
			nomem();
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		}
Packit 4a16fb
		memcpy(*s, from, size);
Packit 4a16fb
	} else {
Packit 4a16fb
		*len += size;
Packit 4a16fb
		*s = realloc(*s, *len);
Packit 4a16fb
		if (*s == NULL) {
Packit 4a16fb
			nomem();
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		}
Packit 4a16fb
		memcpy(*s + strlen(*s), from, size);
Packit 4a16fb
	}
Packit 4a16fb
	(*s)[*len - 1] = '\0';
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int format_parse_char(struct alisp_instance *instance, char **s, int *len, struct alisp_object *p)
Packit 4a16fb
{
Packit 4a16fb
	char b;
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		lisp_warn(instance, "format: expected integer\n");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	b = p->value.i;
Packit 4a16fb
	return append_to_string(s, len, &b, 1);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int format_parse_integer(struct alisp_instance *instance, char **s, int *len, struct alisp_object *p)
Packit 4a16fb
{
Packit 4a16fb
	int res;
Packit 4a16fb
	char *s1;
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    !alisp_compare_type(p, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
		lisp_warn(instance, "format: expected integer or float\n");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	s1 = malloc(64);
Packit 4a16fb
	if (s1 == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	sprintf(s1, "%li", alisp_compare_type(p, ALISP_OBJ_FLOAT) ? (long)floor(p->value.f) : p->value.i);
Packit 4a16fb
	res = append_to_string(s, len, s1, strlen(s1));
Packit 4a16fb
	free(s1);
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int format_parse_float(struct alisp_instance *instance, char **s, int *len, struct alisp_object *p)
Packit 4a16fb
{
Packit 4a16fb
	int res;
Packit 4a16fb
	char *s1;
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_INTEGER) &&
Packit 4a16fb
	    !alisp_compare_type(p, ALISP_OBJ_FLOAT)) {
Packit 4a16fb
		lisp_warn(instance, "format: expected integer or float\n");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	s1 = malloc(64);
Packit 4a16fb
	if (s1 == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	sprintf(s1, "%f", alisp_compare_type(p, ALISP_OBJ_FLOAT) ? p->value.f : (double)p->value.i);
Packit 4a16fb
	res = append_to_string(s, len, s1, strlen(s1));
Packit 4a16fb
	free(s1);
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int format_parse_string(struct alisp_instance *instance, char **s, int *len, struct alisp_object *p)
Packit 4a16fb
{
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_STRING)) {
Packit 4a16fb
		lisp_warn(instance, "format: expected string\n");
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	return append_to_string(s, len, p->value.s, strlen(p->value.s));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (format format value...)
Packit 4a16fb
 * 'format' is C-like format string
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_format(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = eval(instance, car(args)), * p1 = cdr(args), * n;
Packit 4a16fb
	char *s, *s1, *s2;
Packit 4a16fb
	int len;
Packit 4a16fb
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_STRING)) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p);
Packit 4a16fb
		lisp_warn(instance, "format: expected an format string");
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	s = p->value.s;
Packit 4a16fb
	s1 = NULL;
Packit 4a16fb
	len = 0;
Packit 4a16fb
	n = eval(instance, car(p1));
Packit 4a16fb
	do {
Packit 4a16fb
		while (1) {
Packit 4a16fb
			s2 = s;
Packit 4a16fb
			while (*s2 && *s2 != '%')
Packit 4a16fb
				s2++;
Packit 4a16fb
			if (s2 != s) {
Packit 4a16fb
				if (append_to_string(&s1, &len, s, s2 - s) < 0) {
Packit 4a16fb
				      __error:
Packit 4a16fb
					delete_tree(instance, n);
Packit 4a16fb
					delete_tree(instance, cdr(p1));
Packit 4a16fb
					delete_object(instance, p1);
Packit 4a16fb
					delete_tree(instance, p);
Packit 4a16fb
					return NULL;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
			if (*s2 == '%')
Packit 4a16fb
				s2++;
Packit 4a16fb
			switch (*s2) {
Packit 4a16fb
			case '%':
Packit 4a16fb
				if (append_to_string(&s1, &len, s2, 1) < 0)
Packit 4a16fb
					goto __error;
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				break;
Packit 4a16fb
			case 'c':
Packit 4a16fb
				if (format_parse_char(instance, &s1, &len, n) < 0)
Packit 4a16fb
					goto __error;
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				goto __next;
Packit 4a16fb
			case 'd':
Packit 4a16fb
			case 'i':
Packit 4a16fb
				if (format_parse_integer(instance, &s1, &len, n) < 0)
Packit 4a16fb
					goto __error;
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				goto __next;
Packit 4a16fb
			case 'f':
Packit 4a16fb
				if (format_parse_float(instance, &s1, &len, n) < 0)
Packit 4a16fb
					goto __error;
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				goto __next;
Packit 4a16fb
			case 's':
Packit 4a16fb
				if (format_parse_string(instance, &s1, &len, n) < 0)
Packit 4a16fb
					goto __error;
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				goto __next;
Packit 4a16fb
			case '\0':
Packit 4a16fb
				goto __end;
Packit 4a16fb
			default:
Packit 4a16fb
				lisp_warn(instance, "unknown format char '%c'", *s2);
Packit 4a16fb
				s = s2 + 1;
Packit 4a16fb
				goto __next;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	      __next:
Packit 4a16fb
		delete_tree(instance, n);
Packit 4a16fb
		p1 = cdr(n = p1);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
		n = eval(instance, car(p1));
Packit 4a16fb
	} while (*s);
Packit 4a16fb
      __end:
Packit 4a16fb
	delete_tree(instance, n);
Packit 4a16fb
	delete_tree(instance, cdr(p1));
Packit 4a16fb
	delete_object(instance, p1);
Packit 4a16fb
	delete_tree(instance, p);
Packit 4a16fb
	if (len > 0) {
Packit 4a16fb
		p1 = new_string(instance, s1);
Packit 4a16fb
		free(s1);
Packit 4a16fb
	} else {
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	return p1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (compare-strings str1 start1 end1 str2 start2 end2 /opt-case-insensitive)
Packit 4a16fb
 * 'str1' is first compared string
Packit 4a16fb
 * 'start1' is first char (0..)
Packit 4a16fb
 * 'end1' is last char (0..)
Packit 4a16fb
 * 'str2' is second compared string
Packit 4a16fb
 * 'start2' is first char (0..)
Packit 4a16fb
 * 'end2' is last char (0..)
Packit 4a16fb
 * /opt-case-insensitive true - case insensitive match
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_compare_strings(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1 = args, * n, * p[7];
Packit 4a16fb
	char *s1, *s2;
Packit 4a16fb
	int start1, end1, start2, end2;
Packit 4a16fb
	
Packit 4a16fb
	for (start1 = 0; start1 < 7; start1++) {
Packit 4a16fb
		p[start1] = eval(instance, car(p1));
Packit 4a16fb
		p1 = cdr(n = p1);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	}
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	if (alisp_compare_type(p[0], ALISP_OBJ_STRING)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: first argument must be string\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (alisp_compare_type(p[1], ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: second argument must be integer\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (alisp_compare_type(p[2], ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: third argument must be integer\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (alisp_compare_type(p[3], ALISP_OBJ_STRING)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: fifth argument must be string\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (!alisp_compare_type(p[4], ALISP_OBJ_NIL) &&
Packit 4a16fb
	    !alisp_compare_type(p[4], ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: fourth argument must be integer\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (!alisp_compare_type(p[5], ALISP_OBJ_NIL) &&
Packit 4a16fb
	    !alisp_compare_type(p[5], ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		lisp_warn(instance, "compare-strings: sixth argument must be integer\n");
Packit 4a16fb
		p1 = &alsa_lisp_nil;
Packit 4a16fb
		goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	s1 = p[0]->value.s;
Packit 4a16fb
	start1 = p[1]->value.i;
Packit 4a16fb
	end1 = p[2]->value.i;
Packit 4a16fb
	s2 = p[3]->value.s;
Packit 4a16fb
	start2 = alisp_compare_type(p[4], ALISP_OBJ_NIL) ? 0 : p[4]->value.i;
Packit 4a16fb
	end2 = alisp_compare_type(p[5], ALISP_OBJ_NIL) ? start2 + (end1 - start1) : p[5]->value.i;
Packit 4a16fb
	if (start1 < 0 || start2 < 0 || end1 < 0 || end2 < 0 ||
Packit 4a16fb
	    start1 >= (int)strlen(s1) || start2 >= (int)strlen(s2) ||
Packit 4a16fb
	    (end1 - start1) != (end2 - start2)) {
Packit 4a16fb
	    	p1 = &alsa_lisp_nil;
Packit 4a16fb
	    	goto __err;
Packit 4a16fb
	}
Packit 4a16fb
	if (p[6] != &alsa_lisp_nil) {
Packit 4a16fb
		while (start1 < end1) {
Packit 4a16fb
			if (s1[start1] == '\0' ||
Packit 4a16fb
			    s2[start2] == '\0' ||
Packit 4a16fb
			    tolower(s1[start1]) != tolower(s2[start2])) {
Packit 4a16fb
				p1 = &alsa_lisp_nil;
Packit 4a16fb
				goto __err;
Packit 4a16fb
			}
Packit 4a16fb
			start1++;
Packit 4a16fb
			start2++;
Packit 4a16fb
		}
Packit 4a16fb
	} else {
Packit 4a16fb
		while (start1 < end1) {
Packit 4a16fb
			if (s1[start1] == '\0' ||
Packit 4a16fb
			    s2[start2] == '\0' ||
Packit 4a16fb
			    s1[start1] != s2[start2]) {
Packit 4a16fb
				p1 = &alsa_lisp_nil;
Packit 4a16fb
				goto __err;
Packit 4a16fb
			}
Packit 4a16fb
			start1++;
Packit 4a16fb
			start2++;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	p1 = &alsa_lisp_t;
Packit 4a16fb
	
Packit 4a16fb
      __err:
Packit 4a16fb
      	for (start1 = 0; start1 < 7; start1++)
Packit 4a16fb
      		delete_tree(instance, p[start1]);
Packit 4a16fb
      	return p1;	
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  Syntax: (assoc key alist)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_assoc(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * n;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (eq(p1, car(car(p2)))) {
Packit 4a16fb
			n = car(p2);
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p2));
Packit 4a16fb
			delete_object(instance, p2);
Packit 4a16fb
			return n;
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, car(p2));
Packit 4a16fb
		p2 = cdr(n = p2);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p2 != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;	
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  Syntax: (rassoc value alist)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_rassoc(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, *p2, * n;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (eq(p1, cdr(car(p2)))) {
Packit 4a16fb
			n = car(p2);
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p2));
Packit 4a16fb
			delete_object(instance, p2);
Packit 4a16fb
			return n;
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, car(p2));
Packit 4a16fb
		p2 = cdr(n = p2);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p2 != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;	
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  Syntax: (assq key alist)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_assq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * n;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (equal(p1, car(car(p2)))) {
Packit 4a16fb
			n = car(p2);
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p2));
Packit 4a16fb
			delete_object(instance, p2);
Packit 4a16fb
			return n;
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, car(p2));
Packit 4a16fb
		p2 = cdr(n = p2);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p2 != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;	
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  Syntax: (nth index alist)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_nth(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * n;
Packit 4a16fb
	long idx;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(p1, ALISP_OBJ_INTEGER)) {
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	if (!alisp_compare_type(p2, ALISP_OBJ_CONS)) {
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	idx = p1->value.i;
Packit 4a16fb
	delete_object(instance, p1);
Packit 4a16fb
	while (idx-- > 0) {
Packit 4a16fb
		delete_tree(instance, car(p2));
Packit 4a16fb
		p2 = cdr(n = p2);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	}
Packit 4a16fb
	n = car(p2);
Packit 4a16fb
	delete_tree(instance, cdr(p2));
Packit 4a16fb
	delete_object(instance, p2);
Packit 4a16fb
	return n;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  Syntax: (rassq value alist)
Packit 4a16fb
 */
Packit 4a16fb
struct alisp_object * F_rassq(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1, * p2, * n;
Packit 4a16fb
Packit 4a16fb
	p1 = eval(instance, car(args));
Packit 4a16fb
	p2 = eval(instance, car(cdr(args)));
Packit 4a16fb
	delete_tree(instance, cdr(cdr(args)));
Packit 4a16fb
	delete_object(instance, cdr(args));
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
Packit 4a16fb
	do {
Packit 4a16fb
		if (equal(p1, cdr(car(p2)))) {
Packit 4a16fb
			n = car(p2);
Packit 4a16fb
			delete_tree(instance, p1);
Packit 4a16fb
			delete_tree(instance, cdr(p2));
Packit 4a16fb
			delete_object(instance, p2);
Packit 4a16fb
			return n;
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, car(p2));
Packit 4a16fb
		p2 = cdr(n = p2);
Packit 4a16fb
		delete_object(instance, n);
Packit 4a16fb
	} while (p2 != &alsa_lisp_nil);
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, p1);
Packit 4a16fb
	return &alsa_lisp_nil;	
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * F_dump_memory(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = car(args);
Packit 4a16fb
Packit 4a16fb
	if (p != &alsa_lisp_nil && cdr(args) == &alsa_lisp_nil &&
Packit 4a16fb
	    alisp_compare_type(p, ALISP_OBJ_STRING)) {
Packit 4a16fb
		if (strlen(p->value.s) > 0) {
Packit 4a16fb
			dump_objects(instance, p->value.s);
Packit 4a16fb
			delete_tree(instance, args);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		} else
Packit 4a16fb
			lisp_warn(instance, "expected filename");
Packit 4a16fb
	} else
Packit 4a16fb
		lisp_warn(instance, "wrong number of parameters (expected string)");
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, args);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * F_stat_memory(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_printf(instance->out, "*** Memory stats\n");
Packit 4a16fb
	snd_output_printf(instance->out, "  used_objs = %li, free_objs = %li, max_objs = %li, obj_size = %i (total bytes = %li, max bytes = %li)\n",
Packit 4a16fb
			  instance->used_objs,
Packit 4a16fb
			  instance->free_objs,
Packit 4a16fb
			  instance->max_objs,
Packit 4a16fb
			  (int)sizeof(struct alisp_object),
Packit 4a16fb
			  (long)((instance->used_objs + instance->free_objs) * sizeof(struct alisp_object)),
Packit 4a16fb
			  (long)(instance->max_objs * sizeof(struct alisp_object)));
Packit 4a16fb
	delete_tree(instance, args);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * F_check_memory(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	delete_tree(instance, args);
Packit 4a16fb
	if (instance->used_objs > 0) {
Packit 4a16fb
		fprintf(stderr, "!!!alsa lisp - check memory failed!!!\n");
Packit 4a16fb
		F_stat_memory(instance, &alsa_lisp_nil);
Packit 4a16fb
		exit(EXIT_FAILURE);
Packit 4a16fb
	}
Packit 4a16fb
	return &alsa_lisp_t;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * F_dump_objects(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = car(args);
Packit 4a16fb
Packit 4a16fb
	if (p != &alsa_lisp_nil && cdr(args) == &alsa_lisp_nil &&
Packit 4a16fb
	    alisp_compare_type(p, ALISP_OBJ_STRING)) {
Packit 4a16fb
		if (strlen(p->value.s) > 0) {
Packit 4a16fb
			dump_obj_lists(instance, p->value.s);
Packit 4a16fb
			delete_tree(instance, args);
Packit 4a16fb
			return &alsa_lisp_t;
Packit 4a16fb
		} else
Packit 4a16fb
			lisp_warn(instance, "expected filename");
Packit 4a16fb
	} else
Packit 4a16fb
		lisp_warn(instance, "wrong number of parameters (expected string)");
Packit 4a16fb
Packit 4a16fb
	delete_tree(instance, args);
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
struct intrinsic {
Packit 4a16fb
	const char *name;
Packit 4a16fb
	struct alisp_object * (*func)(struct alisp_instance *instance, struct alisp_object * args);
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static const struct intrinsic intrinsics[] = {
Packit 4a16fb
	{ "!=", F_numneq },
Packit 4a16fb
	{ "%", F_mod },
Packit 4a16fb
	{ "&check-memory", F_check_memory },
Packit 4a16fb
	{ "&dump-memory", F_dump_memory },
Packit 4a16fb
	{ "&dump-objects", F_dump_objects },
Packit 4a16fb
	{ "&stat-memory", F_stat_memory },
Packit 4a16fb
	{ "*", F_mul },
Packit 4a16fb
	{ "+", F_add },
Packit 4a16fb
	{ "-", F_sub },
Packit 4a16fb
	{ "/", F_div },
Packit 4a16fb
	{ "<", F_lt },
Packit 4a16fb
	{ "<=", F_le },
Packit 4a16fb
	{ "=", F_numeq },
Packit 4a16fb
	{ ">", F_gt },
Packit 4a16fb
	{ ">=", F_ge },
Packit 4a16fb
	{ "and", F_and },
Packit 4a16fb
	{ "assoc", F_assoc },
Packit 4a16fb
	{ "assq", F_assq },
Packit 4a16fb
	{ "atom", F_atom },
Packit 4a16fb
	{ "car", F_car },
Packit 4a16fb
	{ "cdr", F_cdr },
Packit 4a16fb
	{ "compare-strings", F_compare_strings },
Packit 4a16fb
	{ "concat", F_concat },
Packit 4a16fb
	{ "cond", F_cond },
Packit 4a16fb
	{ "cons", F_cons },
Packit 4a16fb
	{ "defun", F_defun },
Packit 4a16fb
	{ "eq", F_eq },
Packit 4a16fb
	{ "equal", F_equal },
Packit 4a16fb
	{ "eval", F_eval },
Packit 4a16fb
	{ "exfun", F_exfun },
Packit 4a16fb
	{ "format", F_format },
Packit 4a16fb
	{ "funcall", F_funcall },
Packit 4a16fb
	{ "garbage-collect", F_gc },
Packit 4a16fb
	{ "gc", F_gc },
Packit 4a16fb
	{ "if", F_if },
Packit 4a16fb
	{ "include", F_include },
Packit 4a16fb
	{ "list", F_list },
Packit 4a16fb
	{ "not", F_not },
Packit 4a16fb
	{ "nth", F_nth },
Packit 4a16fb
	{ "null", F_not },
Packit 4a16fb
	{ "or", F_or },
Packit 4a16fb
	{ "path", F_path },
Packit 4a16fb
	{ "princ", F_princ },
Packit 4a16fb
	{ "prog1", F_prog1 },
Packit 4a16fb
	{ "prog2", F_prog2 },
Packit 4a16fb
	{ "progn", F_progn },
Packit 4a16fb
	{ "quote", F_quote },
Packit 4a16fb
	{ "rassoc", F_rassoc },
Packit 4a16fb
	{ "rassq", F_rassq },
Packit 4a16fb
	{ "set", F_set },
Packit 4a16fb
	{ "setf", F_setq },
Packit 4a16fb
	{ "setq", F_setq },
Packit 4a16fb
	{ "string-equal", F_equal },
Packit 4a16fb
	{ "string-to-float", F_string_to_float },
Packit 4a16fb
	{ "string-to-integer", F_string_to_integer },
Packit 4a16fb
	{ "string-to-number", F_string_to_float },
Packit 4a16fb
	{ "string=", F_equal },
Packit 4a16fb
	{ "unless", F_unless },
Packit 4a16fb
	{ "unset", F_unset },
Packit 4a16fb
	{ "unsetf", F_unsetq },
Packit 4a16fb
	{ "unsetq", F_unsetq },
Packit 4a16fb
	{ "when", F_when },
Packit 4a16fb
	{ "while", F_while },
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
#include "alisp_snd.c"
Packit 4a16fb
Packit 4a16fb
static int compar(const void *p1, const void *p2)
Packit 4a16fb
{
Packit 4a16fb
	return strcmp(((struct intrinsic *)p1)->name,
Packit 4a16fb
		      ((struct intrinsic *)p2)->name);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline struct alisp_object * eval_cons1(struct alisp_instance *instance, struct alisp_object * p1, struct alisp_object * p2)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p3;
Packit 4a16fb
	struct intrinsic key, *item;
Packit 4a16fb
Packit 4a16fb
	key.name = p1->value.s;
Packit 4a16fb
Packit 4a16fb
	if ((item = bsearch(&key, intrinsics,
Packit 4a16fb
			    sizeof intrinsics / sizeof intrinsics[0],
Packit 4a16fb
			    sizeof intrinsics[0], compar)) != NULL) {
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
		return item->func(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if ((item = bsearch(&key, snd_intrinsics,
Packit 4a16fb
			    sizeof snd_intrinsics / sizeof snd_intrinsics[0],
Packit 4a16fb
			    sizeof snd_intrinsics[0], compar)) != NULL) {
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
		return item->func(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if ((p3 = get_object(instance, p1)) != &alsa_lisp_nil) {
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
		return eval_func(instance, p3, p2);
Packit 4a16fb
	} else {
Packit 4a16fb
		lisp_warn(instance, "function `%s' is undefined", p1->value.s);
Packit 4a16fb
		delete_object(instance, p1);
Packit 4a16fb
		delete_tree(instance, p2);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Syntax: (funcall function args...)
Packit 4a16fb
 */
Packit 4a16fb
static struct alisp_object * F_funcall(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p = eval(instance, car(args)), * p1;
Packit 4a16fb
Packit 4a16fb
	if (!alisp_compare_type(p, ALISP_OBJ_IDENTIFIER) &&
Packit 4a16fb
	    !alisp_compare_type(p, ALISP_OBJ_STRING)) {
Packit 4a16fb
		lisp_warn(instance, "expected an function name");
Packit 4a16fb
		delete_tree(instance, p);
Packit 4a16fb
		delete_tree(instance, cdr(args));
Packit 4a16fb
		delete_object(instance, args);
Packit 4a16fb
		return &alsa_lisp_nil;
Packit 4a16fb
	}
Packit 4a16fb
	p1 = cdr(args);
Packit 4a16fb
	delete_object(instance, args);
Packit 4a16fb
	return eval_cons1(instance, p, p1);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline struct alisp_object * eval_cons(struct alisp_instance *instance, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_object * p1 = car(p), * p2;
Packit 4a16fb
Packit 4a16fb
	if (p1 != &alsa_lisp_nil && alisp_compare_type(p1, ALISP_OBJ_IDENTIFIER)) {
Packit 4a16fb
		if (!strcmp(p1->value.s, "lambda"))
Packit 4a16fb
			return p;
Packit 4a16fb
Packit 4a16fb
		p2 = cdr(p);
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		return eval_cons1(instance, p1, p2);
Packit 4a16fb
	} else {
Packit 4a16fb
		delete_tree(instance, p);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return &alsa_lisp_nil;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * eval(struct alisp_instance *instance, struct alisp_object * p)
Packit 4a16fb
{
Packit 4a16fb
	switch (alisp_get_type(p)) {
Packit 4a16fb
	case ALISP_OBJ_IDENTIFIER: {
Packit 4a16fb
		struct alisp_object *r = incref_tree(instance, get_object(instance, p));
Packit 4a16fb
		delete_object(instance, p);
Packit 4a16fb
		return r;
Packit 4a16fb
	}
Packit 4a16fb
	case ALISP_OBJ_INTEGER:
Packit 4a16fb
	case ALISP_OBJ_FLOAT:
Packit 4a16fb
	case ALISP_OBJ_STRING:
Packit 4a16fb
	case ALISP_OBJ_POINTER:
Packit 4a16fb
		return p;
Packit 4a16fb
	case ALISP_OBJ_CONS:
Packit 4a16fb
		return eval_cons(instance, p);
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return p;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct alisp_object * F_eval(struct alisp_instance *instance, struct alisp_object * args)
Packit 4a16fb
{
Packit 4a16fb
	return eval(instance, eval(instance, car(args)));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 *  main routine
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static int alisp_include_file(struct alisp_instance *instance, const char *filename)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_t *old_in;
Packit 4a16fb
	struct alisp_object *p, *p1;
Packit 4a16fb
	char *name;
Packit 4a16fb
	int retval = 0, err;
Packit 4a16fb
Packit 4a16fb
	err = snd_user_file(filename, &name);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	old_in = instance->in;
Packit 4a16fb
	err = snd_input_stdio_open(&instance->in, name, "r");
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		retval = err;
Packit 4a16fb
		goto _err;
Packit 4a16fb
	}
Packit 4a16fb
	if (instance->verbose)
Packit 4a16fb
		lisp_verbose(instance, "** include filename '%s'", name);
Packit 4a16fb
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		if ((p = parse_object(instance, 0)) == NULL)
Packit 4a16fb
			break;
Packit 4a16fb
		if (instance->verbose) {
Packit 4a16fb
			lisp_verbose(instance, "** code");
Packit 4a16fb
			princ_object(instance->vout, p);
Packit 4a16fb
			snd_output_putc(instance->vout, '\n');
Packit 4a16fb
		}
Packit 4a16fb
		p1 = eval(instance, p);
Packit 4a16fb
		if (p1 == NULL) {
Packit 4a16fb
			retval = -ENOMEM;
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		if (instance->verbose) {
Packit 4a16fb
			lisp_verbose(instance, "** result");
Packit 4a16fb
			princ_object(instance->vout, p1);
Packit 4a16fb
			snd_output_putc(instance->vout, '\n');
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		if (instance->debug) {
Packit 4a16fb
			lisp_debug(instance, "** objects after operation");
Packit 4a16fb
			print_obj_lists(instance, instance->dout);
Packit 4a16fb
		}
Packit 4a16fb
	}	
Packit 4a16fb
Packit 4a16fb
	snd_input_close(instance->in);
Packit 4a16fb
       _err:
Packit 4a16fb
	free(name);
Packit 4a16fb
	instance->in = old_in;
Packit 4a16fb
	return retval;
Packit 4a16fb
}
Packit 4a16fb
 
Packit 4a16fb
int alsa_lisp(struct alisp_cfg *cfg, struct alisp_instance **_instance)
Packit 4a16fb
{
Packit 4a16fb
	struct alisp_instance *instance;
Packit 4a16fb
	struct alisp_object *p, *p1;
Packit 4a16fb
	int i, j, retval = 0;
Packit 4a16fb
	
Packit 4a16fb
	instance = (struct alisp_instance *)calloc(1, sizeof(struct alisp_instance));
Packit 4a16fb
	if (instance == NULL) {
Packit 4a16fb
		nomem();
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	instance->verbose = cfg->verbose && cfg->vout;
Packit 4a16fb
	instance->warning = cfg->warning && cfg->wout;
Packit 4a16fb
	instance->debug = cfg->debug && cfg->dout;
Packit 4a16fb
	instance->in = cfg->in;
Packit 4a16fb
	instance->out = cfg->out;
Packit 4a16fb
	instance->vout = cfg->vout;
Packit 4a16fb
	instance->eout = cfg->eout;
Packit 4a16fb
	instance->wout = cfg->wout;
Packit 4a16fb
	instance->dout = cfg->dout;
Packit 4a16fb
	INIT_LIST_HEAD(&instance->free_objs_list);
Packit 4a16fb
	for (i = 0; i < ALISP_OBJ_PAIR_HASH_SIZE; i++) {
Packit 4a16fb
		for (j = 0; j <= ALISP_OBJ_LAST_SEARCH; j++)
Packit 4a16fb
			INIT_LIST_HEAD(&instance->used_objs_list[i][j]);
Packit 4a16fb
		INIT_LIST_HEAD(&instance->setobjs_list[i]);
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	init_lex(instance);
Packit 4a16fb
Packit 4a16fb
	for (;;) {
Packit 4a16fb
		if ((p = parse_object(instance, 0)) == NULL)
Packit 4a16fb
			break;
Packit 4a16fb
		if (instance->verbose) {
Packit 4a16fb
			lisp_verbose(instance, "** code");
Packit 4a16fb
			princ_object(instance->vout, p);
Packit 4a16fb
			snd_output_putc(instance->vout, '\n');
Packit 4a16fb
		}
Packit 4a16fb
		p1 = eval(instance, p);
Packit 4a16fb
		if (p1 == NULL) {
Packit 4a16fb
			retval = -ENOMEM;
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
		if (instance->verbose) {
Packit 4a16fb
			lisp_verbose(instance, "** result");
Packit 4a16fb
			princ_object(instance->vout, p1);
Packit 4a16fb
			snd_output_putc(instance->vout, '\n');
Packit 4a16fb
		}
Packit 4a16fb
		delete_tree(instance, p1);
Packit 4a16fb
		if (instance->debug) {
Packit 4a16fb
			lisp_debug(instance, "** objects after operation");
Packit 4a16fb
			print_obj_lists(instance, instance->dout);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (_instance)
Packit 4a16fb
		*_instance = instance;
Packit 4a16fb
	else
Packit 4a16fb
		alsa_lisp_free(instance); 
Packit 4a16fb
	
Packit 4a16fb
	return retval;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void alsa_lisp_free(struct alisp_instance *instance)
Packit 4a16fb
{
Packit 4a16fb
	if (instance == NULL)
Packit 4a16fb
		return;
Packit 4a16fb
	done_lex(instance);
Packit 4a16fb
	free_objects(instance);
Packit 4a16fb
	free(instance);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
struct alisp_cfg *alsa_lisp_default_cfg(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_t *output, *eoutput;
Packit 4a16fb
	struct alisp_cfg *cfg;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	err = snd_output_stdio_attach(&output, stdout, 0);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	err = snd_output_stdio_attach(&eoutput, stderr, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		snd_output_close(output);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	cfg = calloc(1, sizeof(struct alisp_cfg));
Packit 4a16fb
	if (cfg == NULL) {
Packit 4a16fb
		snd_output_close(eoutput);
Packit 4a16fb
		snd_output_close(output);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	cfg->out = output;
Packit 4a16fb
	cfg->wout = eoutput;
Packit 4a16fb
	cfg->eout = eoutput;
Packit 4a16fb
	cfg->dout = eoutput;
Packit 4a16fb
	cfg->in = input;
Packit 4a16fb
	return cfg;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void alsa_lisp_default_cfg_free(struct alisp_cfg *cfg)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_close(cfg->in);
Packit 4a16fb
	snd_output_close(cfg->out);
Packit 4a16fb
	snd_output_close(cfg->dout);
Packit 4a16fb
	free(cfg);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int alsa_lisp_function(struct alisp_instance *instance, struct alisp_seq_iterator **result,
Packit 4a16fb
		       const char *id, const char *args, ...)
Packit 4a16fb
{
Packit 4a16fb
	int err = 0;
Packit 4a16fb
	struct alisp_object *aargs = NULL, *obj, *res;
Packit 4a16fb
Packit 4a16fb
	if (args && *args != 'n') {
Packit 4a16fb
		va_list ap;
Packit 4a16fb
		struct alisp_object *p;
Packit 4a16fb
		p = NULL;
Packit 4a16fb
		va_start(ap, args);
Packit 4a16fb
		while (*args) {
Packit 4a16fb
			if (*args++ != '%') {
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			if (*args == '\0') {
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			obj = NULL;
Packit 4a16fb
			err = 0;
Packit 4a16fb
			switch (*args++) {
Packit 4a16fb
			case 's':
Packit 4a16fb
				obj = new_string(instance, va_arg(ap, char *));
Packit 4a16fb
				break;
Packit 4a16fb
			case 'i':
Packit 4a16fb
				obj = new_integer(instance, va_arg(ap, int));
Packit 4a16fb
				break;
Packit 4a16fb
			case 'l':
Packit 4a16fb
				obj = new_integer(instance, va_arg(ap, long));
Packit 4a16fb
				break;
Packit 4a16fb
			case 'f':
Packit 4a16fb
			case 'd':
Packit 4a16fb
				obj = new_integer(instance, va_arg(ap, double));
Packit 4a16fb
				break;
Packit 4a16fb
			case 'p': {
Packit 4a16fb
				char _ptrid[24];
Packit 4a16fb
				char *ptrid = _ptrid;
Packit 4a16fb
				while (*args && *args != '%')
Packit 4a16fb
					*ptrid++ = *args++;
Packit 4a16fb
				*ptrid = 0;
Packit 4a16fb
				if (ptrid == _ptrid) {
Packit 4a16fb
					err = -EINVAL;
Packit 4a16fb
					break;
Packit 4a16fb
				}
Packit 4a16fb
				obj = new_cons_pointer(instance, _ptrid, va_arg(ap, void *));
Packit 4a16fb
				obj = quote_object(instance, obj);
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			default:
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __args_end;
Packit 4a16fb
			if (obj == NULL) {
Packit 4a16fb
				err = -ENOMEM;
Packit 4a16fb
				goto __args_end;
Packit 4a16fb
			}
Packit 4a16fb
			if (p == NULL) {
Packit 4a16fb
				p = aargs = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
			} else {
Packit 4a16fb
				p->value.c.cdr = new_object(instance, ALISP_OBJ_CONS);
Packit 4a16fb
				p = p->value.c.cdr;
Packit 4a16fb
			}
Packit 4a16fb
			if (p == NULL) {
Packit 4a16fb
				err = -ENOMEM;
Packit 4a16fb
				goto __args_end;
Packit 4a16fb
			}
Packit 4a16fb
			p->value.c.car = obj;
Packit 4a16fb
		}
Packit 4a16fb
	      __args_end:
Packit 4a16fb
		va_end(ap);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
#if 0
Packit 4a16fb
		snd_output_printf(instance->wout, ">>>");
Packit 4a16fb
		princ_object(instance->wout, aargs);
Packit 4a16fb
		snd_output_printf(instance->wout, "<<