comparison runtime/builtin.c @ 63:04baa003de5a

Merged latest changes with better C branch
author Mike Pavone <pavone@retrodev.com>
date Wed, 05 May 2010 22:12:23 -0400
parents d2f9b0a9403d 70af7fa155d0
children d4b44ae2e34a
comparison
equal deleted inserted replaced
62:b218af069da7 63:04baa003de5a
2 #include "object.h" 2 #include "object.h"
3 #include "integer.h" 3 #include "integer.h"
4 #include "bool.h" 4 #include "bool.h"
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
7 9
8 void register_builtin_type(uint32_t type) 10 void register_builtin_type(uint32_t type)
9 { 11 {
10 blueprint * bp; 12 blueprint * bp;
11 switch(type) 13 switch(type)
12 { 14 {
13 case TYPE_INT32: 15 case TYPE_BLUEPRINT:
14 bp = register_type_byid(TYPE_INT32, sizeof(int32_t), NULL, NULL, NULL); 16 bp = register_type_byid(TYPE_BLUEPRINT, sizeof(blueprint *), NULL, NULL, NULL);
15 /* add_method(bp, METHOD_ADD, MethodName(_PL_,Int32));
16 add_method(bp, METHOD_SUB, MethodName(_MN_,Int32));
17 add_method(bp, METHOD_MUL, MethodName(_TM_,Int32));
18 add_method(bp, METHOD_DIV, MethodName(_DV_,Int32));
19 add_method(bp, METHOD_LSHIFT, MethodName(LShift,Int32));
20 add_method(bp, METHOD_RSHIFT, MethodName(RShift,Int32));
21 add_method(bp, METHOD_LESS, MethodName(_LT_,Int32));
22 add_method(bp, METHOD_GREATER, MethodName(_GT_,Int32)); */
23 break; 17 break;
24 case TYPE_BOOLEAN:
25 bp = register_type_byid(TYPE_BOOLEAN, sizeof(int32_t), NULL, NULL, NULL);
26 //add_method(bp, METHOD_IF, MethodName(If,Boolean));
27 val_yes = (t_Boolean *)new_object(TYPE_BOOLEAN);
28 val_yes->val = 1;
29 val_no = (t_Boolean *)new_object(TYPE_BOOLEAN);
30 val_no->val = 0;
31 break;
32 /* case TYPE_BLUEPRINT:
33 bp = register_type_byid(TYPE_BLUEPRINT, sizeof(blueprint *), NULL, NULL, NULL);
34 break;*/
35 } 18 }
36 } 19 }
37 20
38 void register_builtin_types() 21 void register_builtin_types()
39 { 22 {
40 uint32_t i; 23 uint32_t i;
41 for(i = 0; i < TYPE_FIRST_USER; ++i) 24 for(i = 0; i < TYPE_FIRST_USER; ++i)
42 register_builtin_type(i); 25 register_builtin_type(i);
43 } 26 }
27 object * make_Int64(int64_t val)
28 {
29 t_Int64 * obj;
30 object * ret = new_object(TYPE_INT64);
31 obj = (t_Int64 *)ret;
32 obj->Num = val;
33 return ret;
34 }
44 35
36
37 object * make_Int32(int32_t val)
38 {
39 t_Int32 * obj;
40 object * ret = new_object(TYPE_INT32);
41 obj = (t_Int32 *)ret;
42 obj->Num = val;
43 return ret;
44 }
45
46 object * make_Int16(int16_t val)
47 {
48 t_Int16 * obj;
49 object * ret = new_object(TYPE_INT16);
50 obj = (t_Int16 *)ret;
51 obj->Num = val;
52 return ret;
53 }
54
55 object * make_Int8(int8_t val)
56 {
57 t_Int8 * obj;
58 object * ret = new_object(TYPE_INT8);
59 obj = (t_Int8 *)ret;
60 obj->Num = val;
61 return ret;
62 }
63
64 object * make_UInt64(uint64_t val)
65 {
66 t_UInt64 * obj;
67 object * ret = new_object(TYPE_UINT64);
68 obj = (t_UInt64 *)ret;
69 obj->Num = val;
70 return ret;
71 }
72
73 object * make_UInt32(uint32_t val)
74 {
75 t_UInt32 * obj;
76 object * ret = new_object(TYPE_UINT32);
77 obj = (t_UInt32 *)ret;
78 obj->Num = val;
79 return ret;
80 }
81
82 object * make_UInt16(uint16_t val)
83 {
84 t_UInt16 * obj;
85 object * ret = new_object(TYPE_UINT16);
86 obj = (t_UInt16 *)ret;
87 obj->Num = val;
88 return ret;
89 }
90
91 object * make_UInt8(uint8_t val)
92 {
93 t_UInt8 * obj;
94 object * ret = new_object(TYPE_UINT8);
95 obj = (t_UInt8 *)ret;
96 obj->Num = val;
97 return ret;
98 }
99
100 object * make_String(char * text)
101 {
102 returntype ret;
103 context * ct;
104 calldata * cdata;
105 object * retobj;
106 t_Array * arr = (t_Array *)_internal_array_allocnaked(strlen(text), make_Blueprint(TYPE_UINT8));
107 arr->payload.Length = arr->payload.Storage;
108 memcpy(((char *)arr) + sizeof(t_Array), text, arr->payload.Length);
109
110 //This is really ugly, but I don't see a good way around it at the moment
111 ct = new_context();
112 cdata = alloc_cdata(ct, 1);
113 cdata->params[0] = (object *)arr;
114 cdata->num_params = 1;
115 cdata->resume = 0;
116 ret = f_String(cdata);
117 while(ret == TAIL_RETURN)
118 ret = cdata->tail_func(cdata);
119 if(ret == EXCEPTION_RETURN)
120 {
121 puts("Exception while building string literal!");
122 exit(-1);
123 }
124 retobj = cdata->params[0];
125 free_context(ct);
126 return retobj;
127 }
128
129