PageRenderTime 29ms CodeModel.GetById 8ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/vm/number.go

http://github.com/feyeleanor/RubyGoLightly
Go | 86 lines | 74 code | 12 blank | 0 comment | 14 complexity | 883e199d8cd5c2e02381f0e35b66f065 MD5 | raw file
 1import(
 2	"tr";
 3)
 4
 5func TrFixnum_add(vm *RunyVM, self, other *RubyObject) RubyObject {
 6	return TR_INT2FIX(TR_FIX2INT(self) + TR_FIX2INT(other));
 7}
 8
 9func TrFixnum_sub(vm *RubyVM, self, other *RubyObject) RubyObject {
10	return TR_INT2FIX(TR_FIX2INT(self) - TR_FIX2INT(other));
11}
12
13func TrFixnum_mul(vm *RubyVM, self, other *RubyObject) RubyObject {
14	return TR_INT2FIX(TR_FIX2INT(self) * TR_FIX2INT(other));
15}
16
17func TrFixnum_div(vm *RubyVM, self, other *RubyObject) RubyObject {
18	return TR_INT2FIX(TR_FIX2INT(self) / TR_FIX2INT(other));
19}
20
21func TrFixnum_eq(vm *RubyVM, self, other *RubyObject) RubyObject {
22	if TR_FIX2INT(self) == TR_FIX2INT(other) {
23		return TR_TRUE;
24	} else {
25		return TR_FALSE;
26	}
27}
28
29func TrFixnum_ne(vm *RubyVM, self, other *RubyObject) RubyObject {
30	if TR_FIX2INT(self) != TR_FIX2INT(other) {
31		return TR_TRUE;
32	} else {
33		return TR_FALSE;
34	}
35}
36
37func TrFixnum_lt(vm *RubyVM, self, other *RubyObject) RubyObject {
38	if TR_FIX2INT(self) < TR_FIX2INT(other) {
39		return TR_TRUE;
40	} else {
41		return TR_FALSE;
42	}
43}
44
45func TrFixnum_gt(vm *RubyVM, self, other *RubyObject) RubyObject {
46	if TR_FIX2INT(self) > TR_FIX2INT(other) {
47		return TR_TRUE;
48	} else {
49		return TR_FALSE;
50	}
51}
52
53func TrFixnum_le(vm *RubyVM, self, other *RubyObject) RubyObject {
54	if TR_FIX2INT(self) <= TR_FIX2INT(other) {
55		return TR_TRUE;
56	} else {
57		return TR_FALSE;
58	}
59}
60
61func TrFixnum_ge(vm *RubyVM, self, other *RubyObject) RubyObject {
62	if TR_FIX2INT(self) >= TR_FIX2INT(other) {
63		return TR_TRUE;
64	} else {
65		return TR_FALSE;
66	}
67}
68
69func TrFixnum_to_s(vm *RubyVM, self *RubyObject) RubyObject {
70	return tr_sprintf(vm, "%d", TR_FIX2INT(self));
71}
72
73void TrFixnum_init(vm *RubyVM) {
74	c := vm.classes[TR_T_Fixnum] = Object_const_set(vm, vm.self, TrSymbol_new(vm, Fixnum), newClass(vm, TrSymbol_new(vm, Fixnum), vm.classes[TR_T_Object]));
75	c.add_method(vm, TrSymbol_new(vm, "+"), newMethod(vm, (TrFunc *)TrFixnum_add, TR_NIL, 1));
76	c.add_method(vm, TrSymbol_new(vm, "-"), newMethod(vm, (TrFunc *)TrFixnum_sub, TR_NIL, 1));
77	c.add_method(vm, TrSymbol_new(vm, "*"), newMethod(vm, (TrFunc *)TrFixnum_mul, TR_NIL, 1));
78	c.add_method(vm, TrSymbol_new(vm, "/"), newMethod(vm, (TrFunc *)TrFixnum_div, TR_NIL, 1));
79	c.add_method(vm, TrSymbol_new(vm, "=="), newMethod(vm, (TrFunc *)TrFixnum_eq, TR_NIL, 1));
80	c.add_method(vm, TrSymbol_new(vm, "!="), newMethod(vm, (TrFunc *)TrFixnum_eq, TR_NIL, 1));
81	c.add_method(vm, TrSymbol_new(vm, "<"), newMethod(vm, (TrFunc *)TrFixnum_lt, TR_NIL, 1));
82	c.add_method(vm, TrSymbol_new(vm, "<="), newMethod(vm, (TrFunc *)TrFixnum_le, TR_NIL, 1));
83	c.add_method(vm, TrSymbol_new(vm, ">"), newMethod(vm, (TrFunc *)TrFixnum_gt, TR_NIL, 1));
84	c.add_method(vm, TrSymbol_new(vm, ">="), newMethod(vm, (TrFunc *)TrFixnum_ge, TR_NIL, 1));
85	c.add_method(vm, TrSymbol_new(vm, "to_s"), newMethod(vm, (TrFunc *)TrFixnum_to_s, TR_NIL, 0));
86}