diff options
| -rw-r--r-- | exec.c | 9 | ||||
| -rw-r--r-- | exec.h | 2 | ||||
| -rw-r--r-- | lex.h | 30 | ||||
| -rw-r--r-- | main.c | 3 | ||||
| -rw-r--r-- | parse.c | 72 | ||||
| -rw-r--r-- | preproc.c (renamed from lex.c) | 73 | ||||
| -rw-r--r-- | preproc.h (renamed from parse.h) | 19 |
7 files changed, 96 insertions, 112 deletions
@@ -1,8 +1,3 @@ -#include "lex.h" -#include "parse.h" -#include "exec.h" -#include "utils.h" - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -10,6 +5,10 @@ #include <signal.h> #include <sys/wait.h> +#include "utils.h" +#include "exec.h" +#include "preproc.h" + char* get_var(char* varname, ShellState* shstate) { MaybeStr maybe_varval = get_map_value(&shstate->shell_vars, varname); @@ -1,7 +1,7 @@ #pragma once #include "utils.h" -#include "parse.h" +#include "preproc.h" typedef struct { @@ -1,30 +0,0 @@ -#pragma once - -#include "defs.h" -#include "utils.h" - -DECLARE_VEC(Chunk, ChunkVec) -DECLARE_VEC(Token, TokenVec) - -typedef struct { - RegionVec regions; - TokenVec tokens; - ChunkVec chunks; -} PreprocArena; - - -typedef struct { - TokenVec top_tokens; - RegionVec reg_arena; -} LexResult; - - -TokenPtrVec lex(char* input, PreprocArena* arena); - - - - - - - - @@ -4,8 +4,7 @@ #include <string.h> #include "defs.h" -#include "lex.h" -#include "parse.h" +#include "preproc.h" #include "exec.h" diff --git a/parse.c b/parse.c deleted file mode 100644 index 2cf74c6..0000000 --- a/parse.c +++ /dev/null @@ -1,72 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> - -#include "lex.h" -#include "parse.h" - - -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); - 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; - } - return result; -} @@ -5,7 +5,7 @@ #include <stdbool.h> #include <stdio.h> -#include "lex.h" +#include "preproc.h" #include "utils.h" #include "defs.h" @@ -292,3 +292,74 @@ TokenPtrVec lex(char* input, PreprocArena* arena) { push_to_TokenPtrVec(&toks, eol_ptr); return toks; } + + + + +// 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); + 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; + } + return result; +} @@ -1,8 +1,23 @@ #pragma once -#include "lex.h" +#include "defs.h" #include "utils.h" +DECLARE_VEC(Chunk, ChunkVec) +DECLARE_VEC(Token, TokenVec) + +typedef struct { + RegionVec regions; + TokenVec tokens; + ChunkVec chunks; +} PreprocArena; + + +TokenPtrVec lex(char* input, PreprocArena* arena); + + +// Parsing + typedef enum { STDIN_REDIR, STDOUT_REDIR_TRUNC, @@ -51,3 +66,5 @@ Pipeline parse_tokstream(Token* tokstream); + + |
