From a2962a360e796be29a1512388dc4986081230d85 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 18 Jul 2022 00:52:03 +0300 Subject: [PATCH] 5 --- .gitignore | 13 +++++ commands.h | 16 ------- src/copler/Assembly.h | 12 +++++ context.c => src/copler/Context.c | 0 context.h => src/copler/Context.h | 8 ++-- commands.c => src/copler/commands.c | 0 src/copler/commands.h | 22 +++++++++ cobek_compiler.h => src/copler/copler.h | 9 ++-- src/copler/main.c | 6 +++ parser.c => src/copler/parser.c | 0 lexer.c => src/lexer/lexer.c | 63 +++++++++++++++++-------- src/lexer/lexer.h | 9 ++++ tokens.h => src/lexer/tokens.h | 36 +++++++------- 13 files changed, 133 insertions(+), 61 deletions(-) create mode 100644 .gitignore delete mode 100644 commands.h create mode 100644 src/copler/Assembly.h rename context.c => src/copler/Context.c (100%) rename context.h => src/copler/Context.h (70%) rename commands.c => src/copler/commands.c (100%) create mode 100644 src/copler/commands.h rename cobek_compiler.h => src/copler/copler.h (59%) create mode 100644 src/copler/main.c rename parser.c => src/copler/parser.c (100%) rename lexer.c => src/lexer/lexer.c (82%) create mode 100644 src/lexer/lexer.h rename tokens.h => src/lexer/tokens.h (84%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e5b49f --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Build results +bin/ +obj/ + +# user files +.old*/ +.vs/ +.vscode/ +.vshistory/ +.editorconfig +*.user +*.vcxproj.filters +.config diff --git a/commands.h b/commands.h deleted file mode 100644 index ad22f08..0000000 --- a/commands.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "../kerep/Autoarr/Autoarr.h" - -typedef enum CommandId{ - -} __attribute__((__packed__)) CommandId; - -struct CommandStruct; -typedef CommandStruct Command; -struct CommandStruct{ - Command* next; - Command* prev; - CommandId id; -} - -Autoarr_declare(Comand); \ No newline at end of file diff --git a/src/copler/Assembly.h b/src/copler/Assembly.h new file mode 100644 index 0000000..caa8f50 --- /dev/null +++ b/src/copler/Assembly.h @@ -0,0 +1,12 @@ +#pragma once + +#include "../../../kerep/src/base/base.h" + +typedef struct AssemblyStruct { + +} Assembly; + +typedef struct AssemblyParamsStruct +{ + /* data */ +} AssemblyParams; diff --git a/context.c b/src/copler/Context.c similarity index 100% rename from context.c rename to src/copler/Context.c diff --git a/context.h b/src/copler/Context.h similarity index 70% rename from context.h rename to src/copler/Context.h index dd97c05..92429ea 100644 --- a/context.h +++ b/src/copler/Context.h @@ -1,10 +1,10 @@ #pragma once +#include "../../../kerep/src/Autoarr/Autoarr.h" #include "commands.h" -struct ContextStruct; -typedef ContextStruct Context; +typedef struct ContextStruct Context; -Autoarr_declare(Context); +Autoarr_declare(Context) struct ContextStruct{ Context* parent; @@ -12,4 +12,4 @@ struct ContextStruct{ Autoarr(Command) commandChain; Autoarr(Constant) constantStack; bool isPartial; // if true, not-defined members won't throw NotDefinedError before linkage -} +}; diff --git a/commands.c b/src/copler/commands.c similarity index 100% rename from commands.c rename to src/copler/commands.c diff --git a/src/copler/commands.h b/src/copler/commands.h new file mode 100644 index 0000000..93dd5ed --- /dev/null +++ b/src/copler/commands.h @@ -0,0 +1,22 @@ +#pragma once +#include "../../../kerep/src/String/string.h" +#include "../../../kerep/src/Autoarr/Autoarr.h" + +typedef enum CommandId{ + CommandVoid +} __attribute__((__packed__)) CommandId; + +typedef struct CommandStruct Command; +struct CommandStruct{ + Command* next; + Command* prev; + CommandId id; +}; + +Autoarr_declare(Command) + +typedef struct ConstantStruct { + +} Constant; + +Autoarr_declare(Constant) \ No newline at end of file diff --git a/cobek_compiler.h b/src/copler/copler.h similarity index 59% rename from cobek_compiler.h rename to src/copler/copler.h index 56a3483..eefac6e 100644 --- a/cobek_compiler.h +++ b/src/copler/copler.h @@ -1,10 +1,9 @@ #pragma once -#include "tokens.h" -#include "context.h" -Autoarr_declare(Token); - -Autoarr(Token)* lexan(char* source, char* filename); +#include "../../../kerep/src/String/StringBuilder.h" +#include "../lexer/lexer.h" +#include "Assembly.h" +#include "Context.h" Context* parse(Autoarr(Token)* tokens); diff --git a/src/copler/main.c b/src/copler/main.c new file mode 100644 index 0000000..53bd933 --- /dev/null +++ b/src/copler/main.c @@ -0,0 +1,6 @@ +#include "copler.h" + +int main(){ + + return 0; +} \ No newline at end of file diff --git a/parser.c b/src/copler/parser.c similarity index 100% rename from parser.c rename to src/copler/parser.c diff --git a/lexer.c b/src/lexer/lexer.c similarity index 82% rename from lexer.c rename to src/lexer/lexer.c index 711add7..eb18510 100644 --- a/lexer.c +++ b/src/lexer/lexer.c @@ -1,9 +1,36 @@ -#include "cobek_compiler.h" +#include "lexer.h" Autoarr_define(Token); +typedef struct SourceFileInfo{ + char* _source; + char* _filename; + Autoarr(Token)* _tokens; + string _context; + string _line; + string _label; + uint32 _linenum; + uint32 _charnum; +} SourceFileInfo; -char charFromEscapeStr(string estr){ +#define source sfi->_source +#define filename sfi->_filename +#define tokens sfi->_tokens +#define context sfi->_context +#define line sfi->_line +#define label sfi->_label +#define linenum sfi->_linenum +#define charnum sfi->_charnum + +Maybe _throw_wrongchar(SourceFileInfo* sfi){ + char* errline=string_extract(line); + printf("\n\e[91mwrong char <%c> at [%s:%u:%u %s]\n >>> %s\n", + source[charnum], filename,linenum,charnum,context, errline); + exit(96); +} +#define throw_wrongchar(freeMem) { freeMem; _throw_wrongchar(sfi); } + +char _charFromEscapeStr(string estr, SourceFileInfo* sfi){ if(*estr.ptr!='\\') throw("first char is not \\"); switch(*(++estr.ptr)){ case 'n': return '\n'; @@ -13,27 +40,25 @@ char charFromEscapeStr(string estr){ case '\\': return '\\'; case '"': return '"'; case '\'': return '\''; - default: throw(ERR_WRONGCHAR); + default: throw_wrongchar(;); } } +#define charFromEscapeStr(estr) _charFromEscapeStr(estr, sfi) - -Autoarr(Token)* lexan(char* source, char* filename){ - Autoarr(Token)* tokens=malloc(sizeof(Autoarr(Token))); - *tokens=Autoarr_create(Token,64,1024); - char c; - string label={source,0}; - string line={source,0}; - uint32 linenum; - uint32 cnum; - - - void throw_wrongchar(){ - char* errline=string_cpToCharPtr(line); - printf("\n\e[91mwrong char <%c> at [%s:%u:%u %s]\n >>> %s\n" ,filename,linenum,cnum,c,errline); - exit(96); +Autoarr(Token)* lexan(char* _source, char* _filename){ + SourceFileInfo sfi={ + ._source=_source, + ._filename=_filename, + ._tokens=Autoarr_create(Token,64,1024), + ._label={_source,0}, + ._line={_source,0}, + ._linenum=0, + ._charnum=0 }; - + return _lexan(&sfi); +} + +Autoarr(Token)* _lexan(SourceFileInfo* sfi){ void addlabel(){ if(label.length==0) return; diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h new file mode 100644 index 0000000..ea9807b --- /dev/null +++ b/src/lexer/lexer.h @@ -0,0 +1,9 @@ +#pragma once + +#include "../../../kerep/src/Autoarr/Autoarr.h" +#include "../../../kerep/src/String/string.h" +#include "tokens.h" + +Autoarr_declare(Token) + +Autoarr(Token)* lexan(char* source, char* filename); diff --git a/tokens.h b/src/lexer/tokens.h similarity index 84% rename from tokens.h rename to src/lexer/tokens.h index 047b0e4..f567000 100644 --- a/tokens.h +++ b/src/lexer/tokens.h @@ -1,23 +1,24 @@ #pragma once -#include "../kerep/base/std.h" + +#include "../../../kerep/src/base/base.h" typedef enum TokenId{ // base types - tok_void, - tok_int8, - tok_uint8, - tok_int16, - tok_uint16, - tok_int32, - tok_uint32, - tok_int64, - tok_uint64, - tok_char, //ascii - tok_char8, //utf8 - tok_char16, //utf16 - tok_bool, - tok_float32, - tok_float64, + tok_void_t, + tok_int8_t, + tok_uint8_t, + tok_int16_t, + tok_uint16_t, + tok_int32_t, + tok_uint32_t, + tok_int64_t, + tok_uint64_t, + tok_char_t, //ascii + tok_char8_t, //utf8 + tok_char16_t, //utf16 + tok_bool_t, + tok_float32_t, + tok_float64_t, // constant tok_null, tok_true, @@ -58,7 +59,7 @@ typedef enum TokenId{ // user-defined tok_label, tok_number, - tok_char, + tok_character, tok_string, tok_comment, // special characters @@ -103,3 +104,4 @@ typedef struct Token{ char* value; TokenId id; } Token; +