Skip to content
ast_for.c 1.54 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 Thu Oct 12 16:01:04 2006 seblu
Seblu's avatar
Seblu committed
*/

#include "ast.h"

Seblu's avatar
Seblu committed
s_ast_node	*ast_for_create(char		*varname,
Seblu's avatar
Seblu committed
				char		**values,
Seblu's avatar
Seblu committed
				s_ast_node	*exec)
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_FOR;
  node->body.child_for.varname = varname;
  node->body.child_for.values = values;
  node->body.child_for.exec = exec;
  return node;
}

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

  if (node->type != T_FOR)
    return;
  fprintf(fs, "%u [label = \"FOR\\nvariable: %s\"];\n",
	  cur_node = *node_id, node->body.child_for.varname);
  ++*node_id;
  //values
  if (node->body.child_for.values) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    fprintf(fs, "%u [ label = \"Values\\n", *node_id);
    ++*node_id;
    for (register size_t i = 0; node->body.child_for.values[i]; ++i)
      fprintf(fs, "id=%u %s\\n", i, node->body.child_for.values[i]);
    fprintf(fs, "\"];");
  }
  //execution
  if (node->body.child_for.exec) {
    fprintf(fs, "%u -> %u\n", cur_node, *node_id);
    ast_print_node(node->body.child_for.exec, fs, node_id);
  }
}

Seblu's avatar
Seblu committed
void		ast_for_destruct(s_ast_node *node)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  if (node->type != T_FOR)
Seblu's avatar
Seblu committed
    return;
  free(node->body.child_for.varname);
  for (register int i = 0; node->body.child_for.values[i]; ++i)
    free(node->body.child_for.values[i]);
  free(node->body.child_for.values);
  ast_destruct(node->body.child_for.exec);
  free(node);
}