summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-01-02 22:31:49 -0800
committerPreston Pan <preston@nullring.xyz>2023-01-02 22:31:49 -0800
commit64feef1b9ea72adf7ba32998e9dca7d507607498 (patch)
treea409e61877bb51aa6fb2477175dabbf3dbccf298 /src/include
a lot of stuff.
Diffstat (limited to 'src/include')
-rw-r--r--src/include/ast.h42
-rw-r--r--src/include/hash_table.h47
-rw-r--r--src/include/lexer.h39
-rw-r--r--src/include/macros.h85
-rw-r--r--src/include/parser.h49
-rw-r--r--src/include/print.h16
-rw-r--r--src/include/sl_list.h23
-rw-r--r--src/include/stack.h16
-rw-r--r--src/include/token.h22
-rw-r--r--src/include/visitor.h26
10 files changed, 365 insertions, 0 deletions
diff --git a/src/include/ast.h b/src/include/ast.h
new file mode 100644
index 0000000..f7be3d5
--- /dev/null
+++ b/src/include/ast.h
@@ -0,0 +1,42 @@
+#ifndef AST_H
+#define AST_H
+#include <stdbool.h>
+
+typedef struct AST_STRUCT {
+ enum {
+ /* complex types */
+ AST_PAIR,
+ AST_SYMBOL,
+ /* self evaluating types */
+ AST_STRING,
+ AST_BOOL,
+ AST_INT,
+ AST_FLOAT,
+ AST_FUNCTION,
+ } type;
+
+ /* For functions, the car will be a list of variables, and the cdr will be the
+ * expression */
+ struct AST_STRUCT *car;
+ struct AST_STRUCT *cdr;
+
+ char *string_value;
+ int int_value;
+ double float_value;
+ bool bool_value;
+} ast_t;
+
+ast_t *init_ast_string(char *value);
+
+ast_t *init_ast_int(int value);
+
+ast_t *init_ast_float(double value);
+
+ast_t *init_ast_pair(ast_t *car, ast_t *cdr);
+
+ast_t *init_ast_bool(bool value);
+
+ast_t *init_ast_symbol(char *value);
+
+ast_t *init_ast_function(ast_t *car, ast_t *cdr);
+#endif
diff --git a/src/include/hash_table.h b/src/include/hash_table.h
new file mode 100644
index 0000000..df2d368
--- /dev/null
+++ b/src/include/hash_table.h
@@ -0,0 +1,47 @@
+#ifndef HASH_TABLE_H
+#define HASH_TABLE_H
+#include "./ast.h"
+
+typedef struct {
+ char *key;
+ ast_t *value;
+} pair_t;
+
+typedef struct SL_NODE_STRUCT {
+ pair_t *value;
+ struct SL_NODE_STRUCT *next;
+} sl_node_t;
+
+typedef struct {
+ sl_node_t *head;
+ int size;
+} sl_list_t;
+
+typedef struct {
+ int size;
+ sl_list_t **buckets;
+} hash_table_t;
+
+pair_t *init_pair(char *key, ast_t *value);
+
+sl_node_t *init_sl_node(char *key, ast_t *value);
+
+sl_list_t *init_sl_list();
+
+void sl_list_add(sl_list_t *l, char *key, ast_t *value);
+
+ast_t *sl_list_get(sl_list_t *l, char *key);
+
+void sl_list_free(sl_list_t *l);
+
+hash_table_t *init_hash_table(int size);
+
+void hash_table_add(hash_table_t *h, char *key, ast_t *value);
+
+ast_t *hash_table_get(hash_table_t *h, char *key);
+
+unsigned long hash(char *key, int size);
+
+void hash_table_free(hash_table_t *h);
+
+#endif
diff --git a/src/include/lexer.h b/src/include/lexer.h
new file mode 100644
index 0000000..36c5db8
--- /dev/null
+++ b/src/include/lexer.h
@@ -0,0 +1,39 @@
+#ifndef LEXER_H
+#define LEXER_H
+
+#include "./token.h"
+
+#include <stdbool.h>
+
+typedef struct {
+ char *source;
+ int i;
+ char c;
+
+ int row;
+ int col;
+ bool finished;
+} lexer_t;
+
+void lexer_reset(lexer_t *lexer, char *source);
+
+lexer_t *init_lexer(char *source);
+
+void lexer_move(lexer_t *lexer);
+
+void lexer_skip_whitespace(lexer_t *lexer);
+
+void lexer_skip_comment(lexer_t *lexer);
+
+token_t *lexer_collect_bool(lexer_t *lexer);
+
+token_t *lexer_collect_id(lexer_t *lexer);
+
+token_t *lexer_collect_num(lexer_t *lexer);
+
+token_t *lexer_collect_string(lexer_t *lexer);
+
+token_t *lexer_collect_next(lexer_t *lexer);
+
+void lexer_error(lexer_t *lexer);
+#endif
diff --git a/src/include/macros.h b/src/include/macros.h
new file mode 100644
index 0000000..ee89597
--- /dev/null
+++ b/src/include/macros.h
@@ -0,0 +1,85 @@
+#ifndef MACROS_H
+#define MACROS_H
+/*
+ * This is free and unencumbered software released into the public domain.
+ * For more information, please refer to <https://unlicense.org>. Code snippet
+ * taken from
+ * https://gist.github.com/RabaDabaDoba/145049536f815903c79944599c6f952a.
+ */
+
+/* Regular text */
+#define BLK "\e[0;30m"
+#define RED "\e[0;31m"
+#define GRN "\e[0;32m"
+#define YEL "\e[0;33m"
+#define BLU "\e[0;34m"
+#define MAG "\e[0;35m"
+#define CYN "\e[0;36m"
+#define WHT "\e[0;37m"
+
+/* Regular bold text */
+#define BBLK "\e[1;30m"
+#define BRED "\e[1;31m"
+#define BGRN "\e[1;32m"
+#define BYEL "\e[1;33m"
+#define BBLU "\e[1;34m"
+#define BMAG "\e[1;35m"
+#define BCYN "\e[1;36m"
+#define BWHT "\e[1;37m"
+
+/* Regular underline text */
+#define UBLK "\e[4;30m"
+#define URED "\e[4;31m"
+#define UGRN "\e[4;32m"
+#define UYEL "\e[4;33m"
+#define UBLU "\e[4;34m"
+#define UMAG "\e[4;35m"
+#define UCYN "\e[4;36m"
+#define UWHT "\e[4;37m"
+
+/* Regular background */
+#define BLKB "\e[40m"
+#define REDB "\e[41m"
+#define GRNB "\e[42m"
+#define YELB "\e[43m"
+#define BLUB "\e[44m"
+#define MAGB "\e[45m"
+#define CYNB "\e[46m"
+#define WHTB "\e[47m"
+
+/* High intensty background */
+#define BLKHB "\e[0;100m"
+#define REDHB "\e[0;101m"
+#define GRNHB "\e[0;102m"
+#define YELHB "\e[0;103m"
+#define BLUHB "\e[0;104m"
+#define MAGHB "\e[0;105m"
+#define CYNHB "\e[0;106m"
+#define WHTHB "\e[0;107m"
+
+/* High intensty text */
+#define HBLK "\e[0;90m"
+#define HRED "\e[0;91m"
+#define HGRN "\e[0;92m"
+#define HYEL "\e[0;93m"
+#define HBLU "\e[0;94m"
+#define HMAG "\e[0;95m"
+#define HCYN "\e[0;96m"
+#define HWHT "\e[0;97m"
+
+/* Bold high intensity text */
+#define BHBLK "\e[1;90m"
+#define BHRED "\e[1;91m"
+#define BHGRN "\e[1;92m"
+#define BHYEL "\e[1;93m"
+#define BHBLU "\e[1;94m"
+#define BHMAG "\e[1;95m"
+#define BHCYN "\e[1;96m"
+#define BHWHT "\e[1;97m"
+
+#define reset "\e[0m"
+#define CRESET "\e[0m"
+#define COLOR_RESET "\e[0m"
+
+void die(char *message);
+#endif
diff --git a/src/include/parser.h b/src/include/parser.h
new file mode 100644
index 0000000..97a7fd9
--- /dev/null
+++ b/src/include/parser.h
@@ -0,0 +1,49 @@
+#ifndef PARSER_H
+#define PARSER_H
+#include "./ast.h"
+#include "./hash_table.h"
+#include "./lexer.h"
+#include "./token.h"
+#include <stdbool.h>
+#include <stdio.h>
+
+typedef struct {
+ token_t **tokens;
+ hash_table_t *symbol_table;
+ int i;
+ int size;
+ bool finished;
+} parser_t;
+
+parser_t *init_parser(lexer_t *lexer);
+
+void parser_error(parser_t *parser);
+
+void parser_move(parser_t *parser);
+
+void parser_eat(parser_t *parser, token_t *token);
+
+ast_t *parse_string(parser_t *parser);
+
+ast_t *parse_int(parser_t *parser);
+
+ast_t *parse_float(parser_t *parser);
+
+ast_t *parse_bool(parser_t *parser);
+
+ast_t *parse_list(parser_t *parser);
+
+ast_t *parse_quote(parser_t *parser);
+
+ast_t *parse_symbol(parser_t *parser);
+
+ast_t *parse_function(parser_t *parser);
+
+ast_t *parse_list(parser_t *parser);
+
+ast_t *parse_bind(parser_t *parser);
+
+ast_t *parse_expr(parser_t *parser);
+
+ast_t *read_in(char *s);
+#endif
diff --git a/src/include/print.h b/src/include/print.h
new file mode 100644
index 0000000..9dbecbd
--- /dev/null
+++ b/src/include/print.h
@@ -0,0 +1,16 @@
+#ifndef PRINT_H
+#define PRINT_H
+#include "./ast.h"
+
+void print_string(ast_t *str);
+
+void print_int(ast_t *i);
+
+void print_bool(ast_t *b);
+
+void print_float(ast_t *f);
+
+void print_func(ast_t *f);
+
+void print(ast_t *res);
+#endif
diff --git a/src/include/sl_list.h b/src/include/sl_list.h
new file mode 100644
index 0000000..5a7c130
--- /dev/null
+++ b/src/include/sl_list.h
@@ -0,0 +1,23 @@
+#ifndef SL_LIST_H
+#define SL_LIST_H
+
+typedef struct SL_NODE_STRUCT {
+ void *p1;
+ void *p2;
+} sl_node_t;
+
+typedef struct {
+ sl_node_t *head;
+ int size;
+} sl_list_t;
+
+sl_node_t *init_sl_node(void *value);
+
+sl_list_t *init_sl_list();
+
+void sl_list_add(sl_list_t *l, void *value, int i);
+
+void sl_list_del(sl_list_t *l, int i);
+
+void *sl_list_get(sl_list_t *l, int i);
+#endif
diff --git a/src/include/stack.h b/src/include/stack.h
new file mode 100644
index 0000000..e8047c8
--- /dev/null
+++ b/src/include/stack.h
@@ -0,0 +1,16 @@
+#ifndef STACK_H
+#define STACK_H
+#include "./hash_table.h"
+typedef struct {
+ hash_table_t **stack;
+ int cur;
+} stack_t;
+
+stack_t *init_stack(int ht_size);
+
+void stack_push(stack_t *s, hash_table_t *h);
+
+hash_table_t *stack_peek(stack_t *s);
+
+hash_table_t *stack_pop(stack_t *s);
+#endif
diff --git a/src/include/token.h b/src/include/token.h
new file mode 100644
index 0000000..83c595b
--- /dev/null
+++ b/src/include/token.h
@@ -0,0 +1,22 @@
+#ifndef TOKEN_H
+#define TOKEN_H
+
+typedef struct {
+ enum {
+ TOKEN_ID,
+ TOKEN_LPAREN,
+ TOKEN_RPAREN,
+ TOKEN_BOOL,
+ TOKEN_INT,
+ TOKEN_FLOAT,
+ TOKEN_STRING,
+ TOKEN_QUOTE,
+ TOKEN_PERIOD,
+ } type;
+ char *value;
+ int row;
+ int col;
+} token_t;
+
+token_t *init_token(int type, char *value, int row, int col);
+#endif
diff --git a/src/include/visitor.h b/src/include/visitor.h
new file mode 100644
index 0000000..87f2853
--- /dev/null
+++ b/src/include/visitor.h
@@ -0,0 +1,26 @@
+#ifndef VISITOR_H
+#define VISITOR_H
+#include "./ast.h"
+#include "./hash_table.h"
+#include "./stack.h"
+
+typedef struct {
+ hash_table_t *symbol_table;
+ stack_t *stack_frame;
+ ast_t *root;
+} visitor_t;
+
+void eval_error(visitor_t *v);
+
+visitor_t *init_visitor(ast_t *root);
+
+bool is_self_evaluating(ast_t *e);
+
+ast_t *eval_symbol(visitor_t *v, ast_t *e);
+
+ast_t *eval_list(visitor_t *v, ast_t *e);
+
+ast_t *eval_expr(visitor_t *v, ast_t *e);
+
+ast_t *eval(visitor_t *v);
+#endif