summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-12 14:50:55 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-12 14:50:55 +0200
commitce7bc08b1db6011a82b27dbccf4e5502b948a820 (patch)
tree073bd967923541e35b5a0a43a0b9ea0b080025ee
parent7519eafc6787cc71dcdc252584fdfd081f4698ad (diff)
mostly finish writing the parser, split its defs into header
-rw-r--r--lex.c1
-rw-r--r--parse.c114
-rw-r--r--parse.h48
3 files changed, 95 insertions, 68 deletions
diff --git a/lex.c b/lex.c
index d7641a4..3689432 100644
--- a/lex.c
+++ b/lex.c
@@ -275,5 +275,6 @@ LexResult lex(char* input) {
RawLine line = {.str = input, .pos = 0};
RegionVec reg_arena = make_RegionVec(256);
TokenVec toks = lex_level(&line, '\0', &reg_arena);
+ push_to_TokenVec(&toks, (Token){.kind = TOK_SPECIAL, .as.spec = EOL});
return (LexResult){.top_tokens = toks, .reg_arena = reg_arena};
}
diff --git a/parse.c b/parse.c
index 87af679..b20b8dc 100644
--- a/parse.c
+++ b/parse.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
-#include "typeutils.h"
#include "lex.h"
-
+#include "typeutils.h"
+#include "parse.h"
DECLARE_VEC(char*, StrVec)
@@ -12,82 +12,55 @@ typedef struct {
size_t pos;
} TokStream;
-typedef enum {
- STDIN_REDIR,
- STDOUT_REDIR_TRUNC,
- STDOUT_REDIR_APPEND,
-} RedirKind;
-
-
-typedef struct {
- RedirKind kind;
-
- union {
- Word std_redir_filename;
- } as;
-} Redirect;
-DECLARE_VEC(Redirect, RedirectVec)
-
-
-typedef struct {
- WordVec args;
- RedirectVec redirects;
- // TODO: prefix variable settings
-} Command;
-
-
-typedef enum {
- CONN_PIPE,
- CONN_EOL,
- // TODO: add the |& thingy
-} Connector;
-
-
-typedef struct {
- Command cmd;
- Connector conn;
-} PipelineElement;
-
-
-DECLARE_VEC(PipelineElement, Pipeline)
+Connector parse_connector(TokStream* toks) {
+ Token tok = toks->ptr[toks->pos];
+ if (tok.kind != TOK_SPECIAL) exit(67);
+ switch (tok.as.spec) {
+ case PIPE: return CONN_PIPE;
+ case EOL: return CONN_EOL;
+ default: exit(76);
+ }
+}
Command parse_command(TokStream* toks) {
- WordVec args = make_WordVec(256);
+ TokenVec args = make_TokenVec(256);
RedirectVec redirects = make_RedirectVec(256);
for (
Token curr_tok;
(curr_tok = toks->ptr[toks->pos], !(curr_tok.kind == TOK_SPECIAL && (curr_tok.as.spec == PIPE && curr_tok.as.spec == EOL)));
toks->pos++
) {
- if (curr_tok.kind == TOK_SPECIAL) {
- Token tok;
- switch (curr_tok.as.spec) {
- case WRITE_REDIR_TRUNC:
- toks->pos++;
- tok = toks->ptr[toks->pos];
- if(tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_TRUNC, .as.std_redir_filename = tok.as.word});
- break;
- case WRITE_REDIR_APPEND:
- toks->pos++;
- tok = toks->ptr[toks->pos];
- if(tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = tok.as.word});
- break;
- case READ_REDIR:
- toks->pos++;
- tok = toks->ptr[toks->pos];
- if(tok.kind != TOK_WORD) exit(1);
- push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = tok.as.word});
- break;
- default:
- exit(69);
- }
- } else if (curr_tok.kind == TOK_WORD){
- push_to_WordVec(&args, curr_tok.as.word);
- }
+ switch (curr_tok.kind) {
+ case TOK_SPECIAL:
+ Token tok;
+ switch (curr_tok.as.spec) {
+ case WRITE_REDIR_TRUNC:
+ toks->pos++;
+ tok = toks->ptr[toks->pos];
+ if(tok.kind != TOK_WORD) exit(1);
+ push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_TRUNC, .as.std_redir_filename = tok.as.word});
+ break;
+ case WRITE_REDIR_APPEND:
+ toks->pos++;
+ tok = toks->ptr[toks->pos];
+ if(tok.kind != TOK_WORD) exit(1);
+ push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = tok.as.word});
+ break;
+ case READ_REDIR:
+ toks->pos++;
+ tok = toks->ptr[toks->pos];
+ if(tok.kind != TOK_WORD) exit(1);
+ push_to_RedirectVec(&redirects, (Redirect){.kind = STDOUT_REDIR_APPEND, .as.std_redir_filename = tok.as.word});
+ break;
+ default:
+ exit(69);
+ }
+ break;
+ default:
+ push_to_TokenVec(&args, curr_tok);
+ }
}
return (Command){.args = args, .redirects = redirects};
}
@@ -96,4 +69,9 @@ Command parse_command(TokStream* toks) {
Pipeline parse_tokstream(TokenVec toks) {
TokStream tokstream = {.ptr = toks.items, .len = toks.count, .pos = 0};
Pipeline result = make_Pipeline(256);
+ while (true) {
+ Command cmd = parse_command(&tokstream);
+ Connector conn = parse_connector(&tokstream);
+ push_to_Pipeline(&result, (PipelineElement){.cmd = cmd, .conn = conn});
+ }
}
diff --git a/parse.h b/parse.h
new file mode 100644
index 0000000..8f95b44
--- /dev/null
+++ b/parse.h
@@ -0,0 +1,48 @@
+#pragma once
+
+#include "lex.h"
+#include "typeutils.h"
+
+typedef enum {
+ STDIN_REDIR,
+ STDOUT_REDIR_TRUNC,
+ STDOUT_REDIR_APPEND,
+} RedirKind;
+
+
+typedef struct {
+ RedirKind kind;
+
+ union {
+ Word std_redir_filename;
+ } as;
+} Redirect;
+
+
+DECLARE_VEC(Redirect, RedirectVec)
+
+
+typedef struct {
+ TokenVec args;
+ RedirectVec redirects;
+ // TODO: prefix variable settings
+} Command;
+
+
+typedef enum {
+ CONN_PIPE,
+ CONN_EOL,
+ // TODO: add the |& thingy
+} Connector;
+
+DECLARE_MAYBE(Connector)
+
+typedef struct {
+ Command cmd;
+ Connector conn;
+} PipelineElement;
+
+
+DECLARE_VEC(PipelineElement, Pipeline)
+
+