summaryrefslogtreecommitdiffstats
path: root/Objects/funcobject.c
blob: 1c6f6a6ace80fcd083f2d880f7c21de48991ec07 (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
/* Function object implementation */

#include <stdio.h>

#include "PROTO.h"
#include "object.h"
#include "node.h"
#include "stringobject.h"
#include "funcobject.h"
#include "objimpl.h"
#include "token.h"

typedef struct {
	OB_HEAD
	node *func_node;
	object *func_globals;
} funcobject;

object *
newfuncobject(n, globals)
	node *n;
	object *globals;
{
	funcobject *op = NEWOBJ(funcobject, &Functype);
	if (op != NULL) {
		op->func_node = n;
		if (globals != NULL)
			INCREF(globals);
		op->func_globals = globals;
	}
	return (object *)op;
}

node *
getfuncnode(op)
	object *op;
{
	if (!is_funcobject(op)) {
		errno = EBADF;
		return NULL;
	}
	return ((funcobject *) op) -> func_node;
}

object *
getfuncglobals(op)
	object *op;
{
	if (!is_funcobject(op)) {
		errno = EBADF;
		return NULL;
	}
	return ((funcobject *) op) -> func_globals;
}

/* Methods */

static void
funcdealloc(op)
	funcobject *op;
{
	/* XXX free node? */
	DECREF(op->func_globals);
	free((char *)op);
}

static void
funcprint(op, fp, flags)
	funcobject *op;
	FILE *fp;
	int flags;
{
	node *n = op->func_node;
	n = CHILD(n, 1);
	fprintf(fp, "<user function %s>", STR(n));
}

static object *
funcrepr(op)
	funcobject *op;
{
	char buf[100];
	node *n = op->func_node;
	n = CHILD(n, 1);
	sprintf(buf, "<user function %.80s>", STR(n));
	return newstringobject(buf);
}

typeobject Functype = {
	OB_HEAD_INIT(&Typetype)
	0,
	"function",
	sizeof(funcobject),
	0,
	funcdealloc,	/*tp_dealloc*/
	funcprint,	/*tp_print*/
	0,		/*tp_getattr*/
	0,		/*tp_setattr*/
	0,		/*tp_compare*/
	funcrepr,	/*tp_repr*/
};