Commit f923fc3f authored by Seblu's avatar Seblu

Fin des fonctions de creation et destruction de l'AST

correction d'un bug dans basename
Ajout des fichiers de l'ast au makefile
Le 42sh est maintenant compile en static sous linux
Reecriture du lexer facon LL1 (gettoken, lookahead)
Separation des tokens en keywords et operators (XSI compliant)
parent 09da92f1
......@@ -8,6 +8,7 @@ bin_PROGRAMS=42sh
ast/ast_and.c \
ast/ast_bang.c \
ast/ast_case.c \
ast/ast_cmd.c \
ast/ast_destruct.c \
ast/ast_for.c \
ast/ast_funcdec.c \
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:40:03 2006 Seblu
** Last update Fri Aug 18 15:56:25 2006 Seblu
** Last update Fri Aug 18 22:38:00 2006 Seblu
*/
#ifndef AST_H_
......@@ -16,7 +16,7 @@
# include <stdlib.h>
# include <assert.h>
typedef struct s_ast ts_ast_node;
typedef struct s_ast_node ts_ast_node;
/*
** If ast node
......@@ -45,7 +45,7 @@ typedef struct s_case_item ts_case_item;
struct s_case_item
{
char **pattern;
ts_ast_node *list;
ts_ast_node *exec;
ts_case_item *next;
};
......@@ -86,12 +86,14 @@ typedef enum e_redir_type
/*
** Redirection ast node
*/
typedef struct s_redir_node
typedef struct s_redir ts_redir;
struct s_redir
{
te_redir_type type;
int fd;
char *string;
} ts_redir_node;
char *word;
ts_redir *next;
};
/*
** Command ast node
......@@ -99,14 +101,14 @@ typedef struct s_redir_node
typedef struct s_cmd_node
{
char **argv;
ts_redir_node **redirs;
ts_redir *redirs;
char **prefix;
} ts_cmd_node;
/*
** Binary ast node
** Generic type, it's a contener !
** T_PIPE, T_SEP_* , T_AND, T_OR : binary operator
** Generic node, it's a contener !
** T_PIPE, T_SEP* , T_AND, T_OR : binary operator
** T_BANG : unary operator but ts_bin_op with right pointer is always NULL
*/
typedef struct s_bin_node
......@@ -152,7 +154,7 @@ typedef union u_node_item
{
ts_if_node child_if;
ts_for_node child_for;
ts_case_node child_case; //todo
ts_case_node child_case;
ts_while_node child_while;
ts_while_node child_until;
ts_cmd_node child_cmd; //todo
......@@ -169,7 +171,7 @@ typedef union u_node_item
/*
** Generic ast node type
*/
struct s_ast
struct s_ast_node
{
te_node_type type;
tu_node_item body;
......@@ -192,7 +194,7 @@ void ast_destruct(ts_ast_node *ast);
**
** @return the node
*/
ts_ast_node *ast_create_if(ts_ast_node *cond,
ts_ast_node *ast_if_create(ts_ast_node *cond,
ts_ast_node *cond_true,
ts_ast_node *cond_false);
......@@ -201,7 +203,7 @@ ts_ast_node *ast_create_if(ts_ast_node *cond,
**
** @param node node to destroy
*/
void ast_destruct_if(ts_ast_node *node);
void ast_if_destruct(ts_ast_node *node);
/*!
** Create a for ast node
......@@ -213,8 +215,8 @@ void ast_destruct_if(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_for(char *varname,
char **values,
ts_ast_node *ast_for_create(char *varname,
char **values,
ts_ast_node *exec);
/*!
......@@ -222,7 +224,35 @@ ts_ast_node *ast_create_for(char *varname,
**
** @param node node to destroy
*/
void ast_destruct_for(ts_ast_node *node);
void ast_for_destruct(ts_ast_node *node);
/*!
** Create a case ast node
**
** @param word reference word
**
** @return the node
*/
ts_ast_node *ast_case_create(char *word);
/*!
** Add a case item into a case node. An item is counpound of a set of word
** which match with reference word an case node and the corresponding
**
** @param node node where item will be added
** @param pattern list of word that can match to reference
** @param exec exec corresponding with match
*/
void ast_case_add_item(ts_ast_node *node,
char **pattern,
ts_ast_node *exec);
/*!
** Destruct a case ast node
**
** @param node node to destroy
*/
void ast_case_destruct(ts_ast_node *node);
/*!
** Create a while ast node
......@@ -232,14 +262,14 @@ void ast_destruct_for(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_while(ts_ast_node *cond, ts_ast_node *exec);
ts_ast_node *ast_while_create(ts_ast_node *cond, ts_ast_node *exec);
/*!
** Destruct a while ast node
**
** @param node node to destroy
*/
void ast_destruct_while(ts_ast_node *node);
void ast_while_destruct(ts_ast_node *node);
/*!
** Create a until ast node
......@@ -249,14 +279,14 @@ void ast_destruct_while(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_until(ts_ast_node *cond, ts_ast_node *exec);
ts_ast_node *ast_until_create(ts_ast_node *cond, ts_ast_node *exec);
/*!
** Destruct a until ast node
**
** @param node node to destroy
*/
void ast_destruct_until(ts_ast_node *node);
void ast_until_destruct(ts_ast_node *node);
/*!
** Create an and (&&) ast node
......@@ -266,14 +296,14 @@ void ast_destruct_until(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_and(ts_ast_node *lhs, ts_ast_node *rhs);
ts_ast_node *ast_and_create(ts_ast_node *lhs, ts_ast_node *rhs);
/*!
** Destruct an and (&&) node
**
** @param node node to destroy
*/
void ast_destruct_and(ts_ast_node *node);
void ast_and_destruct(ts_ast_node *node);
/*!
** Create an or (||) ast node
......@@ -283,14 +313,14 @@ void ast_destruct_and(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_or(ts_ast_node *lhs, ts_ast_node *rhs);
ts_ast_node *ast_or_create(ts_ast_node *lhs, ts_ast_node *rhs);
/*!
** Destruct an or (||) node
**
** @param node node to destroy
*/
void ast_destruct_or(ts_ast_node *node);
void ast_or_destruct(ts_ast_node *node);
/*!
** Create a subshell ($()) ast node
......@@ -299,14 +329,14 @@ void ast_destruct_or(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_subshell(ts_ast_node *child);
ts_ast_node *ast_subshell_create(ts_ast_node *child);
/*!
** Destruct a subshell ($()) node
**
** @param node node to destroy
*/
void ast_destruct_subshell(ts_ast_node *node);
void ast_subshell_destruct(ts_ast_node *node);
/*!
** Create a funcdec (function declaration) ast node
......@@ -316,14 +346,14 @@ void ast_destruct_subshell(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_funcdec(char *name, ts_ast_node *body);
ts_ast_node *ast_fundec_create(char *name, ts_ast_node *body);
/*!
** Destruct a funcdec ast node
**
** @param node node to destroy
*/
void ast_destruct_funcdec(ts_ast_node *node);
void ast_funcdec_destruct(ts_ast_node *node);
/*!
** Create a bang (!) ast node
......@@ -332,14 +362,14 @@ void ast_destruct_funcdec(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_bang(ts_ast_node *child);
ts_ast_node *ast_bang_create(ts_ast_node *child);
/*!
** Destruct a bang (!) node
**
** @param node node to destroy
*/
void ast_destruct_bang(ts_ast_node *node);
void ast_bang_destruct(ts_ast_node *node);
/*!
** Create a pipe (|) ast node
......@@ -349,14 +379,14 @@ void ast_destruct_bang(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_pipe(ts_ast_node *lhs, ts_ast_node *rhs);
ts_ast_node *ast_pipe_create(ts_ast_node *lhs, ts_ast_node *rhs);
/*!
** Destruct a pipe (|) node
**
** @param node node to destroy
*/
void ast_destruct_pipe(ts_ast_node *node);
void ast_pipe_destruct(ts_ast_node *node);
/*!
** Create a separtor (;) ast node
......@@ -366,14 +396,14 @@ void ast_destruct_pipe(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_sep(ts_ast_node *lhs, ts_ast_node *rhs);
ts_ast_node *ast_sep_create(ts_ast_node *lhs, ts_ast_node *rhs);
/*!
** Destruct a sep (;) node
**
** @param node node to destroy
*/
void ast_destruct_sep(ts_ast_node *node);
void ast_sep_destruct(ts_ast_node *node);
/*!
** Create a sepand (&) ast node
......@@ -383,13 +413,13 @@ void ast_destruct_sep(ts_ast_node *node);
**
** @return the node
*/
ts_ast_node *ast_create_sepand(ts_ast_node *lhs, ts_ast_node *rhs);
ts_ast_node *ast_sepand_create(ts_ast_node *lhs, ts_ast_node *rhs);
/*!
** Destruct a sepand (&) node
**
** @param node node to destroy
*/
void ast_destruct_sep(ts_ast_node *node);
void ast_sepand_destruct(ts_ast_node *node);
#endif /* !AST_H_ */
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:40:35 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_and(ts_ast_node *lhs, ts_ast_node *rhs)
ts_ast_node *ast_and_create(ts_ast_node *lhs, ts_ast_node *rhs)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_and(ts_ast_node *lhs, ts_ast_node *rhs)
return node;
}
void ast_destruct_and(ts_ast_node *node)
void ast_and_destruct(ts_ast_node *node)
{
if (node->type != T_AND) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 15:57:05 2006 Seblu
** Last update Fri Aug 18 20:43:52 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_bang(ts_ast_node *child)
ts_ast_node *ast_bang_create(ts_ast_node *child)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_bang(ts_ast_node *child)
return node;
}
void ast_destruct_bang(ts_ast_node *node)
void ast_bang_destruct(ts_ast_node *node)
{
if (node->type != T_BANG) {
ast_destruct(node);
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 15:55:00 2006 Seblu
** Last update Fri Aug 18 22:32:35 2006 Seblu
*/
#include "../common/mem.h"
......@@ -24,41 +24,38 @@ ts_ast_node *ast_case_create(char *word)
void ast_case_add_item(ts_ast_node *node,
char **pattern,
ts_ast_node *list)
ts_ast_node *exec)
{
/* ts_case_item *item; */
ts_case_item *item;
ts_case_item **this;
/* secmalloc(item, sizeof (ts_case_item)); */
/* item->pattern = pattern; */
/* item->list = list; */
node = node;
pattern = pattern;
list = list;
/* for (ts_case_item **this = &node->items; */
/* *this; */
/* *this = (*this)->next) */
/* ; //do nothing */
/* *this = item; */
if (node->type != T_CASE)
return;
secmalloc(item, sizeof (ts_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;
}
void ast_case_destruct(ts_ast_node *node)
{
node = node;
/* ts_case_item *this, *buf; */
ts_case_item *this, *buf;
/* if (node->type != T_CASE) { */
/* ast_destruct(node); */
/* return; */
/* } */
/* free(node->body.child_case.word); */
/* /\* for (ts_case_item *this = node->items; *\/ */
/* /\* this; *\/ */
/* /\* this = this->next) *\/ */
/* /\* for (register int i = 0; node->body.child_case.items[i]; ++i) { *\/ */
/* //fixme for multiple pattern */
/* free(node->body.child_case.items[i].pattern); */
/* ast_destruct(node->body.child_case.items[i].list); */
/* } */
/* free(node->body.child_case.items); */
/* free(node); */
if (node->type != T_CASE) {
ast_destruct(node);
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);
}
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:38:08 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_for(char *varname,
ts_ast_node *ast_for_create(char *varname,
char **values,
ts_ast_node *exec)
{
......@@ -25,7 +25,7 @@ ts_ast_node *ast_create_for(char *varname,
return node;
}
void ast_destruct_for(ts_ast_node *node)
void ast_for_destruct(ts_ast_node *node)
{
if (node->type != T_FOR) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Fri Aug 18 15:55:42 2006 Seblu
** Last update Fri Aug 18 20:43:15 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_funcdec(char *name, ts_ast_node *body)
ts_ast_node *ast_funcdec_create(char *name, ts_ast_node *body)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_funcdec(char *name, ts_ast_node *body)
return node;
}
void ast_destruct_funcdec(ts_ast_node *node)
void ast_funcdec_destruct(ts_ast_node *node)
{
if (node->type != T_FUNCDEC) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:37:47 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_if(ts_ast_node *cond,
ts_ast_node *ast_if_create(ts_ast_node *cond,
ts_ast_node *cond_true,
ts_ast_node *cond_false)
{
......@@ -25,7 +25,7 @@ ts_ast_node *ast_create_if(ts_ast_node *cond,
return node;
}
void ast_destruct_if(ts_ast_node *node)
void ast_if_destruct(ts_ast_node *node)
{
if (node->type != T_IF) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:40:50 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_or(ts_ast_node *lhs, ts_ast_node *rhs)
ts_ast_node *ast_or_create(ts_ast_node *lhs, ts_ast_node *rhs)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_or(ts_ast_node *lhs, ts_ast_node *rhs)
return node;
}
void ast_destruct_or(ts_ast_node *node)
void ast_or_destruct(ts_ast_node *node)
{
if (node->type != T_OR) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:44:37 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_pipe(ts_ast_node *lhs, ts_ast_node *rhs)
ts_ast_node *ast_pipe_create(ts_ast_node *lhs, ts_ast_node *rhs)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_pipe(ts_ast_node *lhs, ts_ast_node *rhs)
return node;
}
void ast_destruct_pipe(ts_ast_node *node)
void ast_pipe_destruct(ts_ast_node *node)
{
if (node->type != T_PIPE) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:48:47 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_sep(ts_ast_node *lhs, ts_ast_node *rhs)
ts_ast_node *ast_sep_create(ts_ast_node *lhs, ts_ast_node *rhs)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_sep(ts_ast_node *lhs, ts_ast_node *rhs)
return node;
}
void ast_destruct_sep(ts_ast_node *node)
void ast_sep_destruct(ts_ast_node *node)
{
if (node->type != T_SEP) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:49:02 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_sepand(ts_ast_node *lhs, ts_ast_node *rhs)
ts_ast_node *ast_sepand_create(ts_ast_node *lhs, ts_ast_node *rhs)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_sepand(ts_ast_node *lhs, ts_ast_node *rhs)
return node;
}
void ast_destruct_sepand(ts_ast_node *node)
void ast_sepand_destruct(ts_ast_node *node)
{
if (node->type != T_SEPAND) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:42:28 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_subshell(ts_ast_node *child)
ts_ast_node *ast_subshell_create(ts_ast_node *child)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_subshell(ts_ast_node *child)
return node;
}
void ast_destruct_subshell(ts_ast_node *node)
void ast_subshell_destruct(ts_ast_node *node)
{
if (node->type != T_SUBSHELL) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:39:25 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_until(ts_ast_node *cond, ts_ast_node *exec)
ts_ast_node *ast_until_create(ts_ast_node *cond, ts_ast_node *exec)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_until(ts_ast_node *cond, ts_ast_node *exec)
return node;
}
void ast_destruct_until(ts_ast_node *node)
void ast_until_destruct(ts_ast_node *node)
{
if (node->type != T_UNTIL) {
ast_destruct(node);
......
......@@ -5,13 +5,13 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 02:41:37 2006 Seblu
** Last update Thu Aug 3 03:03:31 2006 Seblu
** Last update Fri Aug 18 20:39:02 2006 Seblu
*/
#include "../common/mem.h"
#include "ast.h"
ts_ast_node *ast_create_while(ts_ast_node *cond, ts_ast_node *exec)
ts_ast_node *ast_while_create(ts_ast_node *cond, ts_ast_node *exec)
{
ts_ast_node *node;
......@@ -22,7 +22,7 @@ ts_ast_node *ast_create_while(ts_ast_node *cond, ts_ast_node *exec)
return node;
}
void ast_destruct_while(ts_ast_node *node)
void ast_while_destruct(ts_ast_node *node)
{
if (node->type != T_WHILE) {
ast_destruct(node);
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 05:29:46 2006 Seblu
** Last update Thu Aug 3 10:36:04 2006 Seblu
** Last update Sat Aug 19 01:57:49 2006 Seblu
*/
#include <string.h>
......@@ -18,7 +18,7 @@ char *basename(const char *path)
if ((len = strlen(path)) == 0)
return strdup("");
//search first char without '/' from end string
//search first char WITHOUT '/' start at end string
for (i = len - 1; ; --i) {
if (path[i] != '/') {
end = i;
......@@ -38,5 +38,5 @@ char *basename(const char *path)
if (i == 0)
break;
}
return strndup(path + start, (start >= end) ? 0 : end - start);
return strndup(path + start, (start > end) ? 0 : end - start + 1);
}
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:36:53 2006 Seblu
** Last update Thu Aug 3 12:39:34 2006 Seblu
** Last update Sat Aug 19 01:41:32 2006 Seblu
*/
#include <stdio.h>
......@@ -21,18 +21,13 @@
static int lexer_reconize(ts_parser *parser);
/*!
** Set the current token for a parser
**
** @param p the parser
** @param t new token id
** @param s new token str
**
** @return token string representation
*/
static void set_current_token(ts_parser *p, te_tokenid t, const char *s);
static void lexer_eat(ts_parser *lex);
static ts_token token_create(te_tokenid id, const char *string);
ts_token tokens[] =
static void token_set(ts_token *token, te_tokenid id, const char *s);
ts_token operators[] =
{
{TOK_AND, "&&"},
{TOK_OR, "||"},
......@@ -44,6 +39,14 @@ ts_token tokens[] =
{TOK_LESSGREAT, "<>"},
{TOK_DLESSDASH, "<<-"},
{TOK_CLOBBER, ">|"},
{TOK_SEP, ";"},
{TOK_SEPAND, "&"},
{TOK_NEWLINE, "\n"},
{0, NULL}
};
ts_token keywords[] =
{
{TOK_IF, "if"},
{TOK_THEN, "then"},
{TOK_ELSE, "else"},
......@@ -60,34 +63,52 @@ ts_token tokens[] =
{TOK_LBRACE, "{"},
{TOK_RBRACE, "}"},
{TOK_BANG, "!"},
{TOK_SEP, ";"},
{TOK_SEPAND, "&"},
{TOK_NEWLINE, "\n"},
{0,0}
{0, NULL}
};