From 85bb2db8dee7898c890f5d1fc0b3874bee3d88ba Mon Sep 17 00:00:00 2001 From: c+1 Date: Thu, 12 Oct 2023 21:02:04 -0400 Subject: update examples --- examples/hello.halk | 60 +++++++++++++++++++++------------------------- examples/preprocessor.halk | 4 ++-- examples/simple.halk | 4 +--- src/include/token.h | 7 +++--- src/lexer.c | 51 +++++++++++++++++++++++++++++++++++++++ src/main.c | 2 +- 6 files changed, 85 insertions(+), 43 deletions(-) diff --git a/examples/hello.halk b/examples/hello.halk index 62b8360..6e5f7bf 100644 --- a/examples/hello.halk +++ b/examples/hello.halk @@ -1,42 +1,36 @@ -:str:hello = 'hello, '; -:void:greeting. :str:to = { ` functions declared the same way as variables, but with a . after - the name, followed by arguments ` +` comments in backticks ` - :str:message = strcat.hello, to; ` function application is right-associative ` +#inc# #somefile.halk# ` include a file ` +#inc# #stdio# ` include the 'stdio' header (file path stored in the macro #stdio#) ` - io/stdo.message; ` navigate namespaces with a '/' ` -} +:str:hello = "Hello"; ` define a variable 'hello' with the type 'str' ` -:int:sum_all. :int[]:_ = { ` variadic functions are possible with the reserved '_' argument, - which is treated as an array ` - return.foldl.sum, 0, _; -} +:str:greet.:str:target = strcat.hello, ", ", target; ` define a function 'greet' that returns a 'str' and accepts an + argument 'target', also of type 'str' ` -:namespace:new_namespace = { ` create a new namespace ` - :int:value = 1; +` some more functions ` +:void:add1. :int:n = n = +. n, 1; +:int:fac.:int:n=if.(==.n,0),1,*.n,fac.-.n,1; - :int:sum_all. :int[]:_ = { - return.foldl.sum,0,_; - } -} +:int:fib. :int:n = if. (<=. n, 1), ` multiple lines can be used for clarity ` + n, + +. fib.(-. n, 1), fib. -. n, 2); -check_expect.(sum_all. 1, 2, 3, 4), 10; ` tests ` -check_expect. - (sum_all. 1, 2, 3, 4), - (new_namespace/sum_all. 1, 2, 3, 4); - -:int:fibonacci. :int:n = { - if.(eq.n, 0), { - return.1; - }, { - return.sum. - (fibonacci.(sub.n, 1)), - (fibonacci.(sub.n, 2)); - } -} -:int:main. :str[]:args = { ` where code will begin executing ` - greeting.'world.'; ` prints 'hello, world.' ` +:void:print_greet.:str:target = stdio/pln.greet.target; ` namespaces ` + +:namespace:people = { ` create namespace ` + struct.person, :int:age, :str:name; ` create a struct -- syntax not finalized ` + + :str:person_report. :struct:person = { + :str:age = int->str.person.age; + :str:name = person.name; - return.0; + strcat. "NAME:\t", name, "\nAGE:\t", age; + } + + :void:person_birthday. :struct:person = { + stdio/pln. "Happy birthday, ", person.name; + person.age = +.person.age, 1; + } } diff --git a/examples/preprocessor.halk b/examples/preprocessor.halk index d68748e..9e3f6b5 100644 --- a/examples/preprocessor.halk +++ b/examples/preprocessor.halk @@ -19,11 +19,11 @@ #ddo# #BSD# #inc# #bsd_support.halk# #udo# #OS# # - some-error-func."os not specified"; + some_error_func."os not specified"; # #def# #OSTYPE# # os/shell."sysctl kernel.ostype" # -io/print.#OSTYPE# +stdio/pln.#OSTYPE# diff --git a/examples/simple.halk b/examples/simple.halk index 80d70de..6f44d73 100644 --- a/examples/simple.halk +++ b/examples/simple.halk @@ -1,3 +1 @@ -:str:var = "Hello"; - -fprint.stdio, strcat.var, "\n"; +:str:var = "Hello, World!"; diff --git a/src/include/token.h b/src/include/token.h index 6779755..5a3a36c 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -9,16 +9,15 @@ typedef struct TOKEN_STRUC { /* token type */ enum TOKEN_TYPE { TOKEN_UNKNOWN, - TOKEN_CHAR_DELIM, - TOKEN_STR_DELIM, - TOKEN_COMMENT_DELIM, + TOKEN_CHAR, + TOKEN_STR, TOKEN_EXPR_END, TOKEN_SET, TOKEN_LGROUP, TOKEN_RGROUP, TOKEN_APPLY, TOKEN_LIST_DELIM, - TOKEN_TAG_DELIM, + TOKEN_TAG, TOKEN_NAMESPACE_DELIM, TOKEN_LBLOCK, TOKEN_RBLOCK, diff --git a/src/lexer.c b/src/lexer.c index efdc718..ba0e8e1 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -4,6 +4,8 @@ #include #include "include/lexer.h" +#include "include/syntax.h" +#include "include/token.h" lexer_t* lexer_init(char* src) { lexer_t* lexer; @@ -57,11 +59,60 @@ void lexer_do_reg(lexer_t* lexer) { case SYNTAX_APPLY: lexer_add_current_char(lexer, TOKEN_APPLY); break; + case SYNTAX_TAG_DELIM: + lexer_add_current_char(lexer, TOKEN_TAG_DELIM); + break; + case SYNTAX_NAMESPACE_DELIM: + lexer_add_current_char(lexer, TOKEN_NAMESPACE_DELIM); + break; + case SYNTAX_SET: + lexer_add_current_char(lexer, TOKEN_SET); + break; + case SYNTAX_LLIST: + lexer_add_current_char(lexer, TOKEN_LLIST); + break; + case SYNTAX_RLIST: + lexer_add_current_char(lexer, TOKEN_RLIST); + break; + case SYNTAX_LGROUP: + lexer_add_current_char(lexer, TOKEN_LGROUP); + break; + case SYNTAX_RGROUP: + lexer_add_current_char(lexer, TOKEN_RGROUP); + break; + case SYNTAX_EXPR_END: + lexer_add_current_char(lexer, TOKEN_EXPR_END); + break; + case SYNTAX_STR_DELIM: + lexer_add_current_char(lexer, TOKEN_STR_DELIM); + break; + case SYNTAX_CHAR_DELIM: + lexer_add_current_char(lexer, TOKEN_CHAR_DELIM); + break; + case SYNTAX_LIST_DELIM: + lexer_add_current_char(lexer, TOKEN_LIST_DELIM); + break; default: lexer_add_current_char(lexer, TOKEN_UNKNOWN); } } +void lexer_do_chr(lexer_t* lexer) { + if (*lexer->src == '\'') { + lexer->state = LEXER_STATE_REG; + } else { + token_t* t; + + t = token_init(TOKEN_CHAR, *lexer->src); + + lexer_add_token(lexer, t); + } +} + +void lexer_do_str(lexer_t* lexer) { + +} + void lexer_run(lexer_t* lexer) { while (*lexer->src) { if (lexer->state == LEXER_STATE_REG) { lexer_do_reg(lexer); } diff --git a/src/main.c b/src/main.c index 7146c14..2284444 100644 --- a/src/main.c +++ b/src/main.c @@ -28,7 +28,7 @@ int main(int argc, char* argv[]) { pp_run(pp); free(src); src = pp->psrc; - log_dbg(pp->psrc); + log_inf("pre-processed source: %s", pp->psrc); /* destroy pre-processor */ pp_destroy(pp); HLKT_ASS(src); -- cgit v1.2.3