aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authors-over-42023-08-29 10:17:27 -0400
committers-over-42023-08-29 10:17:27 -0400
commit4176dea8503cc8eebfa24b14aaefc3a8dc643230 (patch)
treed2fd320fa4c83fcd397995ff009ae565b6a5e933
parent497a3b690c3828775ad1d141f53484a748d5b61a (diff)
g
-rw-r--r--README.md2
-rw-r--r--resources/HALK_FINAL.svg59
-rw-r--r--src/include/parser.h7
-rw-r--r--src/include/tree.h6
-rw-r--r--src/lexer.c16
-rw-r--r--src/parser.c43
-rw-r--r--src/token.c1
-rw-r--r--src/tree.c25
8 files changed, 52 insertions, 107 deletions
diff --git a/README.md b/README.md
index cb46cfe..400abab 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# HALK
-<img src="./resources/HALK_FINAL.svg" width="350" align="right">
+<img src="./res/HALK_FINAL.svg" width="350" align="right">
*HALK* seeks to be a language.
diff --git a/resources/HALK_FINAL.svg b/resources/HALK_FINAL.svg
deleted file mode 100644
index 01f1c1b..0000000
--- a/resources/HALK_FINAL.svg
+++ /dev/null
@@ -1,59 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128">
- <defs>
- <style>
- .cls-1 {
- fill: #fcc73c;
- }
-
- .cls-2 {
- fill: #fe6142;
- }
-
- .cls-3 {
- fill: #9dd08e;
- }
-
- .cls-4 {
- fill: #c4c431;
- }
-
- .cls-5 {
- fill: #d73925;
- }
-
- .cls-6 {
- fill: #dfa82a;
- }
-
- .cls-7 {
- fill: #a8a521;
- }
-
- .cls-8 {
- fill: #252525;
- }
-
- .cls-9 {
- fill: #79aa7d;
- }
-
- .cls-10 {
- fill: #353535;
- }
-
- .cls-10, .cls-11 {
- font-family: HackNerdFontCompleteM-BoldItalic, 'Hack Nerd Font Mono';
- font-size: 32px;
- font-style: italic;
- font-weight: 700;
- }
- </style>
- </defs>
- <rect class="cls-8" x="-12.87" y="-7.62" width="153.74" height="143.24"/>
- <g>
- <text class="cls-10" transform="translate(22.48 73.04)"><tspan x="0" y="0">HALK</tspan></text>
- <text class="cls-11" transform="translate(24.48 72.04)"><tspan class="cls-5" x="0" y="0">H</tspan><tspan class="cls-6" x="19.27" y="0">A</tspan><tspan class="cls-7" x="38.53" y="0">L</tspan><tspan class="cls-9" x="57.8" y="0">K</tspan></text>
- <text class="cls-11" transform="translate(26.48 70.04)"><tspan class="cls-2" x="0" y="0">H</tspan><tspan class="cls-1" x="19.27" y="0">A</tspan><tspan class="cls-4" x="38.53" y="0">L</tspan><tspan class="cls-3" x="57.8" y="0">K</tspan></text>
- </g>
-</svg>
diff --git a/src/include/parser.h b/src/include/parser.h
index 0ba2038..deb6c7c 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -4,6 +4,8 @@
#include "lexer.h"
#include "tree.h"
+#include <stdarg.h>
+
typedef struct PARSER_STRUC {
lexer_t* lexer; // lexer used by the parser
token_t* token; // current token
@@ -12,9 +14,8 @@ typedef struct PARSER_STRUC {
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 token);
-void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*));
+// expect token(s), or die
+void parser_token_expect(parser_t* parser, int token, ...);
// do the parse
tree_t* parser_parse(parser_t* parser);
diff --git a/src/include/tree.h b/src/include/tree.h
index fde66ea..a2b71da 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -21,15 +21,17 @@ typedef struct TREE_STRUC {
struct { // === DEFINITIONS ===
char* type; // the definition type
char** tags; // the definition tags
+ size_t tags_size; // the number of tags
char* name; // the definition name
+ struct TREE_STRUC** args; // the arguments the definition will accept
+ size_t args_size; // the number of arguments
struct TREE_STRUC* value; // value of definition
- // :D lines line up :D
} def;
struct { // === CALLS ===
char* target; // name of definition being called
struct TREE_STRUC** args; // arguments passed to definition
- size_t args_size; // size of arguments
+ size_t args_size; // the number of arguments
} call;
// === TYPES ===
diff --git a/src/lexer.c b/src/lexer.c
index a5ed31b..09831bb 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -104,9 +104,9 @@ token_t* lexer_next_token(lexer_t* lexer, int token_type) {
return token;
}
-// get the current character as a string
+/* get the current character as a string */
char* lexer_get_c_as_string(lexer_t* lexer) {
- char* str; // the string to return
+ char* str; /* the string to return */
str = calloc(2, sizeof(char));
str[0] = lexer->c;
@@ -115,11 +115,13 @@ char* lexer_get_c_as_string(lexer_t* lexer) {
return str;
}
-// fskip: skip first char?
-// lskip: skip last char?
+/*
+ fskip: skip first char?
+ lskip: skip last char?
+*/
token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lskip, int type) {
- size_t len; // length of collected token so far
- char* token; // collected token so far
+ size_t len; /* length of collected token so far */
+ char* token; /* collected token so far */
len = 0;
token = calloc(len, sizeof(char));
@@ -144,7 +146,7 @@ token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lsk
if (lskip) { lexer_next(lexer); }
- token[len] = '\0'; // null terminate
+ token[len] = '\0'; /* null terminate */
return token_init(type, token);
}
diff --git a/src/parser.c b/src/parser.c
index 4233138..c32202e 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -20,24 +20,19 @@ parser_t* parser_init(lexer_t* lexer) {
void parser_destroy(parser_t* parser) { free(parser); }
-void parser_token_expect(parser_t* parser, int token) {
- token == parser->token->type?
- parser->token = lexer_get_next_token(parser->lexer):
- 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(
- "unexpected token\n\ttype: [%s]\n\tvalue: [%s]",
- token_get_type(parser->token->type),
- parser->token->value
- );
+void parser_token_expect(parser_t* parser, int tokens, ...) {
+ va_list ap;
+ va_start(ap, tokens);
+ for (int i = 0; i < tokens; ++i) {
+ va_arg(ap, int) == parser->token->type?
+ parser->token = lexer_get_next_token(parser->lexer):
+ die(
+ "unexpected token\n\ttype: [%s]\n\tvalue: [%s]",
+ token_get_type(parser->token->type),
+ parser->token->value
+ );
+ }
+ va_end(ap);
}
tree_t* parser_parse(parser_t* parser) { return parser_parse_exprs(parser); }
@@ -73,7 +68,7 @@ tree_t* parser_parse_exprs(parser_t* parser) {
parser_token_expect(parser, TOKEN_STMNT_END);
statement = parser_parse_expr(parser);
- comp->data.comp.size += 1;
+ comp->data.comp.size ++;
comp->data.comp.value = realloc(
comp->data.comp.value,
comp->data.comp.size * sizeof(struct TREE_STRUC)
@@ -97,12 +92,12 @@ tree_t* parser_parse_def(parser_t* parser) {
def->type = TREE_DEF;
- def->data.def.name = parser->token->value;
-
-// def->data.def.
-
while (parser->token->type == TOKEN_DEF_TAG) {
-
+ def->data.def.tags_size ++;
+ def->data.def.tags = realloc(
+ def->data.def.args,
+ def->data.def.args_size + sizeof(parser->token->value)
+ );
}
}
diff --git a/src/token.c b/src/token.c
index 41e7e0c..355a6dd 100644
--- a/src/token.c
+++ b/src/token.c
@@ -2,7 +2,6 @@
#include "include/token.h"
-// token constructor
token_t* token_init(int type, char* val) {
token_t* token = calloc(1, sizeof(struct TOKEN_STRUC));
token->type = type;
diff --git a/src/tree.c b/src/tree.c
index 0ed0697..235d0e6 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -8,23 +8,28 @@ tree_t* tree_init(int type) {
switch (type) {
case TREE_COMP:
- tree->data.comp.value = (void*) 0;
+ tree->data.comp.value = NULL;
tree->data.comp.size = 0;
break;
case TREE_DEF:
- tree->data.def.name = (void*) 0;
- tree->data.def.value = (void*) 0;
+ tree->data.def.type = NULL;
+ tree->data.def.tags = NULL;
+ tree->data.def.tags_size = 0;
+ tree->data.def.name = NULL;
+ tree->data.def.args = NULL;
+ tree->data.def.tags_size = 0;
+ tree->data.def.value = NULL;
break;
case TREE_CALL:
- tree->data.call.args = (void*) 0;
+ tree->data.call.target = NULL;
+ tree->data.call.args = NULL;
tree->data.call.args_size = 0;
- tree->data.call.target = (void*) 0;
break;
- case TREE_TYPE_STR:
- tree->data.type_str.value = (void*) 0;
- break;
- case TREE_TYPE_INT:
- tree->data.type_int.value = (void*) 0;
+ case TREE_TYPE_STR:
+ tree->data.type_str.value = NULL;
+ break;
+ case TREE_TYPE_INT:
+ tree->data.type_int.value = 0;
break;
}