aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.h
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-06-17 23:42:31 +0300
committerkartofen <mladenovnasko0@gmail.com>2023-06-17 23:42:31 +0300
commitbcac686c1bf6a5c1dec2324269e2766babdc0fde (patch)
tree6483461015705efa8290a1ab05482a641739c1dd /src/lexer.h
lexer - done
Diffstat (limited to 'src/lexer.h')
-rw-r--r--src/lexer.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lexer.h b/src/lexer.h
new file mode 100644
index 0000000..942be54
--- /dev/null
+++ b/src/lexer.h
@@ -0,0 +1,55 @@
+#ifndef LEXER_H
+#define LEXER_H
+
+#ifndef LEXER_IDEN_CAP
+#define LEXER_IDEN_CAP 512
+#endif
+
+typedef struct lexer *lexer_t;
+
+struct token {
+ enum token_enum {
+ TOKEN_PARENTHS_OPEN, TOKEN_PARENTHS_CLOSE,
+ TOKEN_SPECIAL_DOT, TOKEN_SPECIAL_QUOTE,
+ TOKEN_LITERAL_NUM_INT, TOKEN_LITERAL_STRING,
+ TOKEN_LITERAL_NUM_FLOAT, TOKEN_SYMBOL,
+ TOKEN_TOKENS // number of token types
+ } type;
+
+ union {
+ char *symbol;
+ char *string;
+ long num_int;
+ float num_float
+ };
+};
+
+struct lexer {
+ struct token *tokens;
+ size_t tokens_cap;
+ size_t ntokens;
+
+ // identifier
+ char iden[LEXER_IDEN_CAP];
+ size_t iden_sz;
+
+ int inside_string;
+};
+
+// allocate a lexer with a maximum number of tokens_cap tokens
+// returns a lexer on success, NULL on fail
+lexer_t lexer_create(size_t tokens_cap);
+
+// destroy a lexer
+void lexer_destroy(lexer_t lexer);
+
+// turn the given non-null-terminated string str of lenght len
+// into into tokens
+// returns 0 on success
+int lexer_tokenize(lexer_t lexer, char *str, size_t len);
+
+#ifdef DEBUG
+void lexer_print_tokens(lexer_t lexer);
+#endif
+
+#endif