diff options
author | s-over-4 | 2023-06-05 13:20:39 -0400 |
---|---|---|
committer | s-over-4 | 2023-06-05 13:20:39 -0400 |
commit | e55519fae1a4053eb7f76dbaa686353c64a8cd9f (patch) | |
tree | 48bb5c6d357cb06a2607de602573a1857d2fcac7 /src | |
parent | abae0069eb6d657aec6d66b9801b937c159e0dc8 (diff) | |
parent | aecec3006ccb1c6ca6a8d9704b47237a344de0b9 (diff) |
Merge branch 'main' of https://github.com/s-over-4/HALK
C
Diffstat (limited to 'src')
-rw-r--r-- | src/include/lexer.h | 28 | ||||
-rw-r--r-- | src/include/parser.h | 37 | ||||
-rw-r--r-- | src/include/token.h | 6 | ||||
-rw-r--r-- | src/include/tree.h | 4 | ||||
-rw-r--r-- | src/lexer.c | 28 | ||||
-rw-r--r-- | src/parser.c | 21 |
6 files changed, 47 insertions, 77 deletions
diff --git a/src/include/lexer.h b/src/include/lexer.h index 7364910..a86f5bb 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -15,7 +15,7 @@ typedef struct LEXER_STRUC { } lexer_t; -// constructor / destroyer +// constructor/destroyer extern lexer_t* lexer_init (char* content); extern void lexer_destroy (lexer_t* lexer); @@ -27,31 +27,13 @@ extern token_t* lexer_next_token (lexer_t* lexer, token_t* token); extern char* lexer_get_c_as_string (lexer_t* lexer); // collectors -extern token_t* lexer_get_array (lexer_t* lexer); -extern token_t* lexer_get_string (lexer_t* lexer); -extern token_t* lexer_get_comment (lexer_t* lexer); +extern token_t* lexer_get_arr (lexer_t* lexer); +extern token_t* lexer_get_str (lexer_t* lexer); +extern token_t* lexer_get_com (lexer_t* lexer); // special def collectors extern token_t* lexer_get_directive (lexer_t* lexer); extern token_t* lexer_get_keyword (lexer_t* lexer); -extern token_t* lexer_get_def_var (lexer_t* lexer); - -// <mutability:?><type:><name>=<value?> -/* -extern token_t* lexer_get_def (lexer_t* lexer); // get the definition -extern token_t* lexer_get_def_mutability (lexer_t* lexer); // look for mut: or immut: (optional, default immut) -extern token_t* lexer_get_def_type (lexer_t* lexer); // get the def type (required) -extern token_t* lexer_det_def_name (lexer_t* lexer); // get the def name (required) -extern token_t* lexer_get_def_deftype (lexer_t* lexer); // get the deftype, either var or fn (ie checks for a . after the def name) - -// var def methods -extern token_t* lexer_get_def_var (lexer_t* lexer); -extern token_t* lexer_get_var_value (lexer_t* lexer); // get the variable value (optional, default null) - -// fn def methods -extern token_t* lexer_get_fn_def (lexer_t* lexer); -extern token_t* lexer_get_fn_args (lexer_t* lexer); // get the function args (optional, default empty) -extern token_t* lexer_get_fn_body (lexer_t* lexer); // get the function body (required) -*/ + #endif diff --git a/src/include/parser.h b/src/include/parser.h index c77fd29..dcacabe 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -1,7 +1,6 @@ #ifndef PARSER_H #define PARSER_H - #include "token.h" #include "tree.h" #include "lexer.h" @@ -16,28 +15,30 @@ typedef struct PARSER_STRUC { parser_t* parser_init(lexer_t* lexer); // check for expected token, or throw syntax error -void parser_check_expect(parser_t* parser, int token_type); +void parser_token_expect(parser_t* parser, int token_type); -// creates the abstract syntax tree +// do the parse tree_t* parser_parse(parser_t* parser); -tree_t* parser_parse_token_id(parser_t* parser); - -// parse a single chunk -tree_t* parser_parse_chunk(parser_t* parser); +tree_t* parser_parse_token(parser_t* parser); -// parse all in list of chunks -tree_t* parser_parse_chunks(parser_t* parser); +// parse hunks +tree_t* parser_parse_hunk(parser_t* parser); +tree_t* parser_parse_hunks(parser_t* parser); // leaves of the tree -tree_t* parser_parse_expr(parser_t* parser); -tree_t* parser_parse_fac(parser_t* parser); -tree_t* parser_parse_term(parser_t* parser); -tree_t* parser_parse_fn_call(parser_t* parser); -tree_t* parser_parse_fn_def(parser_t* parser); -tree_t* parser_parse_var(parser_t* parser); -tree_t* parser_parse_var_def(parser_t* parser); -tree_t* parser_parse_str(parser_t* parser); -tree_t* parser_parse_keyword(parser_t* parser); +tree_t* parser_parse_prim(parser_t* parser); +tree_t* parser_parse_subtree(parser_t* parser); +tree_t* parser_parse_def(parser_t* parser); +tree_t* parser_parse_call(parser_t* parser); + +// primitives +tree_t* parser_parse_prim_str(parser_t* parser); +tree_t* parser_parse_prim_int(parser_t* parser); +tree_t* parser_parse_prim_unkwn(parser_t* parser); + +// defs +tree_t* parser_parse_def_tags(parser_t* parser); +int parser_is_def_tag(char* maybe_tag); #endif diff --git a/src/include/token.h b/src/include/token.h index e52c3a0..8964ccd 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -12,9 +12,9 @@ typedef struct TOKEN_STRUC { enum { TOKEN_KEYWORD, // keyword - TOKEN_PRIM_STR_DELIM, // ' - TOKEN_PRIM_STR, // 'string' - TOKEN_PRIM_INT, // 'string' + TOKEN_PRIM_STR_DELIM, // ' + TOKEN_PRIM_STR, // 'string' + TOKEN_PRIM_INT, // 'string' TOKEN_COMM_DELIM, // ` TOKEN_COMM, // `comment` TOKEN_EXPR_END, // ; diff --git a/src/include/tree.h b/src/include/tree.h index 284a823..20c6714 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -11,10 +11,6 @@ typedef struct PRIM_STRUC { STR, INT, UNKWN, - //ARR, - //FLOAT, - //STRUCT, - //NAMESPACE, } type; union prim_union { diff --git a/src/lexer.c b/src/lexer.c index 5a04da0..e5d3c24 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -53,10 +53,10 @@ token_t* lexer_get_next_token(lexer_t* lexer) { switch (lexer->c) { case '\'': - return lexer_get_string(lexer); + return lexer_get_str(lexer); break; case '`': - return lexer_get_comment(lexer); + return lexer_get_com(lexer); break; case ';': return lexer_next_token( @@ -133,7 +133,7 @@ token_t* lexer_get_next_token(lexer_t* lexer) { lexer_get_c_as_string(lexer) ) ); break; - case '[': return lexer_get_array(lexer); break; + case '[': return lexer_get_arr(lexer); break; case '\0': return token_init(TOKEN_EOF, lexer_get_c_as_string(lexer)); break; default: log_err("Unrecognized token"); @@ -160,7 +160,7 @@ char* lexer_get_c_as_string(lexer_t* lexer) { } // TODO: abstract away this kind of thing -token_t* lexer_get_array(lexer_t* lexer) { +token_t* lexer_get_arr(lexer_t* lexer) { lexer_next(lexer); // skip opening [ char* array_so_far = calloc(1, sizeof(char)); array_so_far[0] = '\0'; @@ -181,7 +181,7 @@ token_t* lexer_get_array(lexer_t* lexer) { return token_init(TOKEN_PRIM_STR, array_so_far); // return the collected array } -token_t* lexer_get_string(lexer_t* lexer) { +token_t* lexer_get_str(lexer_t* lexer) { lexer_next(lexer); char* str_so_far = calloc(1, sizeof(char)); str_so_far[0] = '\0'; @@ -202,7 +202,7 @@ token_t* lexer_get_string(lexer_t* lexer) { return token_init(TOKEN_PRIM_STR, str_so_far); // return the collected string } -token_t* lexer_get_comment(lexer_t* lexer) { +token_t* lexer_get_com(lexer_t* lexer) { lexer_next(lexer); char* comment_so_far = calloc(1, sizeof(char)); @@ -263,19 +263,3 @@ token_t* lexer_get_keyword(lexer_t* lexer) { return token_init(TOKEN_KEYWORD, keyword_so_far); } -token_t* lexer_get_def(lexer_t* lexer) { - char* def_so_far = calloc(1, sizeof(char)); - def_so_far[0] = '\0'; - while (lexer->c != '=') { - char* current = lexer_get_c_as_string(lexer); - def_so_far = realloc( - def_so_far, - (strlen(def_so_far) + strlen(current) * sizeof(char)) - ); - - strcat(def_so_far, current); - lexer_next(lexer); - } - - return token_init(TOKEN_DEF, def_so_far); -} diff --git a/src/parser.c b/src/parser.c index c647e64..613f30a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -19,7 +19,7 @@ parser_t* parser_init(lexer_t* lexer) { } // check for expected token, or throw syntax error -void parser_check_expect(parser_t* parser, int token_type) { +void parser_token_expect(parser_t* parser, int token_type) { if (parser->token->type == token_type) { log_dbg(strcat("Got expected token", lexer_get_c_as_string(parser->lexer))); parser->token = lexer_get_next_token(parser->lexer); @@ -31,14 +31,14 @@ void parser_check_expect(parser_t* parser, int token_type) { // creates the abstract syntax tree tree_t* parser_parse(parser_t* parser) { - return parser_parse_chunks(parser); + return parser_parse_hunks(parser); } -tree_t* parser_parse_token_id(parser_t* parser) { - if (strcmp(parser->token->value, "let")) { - return parser_parse_var(parser); - } else { // if id not recognized, check for variables under that name - return parser_parse_var_def(parser); +tree_t* parser_parse_token(parser_t* parser) { + if (parser_is_def_tag(parser->token->value)) { + return parser_parse_def(parser); + } else { + return parser_parse_call(parser); } } @@ -166,3 +166,10 @@ tree_t* parser_parse_keyword(parser_t* parser) { } } + +int parser_is_def_tag(char* maybe_tag) { + if (strcmp(maybe_tag, "int") || + strcmp(maybe_tag, "str")) { + return 1; + } else { return 0; } +} |