summaryrefslogtreecommitdiff
path: root/src/syntax.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax.h')
-rw-r--r--src/syntax.h194
1 files changed, 194 insertions, 0 deletions
diff --git a/src/syntax.h b/src/syntax.h
new file mode 100644
index 0000000..0cdb76c
--- /dev/null
+++ b/src/syntax.h
@@ -0,0 +1,194 @@
+# 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,
+} TokenKind;
+
+typedef enum {
+ OP_PIPE,
+ OP_REDIR_W_TRUNC,
+ OP_REDIR_W_APPEND,
+ OP_REDIR_R,
+ OP_EOL,
+} OperatorToken;
+
+
+typedef enum {
+ SPEC_OP,
+ SPEC_SUB_IN,
+ SPEC_SUB_OUT,
+} SpecialTokenKind;
+
+typedef struct {
+ SpecialTokenKind kind;
+ union {
+ OperatorToken op;
+ void* proc_sub_region;
+ } as;
+} SpecialToken;
+
+
+typedef struct Token Token;
+DECLARE_VEC(Token*, TokenPtrVec)
+
+struct Token {
+ TokenKind kind;
+ union {
+ Word word;
+ SpecialToken spec;
+ } as;
+};
+
+DECLARE_VEC(Token, TokenVec)
+DECLARE_VEC(TokenVec, TokenVecVec)
+
+typedef struct {
+ TokenVecVec region_pool;
+ TokenVec* root;
+} 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;
+ void* sub_pipeline;
+ } 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)
+DECLARE_VEC(Pipeline, PipelineVec)
+
+typedef struct {
+ PipelineVec pl_pool;
+ Pipeline* root;
+} PipelineTree;
+
+
+
+// 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;