diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/lyn/ast.h | 104 | ||||
| -rw-r--r-- | include/lyn/debug.h | 98 | ||||
| -rw-r--r-- | include/lyn/lyn.h | 12 | ||||
| -rw-r--r-- | include/lyn/parser.h | 57 | 
4 files changed, 271 insertions, 0 deletions
| diff --git a/include/lyn/ast.h b/include/lyn/ast.h new file mode 100644 index 0000000..fd3b98b --- /dev/null +++ b/include/lyn/ast.h @@ -0,0 +1,104 @@ +#ifndef LYN_AST_H +#define LYN_AST_H + +#include <stdlib.h> +#include <string.h> + +enum kind { +	LYN_ID, LYN_STR, LYN_INT, LYN_FLOAT, LYN_CMD, LYN_LIST, LYN_APPLY +}; + +struct ast { +	enum kind kind; +	union { +		long long i; +		double d; +		const char *s; +		struct ast *args; +	}; +	struct ast *next; +}; + +static inline struct ast *gen_id(const char *s) +{ +	struct ast *id = calloc(1, sizeof(struct ast)); +	if (!id) +		return NULL; + +	id->kind = LYN_ID; +	id->s = strdup(s); +	return id; +} + +static inline struct ast *gen_str(const char *s) +{ +	struct ast *str = calloc(1, sizeof(struct ast)); +	if (!str) +		return NULL; + +	str->kind = LYN_STR; +	str->s = strdup(s); +	return str; +} + +static inline struct ast *gen_int(long long i) +{ +	struct ast *integer = calloc(1, sizeof(struct ast)); +	if (!integer) +		return NULL; + +	integer->kind = LYN_INT; +	integer->i = i; +	return integer; +} + +static inline struct ast *gen_float(double d) +{ +	struct ast *floating = calloc(1, sizeof(struct ast)); +	if (!floating) +		return NULL; + +	floating->kind = LYN_FLOAT; +	floating->d = d; +	return floating; +} + +static inline struct ast *gen_apply(struct ast *cmds) +{ +	struct ast *apply = calloc(1, sizeof(struct ast)); +	if (!apply) +		return NULL; + +	apply->kind = LYN_APPLY; +	apply->args = cmds; +	return apply; +} + +static inline struct ast *gen_list(struct ast *cmds) +{ +	struct ast *list = calloc(1, sizeof(struct ast)); +	if (!list) +		return NULL; + +	list->kind = LYN_LIST; +	list->args = cmds; +	return list; +} + +static inline struct ast *gen_cmd(struct ast *args) +{ +	struct ast *cmd = calloc(1, sizeof(struct ast)); +	if (!cmd) +		return NULL; + +	cmd->kind = LYN_CMD; +	cmd->args = args; +	return cmd; +} + +void ast_dump(int depth, struct ast *ast); +void ast_dump_list(int depth, struct ast *ast); + +struct ast *reverse_ast_list(struct ast *ast); + +#endif /* LYN_AST_H */ diff --git a/include/lyn/debug.h b/include/lyn/debug.h new file mode 100644 index 0000000..3f78f17 --- /dev/null +++ b/include/lyn/debug.h @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#ifndef EK_DEBUG_H +#define EK_DEBUG_H + +/** + * @file debug.h + * + * Debugging and general information printing helpers. + */ + +#include <stdio.h> + +#if DEBUG +/** + * Print debugging message. Only active if \c DEBUG is defined, + * + * @param x Format string. Follows standard printf() formatting. + */ +#define debug(x, ...) \ +	do {fprintf(stderr, "debug: " x "\n",##__VA_ARGS__);} while(0) +#else +#define debug(x, ...) +#endif + +/** + * Print error message. + * + * @param x Format string. Follows standard printf() formatting. + */ +#define error(x, ...) \ +	do {fprintf(stderr, "error: " x "\n",##__VA_ARGS__);} while(0) + +/** + * Print warning message. + * + * @param x Format string. Follows standard printf() formatting. + */ +#define warn(x, ...) \ +	do {fprintf(stderr, "warn: " x "\n",##__VA_ARGS__);} while(0) + +/** + * Print info message. + * + * @param x Format string. Follows standard printf() formatting. + */ +#define info(x, ...) \ +	do {fprintf(stderr, "info: " x "\n",##__VA_ARGS__);} while(0) + +/** Keeps track of file name and file buffer. */ +struct file_ctx { +	/** File name. */ +	const char *fname; +	/** File buffer. */ +	const char *fbuf; +}; + +/** Represents a source location, spanning over some bit of code. */ +struct src_loc { +	/** First line of interesting text. */ +	int first_line; +	/** Last line of interesting text. */ +	int last_line; +	/** First column in first line of interesting text. */ +	int first_col; +	/** Last column in last line of interesting text. */ +	int last_col; +}; + +/** Issue categorization. */ +enum issue_level { +	/** Information. */ +	SRC_INFO, +	/** Warning. */ +	SRC_WARN, +	/** Error. */ +	SRC_ERROR +}; + +/** Context for issue in user code. */ +struct src_issue { +	/** How bad the issue is. */ +	enum issue_level level; +	/** Where the issue happened relative to file buffer. */ +	struct src_loc loc; +	/** File context issue happened in. */ +	struct file_ctx fctx; +}; + +/** + * Print a source issue. + * + * @param issue Context for issue. + * @param err_msg Format string. Follows standard printf() formatting. + */ +void src_issue(struct src_issue issue, const char *err_msg, ...); +#endif /* EK_DEBUG_H */ diff --git a/include/lyn/lyn.h b/include/lyn/lyn.h new file mode 100644 index 0000000..43094ca --- /dev/null +++ b/include/lyn/lyn.h @@ -0,0 +1,12 @@ +#ifndef LYN_H +#define LYN_H + +struct lyn { +}; + +struct lyn lyn_create(); +int lyn_eval_file(struct lyn *lyn, const char *fname); +int lyn_eval_str(struct lyn *lyn, const char *name, const char *str); +void lyn_destroy(struct lyn *lyn); + +#endif /* LYN_H */ diff --git a/include/lyn/parser.h b/include/lyn/parser.h new file mode 100644 index 0000000..81ecc49 --- /dev/null +++ b/include/lyn/parser.h @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#ifndef PARSER_H +#define PARSER_H + +/** + * @file parser.h + * + * Glue file to get lexer and parser to play nice. + */ + +#include <stddef.h> +#include <stdbool.h> +#include <lyn/ast.h> + +/** Stuff the parser needs to do its job. */ +struct parser { +	/** Whether parsing failed or succeeded. */ +	bool failed; +	/** Lexer. Parser owns the lexer and is responsible for initializing +	 * and destroyint the lexer. +	 */ +	void *lexer; + +	struct ast *tree; + +	/** File content in memory. */ +	const char *buf; +	/** Filename. */ +	const char *fname; +}; + +/** + * Create new parser. + * + * @return Created parser. + */ +struct parser *create_parser(); + +/** + * Destroy parser. + * + * @param p Parser to destroy. + */ +void destroy_parser(struct parser *p); + +/** + * Run parser on buffer \p buf with name \p fname. + * + * @param p Parser to run. + * @param fname Name of file \p buf was read from. + * @param buf Contents of \p fname. + */ +void parse(struct parser *p, const char *fname, const char *buf); + +#endif /* PARSER_H */ | 
