summaryrefslogtreecommitdiff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/parse.c b/parse.c
new file mode 100644
index 0000000..b025d08
--- /dev/null
+++ b/parse.c
@@ -0,0 +1,66 @@
+typedef struct {
+ LineNo line;
+ LineNo col;
+} Location;
+
+typedef struct {
+ Location where;
+ char *var;
+} Declaration;
+
+arr_declaration(Declarations, Declaration, decls_)
+
+typedef struct {
+ int type;
+ Location where;
+ union {
+ Declarations decls;
+ };
+} Statement;
+
+arr_declaration(Statements, Statement, stmts_)
+
+typedef struct {
+ Statements stmts;
+} ParsedFile;
+
+/* TODO: Add newline tokens back in; give tokens pointer to text */
+static bool parse_decls(Declarations *ds, Tokenizer *t) {
+ if (t->token->kind != TOKEN_IDENT) {
+ tokr_err(t, "Cannot declare non-identifier.");
+ return false;
+ }
+ t->token++;
+ return true;
+}
+
+static bool parse_stmt(Statement *s, Tokenizer *t) {
+ if (token_is_kw(t->token + 1, KW_COLON)) {
+ return parse_decls(&s->decls, t);
+ } else {
+ t->token++; /* TODO: This is temporary */
+ }
+ return true;
+}
+
+static bool parse_file(ParsedFile *f, Tokenizer *t) {
+ stmts_create(&f->stmts);
+ bool ret = true;
+ while (t->token->kind != TOKEN_EOF) {
+ Statement stmt = {0};
+ if (!parse_stmt(&stmt, t))
+ ret = false;
+ stmts_add(&f->stmts, &stmt);
+ }
+ return ret;
+}
+
+static void stmt_fprint(FILE *out, Statement *s) {
+ fprintf(out, "statement!\n");
+}
+
+static void parsed_file_fprint(FILE *out, ParsedFile *f) {
+ arr_foreach(f->stmts, Statement, stmt) {
+ stmt_fprint(out, stmt);
+ }
+}