From 2cc68205a1c0b746ad405607940e7183c4fb09b0 Mon Sep 17 00:00:00 2001 From: c+1 Date: Fri, 17 Nov 2023 18:58:54 -0500 Subject: Cleaned up. --- src/include/hlkt.h | 4 ++-- src/include/lexer.h | 58 ++++++++++++++++++++++++++++------------------------ src/include/parser.h | 15 +++++++++++--- src/include/pp.h | 32 +++++++++++++++-------------- src/include/source.h | 3 +++ src/include/syntax.h | 4 ++-- src/include/token.h | 22 ++++++++++++-------- src/include/tree.h | 40 +++++++++++++++++++++++++++--------- src/include/util.h | 18 ++++++++-------- 9 files changed, 119 insertions(+), 77 deletions(-) (limited to 'src/include') diff --git a/src/include/hlkt.h b/src/include/hlkt.h index ebcb7f6..c4db5e2 100644 --- a/src/include/hlkt.h +++ b/src/include/hlkt.h @@ -3,8 +3,8 @@ #include "util.h" -static int hlkt_run = 0; /* number of tests run */ -static int hlkt_failed = 0; /* number of tests that have failed */ +static int hlkt_run = 0; /* Number of tests run. */ +static int hlkt_failed = 0; /* Number of tests that have failed. */ #define HLKT_HIDE(stuff) do { stuff } while (0) diff --git a/src/include/lexer.h b/src/include/lexer.h index d83e35f..02275cb 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -5,66 +5,70 @@ #include "syntax.h" #include "token.h" -#define LEXER_VALID (lexer->c != '\0' && lexer->i < strlen(lexer->content)) - -/* the lexer struct */ +/* The Lexer. */ typedef struct LEXER_STRUC { - /* source being read */ + /* Source being read. */ char* src; - /* what the lexer is looking at right now */ + /* What the lexer is looking at right now. */ enum LEXER_STATE { - /* normal 1-character token */ + /* Normal 1-character token. */ LEXER_STATE_REG, - /* definition tag */ + /* Definition tag. */ LEXER_STATE_TAG, - /* string */ + /* String. */ LEXER_STATE_STR, - /* escaped character in string */ + /* Escaped character in string. */ LEXER_STATE_STR_ESC, - /* integer */ + /* Integer. */ LEXER_STATE_INT, - /* keyword */ + /* Keyword. */ LEXER_STATE_KWD, } state; - /* the linked list of tokens generated */ + /* The linked list of tokens generated. */ token_t* tokenl; - /* pointer to the last token in tokenl */ + /* Pointer to the last token in tokenl. */ token_t* tokenl_last; - /* number of tokens in tokenl */ + /* Number of tokens in tokenl. */ int tokenc; } lexer_t; -/* create lexer from source */ +/* Create lexer from source. */ lexer_t* lexer_init (char* src); -/* destroy lexer **but not src or tokenl** */ +/* + Destroy a lexer. + - Does not free `src. + - Does not free `tokenl`. +*/ void lexer_destroy (lexer_t* lexer); -/* add token to tokenl */ +/* Add token to tokenl. */ void lexer_add_token(lexer_t* lexer, token_t* token); -/* add the current character as a token to tokenl -- utility function for - lexer_do_reg() */ +/* Add the current character as a token to tokenl. Utility function `for lexer_do_reg()`. */ void lexer_add_current_char(lexer_t* lexer, int type); -/* add first character of lexer's src to the value of the last token in tokenl, if it exists. otherwise, create new token and add it */ +/* + Add first character of given lexer's `src` to the value of the last token in `tokenl`, if it exists. + Otherwise, create new token and add it. +*/ void lexer_add_current_char_to_last_token(lexer_t* lexer, int type); -/* handle regular state */ +/* Handle regular state. */ void lexer_do_reg(lexer_t* lexer); -/* handle definition tag state*/ +/* Handle definition tag. state*/ void lexer_do_tag(lexer_t* lexer); -/* TODO: handle character state */ +/* TODO: handle character state. */ void lexer_do_chr(lexer_t* lexer); -/* handle string state */ +/* Handle string state. */ void lexer_do_str(lexer_t* lexer); -/* handle integer */ +/* Handle integer. */ void lexer_do_int(lexer_t* lexer); -/* handle keywords */ +/* Handle keywords. */ void lexer_do_kwd(lexer_t* lexer); -/* run lexer */ +/* Run lexer. */ void lexer_run(lexer_t* lexer); #endif diff --git a/src/include/parser.h b/src/include/parser.h index b101485..79e1289 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -6,15 +6,24 @@ #include "token.h" typedef struct PARSER { - /* the token list being parsed */ + /* The token list being consumed. */ token_t* token; - /* the abstract syntax tree being generated */ + /* The AST being produced. */ tree_t* tree; } parser_t; +/* Creates a new parser. */ parser_t* parser_init(token_t* token); -/* free parser struct, **but not þe token list ∨ ast** */ + +/* + Destroys a parser. + - Does not free the token list. + - Does not free the AST. +*/ void parser_destroy(parser_t* parser); +/* Step the parser forward by 1 token. */ +void parser_nxt_token(parser_t* parser); + #endif diff --git a/src/include/pp.h b/src/include/pp.h index 4ab4f94..4186ef3 100644 --- a/src/include/pp.h +++ b/src/include/pp.h @@ -11,42 +11,44 @@ typedef struct MACRO_STRUC { } macro_t; /* - preprocessor struct + The preprocessor struct. - TODO: keep track of macros + TODO: Keep track of macros. */ typedef struct PP_STRUC { - /* original source */ + /* Original source. */ char* src; - /* pre-processed source */ + /* Pre-processed source. */ char* psrc; - /* what the preprocessor is looking at right now */ + /* What the preprocessor is looking at right now. */ enum PP_STATE { - PP_STATE_REG, /* regular */ - PP_STATE_STR, /* string */ - PP_STATE_COM, /* comment */ - PP_STATE_ESC, /* escaped character in string */ - /* PP_STATE_MCO, */ /* macro */ + PP_STATE_REG, /* Regular. */ + PP_STATE_STR, /* String. */ + PP_STATE_COM, /* Comment. */ + PP_STATE_ESC, /* Escaped character in string. */ + /* PP_STATE_MCO, */ /* Macro. */ } state; } pp_t; -/* creates a new preprocessor from some source code */ +/* Creates a new preprocessor from some source code. */ pp_t* pp_init(char*); -/* destroys the preprocessor **but not the pre-processed source** */ +/* + Destroys a preprocessor. + - Does not free the pre-processed source. +*/ void pp_destroy(pp_t*); -/* copy over the current character from src to psrc */ +/* Copy over the current character from src to psrc. */ void pp_cpy_char(pp_t*); void pp_do_reg(pp_t*); void pp_do_str(pp_t*); void pp_do_com(pp_t*); -/* run the preprocessor */ +/* Run the preprocessor. */ void pp_run(pp_t*); #endif - diff --git a/src/include/source.h b/src/include/source.h index 1fe9c41..07f83ed 100644 --- a/src/include/source.h +++ b/src/include/source.h @@ -3,8 +3,11 @@ #include "util.h" +/* Interpret any command line arguments to get the source. */ char* source_get(char* arg); +/* Get the source from stdin. */ char* source_get_from_stdin(); +/* Get the source from a file path. */ char* source_get_from_fpath(char* path); #endif diff --git a/src/include/syntax.h b/src/include/syntax.h index 777fea4..ab92845 100644 --- a/src/include/syntax.h +++ b/src/include/syntax.h @@ -1,7 +1,7 @@ #ifndef SYNTAX_H #define SYNTAX_H -/* Syntax Definitions */ +/* syntax Definitions. */ #define SYNTAX_CHAR_DELIM '\'' #define SYNTAX_STR_DELIM '"' @@ -21,7 +21,7 @@ #define SYNTAX_LLIST ']' #define SYNTAX_ESC '\\' -/* characters that can appear in keywords */ +/* Characters that can appear in keywords. */ #define SYNTAX_KWD_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_~|&+-/*<>=%^$@?" #endif diff --git a/src/include/token.h b/src/include/token.h index 9e64c97..08de829 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -4,9 +4,9 @@ #include "util.h" #include "hlkt.h" -/* token struct */ +/* Token struct. */ typedef struct TOKEN_STRUC { - /* token type */ + /* Token type. */ enum TOKEN_TYPE { TOKEN_UNKNOWN, TOKEN_CHAR, @@ -28,24 +28,28 @@ typedef struct TOKEN_STRUC { TOKEN_INT } type; - /* token value */ + /* Token value. */ char* val; - /* next token */ + /* Next token. */ struct TOKEN_STRUC* nxt; } token_t; -/* creates a token */ +/* Creates a token. */ token_t* token_init(int type, char val); -/* destroys a token **and all tokens contained in nxt** **Make sure to set the nxt of any parent tokens to NULL** */ +/* + Destroys a token. + - Frees all tokens contained in `nxt`. + - Make sure to set the `nxt` field of a parent token to `NULL`. +*/ void token_destroy(token_t* token); -/* return pointer to the last token */ +/* Return pointer to the last token. */ token_t* token_last(token_t* token); -/* add a character to the token value */ +/* Add a character to the token value. */ void token_add_char(token_t*, char); -/* print a token -- for debugging purposes */ +/* Print a token -- for debugging purposes. */ void token_print(token_t* token); #endif diff --git a/src/include/tree.h b/src/include/tree.h index fa64fce..b878852 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -3,8 +3,10 @@ #include "util.h" +/* The Abstract Syntax Tree (AST) structure. */ typedef struct TREE { enum TREE_TYPE { + TREE_TYPE_BLOCK, TREE_TYPE_LINT, TREE_TYPE_LSTR, TREE_TYPE_TAG, @@ -15,44 +17,51 @@ typedef struct TREE { } type; union TREE_DATA{ + /* Block. */ + struct TREE_DATA_BLOCK { + /* The first expression in the block. */ + struct TREE* val; + /* The next expression in the block. If it's'nt a block, end the block. */ + struct TREE* nxt; + } block; - /* literal integer */ + /* Literal integer. */ struct TREE_DATA_LINT { int val; } lint; - /* literal string */ + /* Literal string. */ struct TREE_DATA_LSTR { size_t len; char* val; } lstr; - /* tags */ + /* Tags. */ struct TREE_DATA_TAG { char* val; struct TREE_DATA_TAG* nxt; } tag; - /* definition arguments */ + /* Definition arguments. */ struct TREE_DATA_DARG { struct TREE_DATA_TAG* tag; struct TREE_DATA_DARG* nxt; } darg; - /* call arguments */ + /* Call arguments. */ struct TREE_DATA_CARG { struct TREE* val; struct TREE_DATA_CARG* nxt; } carg; - /* definitions */ + /* Definitions. */ struct TREE_DATA_DEF { struct TREE_DATA_TAG* tag; struct TREE_DATA_DARG* arg; struct TREE* val; } def; - /* calls */ + /* Calls. */ struct TREE_DATA_CAL { char* target; struct TREE_DATA_CARG* arg; @@ -61,13 +70,24 @@ typedef struct TREE { } data; } tree_t; +/* Create a new AST. */ tree_t* tree_init(int type); +/* Destroy the AST. */ void tree_destroy(tree_t* tree); -/* TODO: +/* TODO: Implement a better target organization structure that's better for searching. */ +typedef struct TREE_TARG { + tree_t* tree; + struct TREE_TARG* nxt; +} tree_targ_t; - Array of targets, for now just linear but implement something better in the future - tree_targ_t struct contains pointer to a tree_t +/* Create a new target. */ +tree_targ_t* tree_targ_init(tree_t* tree); +/* + Destroy a target. + - Frees all subsequent targets in `nxt`. + - Does not free the `tree`. */ +void tree_targ_destroy(tree_targ_t* targ); #endif diff --git a/src/include/util.h b/src/include/util.h index 854d18d..cf59a44 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -6,24 +6,24 @@ #include #include -/* log some debug information */ +/* Log some debug information. */ void log_dbg(const char*, ...); -/* log some information */ +/* Log some information. */ void log_inf(const char*, ...); -/* log something with no formatting */ +/* Log something with no formatting. */ void log_raw(const char*, ...); -/* log a warning */ +/* Log a warning. */ void log_war(const char*, ...); -/* log an error */ +/* Log an error. */ void log_err(const char*, ...); -/* die and leave message */ +/* Die and leave message. */ void die(const char*, ...); -/* if calloc() returns null, die */ +/* If `calloc()` returns null, die. */ void* ecalloc(size_t, size_t); -/* if malloc() returns null, die */ +/* If `malloc()` returns null, die. */ void* emalloc(size_t); -/* if realloc() returns null, die */ +/* If `realloc()` returns null, die. */ void* erealloc(void*, size_t); #endif -- cgit v1.2.3