summaryrefslogtreecommitdiff
path: root/libgpython/include/gpython/gpython.h
blob: 983bef0f264d6e74c47e23f6c0a022ba8fd2c7c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef __GCC_GPYTHON_H__
#define __GCC_GPYTHON_H__

#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
#  define __func__ __FUNCTION__
# else
#  define __func__ "<unknown>"
# endif
#endif

/* Abstract out some useful compiler attributes from GCC */
#if defined( GUNC )
# define __gpy_fmt_check( x, y )		\
  __attribute__ ((format (printf, x, y)))
# define __gpy_no_return			\
  __attribute__ ((noreturn))
# define __gpy_malloc				\
  __attribute__ ((malloc))
# define __gpy_pure				\
  __attribute__ ((pure))
# define __gpy_unused				\
  __attribute__ ((unused))
# define __gpy_nonnull				\
  __attribute__((nonnull)) 
#else
# define __gpy_fmt_check( x, y )
# define __gpy_no_return
# define __gpy_malloc
# define __gpy_pure
# define __gpy_unused
# define __gpy_nonnull
#endif

typedef struct gpy_rr_object_state_t {
  char * obj_t_ident;
  signed long ref_count;
  void * self;
} gpy_rr_object_state_t ;

#define Gpy_Object_State_Init( x )			\
  x = gpy_malloc( sizeof(gpy_rr_object_state_t) );	\
  debug("object created at <%p>!\n", (void*)x );	\
  x->obj_t_ident = NULL; x->ref_count = 0;		\
  x->self = NULL;

#define Gpy_Object_State_Init_Ctx( x,y )		\
  x = gpy_malloc( sizeof(gpy_rr_object_state_t) );	\
  x->obj_t_ident = NULL; x->ref_count = 0;		\
  x->self = NULL;					\
  gpy_vec_push( y->vector[y->length-1]->symbols, x );	\
  debug("object created at <%p> within conext <%p>!",	\
	(void *)x, (void*)y->vector[y->length-1]->symbols );

typedef gpy_rr_object_state_t * gpy_object_state_t;
#define NULL_OBJ_STATE  NULL

typedef gpy_object_state_t (*binary_op)( gpy_object_state_t,
					 gpy_object_state_t );

enum GPY_LIT_T { TYPE_INTEGER, TYPE_STRING, TYPE_NONE };

typedef struct gpy_rr_literal_t {
  enum GPY_LIT_T type;
  union {
    int integer;
    char * string;
    /* ... */
  } literal ;
} gpy_literal_t ;

#define Gpy_Lit_Init( x )			\
  x = gpy_malloc( sizeof(gpy_literal_t) );	\
  x->type = TYPE_NONE;				\
  x->literal.integer = 0;

typedef struct gpy_number_prot_t
{
  bool init;

  binary_op n_add;
  binary_op n_sub;
  binary_op n_div;
  binary_op n_mul;
  binary_op n_pow;

  binary_op n_let;
  binary_op n_lee;
  binary_op n_get;
  binary_op n_gee;
  binary_op n_eee;
  binary_op n_nee;
  binary_op n_orr;
  binary_op n_and;

} gpy_num_prot_t ;

typedef struct gpy_type_obj_def_t {
  char * identifier;
  size_t builtin_type_size;
  void * (*init_hook)( gpy_literal_t * );
  void (*destroy_hook)( void * );
  void (*print_hook)( void * , FILE * , bool );
  const struct gpy_number_prot_t * binary_protocol;
} gpy_type_obj_def_t ;

#ifdef DEBUG
# define gpy_assert( expr )						\
  ((expr) ? (void) 0 : gpy_assertion_failed( #expr, __LINE__,		\
					     __FILE__, __func__ ));
#else
# define gpy_assert( expr )
#endif

extern void * gpy_malloc( size_t );

extern void * gpy_realloc( void * , size_t );

extern void * gpy_calloc( size_t , size_t );

#define gpy_free( x ) \
  gpy_assert( x );    \
  free( x );	      \
  x = NULL;

#ifdef HAVE_STRDUP
# define gpy_strdup( x )			\
  strdup( x )
#endif

#ifdef DEBUG
extern void
__gpy_debug__( const char *, unsigned ,
               const char *, const char *, ... )
  __gpy_fmt_check(4,5) ;
#endif

extern void
__gpy_error__( const char *, unsigned ,
               const char *, const char *, ... )
  __gpy_fmt_check(4,5) ;

extern void
__gpy_fatal__( const char *, unsigned ,
               const char *, const char *, ... )
  __gpy_fmt_check(4,5) ;

#ifdef DEBUG
# define debug( ... )						\
  __gpy_debug__( __FILE__, __LINE__, __func__, __VA_ARGS__ );
#else
# define debug( ... )
#endif

#define fatal( ... )						\
  __gpy_fatal__( __FILE__, __LINE__, __func__, __VA_ARGS__ );

#define error( ... )						\
  __gpy_error__( __FILE__, __LINE__, __func__, __VA_ARGS__ );


extern void gpy_assertion_failed( const char * , unsigned , const char * ,
				  const char * );

extern void gpy_rr_init_runtime( void );
extern gpy_object_state_t gpy_rr_fold_integer( int );

extern void gpy_obj_integer_mod_init( void );
extern void gpy_rr_init_primitives( void );

#endif //__GCC_GPYTHON_H__