Skip to content

Commit

Permalink
Merge pull request #63 from ErwannLesech/variable-part2.3
Browse files Browse the repository at this point in the history
fix<variable>: variable implemented (not full test)
  • Loading branch information
Nimu93 authored Jan 17, 2024
2 parents b0bf768 + 8d8be2a commit 247126d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/execute/ast_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int exec_cmd(struct ast_node *node, bool logger_enabled)
logger("cmd: ", LOGGER_EXEC, logger_enabled);
for (int i = 0; i < node->children_count; i++)
{
args[i] = node->children[i]->value;
args[i] = handle_word(node->children[i]);
//printf("%s ", args[i]);
logger(args[i], LOGGER_EXEC, logger_enabled);
}
Expand Down
13 changes: 8 additions & 5 deletions src/execute/ast_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ void init_variables()
{
if (variables == NULL)
{
printf("init_variables\n");
variables = hash_map_init(100);
}
}
Expand All @@ -28,22 +27,28 @@ void set_variable(char *key, char *value)

char *get_variable(char *key)
{
key++;
//gerer les variables envirovment
init_variables();
if (variables == NULL)
{
return NULL;
}
size_t index = hash(key) % variables->size;
if (variables->data[index] == NULL)
return NULL;
return "";
struct pair_list *ind = variables->data[index];
if (strcmp(ind->key, key) == 0)
{
return ind->value;
}
while (ind->next != NULL)
{
if (strcmp(ind->key, key) == 0)
return ind->value;
ind = ind->next;
}
return "\n";
return "";
}

int ast_eval_assignment(struct ast_node *node, bool logger_enabled)
Expand All @@ -54,8 +59,6 @@ int ast_eval_assignment(struct ast_node *node, bool logger_enabled)
}
char *key = node->children[0]->value;
char *value = node->children[1]->value;
//printf("key: %s\n", key);
//printf("value: %s\n", value);
set_variable(key, value);
return 0;
}
Expand Down
28 changes: 14 additions & 14 deletions src/execute/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,35 @@ void print_echo(struct ast_node *node, int enable_escapes, int j)
{
for (int i = j; i < node->children_count; i++)
{
for (size_t k = 0; k < strlen(node->children[i]->value); k++)
for (size_t k = 0; k < strlen(handle_word(node->children[i])); k++)
{
if (enable_escapes && (node->children[i]->value)[k] == '\\')
if (enable_escapes && (handle_word(node->children[i]))[k] == '\\')
{
if ((node->children[i]->value)[k + 1] == 'n')
if ((handle_word(node->children[i]))[k + 1] == 'n')
{
putchar('\n');
k++;
}
else if ((node->children[i]->value)[k + 1] == 't')
else if ((handle_word(node->children[i]))[k + 1] == 't')
{
putchar('\t');
k++;
}
else if ((node->children[i]->value)[k + 1] == '\\')
else if ((handle_word(node->children[i]))[k + 1] == '\\')
{
putchar('\\');
k++;
}
else
{
putchar('\\');
putchar((node->children[i]->value)[k]);
putchar((handle_word(node->children[i]))[k]);
k++;
}
}
else
{
putchar((node->children[i]->value)[k]);
putchar((handle_word(node->children[i]))[k]);
}
}
if (i != node->children_count - 1)
Expand All @@ -74,20 +74,20 @@ int echo_fun(struct ast_node *node)
int j = 1;
for (int i = 1; i < node->children_count; i++)
{
if (node->children[i]->value[0] == '-')
if (strlen(handle_word(node->children[i])) >= 1 && handle_word(node->children[i])[0] == '-')
{
if (strspn(node->children[i]->value, "-neE")
!= strlen(node->children[i]->value))
if (strspn(handle_word(node->children[i]), "-neE")
!= strlen(handle_word(node->children[i])))
{
goto DEFAULT;
}
for (size_t k = 1; k < strlen(node->children[i]->value); k++)
for (size_t k = 1; k < strlen(handle_word(node->children[i])); k++)
{
if (node->children[i]->value[k] == 'n')
if (handle_word(node->children[i])[k] == 'n')
no_newline = 1;
else if (node->children[i]->value[k] == 'e')
else if (handle_word(node->children[i])[k] == 'e')
enable_escapes = 1;
else if (node->children[i]->value[k] == 'E')
else if (handle_word(node->children[i])[k] == 'E')
enable_escapes = 0;
else
{
Expand Down
1 change: 1 addition & 0 deletions tests/variable/variable/condition_variable1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if a=4; then echo $a; fi
1 change: 1 addition & 0 deletions tests/variable/variable/simple_variable1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=5 ; echo $a
1 change: 1 addition & 0 deletions tests/variable/variable/simple_variable2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=5 ; b=6; echo '$a $b'
1 change: 1 addition & 0 deletions tests/variable/variable/simple_variable3.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=5 ; a=6; echo '$a $b'
5 changes: 5 additions & 0 deletions tests/variable/variable/testsuite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run_test simple_variable1.test
run_test simple_variable2.test
run_test simple_variable3.test
run_test condition_variable1.test

3 changes: 3 additions & 0 deletions tests/variable/variable_error/testsuite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


run_test while_variable_error1.test
1 change: 1 addition & 0 deletions tests/variable/variable_error/while_variable_error1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=0; while $a do echo $a; a=1; done

0 comments on commit 247126d

Please sign in to comment.