/* $Id: typecheck.c,v 1.1.1.1 2008/10/28 13:28:20 kiesling Exp $ */

/*
  This file is part of ctalk.
  Copyright © 2005-2008 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.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include "object.h"
#include "message.h"
#include "ctalk.h"
#include "lex.h"
#include "typeof.h"
#include "cvar.h"

int operand_type_check (MESSAGE_STACK messages, int op_idx) {

  if (IS_C_UNARY_MATH_OP(M_TOK(messages[op_idx]))) 
    unary_type_check (messages, op_idx);
  if (IS_C_BINARY_MATH_OP(M_TOK(messages[op_idx])))
    binary_type_check (messages, op_idx);

  return SUCCESS;
}

int unary_type_check (MESSAGE_STACK messages, int op_idx) {
  return SUCCESS;
}

enum op_type_class {
  op_type_null,
  op_type_object
};


/*
 *  Still very much in the beginning stages... doesn't do
 *  anything yet.
 */
int binary_type_check (MESSAGE_STACK messages, int op_idx) {
  int r, op1_idx, op2_idx;
  MESSAGE *m_op1, *m_op2;
  OBJECT *op1_object, *op2_object;
  enum op_type_class op1_class = op_type_null, op2_class = op_type_null;

  if ((r = operands (messages, op_idx, &op1_idx, &op2_idx)) == ERROR) {
    warning (messages[op_idx], "Parser error.");
    return ERROR;
  }

  m_op1 = messages[op1_idx];
  m_op2 = messages[op2_idx];

  switch (M_TOK(m_op1))
    {
    case LABEL:
      if ((op1_object = get_object (M_NAME(m_op1), NULL)) != NULL) {
	op1_class = op_type_object;
      } 
      break;
    }

  switch (M_TOK(m_op2))
    {
    case LABEL:
      if ((op2_object = get_object (M_NAME(m_op1), NULL)) != NULL) {
	op2_class = op_type_object;
      } 
      break;
    }

  /*
   *  We haven't found a way yet to analyze the operand.
   */
  if ((op1_class == op_type_null) || (op2_class == op_type_null))
    return SUCCESS;

  return SUCCESS;
}
