From 5b37568baac046ac6b21453bbe037e0eeec16e89 Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Fri, 7 Jul 2023 20:56:29 -0400 Subject: expectf --- .gitignore | 1 + Makefile | 21 +++++++----- examples/simple.halk | 1 + src/include/parser.h | 3 +- src/include/token.h | 6 ++-- src/include/tree.h | 16 ++++++--- src/lexer.c | 2 +- src/main.c | 14 -------- src/parser.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/token.c | 2 +- src/tree.c | 4 +++ 11 files changed, 129 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 324b9a3..248b074 100755 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ tags *.out *.tmp.* +*_notes.md [._]sw[a-p] [._]*.sw[a-p] [._]ss[a-gi-z] diff --git a/Makefile b/Makefile index 0550a72..5d23c0e 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,31 @@ +NAME := HALK PREFIX := /usr/local BINDIR := $(PREFIX)/bin -BIN := halk -TMPBIN := halk.out +BIN := $(shell echo $(NAME) | tr '[:upper:]' '[:lower:]') +TMPBIN := $(BIN).out CC := gcc -FLAGS := -O3 +FLAGS := -O3 -s DEVFLAGS := -ggdb -fsanitize=leak,address,undefined -fno-omit-frame-pointer SRCS := $(wildcard src/*.c) -SRCS := $(filter-out src/parser.c, $(SRCS)) # exclude the incomplete parser for now. +#SRCS := $(filter-out src/parser.c, $(SRCS)) # exclude the incomplete parser for now. OBJS := $(SRCS:.c=.o) all: options HALK options: - @echo "HALK build options " - @echo "================== " + @$(eval $@_TITLE := "$(NAME) build options") + @$(eval $@_ULINE := $(shell echo "$($@_TITLE)" | tr '[:print:]' '=')) + @echo "$($@_TITLE)" + @echo "$($@_ULINE)" @echo "cc: $(CC) " @echo "flags: $(FLAGS) " @echo " " devoptions: - @echo "HALK build options (development) " - @echo "================================ " + @$(eval $@_TITLE := "$(NAME) build options \(development\)") + @$(eval $@_ULINE := $(shell echo "$($@_TITLE)" | tr '[:print:]' '=')) + @echo "$($@_TITLE)" + @echo "$($@_ULINE)" @echo "cc: $(CC) " @echo "flags: $(DEVFLAGS) " @echo " " diff --git a/examples/simple.halk b/examples/simple.halk index 5a651e1..e100f75 100644 --- a/examples/simple.halk +++ b/examples/simple.halk @@ -1,2 +1,3 @@ :str:mut:var = 'Hello'; + io/fprint.stdio, var; diff --git a/src/include/parser.h b/src/include/parser.h index 32cd4bc..694bd5e 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -13,7 +13,8 @@ parser_t* parser_init(lexer_t* lexer); void parser_destroy(parser_t* parser); // expect token, or die -void parser_token_expect(parser_t* parser, int (*expected_token)(token_t*)); +void parser_token_expect(parser_t* parser, token_t* token); +void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*)); // do the parse tree_t* parser_parse(parser_t* parser); diff --git a/src/include/token.h b/src/include/token.h index a2a47bc..d3dda67 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -1,8 +1,8 @@ #ifndef TOKEN_H #define TOKEN_H -#define TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" -#define TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS_LEN 53 +#define TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS "+-/*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_" +#define TOKEN_DEFNAME_FIRST_CHAR_ALLOWED_CHARS_LEN 57 #define TOKEN_DEFNAME_SPLIT_CHAR_ALLOWED_CHARS "1234567890_-" #define TOKEN_DEFNAME_SPLIT_CHAR_ALLOWED_CHARS_LEN 12 #define TOKEN_CHAR_IGNORE " \t\n\r" @@ -16,7 +16,7 @@ typedef struct TOKEN_STRUC { TOKEN_PRIM_STR, // 'string' TOKEN_PRIM_INT, // 42 TOKEN_COMM, // `comment` - TOKEN_EXPR_END, // ; + TOKEN_STMNT_END, // ; TOKEN_LGROUP, // ( TOKEN_RGROUP, // ) TOKEN_DIRECTIVE, // #DIRECTIVE# diff --git a/src/include/tree.h b/src/include/tree.h index 4ffefcf..6374062 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -5,6 +5,7 @@ typedef struct TREE_STRUC { enum { + TREE_COMP, TREE_DEF, TREE_CALL, TREE_TYPE_STR, @@ -12,25 +13,30 @@ typedef struct TREE_STRUC { } type; union { - struct { // === DEFINITIONS === + struct { // === "COMPOUND" === + struct TREE_STRUC** value; + size_t size; + } comp; + + struct { // === DEFINITIONS === char* name; // name of definition int mutability; // mutability of definition struct TREE_STRUC* value; // value of definition } def; - struct { // === CALLS === + struct { // === CALLS === char* target; // name of definition being called struct TREE_STRUC** args; // arguments passed to definition size_t args_size; // size of arguments } call; // === TYPES === - struct { // strings + struct { // strings char* value; } type_str; - struct { // integers - int* value; + struct { // integers + int value; } type_int; } data; } tree_t; diff --git a/src/lexer.c b/src/lexer.c index 6d9d5a8..a5ed31b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -49,7 +49,7 @@ token_t* lexer_get_next_token(lexer_t* lexer) { return lexer_collect(lexer, token_char_grave, 1, 1, TOKEN_COMM); break; case ';': - return lexer_next_token(lexer, TOKEN_EXPR_END); + return lexer_next_token(lexer, TOKEN_STMNT_END); break; case '=': return lexer_next_token(lexer, TOKEN_DEF_SET); diff --git a/src/main.c b/src/main.c index d756f0b..1efc797 100644 --- a/src/main.c +++ b/src/main.c @@ -7,24 +7,10 @@ #include "include/source.h" int main(int argc, char* argv[]) { - //FILE* fsource; - //long fsource_size; char* source; lexer_t* lexer; int in_file; - /* - fsource = fopen(argv[1], "rb"); - if (!fsource) { free(fsource); die("source file not found"); }; - fseek(fsource, 0L, SEEK_END); - fsource_size = ftell(fsource); - rewind(fsource); - source = calloc(1, fsource_size + 1); - if (!source) { fclose(fsource); free(source); die("calloc failed"); } - if (1 != fread(source, fsource_size, 1, fsource)) { fclose(fsource); free(source); die("could not read source"); } - log_inf("source file loaded"); - */ - source = source_get(argv[1]); lexer = lexer_init(source); diff --git a/src/parser.c b/src/parser.c index d2720b7..ae6f615 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3,8 +3,8 @@ #include #include "include/token.h" -#include "include/tree.h" #include "include/util.h" +#include "include/tree.h" #include "include/lexer.h" #include "include/parser.h" @@ -20,7 +20,17 @@ parser_t* parser_init(lexer_t* lexer) { void parser_destroy(parser_t* parser) { free(parser); } -void parser_token_expect(parser_t* parser, int (*expected_token)(token_t*)) { +void parser_token_expect(parser_t* parser, token_t* token) { + if (token == parser->token) { + die( + "unexpected token!\n\ttype: [%s]\n\tvalue: [%s]", + token_get_type(parser->token->type), + parser->token->value + ); + } +} + +void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*)) { expected_token(parser->token)? parser->token = lexer_get_next_token(parser->lexer): die( @@ -53,3 +63,83 @@ tree_t* parser_parse_statements(parser_t* parser) { return comp; } + +tree_t* parser_parse_statement(parser_t* parser) { + +} + +tree_t* parser_parse_blocks(parser_t* parser) { + +} + +tree_t* parser_parse_block(parser_t* parser) { + +} + +tree_t* parser_parse_def(parser_t* parser) { + tree_t* tree; + + tree->type = TREE_DEF; + tree->data.def.name = parser->token->value; + + lexer_next(parser->lexer); + + tree->data.def.mutability = strcmp(parser->token->value, "mut"); + + // collect other tags + // check for arguments + // wait for DEF_SET + // return value as parse_statement (could be just one or block) + + return tree; +} + +tree_t* parser_parse_call(parser_t* parser) { + tree_t* tree; + + tree->type = TREE_CALL; + tree->data.call.target = parser->token->value; + + lexer_next(parser->lexer); + + if (parser->token->type == TOKEN_FN_APPLY) { + + lexer_next(parser->lexer); + + tree->data.call.args = realloc(tree->data.call.args, sizeof(tree->data.call.args) + sizeof(parser->token->value)); + tree->data.call.args_size += sizeof(parser->token->value); + + + lexer_next(parser->lexer); + + while (parser->token->type == TOKEN_LIST_DELIM) { + lexer_next(parser->lexer); + tree->data.call.args = realloc(tree->data.call.args, sizeof(tree->data.call.args) + sizeof(parser->token->value)); + tree->data.call.args_size += sizeof(parser->token->value); + lexer_next(parser->lexer); + } + + return tree; + } else { + return tree; + } +} + +tree_t* parser_parse_type_str(parser_t* parser) { + tree_t* tree; + + tree->type = TREE_TYPE_STR; + tree->data.type_str.value = parser->token->value; + + return tree; +} + +tree_t* parser_parse_type_int(parser_t* parser) { + tree_t* tree; + + tree->type = TREE_TYPE_INT; + tree->data.type_int.value = strtol(parser->token->value, NULL, 10); + + return tree; +} + diff --git a/src/token.c b/src/token.c index 9b23810..41e7e0c 100644 --- a/src/token.c +++ b/src/token.c @@ -25,7 +25,7 @@ char* token_get_type(int type) { case TOKEN_COMM: return "TOKEN_COMM"; break; - case TOKEN_EXPR_END: + case TOKEN_STMNT_END: return "TOKEN_EXPR_END"; break; case TOKEN_LGROUP: diff --git a/src/tree.c b/src/tree.c index 1f57d5b..aadfc3f 100644 --- a/src/tree.c +++ b/src/tree.c @@ -7,6 +7,10 @@ tree_t* tree_init(int type) { tree->type = type; switch (type) { + case TREE_COMP: + tree->data.comp.value = (void*) 0; + tree->data.comp.size = 0; + break; case TREE_DEF: tree->data.def.mutability = 0; tree->data.def.name = (void*) 0; -- cgit v1.2.3