Add an implementation of base types.

These will hold information about a type such as (possibly) the
interfaces it implements and the parameters it accepts.
This commit is contained in:
Danila Fedorin 2018-04-10 23:37:59 -07:00
parent 0cfc1df02c
commit d146b71e56
3 changed files with 76 additions and 1 deletions

View File

@ -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 src/ref_vec.c src/ref_trie.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/ref_vec.c src/ref_trie.c src/basetype.c)
add_executable(libabacus src/main.c)
add_subdirectory(external/liblex)

65
include/basetype.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef LIBABACUS_BASETYPE_H
#define LIBABACUS_BASETYPE_H
#include "vec.h"
#include "result.h"
/**
* An enum that represents the various
* types of the basetype parameters.
*/
enum libab_basetype_variant_e {
BT_EMPTY,
BT_NAME,
BT_LIST
};
/**
* A struct that holds information about the basetype
* parameter, such as a type name or a "..." (called a list).
*/
struct libab_basetype_param_s {
/**
* The variant of this type parameter.
*/
enum libab_basetype_variant_e variant;
/**
* The name of the parameter, if one is appropriate.
*/
const char* name;
};
/**
* A struct that holds all the information about a base type.
*/
struct libab_basetype_s {
/**
* The function used to free the value.
*/
void (*free_function)(void*);
/**
* The parameters of the type. This is a constant array, and
* doesn't need memory allocation.
*/
const struct libab_basetype_param_s* params;
/**
* The size of the param aray.
*/
int count;
};
typedef enum libab_basetype_variant_e libab_basetype_variant;
typedef struct libab_basetype_param_s libab_basetype_param;
typedef struct libab_basetype_s libab_basetype;
/**
* Initializes a base type, including all the parameters that it needs
* to take.
* @param basetype the type that is being initialized.
* @param n the number of type parameters it has.
* @param params the parameters this basetype accepts.
*/
void libab_basetype_init(libab_basetype* basetype, int n,
const libab_basetype_param params[]);
#endif

10
src/basetype.c Normal file
View File

@ -0,0 +1,10 @@
#include "basetype.h"
#include "util.h"
#include <stdlib.h>
void libab_basetype_init(libab_basetype* basetype,
int n, const libab_basetype_param params[]) {
basetype->params = params;
basetype->count = n;
}