diff options
Diffstat (limited to 'syntax.y')
-rw-r--r-- | syntax.y | 42 |
1 files changed, 23 insertions, 19 deletions
@@ -1,6 +1,7 @@ | |||
1 | %{ | 1 | %{ |
2 | #include "cmd.h" | 2 | #include "cmd.h" |
3 | #include "lex.yy.c" | 3 | #include "lex.yy.c" |
4 | #include <stdio.h> | ||
4 | %} | 5 | %} |
5 | 6 | ||
6 | %union { | 7 | %union { |
@@ -9,23 +10,34 @@ | |||
9 | CommandType cmdType; | 10 | CommandType cmdType; |
10 | } | 11 | } |
11 | 12 | ||
12 | %token <str> WORD STRING | 13 | %token <str> WORD STRING error |
13 | %token NEWLINE PIPE AND OR | 14 | %token NEWLINE PIPE AND OR |
14 | 15 | ||
15 | %type <cmd> line command part | 16 | %type <cmd> line command part runcommand |
16 | %type <cmdType> separator | 17 | %type <cmdType> separator |
17 | 18 | ||
18 | %% | 19 | %% |
19 | 20 | ||
20 | line: | 21 | line: |
21 | command NEWLINE { $$ = $1; runcmd($$); } | 22 | runcommand line { /* empty */ } |
23 | | /* empty */ { $$ = NULL; } | ||
24 | ; | ||
25 | |||
26 | runcommand: | ||
27 | command NEWLINE { | ||
28 | $$ = $1; | ||
29 | printf("running command: \n"); | ||
30 | runcmd($$); | ||
31 | } | ||
22 | | NEWLINE { $$ = NULL; } | 32 | | NEWLINE { $$ = NULL; } |
23 | ; | 33 | ; |
24 | 34 | ||
25 | command: | 35 | command: |
26 | part { $$ = $1; } | 36 | part { |
37 | $$ = $1; | ||
38 | } | ||
27 | | part separator command { | 39 | | part separator command { |
28 | $$ = malloc(sizeof(Command)); | 40 | $$ = newcmd(); |
29 | $$->type = $2; | 41 | $$->type = $2; |
30 | $$->left = $1; | 42 | $$->left = $1; |
31 | $$->right = $3; | 43 | $$->right = $3; |
@@ -39,7 +51,6 @@ part: | |||
39 | $$->args = realloc($$->args, ($$->argc) * sizeof(char *)); | 51 | $$->args = realloc($$->args, ($$->argc) * sizeof(char *)); |
40 | $$->args[$$->argc - 1] = $2; | 52 | $$->args[$$->argc - 1] = $2; |
41 | $$->args[$$->argc] = NULL; | 53 | $$->args[$$->argc] = NULL; |
42 | printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); | ||
43 | } | 54 | } |
44 | | part STRING { | 55 | | part STRING { |
45 | $$ = $1; | 56 | $$ = $1; |
@@ -49,23 +60,16 @@ part: | |||
49 | $$->args[$$->argc] = NULL; | 60 | $$->args[$$->argc] = NULL; |
50 | } | 61 | } |
51 | | WORD { | 62 | | WORD { |
52 | $$ = malloc(sizeof(Command)); | 63 | $$ = newcmd(); |
53 | $$->type = CMD_TYPE_NORMAL; | ||
54 | $$->args = malloc(2 * sizeof(char *)); | 64 | $$->args = malloc(2 * sizeof(char *)); |
55 | $$->args[0] = $1; | 65 | $$->args[$$->argc++] = $1; |
56 | $$->args[1] = NULL; | 66 | $$->args[$$->argc] = NULL; |
57 | $$->argc = 1; | ||
58 | printf("word[%d] = %s\n", $$->argc-1,$$->args[$$->argc-1]); | ||
59 | $$->left = $$->right = NULL; | ||
60 | } | 67 | } |
61 | | STRING { | 68 | | STRING { |
62 | $$ = malloc(sizeof(Command)); | 69 | $$ = newcmd(); |
63 | $$->type = CMD_TYPE_NORMAL; | ||
64 | $$->args = malloc(2 * sizeof(char *)); | 70 | $$->args = malloc(2 * sizeof(char *)); |
65 | $$->args[0] = $1; | 71 | $$->args[$$->argc++] = $1; |
66 | $$->args[1] = NULL; | 72 | $$->args[$$->argc] = NULL; |
67 | $$->argc = 1; | ||
68 | $$->left = $$->right = NULL; | ||
69 | } | 73 | } |
70 | ; | 74 | ; |
71 | 75 | ||