summaryrefslogtreecommitdiff
path: root/preproc.c
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-18 16:23:26 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-18 16:23:26 +0200
commitca675175742174d6bfcd0263583f840e250fc58f (patch)
treecd11ec9f8345fa31dddb2a5f0c21d59bb19816b1 /preproc.c
parent24ba34266cc977b22d777bceca9683a45dd73042 (diff)
quicksaving
Diffstat (limited to 'preproc.c')
-rw-r--r--preproc.c69
1 files changed, 11 insertions, 58 deletions
diff --git a/preproc.c b/preproc.c
index c982636..1d9e921 100644
--- a/preproc.c
+++ b/preproc.c
@@ -298,68 +298,21 @@ TokenPtrVec lex(char* input, PreprocArena* arena) {
// Parsing
-Connector parse_connector(Token* toks, size_t* tok_pos) {
- Token tok = toks[*tok_pos];
- if (tok.kind != TOK_SPECIAL) exit(67);
- switch (tok.as.spec) {
- case PIPE:
- (*tok_pos)++;
- return CONN_PIPE;
- case EOL:
- (*tok_pos)++;
- return CONN_EOL;
- default: exit(76);
- }
-}
-
-
-Command parse_command(Token* toks, size_t* tok_pos) {
- TokenVec args = make_TokenVec(256);
- RedirectVec redirects = make_RedirectVec(256);
- while (true) {
- Token curr_tok = toks[*tok_pos];
- if (curr_tok.kind == TOK_SPECIAL &&(curr_tok.as.spec == PIPE || curr_tok.as.spec == EOL)) {
- break;
- }
-
- switch (curr_tok.kind) {
- case TOK_SPECIAL:
- (*tok_pos)++;
- Token name_tok = toks[*tok_pos];
- switch (curr_tok.as.spec) {
- case WRITE_REDIR_TRUNC:
- if(name_tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_TRUNC, .as.std_redir_filename = name_tok.as.word});
- break;
- case WRITE_REDIR_APPEND:
- if(name_tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = name_tok.as.word});
- break;
- case READ_REDIR:
- if(name_tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = name_tok.as.word});
- break;
- default:
- exit(69);
- }
- break;
- default:
- push_to_TokenVec(&args, curr_tok);
- }
- (*tok_pos)++;
- }
- return (Command){.args = args, .redirects = redirects};
-}
-
-
Pipeline parse_tokstream(Token* toks) {
size_t tok_pos = 0;
Pipeline result = make_Pipeline(256);
+ TokenVec tok_buf = make_TokenVec(256);
while (true) {
- Command cmd = parse_command(toks, &tok_pos);
- Connector conn = parse_connector(toks, &tok_pos);
- push_to_Pipeline(&result, (PipelineElement){.cmd = cmd, .conn = conn});
- if (conn == CONN_EOL) break;
+ Token curr_tok = toks[tok_pos++];
+ if (curr_tok.kind == TOK_SPECIAL && curr_tok.as.spec == PIPE) {
+ push_to_Pipeline(&result, (PipelineElement){.cmd_toks = tok_buf, .conn = CONN_PIPE});
+ tok_buf = make_TokenVec(256);
+ } else if (curr_tok.kind == TOK_SPECIAL && curr_tok.as.spec == EOL) {
+ push_to_Pipeline(&result, (PipelineElement){.cmd_toks = tok_buf, .conn = CONN_EOL});
+ break;
+ } else {
+ push_to_TokenVec(&tok_buf, curr_tok);
+ }
}
return result;
}