/*=======================================================================*
 * 膊�茵�綣����篏�                                                          *
 *=======================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "exp.h"

/*==============================*
 * �����弱����                   *
 *==============================*/

void *exit_error(const char *str) {
  fprintf(stderr, "*** %s ***\n", str);
  exit(EXIT_FAILURE);
}

/*==============================*
 * 膊�茵�綣���堺���∽��             *
 *==============================*/

/*----- 膊�茵�綣���������茖純������ -----*/

Exp make0_exp(char c)                  { return leaf(c); }
Exp make1_exp(char c, const Exp e)     { return branch(c, e, NULL); }
Exp make2_exp(char c, const Exp e1, const Exp e2)
                                       { return branch(c, e1, e2); }
void copy_exp(const Exp src, Exp *dst) { *dst = copy_tree(src); }
void delete_exp(Exp *ep)               { free_tree(*ep); *ep = NULL; }

/*----- 膊�茵�綣����粋昭�� -----*/

/* ��������������蕁� 2a ��絎���������羔��� */
#define NOT_IMPLEMENTED  NULL

Exp read_exp(void) {
  int c;
  Exp l = NULL, r = NULL;   /* 綏�����綣���渇����綣� */
  char op;                  /* 羲�膊�絖� */
  c = getchar();
  if (c == EOF) { return NULL; }
  /* 茯臥� 2a */ /* NOT_IMPLEMENTED ������������������� */
  if (isalnum(c)) {
    return NOT_IMPLEMENTED;
  } else {
    if (c != '(') { exit_error("expression not found"); }
    l = NOT_IMPLEMENTED;
    c = getchar();
    if (c != '+' && c != '*') { exit_error("operator not found"); }
    op = (char)c;
    r = NOT_IMPLEMENTED;
    c = getchar();
    if (c != ')') { exit_error("closing parenthesis not found"); }
    return NOT_IMPLEMENTED;
  }
}

/*----- 膊�茵�綣��;腓� -----*/

void show_exp(const Exp e) {
  /* 茯臥� 2a */
}

/*----- 膊�茵�綣���ゃ���膊� -----*/

/* ��������������蕁� 2b ��絎���������羔��� */
#define UNKNOWN_VALUE  0

int eval_exp(const Exp e) {
  /* 茯臥� 2b */
  return UNKNOWN_VALUE;
}

/*==============================*
 * 膊�茵�綣���綵�                 *
 *==============================*/

/*----- ��荵� -----*/

/*
 *     %              @
 *    / \   綏���荵�   / \
 *  E1   @    ��    %   E3
 *      / \        / \
 *    E2   E3    E1   E2
 */

void rotate_left_exp(Exp *ep) {
  /* 茯臥� 2c */
}

void rotate_right_exp(Exp *ep) {
  /* 茯臥� 3b */
}

/*----- ��磁�������叵�� -----*/

/*
 * (E1+(E2+E3)) �� ((E1+E2)+E3)
 * (E1*(E2*E3)) �� ((E1*E2)*E3)
 */

void assoc_left_exp(Exp *ep) {
  /* 茯臥� 3a */
}

/*----- 腥���������������� -----*/

/*
 * (E1*(E2+E3)) �� ((E1*E2)+(E1*E3))
 * ((E1+E2)*E3) �� ((E1*E3)+(E2*E3))
 */

void dist_prod_exp(Exp *ep) {
  /* 茯臥� 3b */
}