Skip to content
ast_if.c 1.43 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** ast_if.c for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Thu Aug  3 02:41:37 2006 Seblu
Seblu's avatar
Seblu committed
** Last update Tue Oct 10 17:15:53 2006 seblu
Seblu's avatar
Seblu committed
*/

#include "ast.h"

Seblu's avatar
Seblu committed
s_ast_node	*ast_if_create(s_ast_node *cond,
			       s_ast_node *cond_true,
			       s_ast_node *cond_false)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_ast_node	*node;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  secmalloc(node, sizeof (s_ast_node));
Seblu's avatar
Seblu committed
  node->type = T_IF;
  node->body.child_if.cond = cond;
  node->body.child_if.cond_true = cond_true;
  node->body.child_if.cond_false = cond_false;
  return node;
}

Seblu's avatar
Seblu committed
void		ast_if_print(s_ast_node *node, FILE *fs, unsigned *node_id)
{
  unsigned	cur_node;

  if (node->type != T_IF)
    return;
  fprintf(fs, "%u [label = \"IF\"];\n", cur_node = *node_id);
  ++*node_id;
  //if
  if (node->body.child_if.cond) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    ast_print_node(node->body.child_if.cond, fs, node_id);
  }
  //then
  if (node->body.child_if.cond_true) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    ast_print_node(node->body.child_if.cond_true, fs, node_id);
  }
  //else
  if (node->body.child_if.cond_false) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    ast_print_node(node->body.child_if.cond_false, fs, node_id);
  }
}

Seblu's avatar
Seblu committed
void		ast_if_destruct(s_ast_node *node)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  if (node->type != T_IF)
Seblu's avatar
Seblu committed
    return;
  ast_destruct(node->body.child_if.cond);
  ast_destruct(node->body.child_if.cond_true);
  ast_destruct(node->body.child_if.cond_false);
  free(node);
}