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

#include "microc.h"


void call_getche(struct variable *value) {
  char ch;

  ch = getchar();
  while (*prog != ')')  prog++;
  prog++;                     // advance to end of line
  value->type = TOKEN_CHAR;
  value->value.c = ch;
}

// Put a character to the display.
void call_putch(struct variable *value) {
  struct variable val;

  eval_exp(&val);
  printf("%c", val.value.c);
  value->type = TOKEN_CHAR;
  value->value.c = val.value.c;
}

// Call puts().
void call_puts(struct variable *value) {

  get_token();
  if (*token != '(')  sntx_err(E_PAREN_EXPECTED);
  get_token();
  if (token_type != STRING)  sntx_err(E_QUOTE_EXPECTED);

  puts(token);
  get_token();
  if (*token != ')')  sntx_err(E_PAREN_EXPECTED);

  get_token();
  if (*token != ';')  sntx_err(E_SEMI_EXPECTED);
  putback();
}

// A built-in console output function.
void print(struct variable *value) {
  struct variable i;

  get_token();
  if (*token != '(')  sntx_err(E_PAREN_EXPECTED);

  get_token();
  if (token_type == STRING) { // output a string
    printf("%s ", token);
  } else {                    // output a number
   putback();
   eval_exp(&i);
   printf("%d ", i.value.i);
  }

  get_token();

  if (*token != ')')  sntx_err(E_PAREN_EXPECTED);

  get_token();
  if (*token != ';')  sntx_err(E_SEMI_EXPECTED);
  putback();
}

// Read an integer from the keyboard.
void getnum(struct variable *value) {
  char s[80];

  gets(s);
  while (*prog != ')')  prog++;
  prog++;                     // advance to end of line
  value->type = TOKEN_INT;
  value->value.i = atoi(s);
}
