From cbc7fa34f39648ceb71496172aec2dfdf75cab11 Mon Sep 17 00:00:00 2001 From: sfja Date: Wed, 16 Sep 2026 00:21:40 +0200 Subject: [PATCH] delete old --- lex.c | 111 -------------------------------------------------------- lex.h | 42 --------------------- parse.c | 64 -------------------------------- parse.h | 4 -- 4 files changed, 221 deletions(-) delete mode 100644 lex.c delete mode 100644 lex.h delete mode 100644 parse.c delete mode 100644 parse.h diff --git a/lex.c b/lex.c deleted file mode 100644 index aeb0333..0000000 --- a/lex.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "lex.h" -#include -#include - -static bool done(Lexer* l) -{ - return l->text[l->idx] != '\0'; -} - -static Tok tok(Lexer* l, TokTy ty, Loc loc) -{ - return (Tok) { - .ty = ty, - .loc = loc, - .len = l->idx - loc.idx, - }; -} - -static void step(Lexer* l) -{ - if (done(l)) { - return; - } - - if (l->ch == '\n') { - l->line += 1; - l->col = 1; - } else { - l->col += 1; - } - l->idx += 1; - l->ch = l->text[l->idx]; -} - -static TokTy match_keyword(const char* text, size_t len) -{ - if (strncmp("if", text, len)) - return Tok_If; - return Tok_Ident; -} - -Tok lexer_next(Lexer* l) -{ - Loc loc = { l->idx, l->line, l->col }; - if (done(l)) { - return tok(l, Tok_Eof, loc); - } - bool matched = false; - while (strchr(" \t\r", l->ch)) { - matched = true; - step(l); - } - if (matched) { - return lexer_next(l); - } - while ((l->ch >= 'a' && l->ch <= 'z') || (l->ch >= 'A' && l->ch <= 'Z') - || (matched && l->ch >= '0' && l->ch <= '9') || strchr("_", l->ch)) { - matched = true; - step(l); - } - if (matched) { - return tok(l, match_keyword(&l->text[loc.idx], l->idx - loc.idx), loc); - } - while ((l->ch >= '1' && l->ch <= '9') || (matched && l->ch == '0')) { - matched = true; - step(l); - } - if (matched) { - return tok(l, Tok_Int, loc); - } - if (l->ch == '"') { - step(l); - while (l->ch != '"') { - if (l->ch == '\\') { - step(l); - if (done(l)) - break; - } - step(l); - } - if (done(l) || l->ch != '"') { - fprintf(stderr, "error: malformed string literal\n"); - return lexer_next(l); - } - step(l); - return tok(l, Tok_Str, loc); - } - if (l->ch == '\'') { - step(l); - if (l->ch == '\\') { - step(l); - } - if (!done(l)) { - step(l); - } - if (done(l) && l->ch != '\'') { - fprintf(stderr, "error: malformed character literal\n"); - return lexer_next(l); - } - step(l); - return tok(l, Tok_Char, loc); - } - if (strchr("\n(){}", l->ch)) { - TokTy ty = (TokTy)l->ch; - step(l); - return tok(l, ty, loc); - } - fprintf(stderr, "error: illegal character '%c'\n", l->ch); - step(l); - return lexer_next(l); -} diff --git a/lex.h b/lex.h deleted file mode 100644 index 4c4c634..0000000 --- a/lex.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef LEX_H -#define LEX_H - -#include - -typedef struct { - size_t idx; - int line; - int col; -} Loc; - -typedef enum { - Tok_Eof, - Tok_Ident, - Tok_Int, - Tok_Str, - Tok_Char, - Tok_If, - Tok_Nl = '\n', - Tok_LParen = '(', - Tok_RParen = ')', - Tok_LBrace = '{', - Tok_RBrace = '}', -} TokTy; - -typedef struct { - TokTy ty; - Loc loc; - size_t len; -} Tok; - -typedef struct { - const char* text; - size_t idx; - int line; - int col; - char ch; -} Lexer; - -Tok lexer_next(Lexer* l); - -#endif diff --git a/parse.c b/parse.c deleted file mode 100644 index 9e484c3..0000000 --- a/parse.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "parse.h" -#include - -// Using this as a reference: -// https://en.cppreference.com/c/language/translation_phases - -typedef struct { - int line; - int col; -} Loc; - -// Phase 1 already happens when the file is read. - -typedef struct { - Loc loc; - char ch; -} CannonicalChar; - -typedef struct { - const char* text; - size_t idx; - Loc loc; -} NewlineCanonicalizer; - -CannonicalChar next_cannonical_char(NewlineCanonicalizer* iter) -{ - Loc loc = iter->loc; - char ch = iter->text[iter->idx]; - if (ch == '\0') - return (CannonicalChar) { loc, ch }; - do { - if (ch == '\n') { - - } else { - } - iter->loc.col += 1; - - } while (iter->text[iter->idx]); -} - -// Phase 2 is an iterator that handles escaped newlines. - -typedef struct { - /// when this is '\0', it means EOF - char ch; - int line; - int col; -} Char; - -typedef struct { - const char* text; - size_t idx; - int line; - int col; -} CharIter; - -static Char next_src_char(CharIter* iter) -{ - if (!iter->text[iter->idx]) - return (Char) { .ch = '\0', iter->line, iter->col }; - char ch = iter->text[iter->idx]; - iter->idx += 1; - while (ch == '\\' &&) -} diff --git a/parse.h b/parse.h deleted file mode 100644 index 30c06e4..0000000 --- a/parse.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef PARSE_H -#define PARSE_H - -#endif