-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.h
144 lines (112 loc) · 3.25 KB
/
runtime.h
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
#ifndef RUNTIME_H
#define RUNTIME_H
#include "hashtable.h"
#include "hashtable_itr.h"
#include "hashtable_utility.h"
/* for old times sake */
void print_int_nl(int x);
int input();
/* Structure and type-tag definitions */
#define MASK 3 /* 11 */
#define SHIFT 2
#define INT_TAG 0 /* 00 */
#define BOOL_TAG 1 /* 01 */
/* The following is for a non-standard single-precision (21-bit fraction)
floating point number */
#define FLOAT_TAG 2 /* 10 */
/*
For larger objects, we have a pointer to the object, and the
object starts with a tag that says what it is.
*/
#define BIG_TAG 3 /* 11 */
enum big_type_tag { LIST, DICT, FUN, CLASS, OBJECT, UBMETHOD, BMETHOD };
typedef long int pyobj;
struct pyobj_struct;
struct list_struct {
pyobj* data;
unsigned int len;
};
typedef struct list_struct list;
typedef struct hashtable* dict;
struct fun_struct {
void* function_ptr;
pyobj free_vars;
};
typedef struct fun_struct function;
struct class_struct {
struct hashtable *attrs;
int nparents;
struct class_struct *parents;
};
typedef struct class_struct class;
struct object_struct {
struct hashtable *attrs;
class cl;
};
typedef struct object_struct object;
struct unbound_method_struct {
function fun;
class cl;
};
typedef struct unbound_method_struct unbound_method;
struct bound_method_struct {
function fun;
object receiver;
};
typedef struct bound_method_struct bound_method;
struct pyobj_struct {
enum big_type_tag tag;
union {
dict d;
list l;
function f;
class cl;
object obj;
unbound_method ubm;
bound_method bm;
} u;
};
typedef struct pyobj_struct big_pyobj;
int tag(pyobj val);
int is_int(pyobj val);
int is_bool(pyobj val);
int is_big(pyobj val);
int is_function(pyobj val);
int is_object(pyobj val);
int is_class(pyobj val);
int is_unbound_method(pyobj val);
int is_bound_method(pyobj val);
void assert_type(pyobj x, char* type, int lineno);
pyobj inject_int(int i);
pyobj inject_bool(int b);
pyobj inject_big(big_pyobj* p);
int project_int(pyobj val);
int project_bool(pyobj val);
int project_boolint(pyobj val);
big_pyobj* project_big(pyobj val);
/* Operations */
int is_true(pyobj v);
void print_any(pyobj p);
pyobj input_int();
big_pyobj* create_list(pyobj length);
big_pyobj* create_dict();
pyobj set_subscript(pyobj c, pyobj key, pyobj val);
pyobj get_subscript(pyobj c, pyobj key);
big_pyobj* add(big_pyobj* a, big_pyobj* b);
int equal(big_pyobj* a, big_pyobj* b);
int not_equal(big_pyobj* x, big_pyobj* y);
big_pyobj* create_closure(void* fun_ptr, pyobj free_vars);
void* get_fun_ptr(pyobj);
pyobj get_free_vars(pyobj);
big_pyobj* set_free_vars(big_pyobj* b, pyobj free_vars);
big_pyobj* create_class(pyobj bases); /* bases should be a list of classes */
big_pyobj* create_object(pyobj cl);
int inherits(pyobj c1, pyobj c2); /* Returns true if class c1 inherits from class c2 */
big_pyobj* get_class(pyobj o); /* Get the class from an object or unbound method */
big_pyobj* get_receiver(pyobj o); /* Get the receiver from inside a bound method */
big_pyobj* get_function(pyobj o); /* Get the function from inside a method */
int has_attr(pyobj o, char* attr);
pyobj get_attr(pyobj c, char* attr);
pyobj set_attr(pyobj obj, char* attr, pyobj val);
pyobj error_pyobj(char* string);
#endif /* RUNTIME_H */