diff options
-rw-r--r-- | examples/functional.halk | 1 | ||||
-rw-r--r-- | examples/hello.halk | 8 | ||||
-rw-r--r-- | src/include/lexer.h | 2 | ||||
-rw-r--r-- | src/lexer.c | 24 | ||||
-rw-r--r-- | src/main.c | 17 |
5 files changed, 28 insertions, 24 deletions
diff --git a/examples/functional.halk b/examples/functional.halk index 758559c..ba61210 100644 --- a/examples/functional.halk +++ b/examples/functional.halk @@ -7,3 +7,4 @@ let.Y => { λ.x => f.x.x; }.λ.x => f.x.x; } + diff --git a/examples/hello.halk b/examples/hello.halk index 690fb5e..f6a7c9f 100644 --- a/examples/hello.halk +++ b/examples/hello.halk @@ -17,13 +17,13 @@ fn.greeting,to -> { [functions defined with do not prefix it with a namespace/] }; -fn.sum_all._ -> { [variadic functions are possible with the reserved '_' argument, +fn.sum_all,_ -> { [variadic functions are possible with the reserved '_' argument, which is treated as an array] return.foldl.sum,0,_; }; -fn.fibonacci.n -> { - if.or.(=.n, 0), (=.n, 1) -> { [functions ending in '?' should be predicates] +fn.fibonacci,n -> { + if.or.(=.n, 0), (=.n, 1) -> { [functions ending in '?' should be predicates] return.1; }; return.sum. @@ -31,7 +31,7 @@ fn.fibonacci.n -> { (fibonacci. sub.n, 2); }; -fn.main -> { [where our code will begin executing] +fn.main -> { [where our code will begin executing] greeting.[comments can be placed *anywhere*]"world."; exit.0; [exit with code 0 for success] }; diff --git a/src/include/lexer.h b/src/include/lexer.h index bff0a80..cc96216 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -34,5 +34,7 @@ extern token_t* lexer_next_token(lexer_t* lexer, token_t* token); extern char* lexer_get_c_as_string(lexer_t* lexer); +extern void lexer_destroy(lexer_t* lexer); + #endif diff --git a/src/lexer.c b/src/lexer.c index 345d5c5..acfae12 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,7 +1,5 @@ -#include "include/lexer.h" +#include "include/lexer.h" #include "include/token.h" - - #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -40,16 +38,12 @@ token_t* lexer_get_next_token(lexer_t* lexer) { lexer->c == ' ' || lexer->c == '\t' || lexer->c == '\n' - ) { - lexer_pass(lexer); - } + ) { lexer_pass(lexer); } if ( isalnum(lexer->c) || lexer->c == '_' - ) { - return lexer_get_keyword(lexer); - } + ) { return lexer_get_keyword(lexer); } switch (lexer->c) { case '\'': @@ -221,19 +215,18 @@ token_t* lexer_get_directive(lexer_t* lexer) { } token_t* lexer_get_keyword(lexer_t* lexer) { - lexer_next(lexer); - char* str_so_far = calloc(1, sizeof(char)); str_so_far[0] = '\0'; while (isalnum(lexer->c)) { char* current = lexer_get_c_as_string(lexer); str_so_far = realloc(str_so_far, (strlen(str_so_far) + strlen(current) * sizeof(char))); - strcat(str_so_far, current); - + strcat(current, str_so_far); + free(current); lexer_next(lexer); } + lexer_next(lexer); return token_init(TOKEN_KEYWORD, str_so_far); } @@ -251,3 +244,8 @@ char* lexer_get_c_as_string(lexer_t* lexer) { return str; } + +void lexer_destroy(lexer_t* lexer) { + free(lexer->content); + free(lexer); +} @@ -11,16 +11,20 @@ int main(int argc, char* argv[]) { char *source; fsource = fopen ("examples/hello.halk", "rb"); - if (!fsource) { fputs("Source file not found.", stderr); exit(1); }; + if (!fsource) { + fputs("Source file not found.", stderr); + exit(1); + }; fseek(fsource, 0L, SEEK_END); fsource_size = ftell(fsource); rewind(fsource); source = calloc(1, fsource_size + 1); + if (!source) { fclose(fsource); - fputs("Memory allocation faled.", stderr); + fputs("Memory allocation failed.", stderr); exit(1); } @@ -31,12 +35,9 @@ int main(int argc, char* argv[]) { exit(1); } - lexer_t* lexer = lexer_init( - source - ); - fclose(fsource); - free(source); + + lexer_t* lexer = lexer_init( source ); printf("\n=== INPUT =======\n%s\n=== END INPUT ===\n", lexer->content); @@ -46,5 +47,7 @@ int main(int argc, char* argv[]) { printf("===\ntoken type: %d\ntoken value: %s\n===\n", token->type, token->value); } + lexer_destroy(lexer); + return 0; } |