/* $Id: return.c,v 1.2 2009/01/04 15:49:32 kiesling Exp $ */

/*
  This file is part of Ctalk.
  Copyright © 2005-2009 Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation, 
  Inc., 51 Franklin St., Fifth Floor, Boston, MA 02110-1301 USA.
*/

/* 
 *  Functions to manage the return value stack.  They are only
 *  accounted for by the parser frame, because ctalk, at this time,
 *  does not need to recurse into functions.
 */

#include <stdio.h>
#include "object.h"
#include "message.h"
#include "ctalk.h"
#include "lex.h"

static OBJECT *returnvals[MAXARGS+1];
static int returnvalsptr;

int return_val_push (OBJECT *o) {
  if (returnvalsptr == 0) {
    _warning ("Return_val_push: stack overflow.\n");
    return ERROR;
  }
  returnvals[--returnvalsptr] = o;
#ifdef TRACE_RETURN_VALUES
  debug ("return_val_push %d. %s\n", 
	  returnvalsptr, returnvals[returnvalsptr]); 
#endif
  return SUCCESS;
}

int get_return_val_pointer (void) {
  return returnvalsptr;
}

OBJECT *return_val_pop (void) {

  OBJECT *ret;

  if (returnvalsptr >= MAXARGS)
    _warning ("Return_val_pop: stack underflow.");

#ifdef TRACE_RETURN_VALUES
  debug ("return_val_pop %d. %s", 
	  returnvalsptr, returnvals[returnvalsptr]); 
#endif
  ret = returnvals[returnvalsptr++];
  return ret;
}

int init_return_vals (void) {
  returnvalsptr = MAXARGS;
  return SUCCESS;
}

