/
tclint.pmc
75 lines (60 loc) · 1.97 KB
/
tclint.pmc
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
/*
* Copyright (C) 2001-2009, The Parrot Foundation.
*/
#include "pmc/pmc_float.h"
#include "pmc/pmc_string.h"
static INTVAL dynpmc_TclFloat;
static INTVAL dynpmc_TclString;
pmclass TclInt
dynpmc auto_attrs
extends Integer
does integer
group tcl_group
hll tcl
maps Integer
{
VTABLE void class_init() {
dynpmc_TclFloat = Parrot_pmc_get_type_str(INTERP, CONST_STRING(INTERP, "TclFloat"));
dynpmc_TclString = Parrot_pmc_get_type_str(INTERP, CONST_STRING(INTERP, "TclString"));
}
VTABLE void set_number_native(FLOATVAL value) {
pmc_reuse(INTERP, SELF, dynpmc_TclFloat, 0);
SETATTR_Float_fv(INTERP, SELF, value);
}
VTABLE void set_string_native(STRING *value) {
pmc_reuse(INTERP, SELF, dynpmc_TclString, 0);
SETATTR_String_str_val(INTERP, SELF, Parrot_str_copy(INTERP, value));
}
/*
* TclInt shouldn't automatically promote division to float;
* also, explicitly use floor when dividing.
*/
MULTI PMC *divide(Integer *value, PMC* dest) {
FLOATVAL d = VTABLE_get_number(INTERP, value);
if (d == 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
"divide by zero");
if (!dest)
dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
VTABLE_set_integer_native(INTERP, dest, floor(SELF.get_integer() / d));
return dest;
}
/*
* Workaround for bug #1811 when "div TclInt, TclFloat" dispatched
* to Integer.divide(DEFAULT).
*/
MULTI PMC *divide(DEFAULT *value, PMC* dest) {
FLOATVAL d = VTABLE_get_number(INTERP, value);
if (d == 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
"divide by zero");
dest = Parrot_pmc_box_number(INTERP, SELF.get_integer() / d);
return dest;
}
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/