OSDN Git Service

first commit
[soopy/alpha1.git] / src / Time.cpp
1 /*
2  * Programming Language SOOPY
3  *   (Simple Object Oriented Programming sYstem)
4  * 
5  * Copyright (C) 2002,2003 SUZUKI Jun
6  * 
7  * URL: http://sourceforge.jp/projects/soopy/
8  * License: GPL(GNU General Public License)
9  * 
10  * 
11  * $Id: Time.cpp,v 1.8 2004/07/04 12:32:36 randy Exp $
12  */
13
14 #include <time.h>
15 #include "soopy.h"
16
17 void SpTime::regular()
18 {
19     // minute
20     if(minute < 0){
21         while(minute < 0){
22             minute += 60;
23             hour--;
24         }
25     }else if(minute >= 60){
26         while(minute >= 60){
27             minute -= 60;
28             hour++;
29         }
30     }
31
32     // second
33     if(second < 0){
34         while(second < 0){
35             second += 60;
36             minute--;
37         }
38     }else if(second >= 60){
39         while(second >= 60){
40             second -= 60;
41             minute++;
42         }
43     }
44 }
45
46 SpValue& SpTime::plus(SpValue& e1, SpValue& e2)
47 {
48     SpTime* t1 = e1.asTime();
49     SpTime* time;
50     if(e2.isInt()){
51         SpInt i = e2.getInt();
52         time = new SpTime(t1->hour,
53                           t1->minute,
54                           t1->second + i);
55     }else if(e2.isTime()){
56         SpTime* t2 = e2.asTime();
57         time = new SpTime(t1->hour   + t2->hour,
58                           t1->minute + t2->minute,
59                           t1->second + t2->second);
60     }else{
61         throw SpException("not time operator+");
62     }
63     time->regular();
64     //    static SpValue result;
65     //    result.setNewObject(time);
66     //    return result;
67     return SpObjectResult(time);
68 }
69
70 SpValue& SpTime::minus(SpValue& e1, SpValue& e2)
71 {
72     SpTime* t1 = e1.asTime();
73     SpTime* time;
74     if(e2.isInt()){
75         SpInt i = e2.getInt();
76         time = new SpTime(t1->hour,
77                           t1->minute,
78                           t1->second - i);
79     }else if(e2.isTime()){
80         SpTime* t2 = e2.asTime();
81         time = new SpTime(t1->hour   - t2->hour,
82                           t1->minute - t2->minute,
83                           t1->second - t2->second);
84     }else{
85         throw SpException("not time operator-");
86     }
87     time->regular();
88     //    static SpValue result;
89     //    result.setNewObject(time);
90     //    return result;
91     return SpObjectResult(time);
92 }
93
94 SpValue& SpTime::eq(SpValue& e1, SpValue& e2)
95 {
96     if(!e2.isTime()){
97         return SpBoolResult(false);
98     }
99     SpTime* t1 = e1.asTime();
100     SpTime* t2 = e2.asTime();
101     return SpBoolResult((t1->hour == t2->hour) && (t1->minute == t2->minute) && (t1->second == t2->second));
102 }
103
104 SpValue& SpTime::ne(SpValue& e1, SpValue& e2)
105 {
106     if(!e2.isTime()){
107         return SpBoolResult(false);
108     }
109     SpTime* t1 = e1.asTime();
110     SpTime* t2 = e2.asTime();
111     return SpBoolResult((t1->hour != t2->hour) || (t1->minute != t2->minute) || (t1->second != t2->second));
112 }
113
114 SpValue& SpTime::gt(SpValue& e1, SpValue& e2)
115 {
116     if(!e2.isTime()){
117         throw SpException("unmatch type (Time >)");
118     }
119     SpTime* t1 = e1.asTime();
120     SpTime* t2 = e2.asTime();
121     if(t1->hour > t2->hour){
122         return SpBoolResult(true);
123     }else if(t1->hour == t2->hour){
124         if(t1->minute > t2->minute){
125             return SpBoolResult(true);
126         }else if(t1->minute == t2->minute){
127             return SpBoolResult(t1->second > t2->second);
128         }
129     }
130     return SpBoolResult(false);
131 }
132
133 SpValue& SpTime::ge(SpValue& e1, SpValue& e2)
134 {
135     if(!e2.isTime()){
136         throw SpException("unmatch type (>=)");
137     }
138     SpTime* t1 = e1.asTime();
139     SpTime* t2 = e2.asTime();
140     if(t1->hour > t2->hour){
141         return SpBoolResult(true);
142     }else if(t1->hour == t2->hour){
143         if(t1->minute > t2->minute){
144             return SpBoolResult(true);
145         }else if(t1->minute == t2->minute){
146             return SpBoolResult(t1->second >= t2->second);
147         }
148     }
149     return SpBoolResult(false);
150 }
151
152 SpValue& SpTime::lt(SpValue& e1, SpValue& e2)
153 {
154     if(!e2.isTime()){
155         throw SpException("unmatch type (<)");
156     }
157     SpTime* t1 = e1.asTime();
158     SpTime* t2 = e2.asTime();
159     if(t1->hour < t2->hour){
160         return SpBoolResult(true);
161     }else if(t1->hour == t2->hour){
162         if(t1->minute < t2->minute){
163             return SpBoolResult(true);
164         }else if(t1->minute == t2->minute){
165             return SpBoolResult(t1->second < t2->second);
166         }
167     }
168     return SpBoolResult(false);
169 }
170
171 SpValue& SpTime::le(SpValue& e1, SpValue& e2)
172 {
173     if(!e2.isTime()){
174         throw SpException("unmatch type (<=)");
175     }
176     SpTime* t1 = e1.asTime();
177     SpTime* t2 = e2.asTime();
178     if(t1->hour < t2->hour){
179         return SpBoolResult(true);
180     }else if(t1->hour == t2->hour){
181         if(t1->minute < t2->minute){
182             return SpBoolResult(true);
183         }else if(t1->minute == t2->minute){
184             return SpBoolResult(t1->second <= t2->second);
185         }
186     }
187     return SpBoolResult(false);
188 }
189
190 SpValue& SpTime::toString()
191 {
192     char buf[10];
193     SpString* str = new SpString;
194     sprintf(buf, "%d", hour);
195     *str += buf;
196     sprintf(buf, ":%d", minute);
197     *str += buf;
198     sprintf(buf, ":%d", second);
199     *str += buf;
200     //    static SpValue val;
201     //    val.setNewObject(str);
202     //    return val;
203     return SpObjectResult(str);
204 }
205
206 void SpTime::assign(SpValue& feature, SpValue& value)
207 {
208     SpValue v;
209
210     if(feature == SymHour){
211         v = value.eval();
212         if(!v.isInt()){
213             throw SpException("not int(assign time)");
214         }
215         hour = v.getInt();
216     }else if(feature == SymMinute){
217         v = value.eval();
218         if(!v.isInt()){
219             throw SpException("not int(assign time)");
220         }
221         minute = v.getInt();
222     }else if(feature == SymSecond){
223         v = value.eval();
224         if(!v.isInt()){
225             throw SpException("not int(assign time)");
226         }
227         second = v.getInt();
228     }else{
229         throw SpException("no such a feature(assign time)");
230     }
231 }
232
233 // primitives
234 SpValue& SpTime::prim_now()
235 {
236     SpInt h, m, s;
237     time_t t;
238     struct tm* area;
239
240     t = time(NULL);
241     area = localtime(&t);
242     h = area->tm_hour;
243     m = area->tm_min;
244     s = area->tm_sec;
245
246     //    static SpValue result;
247     //    result.setNewObject(new SpTime(h, m, s));
248     //    return result;
249     return SpObjectResult(new SpTime(h, m, s));
250 }
251
252 SpValue& SpTime::prim_new(SpValue& v1, SpValue& v2, SpValue& v3)
253 {
254     SpInt h, m, s;
255
256     if(!v1.isInt()){
257         throw SpException("not int (Date.new)");
258     }
259     if(!v2.isInt()){
260         throw SpException("not int (Date.new)");
261     }
262     if(!v3.isInt()){
263         throw SpException("not int (Date.new)");
264     }
265     h = v1.getInt();
266     m = v2.getInt();
267     s = v3.getInt();
268
269     //    static SpValue result;
270     //    result.setNewObject(new SpTime(h, m, s));
271     //    return result;
272     return SpObjectResult(new SpTime(h, m, s));
273 }
274
275 /*
276 SpValue& SpTime::prim_succ(SpValue& self)
277 {
278     SpInt h, m, s;
279     SpTime* time = self.asTime();
280
281     h = time->hour;
282     m = time->minute;
283     s = time->second;
284     SpTime* time2 = new SpTime(h, m, s+1);
285     time2->regular();
286
287     static SpValue result;
288     result.setNewObject(time2);
289     return result;
290 }
291
292 SpValue& SpTime::prim_pred(SpValue& self)
293 {
294     SpInt h, m, s;
295     SpTime* time = self.asTime();
296
297     h = time->hour;
298     m = time->minute;
299     s = time->second;
300     SpTime* time2 = new SpTime(h, m, s-1);
301     time2->regular();
302
303     static SpValue result;
304     result.setNewObject(time2);
305     return result;
306 }
307 */
308
309 SpValue& SpTime::prim_getHour(SpValue& self)
310 {
311     SpTime* p = self.asTime();
312     //    static SpValue result;
313     //    result.setInt(p->hour);
314     //    return result;
315     return SpIntResult(p->hour);
316 }
317
318 SpValue& SpTime::prim_getMinute(SpValue& self)
319 {
320     SpTime* p = self.asTime();
321     //    static SpValue result;
322     //    result.setInt(p->minute);
323     //    return result;
324     return SpIntResult(p->minute);
325 }
326
327 SpValue& SpTime::prim_getSecond(SpValue& self)
328 {
329     SpTime* p = self.asTime();
330     //    static SpValue result;
331     //    result.setInt(p->second);
332     //    return result;
333     return SpIntResult(p->second);
334 }
335
336 SpValue& SpTime::prim_fromString(SpValue& time_str)
337 {
338     SpInt h, m, s;
339     SpValue temp;
340     SpString* str = time_str.asString();
341
342     SpValue v = str->split(MakeSpChar(CodeJIS, ':'));
343     SpCons* list = (SpCons*)v.asList();
344     if(list->length() != 3){
345         throw SpException("illegal time format");
346     }
347
348     //h = int_fromString(list->value().asString());
349     temp = list->value();
350     h = int_fromString(temp.asString());
351     temp = list->nextList();
352     list = (SpCons*)temp.asList();
353     //m = int_fromString(list->value().asString());
354     temp = list->value();
355     m = int_fromString(temp.asString());
356     temp = list->nextList();
357     list = (SpCons*)temp.asList();
358     //s = int_fromString(list->value().asString());
359     temp = list->value();
360     s = int_fromString(temp.asString());
361
362     //    static SpValue result;
363     //    result.setNewObject(new SpTime(h, m, s));
364     //    return result;
365     return SpObjectResult(new SpTime(h, m, s));
366 }
367
368
369 // Message Handler
370 SpValue& SpTime::onMessage(SpValue& rec, SpValue& msg)
371 {
372     return TimeMsgHandler(rec, msg);
373 }
374
375 // init
376 void SpTime::init()
377 {
378     SpNameSpace* pTimeNS = new SpNameSpace;
379     SpValue TimeNS(pTimeNS);
380     PMainNameSpace->internConst(SymTime, TimeNS);
381
382     // for Class
383     SpValue SymNow(new SpSymbol("now"));
384     SpValue PrimNow(new SpPrim0(prim_now));
385     pTimeNS->internPrimProperty(SymNow, NilObject, PrimNow, NilObject);
386
387     SpValue SymNew(new SpSymbol("new"));
388     SpValue PrimNew(new SpPrim3(prim_new));
389     pTimeNS->internConst(SymNew, PrimNew);
390
391     SpValue SymFromString(new SpSymbol("fromstring"));
392     SpValue PrimFromString(new SpPrim1(prim_fromString));
393     pTimeNS->internConst(SymFromString, PrimFromString);
394
395     // for Object
396     SpValue GetHour(new SpPrim1(prim_getHour));
397     TimeMsgHandler.append(SymHour, GetHour);
398
399     SpValue GetMinute(new SpPrim1(prim_getMinute));
400     TimeMsgHandler.append(SymMinute, GetMinute);
401
402     SpValue GetSecond(new SpPrim1(prim_getSecond));
403     TimeMsgHandler.append(SymSecond, GetSecond);
404
405 /*
406     SpValue PrimSucc(new SpPrim1(prim_succ));
407     TimeMsgHandler.append(SymSucc, PrimSucc);
408
409     SpValue PrimPred(new SpPrim1(prim_pred));
410     TimeMsgHandler.append(SymPred, PrimPred);
411 */
412 }
413
414