#include "Node.h"

#include <stdlib.h>

extern oop s_SmallInteger;
extern oop s__5fvalue_;

//  int mValue;

IntegerNode::IntegerNode(int value)
{
  mValue= value;
}

oop IntegerNode::fromString(oop string)
{
  char *in= string->cString(), *out= 0;
  long value= strtol(in, &out, 0);
  if (*out) abort();
  return new IntegerNode((int)value);
}

oop IntegerNode::encode(oop compiler)
{
  LiteralNode::encode(compiler);
  compiler->translationUnit()->lookupClass(s_SmallInteger);
  compiler->translationUnit()->declareSelector(s__5fvalue_);
  return this;
}

oop IntegerNode::genInitialisation(oop unit)
{
  unit->outputStream()->format("  l_%d= _bind(v_%s, s__5fvalue_)(v_%s, (oop)%d);\n",
	mTag, s_SmallInteger->cString(), s_SmallInteger->cString(), mValue);
  return this;
}

oop IntegerNode::genByte(oop unit)
{
  if ((mValue < 0) || (mValue > 255))
    error("ByteArray element out of range: %d", mValue);
  unit->outputStream()->format("\\%03o", mValue);
  return this;
}

oop IntegerNode::genWord(oop unit)
{
  unit->outputStream()->format("0x%x", (unsigned)mValue);
  return this;
}

oop IntegerNode::printOn(oop stream)
{
  stream->format("IntegerNode(%d)", mValue);
  return this;
}
