diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/lyn/ast.h | 89 | ||||
| -rw-r--r-- | include/lyn/lyn.h | 72 | ||||
| -rw-r--r-- | include/lyn/parser.h | 2 | ||||
| -rw-r--r-- | include/lyn/vec.h | 46 | 
4 files changed, 137 insertions, 72 deletions
| diff --git a/include/lyn/ast.h b/include/lyn/ast.h index fd3b98b..ba9a0d5 100644 --- a/include/lyn/ast.h +++ b/include/lyn/ast.h @@ -3,102 +3,49 @@  #include <stdlib.h>  #include <string.h> +#include <lyn/vec.h> -enum kind { -	LYN_ID, LYN_STR, LYN_INT, LYN_FLOAT, LYN_CMD, LYN_LIST, LYN_APPLY +enum lyn_kind { +	LYN_ID, LYN_STR, LYN_INT, LYN_FLOAT, LYN_CMD, LYN_GROUP, LYN_APPLY  }; -struct ast { -	enum kind kind; +struct lyn_value { +	enum lyn_kind kind;  	union {  		long long i;  		double d;  		const char *s; -		struct ast *args; +		struct vec args;  	}; -	struct ast *next;  }; -static inline struct ast *gen_id(const char *s) +static inline struct lyn_value 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; +	return (struct lyn_value){.kind = LYN_ID, .s = strdup(s)};  } -static inline struct ast *gen_str(const char *s) +static inline struct lyn_value 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; +	return (struct lyn_value){.kind = LYN_STR, .s = strdup(s)};  } -static inline struct ast *gen_int(long long i) +static inline struct lyn_value 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; +	return (struct lyn_value){.kind = LYN_INT, .i = i};  } -static inline struct ast *gen_apply(struct ast *cmds) +static inline struct lyn_value gen_float(double d)  { -	struct ast *apply = calloc(1, sizeof(struct ast)); -	if (!apply) -		return NULL; - -	apply->kind = LYN_APPLY; -	apply->args = cmds; -	return apply; +	return (struct lyn_value){.kind = LYN_FLOAT, .d = d};  } -static inline struct ast *gen_list(struct ast *cmds) +static inline struct lyn_value gen_list()  { -	struct ast *list = calloc(1, sizeof(struct ast)); -	if (!list) -		return NULL; - -	list->kind = LYN_LIST; -	list->args = cmds; +	struct lyn_value list; +	list.args = vec_create(sizeof(struct lyn_value));  	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); +void ast_dump(int depth, struct lyn_value val);  #endif /* LYN_AST_H */ diff --git a/include/lyn/lyn.h b/include/lyn/lyn.h index 43094ca..27a2fe3 100644 --- a/include/lyn/lyn.h +++ b/include/lyn/lyn.h @@ -1,12 +1,84 @@  #ifndef LYN_H  #define LYN_H +#include <lyn/parser.h> +#include <lyn/vec.h> + +#define lyn_at(v, i)\ +	vect_at(struct lyn_value, v, i) + +struct lyn_scope { +	struct lyn_scope *parent; +	struct vec visible; +}; +  struct lyn { +	struct lyn_scope *root; +	struct lyn_scope *cur; + +	struct lyn_value res; +}; + +typedef int (*lyn_call)(struct lyn *lyn, struct vec args); + +struct lyn_symbol { +	enum lyn_symbol_kind { +		LYN_BUILTIN_SYNTAX, +		LYN_BUILTIN_PROC, +		LYN_PROC, +		LYN_SYNTAX, +		LYN_VAR, +	} kind; + +	union { +		lyn_call call; +		struct lyn_value value; +	};  }; +static inline struct lyn_symbol lyn_syntax(lyn_call call) +{ +	return (struct lyn_symbol){ +		.kind = LYN_BUILTIN_SYNTAX, +		.call = call +	}; +} + +static inline struct lyn_symbol lyn_proc(lyn_call call) +{ +	return (struct lyn_symbol){ +		.kind = LYN_BUILTIN_PROC, +		.call = call +	}; +} + +static inline struct lyn_symbol lyn_var(struct lyn_value value) +{ +	return (struct lyn_symbol){ +		.kind = LYN_VAR, +		.value = value +	}; +} +  struct lyn lyn_create(); +int lyn_init(struct lyn *lyn); + +int lyn_eval(struct lyn *lyn, struct lyn_value value);  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); +int lyn_new_frame(struct lyn *lyn, lyn_call call, struct lyn_value args); +int lyn_apply_cmd(struct lyn *lyn, struct lyn_symbol *symb, struct vec args); + +struct lyn_symbol *lyn_lookup_symbol(struct lyn *lyn, const char *name); + +int lyn_create_symbol(struct lyn *lyn, const char *name, struct lyn_symbol symb); +int lyn_replace_symbol(struct lyn *lyn, const char *name, struct lyn_symbol symb); + +int lyn_create_scope(struct lyn *lyn); + +void lyn_return(struct lyn *lyn, struct lyn_value res); +struct lyn_value lyn_res(struct lyn *lyn); +  #endif /* LYN_H */ diff --git a/include/lyn/parser.h b/include/lyn/parser.h index 81ecc49..105382e 100644 --- a/include/lyn/parser.h +++ b/include/lyn/parser.h @@ -23,7 +23,7 @@ struct parser {  	 */  	void *lexer; -	struct ast *tree; +	struct lyn_value tree;  	/** File content in memory. */  	const char *buf; diff --git a/include/lyn/vec.h b/include/lyn/vec.h new file mode 100644 index 0000000..08a608a --- /dev/null +++ b/include/lyn/vec.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ + +#ifndef VEC_H +#define VEC_H + +#include <stddef.h> + +struct vec { +	size_t n; +	size_t s; +	size_t ns; +	void *buf; +}; + +struct vec vec_create(size_t s); +void vec_destroy(struct vec *v); +void vec_reset(struct vec *v); + +size_t vec_len(struct vec *v); +void *vec_at(struct vec *v, size_t i); +void *vec_back(struct vec *v); +void *vec_pop(struct vec *v); +void vec_append(struct vec *v, void *n); + +typedef int (*vec_comp_t)(const void *, const void *); +void vec_sort(struct vec *v, vec_comp_t comp); + +#define foreach_vec(iter, v) \ +	for (size_t iter = 0; iter < vec_len(&v); ++iter) + +#define vect_at(type, v, i) \ +	*(type *)vec_at(&v, i) + +#define vect_append(type, v, e) \ +	vec_append(&v, (type *)(e)) + +#define vect_back(type, v) \ +	*(type *)vec_back(&v) + +#define vect_pop(type, v) \ +	*(type *)vec_pop(&v) + +#define vec_uninit(v) \ +	(v.buf == NULL) + +#endif /* VEC_H */ | 
