-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.c
56 lines (41 loc) · 1.45 KB
/
number.c
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
#include "number.h"
struct Object *num_plus(struct Object *parent, struct Object *n)
{
if (parent == NULL || n == NULL ||
parent->type != T_NUMBER || n->type != T_NUMBER)
return NULL;
struct Object* ret = clone_object(parent);
if (ret == NULL)
fail("Unable to clone object at %s, %d", __FILE__, __LINE__);
ret->value.i_value += n->value.i_value;
return ret;
}
struct Object *num_minus(struct Object *parent, struct Object *n)
{
if (parent == NULL || n == NULL ||
parent->type != T_NUMBER || n->type != T_NUMBER)
return NULL;
struct Object* ret = clone_object(parent);
if (ret == NULL)
fail("Unable to clone object at %s, %d", __FILE__, __LINE__);
ret->value.i_value -= n->value.i_value;
return ret;
}
void create_number_object(void)
{
/* init number object */
number_object = clone_object(base_object);
number_object->type = T_NUMBER;
number_object->name = "Number";
number_object->value.i_value = 0;
struct Object *plus_met = clone_object(builtin_object);
plus_met->name = "+";
plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1,
struct Object *arg2))num_plus;
list_append(number_object->methods, plus_met);
struct Object *minus_met = clone_object(builtin_object);
minus_met->name = "-";
minus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1,
struct Object *arg2))num_minus;
list_append(number_object->methods, minus_met);
}