From 35fc0e7fd15c8a9ab7e94600ecd755e285cb8744 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 31 Mar 2018 15:54:59 -0700 Subject: [PATCH] Begin the implementation of a type. --- CMakeLists.txt | 2 +- include/type.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/type.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 include/type.h create mode 100644 src/type.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d35f0c3..c3c6030 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(libabacus) add_compile_options(-pedantic -Wall) -add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c) +add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/type.c) add_executable(libabacus src/main.c) add_subdirectory(external/liblex) diff --git a/include/type.h b/include/type.h new file mode 100644 index 0000000..0ff0f04 --- /dev/null +++ b/include/type.h @@ -0,0 +1,40 @@ +#ifndef LIBABACUS_TYPE_H +#define LIBABACUS_TYPE_H + +#include "vec.h" +#include "result.h" +#include "refcount.h" +#include "parsetype.h" +#include "table.h" + +/** + * A type in libabacus. + */ +struct libab_type_s { + /** + * The name of the type. + * This is also a unique identifier, + * used to compare types. + */ + char* name; +}; + +typedef struct libab_type_s libab_type; + +/** + * Creates a new instance of a type, with no type parameters, and + * the given name. + * @param ref the reference to populate with the new type. + * @param name the name to use. + * @return the result of the creation function, which can fail. + */ +libab_result libab_type_create(libab_ref* ref, const char* name); +/** + * Constructs a new instance of a type from the given parsetype. + * @param ref the reference to populate with the new type. + * @param type the parse type to load. + * @param table the table to use for looking up types. + */ +libab_result libab_type_from_parsetype(libab_ref* ref, libab_parsetype* type, libab_table* table); + +#endif diff --git a/src/type.c b/src/type.c new file mode 100644 index 0000000..983a629 --- /dev/null +++ b/src/type.c @@ -0,0 +1,28 @@ +#include "type.h" +#include "util.h" +#include + +void _libab_type_free(void* type) { + free(((libab_type*) type)->name); +} + +libab_result libab_type_create(libab_ref* ref, const char* name) { + libab_type* type; + libab_result result = LIBAB_SUCCESS; + if((type = malloc(sizeof(*type)))) { + result = libab_copy_string(&type->name, name); + } else { + result = LIBAB_MALLOC; + } + + if(result == LIBAB_SUCCESS) { + result = libab_ref_new(ref, type, _libab_type_free); + if(result != LIBAB_SUCCESS) free(type->name); + } + + if(result != LIBAB_SUCCESS) { + free(type); + } + + return result; +}