1. Armin Ronacher
  2. arana-main

Source

arana-main / arana / arana.h

/*
 * arana.h
 *
 * (c) Copyright 2009 by Armin Ronacher.
 */

#ifndef _ARANA_H
#define _ARANA_H

/* headers we need */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gc/gc.h"

/* forward declarated stuff */
struct _ArInterpreter;
struct _ArFrame;

/* typedefs */
typedef size_t Ar_size_t;
typedef ssize_t Ar_ssize_t;
typedef unsigned long AR;

/* arana function signatures.  AI is the current interpreter, C
   the active execution context. */
#define AR_SIG struct _ArInterpreter *AI, struct _ArFrame *CF
#define AR_SIG_LASTARG CF
#define AR_ISIG AI, CF

/* memory helpers */
#define AR_ALLOC(x) GC_MALLOC(x)
#define AR_ALLOCN(x, n) GC_MALLOC((n) * (x))
#define AR_ALLOCTE(x, e) GC_MALLOC(sizeof(x) + (e))
#define AR_ALLOCT(x) AR_ALLOCTE(x, 0)
#define AR_ALLOCTN(x, n) GC_MALLOC((n) * sizeof(x))
/* the "atomic" version, for memory that won't contain pointers */
#define AR_ALLOCA(x) GC_MALLOC_ATOMIC(x)
#define AR_ALLOCNA(x, n) GC_MALLOC_ATOMIC((n) * (x))
#define AR_ALLOCTEA(x, e) GC_MALLOC_ATOMIC(sizeof(x) + (e))
#define AR_ALLOCTA(x) AR_ALLOCTEA(x, 0)
#define AR_ALLOCTNA(x, n) GC_MALLOC_ATOMIC((n) * sizeof(x))
/* realloc figures out atomic or not for itself */
#define AR_REALLOC(p, x) GC_REALLOC(p, x)
#define AR_REALLOCN(p, x, n) GC_REALLOC(p, (n) * (x))
#define AR_REALLOCTE(p, x, e) GC_REALLOC(p, sizeof(x) + (e))
#define AR_REALLOCT(p, x) AR_REALLOCTE(p, x, 0)
#define AR_REALLOCTN(p, x, n) GC_REALLOC(p, (n) * sizeof(x))
/* citing Boehm: "If you insist..." */
#define AR_FREE(x) GC_FREE((void *)x)
#define AR_COPY_CHARP(s) _arana_copy_charp(s)

/* various helpers */
#define AR_FATAL(...) (fprintf(stderr, __VA_ARGS__), exit(EXIT_FAILURE))

/* helpers for externals.  Should fight portability problems */
#define AR_API_FUNC(x) x
#define AR_API_DATA(x) extern x

/* build configuration defines */
#include "config.h"
/* the version information */
#include "_version.h"
/* other headers */
#include "object.h"
#include "error.h"
#include "parse_args.h"
#include "stringmap.h"
#include "primitives.h"
#include "string.h"
#include "tuple.h"
#include "list.h"
#include "float.h"
#include "range.h"
#include "file.h"
#include "interpreter.h"
#include "repl.h"
#include "ast.h"
#include "compile.h"
#include "eval.h"
#include "grammar.h"
#include "lexnparse.h"
/* core library because string to_string functions use it */
#include "utils/stringbuilder.h"

/* helper for AR_TYPE */
static inline ArObjectType *
_arana_get_object_type(AR self)
{
    /* null is NULL, no type for that, only error signalling */
    switch (self) {
    case AR_NULL:
        return NULL;
    case AR_NONE:
        return &ArNoneType;
    case AR_TRUE:
    case AR_FALSE:
        return &ArBooleanType;
    default:
        if (AR_IS_INTEGER(self))
            return &ArIntegerType;
        return ((ArObject *)self)->_type;
    }
}

/* helper for AR_TYPE_NAME */
static inline const char *
_arana_get_object_type_name(AR self)
{
    ArObjectType *type = AR_TYPE(self);
    if (!type)
        return "<NULL>";
    else if (!type->name)
        return "<UnknownType>";
    return type->name;
}

/* helper for AR_COPY_CHARP */
static inline char *
_arana_copy_charp(char *string)
{
    Ar_size_t length = strlen(string) + 1;
    char *copy = AR_ALLOCA(length);
    memcpy(copy, string, length);
    return copy;
}

#endif /* _ARANA_H */