#ifndef _TREES_HPP_
#define _TREES_HPP_

typedef enum { bad_node_kind, 
	       non_terminal, 
	       id_node, 
	       const_node,
	       last_node } NODE_KIND;

class node {
private:
  node *kid;
  node *sib;
  node_token token;
  
public:
  node(void) {
    kid = NULL;
    sib = NULL;
    token = n_bad_node;
  }
  
  node( const node_token n ) : token( n )
  {
    kid = NULL;
    sib = NULL;    
  }

  void set_token( const node_token n )  { token = n; }
  const node_token get_token(void)  { return token; }
  
  void set_kid( const node *k )  { kid = (node *)k; }
  node *get_kid(void)        { return kid; }
  
  void set_sib( const node *s )    { sib = (node *)s; };
  node *get_sib(void)        { return sib; }
  
  /* tree "macros" */
  Boolean is_leaf(void) { return kid == NULL; }
  Boolean is_nonleaf(void) { return kid != NULL; }

  const char *get_name(void);

  virtual NODE_KIND get_node_kind(void) { return non_terminal; }

  virtual const char *get_id_name()
  {
    assert( FALSE );
    return NULL;
  }

  virtual const int get_const_val()
  {
    assert( FALSE );
    return 0;
  }
}; // node



class ident_node : public node {
private:
  const char *id_name;
public:
  ident_node(const char *str) : id_name(str), node( n_id ) { };

  NODE_KIND get_node_kind(void) { return id_node; }
  const char *get_id_name()     { return id_name; }
};  // ident_node


class constant_node : public node {
private:
  const int const_val;
public:
  constant_node(const int val) : const_val(val), node( n_icon ) { }

  NODE_KIND get_node_kind(void) { return const_node; }  
  const int get_const_val(void) { return const_val; }
};  // constant_node

typedef node *pNode;

#endif

back to ANTLR examples page