/* functions.c - Miscellaneous single argument functions.
 *  Copyright (C) 2022 by Jed Marti
 *   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 3 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, see https://www.gnu.org/licenses/.
 */


#include <C8051F380.h>
#include <stdint.h>
#include <math.h>

#include "lfxmit.h"
#include "serial.h"
#include "storage.h"


// Error and execution control.
extern __bit    errorThrown;
extern __bit    doit;

// Boolean values.
extern __xdata uint16_t iONE;
extern __xdata uint16_t iZERO;

extern uint16_t           DREG;
extern __xdata floatOrInt FACC;
extern __xdata floatOrInt IACC;


void isInt(void)
//    Returns 1 if argument is an integer and 0 if not.
{
  // Skipping, then just return.
  if (errorThrown || !doit) return;

  DREG = popST();
  pushST(tag() == DESC_INT?iONE:iZERO);
}



void isFloat(void)
// Returns 1 if argument is a float and 0 if not.
{
  if (errorThrown || !doit) return;
  DREG = popST();
  pushST(tag() == DESC_FLOAT?iONE:iZERO);
}



void isString(void)
// Returns 1 if argument is a string and 0 if not.
{
  if (errorThrown || !doit) return;
  DREG = popST();
  pushST(tag() == DESC_STRING?iONE:iZERO);
}



void isByteVector(void)
// Returns 1 if argument is a ByteVector and 0 if not.
{
  if (errorThrown || !doit) return;
  DREG = popST();
  pushST(tag() == DESC_BYTEVECTOR?iONE:iZERO);
}



void isFloatVector(void)
// Returns 1 if argument is a FloatVector  and 0 if not.
{
  if (errorThrown || !doit) return;
  DREG = popST();
  pushST(tag() == DESC_FLOATVECTOR?iONE:iZERO);
}



void doRound(void)
// Round a floating-point value up or down depending upon sign. Integers passed through, others
// cause errors.
{
  if (errorThrown || !doit) return;

  DREG = popST();
  switch (tag()) {
  case DESC_INT: pushST(DREG); break;
  case DESC_FLOAT:
    getFACC();
    if (FACC.f < 0.0f) IACC.i = (int32_t) (FACC.f - 0.5);
    else IACC.i = (int32_t) (FACC.f + 0.5);
    pushST(newInt(IACC.i));
    break;
  default:
    putstring("ROUND non-number\n");
    errorThrown = 1;
  }
}


  

void doSqrt(void)
// Take the square root of a number - must be positive and a number or error signaled.
{
  if (errorThrown || !doit) return;

  DREG = popST();
  switch (tag()) {
  case DESC_INT:
    getIACC();
    if (IACC.i < 0)
    {
      putstring("SQRT < 0\n");
      errorThrown = 1;
      return;
    }
    FACC.f = sqrtf((float) IACC.i);
    break;
  case DESC_FLOAT:
    getFACC();
    if (FACC.f < 0.0f)
    {
      putstring("SQRT < 0.0\n");
      errorThrown = 1;
      return;
    }
    FACC.f = sqrtf(FACC.f);
    break;
  default:
    putstring("SQRT non-number\n");
    errorThrown = 1;
    return;
  }
  pushST(newFloatAddr((uint32_t __xdata *) &FACC));
}



void doAbs(void)
// Take the absolute value of a number and return. Error signaled on non-number.
{
  if (errorThrown || !doit) return;
  DREG = popST();
  switch (tag()) {
  case DESC_INT:
    getIACC();
    if (IACC.i < 0) IACC.i = -IACC.i;
    pushST(newInt(IACC.i));
    break;

  case DESC_FLOAT:
    getFACC();
    if (FACC.f < 0.0) FACC.f = -FACC.f;
    pushST(newFloatAddr((uint32_t __xdata *) &FACC));
    return;

  default:
    putstring("ABS non-number\n");
    errorThrown = 1;
    return;
  }
}

// EOF
