commit b5270924ba9e2bee9831bf210ad98aeb91b6e34f Author: SimonFJ20 Date: Tue Apr 2 17:21:48 2024 +0200 init diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8929d9b --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +Language: Cpp +BasedOnStyle: WebKit +IndentWidth: 4 +ColumnLimit: 80 +IndentCaseLabels: true + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2960771 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +*.o +semos + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aaa244b --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ + +OUTPUT = semos + +CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion -pedantic -pedantic-errors + +CFILES = \ + main.c \ + +OBJECT_FILES = $(patsubst %.c,%.o,$(CFILES)) + +HEADER_FILES = $(wildcard *.h) + +$(OUTPUT): $(OBJECT_FILES) + gcc -o $@ $^ + +%.o: %.c $(HEADER_FILES) + gcc -c -o $@ $(CFLAGS) $< + +.PHONY: clean + +clean: + rm -rf *.o $(OUTPUT) + diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..91ffc4f --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,9 @@ +-xc +-std=c17 +-Wall +-Wextra +-Wpedantic +-Wconversion +-pedantic +-pedantic-errors + diff --git a/main.c b/main.c new file mode 100644 index 0000000..50c765e --- /dev/null +++ b/main.c @@ -0,0 +1,3 @@ +#include + +int main(void) { printf("hello world\n"); } diff --git a/parser.h b/parser.h new file mode 100644 index 0000000..417c1b5 --- /dev/null +++ b/parser.h @@ -0,0 +1,167 @@ +#ifndef PARSER_H +#define PARSER_H + +#include + +typedef enum { + TokenType_Error, + TokenType_Id, + TokenType_Int, + TokenType_Not, + TokenType_And, + TokenType_Or, + TokenType_Loop, + TokenType_Fn, + TokenType_Break, + TokenType_LParen, + TokenType_RParen, + TokenType_LBrace, + TokenType_RBrace, + TokenType_LBracket, + TokenType_RBracket, + TokenType_Comma, + TokenType_Semicolon, + TokenType_Exclamation, + TokenType_Plus, + TokenType_Minus, + TokenType_Asterisk, + TokenType_EE, + TokenType_NE, + TokenType_LT, + TokenType_GT, + TokenType_LTE, + TokenType_GTE, +} TokenType; + +typedef struct { + TokenType token; + size_t index; + size_t length; + int line; +} Token; + +typedef enum { + ASTNodeType_Error, + ASTNodeType_Id, + ASTNodeType_Int, + ASTNodeType_Block, + ASTNodeType_If, + ASTNodeType_Loop, + ASTNodeType_Call, + ASTNodeType_Index, + ASTNodeType_Unary, + ASTNodeType_Binary, + ASTNodeType_Assign, + ASTNodeType_Let, + ASTNodeType_Break, + ASTNodeType_Fn, +} ASTNodeType; + +typedef struct ASTNode ASTNode; + +typedef struct { + ASTNode** data; + size_t length; + size_t capacity; +} ASTNodeVec; + +void ast_node_vec_construct(ASTNodeVec* vec); +void ast_node_vec_destroy(ASTNodeVec* vec); +void ast_node_vec_push(ASTNodeVec* vec, ASTNode* item); + +typedef struct { + ASTNode* condition; + ASTNode* truthy; + ASTNode* falsy; +} ASTIfNode; + +typedef struct { + ASTNode* body; +} ASTLoopNode; + +typedef struct { + ASTNode* subject; + ASTNodeVec args; +} ASTCallNode; + +typedef struct { + ASTNode* subject; + ASTNode* value; +} ASTIndexNode; + +typedef enum { + UnaryType_Not, + UnaryType_Negate, +} UnaryType; + +typedef struct { + UnaryType unary_type; + ASTNode* subject; +} ASTUnaryNode; + +typedef enum { + BinaryType_And, + BinaryType_Or, + BinaryType_Add, + BinaryType_Subtract, + BinaryType_Multiply, + BinaryType_EE, + BinaryType_NE, + BinaryType_LT, + BinaryType_GT, + BinaryType_LTE, + BinaryType_GTE, +} BinaryType; + +typedef struct { + BinaryType binary_type; + ASTNode* left; + ASTNode* right; +} ASTBinaryNode; + +typedef enum { + AssignType_Assign, + AssignType_Add, + AssignType_Subtract, + AssignType_Multiply, +} AssignType; + +typedef struct { + AssignType assign_type; + ASTNode* subject; + ASTNode* value; +} ASTAssignNode; + +typedef struct { + char* id; + ASTNode* value; +} ASTLetNode; + +typedef struct { + char* id; + ASTNodeVec params; + ASTNode* body; +} ASTFnNode; + +struct ASTNode { + ASTNodeType node_type; + int line; + union { + char* id_value; + int int_value; + ASTNodeVec statements; + ASTIfNode if_node; + ASTLoopNode loop_node; + ASTCallNode call_node; + ASTIndexNode index_node; + ASTUnaryNode unary_node; + ASTBinaryNode binary_node; + ASTAssignNode assign_node; + ASTLetNode let_node; + ASTFnNode fn_node; + }; +}; + +void ast_node_destroy(ASTNode* node); + +#endif