From 80abbf2be25e4c760fe80c9f6694047db6c7cfe4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 9 Apr 2015 01:37:18 +0200 Subject: Emit alloc and free functions --- src/output.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/output.cpp b/src/output.cpp index e1ca496..7f074ab 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -96,7 +96,12 @@ void output_t::emit_header() { emit_tokens(); emit_token_value(); - std::fprintf(header_file, "typedef struct %scontext %scontext_t;\n", prefix(), prefix()); + std::fprintf(header_file, "typedef struct %scontext %scontext_t;\n\n", prefix(), prefix()); + + std::fprintf(header_file, "%scontext_t * %salloc(void *(*alloc_func)(size_t));\n", prefix(), prefix()); + std::fprintf(header_file, "void %sfree(%scontext_t *parser, void (*free_func)(void *));\n\n", prefix(), prefix()); + + std::fprintf(header_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value);\n", prefix(), prefix(), prefix()); } void output_t::emit_reduction(unsigned rule_id) { @@ -295,7 +300,15 @@ void output_t::emit_source() { std::fprintf(source_file, "struct %scontext {\n", prefix()); std::fprintf(source_file, "\tunsigned top;\n"); std::fprintf(source_file, "\t%scontext_state_t stack[%u];\n", prefix(), stack_size); - std::fprintf(source_file, "};\n\n"); + std::fprintf(source_file, "};\n\n\n"); + + std::fprintf(source_file, "%scontext_t * %salloc(void *(*alloc_func)(size_t)) {\n", prefix(), prefix()); + std::fprintf(source_file, "\treturn (%scontext_t *)alloc_func(sizeof(%scontext_t));\n", prefix(), prefix()); + std::fprintf(source_file, "}\n\n"); + + std::fprintf(source_file, "void %sfree(%scontext_t *parser, void (*free_func)(void *)) {\n", prefix(), prefix()); + std::fprintf(source_file, "\tfree_func(parser);\n"); + std::fprintf(source_file, "}\n\n"); emit_reductions(); -- cgit v1.2.3