2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: Prim.cpp,v 1.43 2004/05/22 05:22:34 randy Exp $
19 #endif /* __WIN32__ */
24 * Primitive: arg length 0
27 SpValue& SpPrim0::operator()(SpValue&)
32 SpValue& SpPrim0::toString()
37 *str = "#<Primitive: arg length 0>";
39 // s.setNewObject(str);
41 return SpObjectResult(str);
45 * Primitive: arg length 1
48 SpValue& SpPrim1::operator()(SpValue& arg)
53 SpValue& SpPrim1::toString()
58 *str = "#<Primitive: arg length 1>";
60 // s.setNewObject(str);
62 return SpObjectResult(str);
66 * Primitive: arg length 2
69 SpValue& SpPrim2::operator()(SpValue& arg)
72 SpTuple* tpl = arg.asTuple();
73 if(tpl->length() != 2){
74 throw SpException("too much args(primitive<2>)");
76 SpValue a1 = (*tpl)[0];
77 SpValue a2 = (*tpl)[1];
78 return operator()(a1, a2);
82 SpClosureP2* closure = new SpClosureP2(temp, arg, getCurrentNS());
84 // v.setNewObject(closure);
86 return SpObjectResult(closure);
89 SpValue& SpPrim2::operator()(SpValue& arg1, SpValue& arg2)
91 return func(arg1, arg2);
94 SpValue& SpPrim2::toString()
99 *str = "#<Primitive: arg length 2>";
101 // s.setNewObject(str);
103 return SpObjectResult(str);
107 * Primitive: arg length 3
110 SpValue& SpPrim3::operator()(SpValue& arg)
113 SpTuple* tpl = arg.asTuple();
114 if(tpl->length() != 3){
115 throw SpException("arg length mismatch(primitive<3>)");
117 SpValue a1 = (*tpl)[0];
118 SpValue a2 = (*tpl)[1];
119 SpValue a3 = (*tpl)[2];
120 return operator()(a1, a2, a3);
124 SpClosureP3_1* closure = new SpClosureP3_1(temp, arg, getCurrentNS());
126 // v.setNewObject(closure);
128 return SpObjectResult(closure);
131 SpValue& SpPrim3::operator()(SpValue& arg1, SpValue& arg2)
136 SpTuple* t = arg2.asTuple();
137 if(t->length() != 2){
138 throw SpException("too much args");
140 SpValue a2 = (*t)[0];
141 SpValue a3 = (*t)[1];
142 v = func(arg1, a2, a3);
145 SpClosureP3_2* closure = new SpClosureP3_2(temp, arg1, arg2, getCurrentNS());
146 v.setNewObject(closure);
149 return SpValueResult(v);
152 SpValue& SpPrim3::operator()(SpValue& arg1, SpValue& arg2, SpValue& arg3)
154 return func(arg1, arg2, arg3);
157 SpValue& SpPrim3::toString()
161 str = new SpString();
162 *str = "#<Primitive: arg length 3>";
164 // s.setNewObject(str);
166 return SpObjectResult(str);
177 SpValue& primPrint(SpValue& v)
179 //*spout << v.eval().toString();
182 temp = temp.toString();
187 SpValue& primPrintLn(SpValue& v)
189 //*spout << v.eval().toString() << "\n";
192 temp = temp.toString();
193 *spout << temp << "\n";
197 SpValue& primLoad(SpValue& v)
199 // extern int load(const char*);
200 extern SpValue& load(const char*);
202 SpValue name = v.eval();
203 if(!name.isString()){
204 throw SpException("filename is not string (load)");
206 SpString* str = name.asString();
207 const char* filename = str->toCStringWithEncoder();
209 int result = load(filename);
215 return load(filename);
218 SpValue& primOpenIn(SpValue& v)
220 SpValue name = v.eval();
221 if(!name.isString()){
222 throw SpException("filename is not string (openIn)");
224 SpString* str = name.asString();
225 char* filename = (char*)str->toCStringWithEncoder();
226 ifstream* fin = new ifstream;
228 fin->open(filename, ios::binary);
234 throw SpException("can't open file");
236 FileStreamReader* sr = new FileStreamReader(fin, filename);
238 ReadEncoder* encoder = new EucJPReadDecoder(sr);
240 ReadEncoder* encoder = new SjisReadDecoder(sr);
242 // static SpValue result;
243 // result.setNewObject(encoder);
245 return SpObjectResult(encoder);
248 SpValue& primOpenOut(SpValue& v)
250 SpValue name = v.eval();
251 if(!name.isString()){
252 throw SpException("filename is not string (openOut)");
254 SpString* str = name.asString();
255 char* filename = (char*)str->toCStringWithEncoder();
256 ofstream* fout = new ofstream;
258 fout->open(filename, ios::binary);
260 fout->open(filename);
264 throw SpException("can't open file");
266 FileStreamWriter* sw = new FileStreamWriter(fout, filename);
268 WriteEncoder* encoder = new EucJPWriteEncoder(sw);
270 WriteEncoder* encoder = new SjisWriteEncoder(sw);
272 // static SpValue result;
273 // result.setNewObject(encoder);
275 return SpObjectResult(encoder);
278 SpValue& primOpenAppend(SpValue& v)
280 SpValue name = v.eval();
281 if(!name.isString()){
282 throw SpException("filename is not string (openAppend)");
284 SpString* str = name.asString();
285 char* filename = (char*)str->toCStringWithEncoder();
286 ofstream* fout = new ofstream;
288 fout->open(filename, ios::binary | ios_base::app);
290 fout->open(filename, ios::app);
294 throw SpException("can't open file");
296 FileStreamWriter* sw = new FileStreamWriter(fout, filename);
298 WriteEncoder* encoder = new EucJPWriteEncoder(sw);
300 WriteEncoder* encoder = new SjisWriteEncoder(sw);
302 // static SpValue result;
303 // result.setNewObject(encoder);
305 return SpObjectResult(encoder);
308 SpValue& primSetReader(SpValue& reader)
310 //if(reader.eq(SymSjisIn).isTrue()){
312 temp = reader.eq(SymSjisIn);
315 }else if((temp = reader.eq(SymEucjpIn)).isTrue()){
318 throw SpException("no such a reader");
323 SpValue& primSystem(SpValue& arg)
326 const char* cmd = arg.toCStringWithEncoder();
327 if(system(cmd) == -1){
331 cout << "no entry\n";
334 cout << "not exec\n";
337 cout << "no memory\n";
340 cout << "other error\n";
347 throw SpException("not string (system)");
350 SpValue& primExec(SpValue& arg)
353 const char* cmd = arg.toCStringWithEncoder();
354 char* p = (char*)cmd;
361 if(execlp(cmd,cmd,NULL) == -1){
365 cout << "can't access\n";
368 cout << "too many opened files.\n";
371 cout << "no entry\n";
374 cout << "not exec\n";
377 cout << "no memory\n";
380 cout << "other error\n";
388 //cout << "cmd: '" << cmd << "', p: '" << p << "'" << endl;
389 if(execlp(cmd,cmd,p,NULL) == -1){
395 throw SpException("not string (exec)");
405 // set global variables
406 SpValue SymPrint(new SpSymbol("print"));
407 PrimPrint.setNewObject(new SpPrim1(&primPrint));
408 PMainNameSpace->internConst(SymPrint, PrimPrint);
410 SpValue SymPrintLn(new SpSymbol("println"));
411 PrimPrintLn.setNewObject(new SpPrim1(&primPrintLn));
412 PMainNameSpace->internConst(SymPrintLn, PrimPrintLn);
414 SpValue SymLoad(new SpSymbol("load"));
415 PrimLoad.setNewObject(new SpPrim1(&primLoad));
416 PMainNameSpace->internConst(SymLoad, PrimLoad);
418 SpValue PrimOpenIn(new SpPrim1(&primOpenIn));
419 PMainNameSpace->internConst(SymOpenIn, PrimOpenIn);
421 SpValue PrimOpenOut(new SpPrim1(&primOpenOut));
422 PMainNameSpace->internConst(SymOpenOut, PrimOpenOut);
424 SpValue PrimOpenAppend(new SpPrim1(&primOpenAppend));
425 PMainNameSpace->internConst(SymOpenAppend, PrimOpenAppend);
427 SpValue SymSystem(new SpSymbol("system"));
428 SpValue PrimSystem(new SpPrim1(&primSystem));
429 PMainNameSpace->internConst(SymSystem, PrimSystem);
431 SpValue SymExec(new SpSymbol("exec"));
432 SpValue PrimExec(new SpPrim1(&primExec));
433 PMainNameSpace->internConst(SymExec, PrimExec);
435 SpValue SymQuit(new SpSymbol("quit"));
436 SpValue PrimQuit(new SpPrim0(&primQuit));
437 PMainNameSpace->internConst(SymQuit, PrimQuit);
440 SpValue SymSetReader(new SpSymbol("setreader"));
441 SpValue PrimSetReader(new SpPrim1(primSetReader));
442 PSoopyNameSpace->internConst(SymSetReader, PrimSetReader);