From 26a4153f017f51d79ba4f5ceddd5345c50e4e1ef Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 6 Feb 2023 12:30:47 +0100 Subject: [PATCH] init Author: Simon --- .clang-format | 9 ++++ .gitignore | 5 ++ Makefile | 25 +++++++++ main.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.o | Bin 0 -> 5504 bytes 5 files changed, 179 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c create mode 100644 main.o diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..af281a7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: WebKit +IndentWidth: 4 +ColumnLimit: 80 +IndentCaseLabels: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: true + SplitEmptyFunction: false + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03917ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ + +compile_flags.txt +cli +*.o + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..53b79df --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ + +CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion +LFLAGS = -lm + +SRC = $(wildcard *.c) +OBJ = $(patsubst %.c, %.o, $(SRC)) + +HEADERS = $(wildcard *.h) + +TARGET = cli + +all: compile_flags.txt$(TARGET) + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LFLAGS) + +%.o: %.c $(HEADERS) + gcc $< -c -o $@ $(CFLAGS) + +clean: + $(RM) *.o $(TARGET) + +compile_flags.txt: + echo -xc $(CPP_FLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt + diff --git a/main.c b/main.c new file mode 100644 index 0000000..13f5f5d --- /dev/null +++ b/main.c @@ -0,0 +1,140 @@ +#include +#include +#include + +typedef enum { + TokenTypeEof, + TokenTypeNewline, + TokenTypeSemicolon, + TokenTypePlus, + TokenTypeInt, + TokenTypeId, +} TokenType; + +typedef struct { + TokenType type; + size_t index, length; + int line, column; +} Token; + +typedef struct { + size_t index; + int line, column; +} LexerPosition; + +typedef struct { + const char* text; + size_t length; + LexerPosition position; +} Lexer; + +void construct_lexer(Lexer* self, const char* text, size_t length) +{ + *self = (Lexer) { + .text = text, + .length = length, + .position = { + .index = 0, + .line = 1, + .column = 1, + }, + }; +} + +bool is_whitespace_char(char c) { return c == ' ' || c == '\t'; } +bool is_int_char(char c) { return c >= '0' && c <= '9'; } +bool is_id_char(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' + || is_int_char(c); +} + +bool lexer_done(const Lexer* self) +{ + return self->position.index >= self->length; +} + +void lexer_step(Lexer* self) +{ + if (self->text[self->position.index] == '\n') { + self->position.line += 1; + self->position.column = 1; + } else { + self->position.column += 1; + } + self->position.index += 1; +} + +char lexer_current(const Lexer* self) +{ + return self->text[self->position.index]; +} + +LexerPosition lexer_position(const Lexer* self) { return self->position; } + +Token lexer_token_from(const Lexer* self, TokenType type, LexerPosition start) +{ + return (Token) { + .type = type, + .index = start.index, + .length = self->position.index, + .line = start.line, + .column = start.column, + }; +} + +void lexer_skip_whitespace(Lexer* self) +{ + lexer_step(self); + while (!lexer_done(self) && is_whitespace_char(lexer_current(self))) + lexer_step(self); +} + +Token lexer_make_int_token(Lexer* self) +{ + LexerPosition start = self->position; + lexer_step(self); + while (!lexer_done(self) && is_int_char(lexer_current(self))) + lexer_step(self); + return lexer_token_from(self, TokenTypeInt, start); +} + +Token lexer_make_id_token(Lexer* self) +{ + LexerPosition start = self->position; + lexer_step(self); + while (!lexer_done(self) && is_id_char(lexer_current(self))) + lexer_step(self); + return lexer_token_from(self, TokenTypeId, start); +} + +Token lexer_make_static_token(Lexer* self) +{ + switch (lexer_current(self)) { + case '\n': + + default: + printf("unrecognized char '%c'\n", lexer_current(self)); + exit(EXIT_FAILURE); + } +} + +Token lexer_make_token(Lexer* self) +{ + if (lexer_done(self)) { + return lexer_token_from(self, TokenTypeEof, lexer_position(self)); + } else if (is_whitespace_char(lexer_current(self))) { + lexer_skip_whitespace(self); + return lexer_make_token(self); + } else if (is_int_char(lexer_current(self))) { + return lexer_make_int_token(self); + } else if (is_id_char(is_int_char(lexer_current(self)))) { + return lexer_make_id_token(self); + } else { + return lexer_make_static_token(self); + }; +} + +Token lexer_next(Lexer* self) { return lexer_make_token(self); } + +int main() { printf("hello world\n"); } diff --git a/main.o b/main.o new file mode 100644 index 0000000000000000000000000000000000000000..0ed07d0ba292817b0045de3c9dfdb960492406de GIT binary patch literal 5504 zcmd5raD*b}UsRwrdg#twqZE6CE*@)DjV^tef45S(EH8v$Hi? zje&?bES8i~=z~R}Pzrs~KG9J@%dU41GB!>^(LRh^e>{k3l2(e2jDP_7ZZ zaOyJno6rl{aAD6#q2oHNCbt(_?{2qcb#%MmDW09holV+0Mn7q{W0;fV0P^fcMwy(h z1^$dqD^3qR3P74ya42+R)Ga`MY?x9$^sou6%FvT~Vb1DCC=kRw(>w~g5V5mIK&D_P zMTv3e(1K1;Ij7{@WtTe#;YsMM#Gd|cmpyZBj`Z!?n@Q^Hoq(*lvT;!De;D@M ze-d_B*3iyBg3UZ)$#ZGU)NTDd8ihiP$yZYD{wsV(C55l>oABN>`cwZh>UpWR(ux%BM{u zkvWo1oiLJGqC0MCYgZ@ME)?BHZ*N9BmN9#iLhRhO?NzOD=j$EowC0vbOQczAX=>Tn z)ZE-G@CDG|!Qz8}2#UZcVrm3vjh%pFsWXYdiM=Aw9}cXnonJi+%m%>m!O@04TNZ`S z2DjDC3+@C7hqoDV7;ErH@#B&$bq^vAqZ0q5!xp=7R{FsmuKf$d*Es*JlKtz%Kf?LJ zPFMdv@t@`V110@g;xFg?H%feY@B=T-`N4y({mqobePw*dKm%Y0iT_0z|8rOWTpJzE{t@yUs4_?V_Qa1@ z<^M$@s$@5oYd{OZrwRoCw2TpE+&AKCEqe#B^@N)+<52j^K%@MJ5*1D_0lk&*2ZU=h z?;*U3T+7}8Y(L>g370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