1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# pragma once
#include "../src/utils.h"
// Stage 1
typedef enum {
PLUS,
MINUS,
MULT,
DIV,
} BinOp;
typedef enum {
ARITHM_INT_LIT,
ARITHM_BINOP,
} ArithmNodeKind;
typedef struct ArithmNode ArithmNode;
struct ArithmNode {
ArithmNodeKind kind;
union {
int lit_val;
IntVec open_fds;
struct {
BinOp op;
ArithmNode* left;
ArithmNode* right;
} binop;
} as;
};
DECLARE_VEC(ArithmNode, ArithmNodeVec)
typedef struct {
ArithmNodeVec nodes;
ArithmNode* root;
} ArithmAst;
typedef enum {
UN_Q,
SINGLE_Q,
DOUBLE_Q,
} QuotationMode;
typedef enum {
CHUNK_LIT,
CHUNK_VAR,
CHUNK_CMDSUB,
CHUNK_ARITHM,
} ChunkKind;
typedef struct {
QuotationMode qmode;
ChunkKind kind;
union {
char* lit_str;
char* varname;
ArithmAst arithm_expr;
// TODO: CHUNK_CMDSUB
} as;
} Chunk;
DECLARE_VEC(Chunk, Word)
typedef enum {
TOK_WORD,
TOK_SPEC,
TOK_SUB_IN,
TOK_SUB_OUT,
} TokenKind;
typedef enum {
SPEC_PIPE,
SPEC_REDIR_W_TRUNC,
SPEC_REDIR_W_APPEND,
SPEC_REDIR_R,
SPEC_EOL,
} SpecialToken;
typedef struct Token Token;
DECLARE_VEC(Token*, TokenPtrVec)
struct Token {
TokenKind kind;
union {
Word word;
SpecialToken spec;
TokenPtrVec procsub_toks;
} as;
};
DECLARE_VEC(Token, TokenVec)
typedef struct {
TokenVec token_pool;
TokenPtrVec top_tokens;
} Ast;
// Stage 2
typedef enum {
REDIR_WRITE_TRUNC,
REDIR_WRITE_APPEND,
REDIR_READ,
REDIR_FDS,
} RedirectKind;
typedef struct {
RedirectKind kind;
union {
char* filename;
struct{int from; int to;} fds;
} as;
} Redirect;
DECLARE_VEC(Redirect, RedirectVec)
typedef struct PlCommand PlCommand;
typedef enum {
ARG_PS_IN,
ARG_PS_OUT,
ARG_LIT,
} ArgKind;
typedef struct {
ArgKind kind;
union {
char* lit_str;
PlCommand* sub_tree;
} as;
} Arg;
DECLARE_VEC(Arg, ArgVec)
typedef enum {
CONN_PIPE,
CONN_EOL,
} Connector;
struct PlCommand {
ArgVec args;
RedirectVec redirects;
Connector conn;
};
DECLARE_VEC(PlCommand, Pipeline)
// Stage 3
typedef struct {
int redirected;
int to;
} FdRedirect;
DECLARE_VEC(FdRedirect, FdRedirectVec)
typedef struct {
char** args;
FdRedirectVec redirects;
int stdin_fd, stdout_fd;
IntVec open_fds;
} ExecutableCommand;
|