#include <stdio.h>
#include <stdlib.h>

void *GC_malloc(int size);

/* This defines the "thread-local area" for a single-threaded version of
 * TAL.  I've arbitrarily set this up to be about 80 words worth of data
 * so that we can have plenty of space to store things.
 */
int taltla[80];

typedef struct {int size; char chars[1];} *string;

extern void tal_main(void);

void array_bounds_error(void)
{
  printf("\narray bounds error!\n");
  exit(2);
}

void out_of_memory(void)
{
  printf("\nout of memory!\n");
  exit(2);
}

struct {int size; string args[1];} args;

static string mk_string(char *s)
{
  string str;
  int size=strlen(s);
  str=GC_malloc(sizeof(int)+size);
  str->size=size;
  while(size--) str->chars[size]=s[size];
  return str;
}

int main(int argc, char **argv)
{
  args.size=argc;
  while(argc--) args.args[argc]=mk_string(argv[argc]);
  tal_main();
  return 0;
}

typedef struct {int size; void *elms[1];} *array;

array new_array(int size, void *init)
{
  array na=GC_malloc(4+size*4);
  na->size=size;
  while(size-->0)
    na->elms[size]=init;
  return na;
}

typedef void *(*gen_fun)(void *,int);

array new_array_f(int size, gen_fun f, void *env)
{
  int i;
  array na=GC_malloc(4+size*4);
  na->size=size;
  for(i=0; i<size; i++)
    na->elms[size]=(*f)(env,i);
  return na;
}

/* EOF: tal_start.c */
