Skip to content
ast_case.c 1.95 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** ast_for.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 Wed Oct 11 13:07:50 2006 seblu
Seblu's avatar
Seblu committed
*/

#include "ast.h"

Seblu's avatar
Seblu committed
s_ast_node	*ast_case_create(char *word)
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_CASE;
  node->body.child_case.word = word;
  node->body.child_case.items = NULL;
  return node;
}

Seblu's avatar
Seblu committed
void		ast_case_add_item(s_ast_node	*node,
Seblu's avatar
Seblu committed
				  char		**pattern,
Seblu's avatar
Seblu committed
				  s_ast_node	*exec)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_case_item	*item;
  s_case_item	**this;
Seblu's avatar
Seblu committed

  if (node->type != T_CASE)
    return;
Seblu's avatar
Seblu committed
  secmalloc(item, sizeof (s_case_item));
  item->pattern = pattern;
  item->exec = exec;
  item->next = NULL;
  for (this = &node->body.child_case.items; *this; *this = (*this)->next)
    ; //do nothing
  *this = item;
Seblu's avatar
Seblu committed
}

Seblu's avatar
Seblu committed
void		ast_case_print(s_ast_node *node, FILE *fs, unsigned *node_id)
{
  unsigned	cur_node;
  s_case_item	*item;
  unsigned	item_id;

  if (node->type != T_CASE)
    return;
  fprintf(fs, "%u [label = \"CASE\\nword: %s\"];\n",
	  cur_node = *node_id, node->body.child_case.word);
  ++*node_id;
  for (item = node->body.child_case.items, item_id = 0;
       item;
       item = item->next, ++item_id) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    fprintf(fs, "%u [ label = \"Item %u\\n", *node_id, item_id);
    ++*node_id;
    //print pattern
    if (item->pattern)
      for (int i = 0; item->pattern[i]; ++i)
	fprintf(fs, "%s\\n", item->pattern[i]);
    fprintf(fs, "\"];\n");
    //print exec
    if (item->exec)
      ast_print_node(item->exec, fs, node_id);
  }
}

Seblu's avatar
Seblu committed
void		ast_case_destruct(s_ast_node *node)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_case_item	*this, *buf;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  if (node->type != T_CASE)
    return;
  free(node->body.child_case.word);
  for (this = node->body.child_case.items; this; this = buf) {
    for (register int i = 0; this->pattern[i]; ++i)
      free(this->pattern[i]);
    free(this->pattern);
    ast_destruct(this->exec);
    buf = this->next;
    free(this);
  }
  free(node);
Seblu's avatar
Seblu committed
}