Commit b974b146 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Simple implementations of file.readline(), map()

parent 0d5ba1e2
......@@ -17,6 +17,8 @@
#include "core/ast.h"
#include "core/types.h"
#include "codegen/compvars.h"
#include "runtime/gc_runtime.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
......@@ -217,6 +219,29 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) {
return rtn;
}
Box* map2(Box* f, Box* container) {
static std::string _iter("__iter__");
static std::string _hasnext("__hasnext__");
static std::string _next("next");
Box* iter = callattr(container, &_iter, true, 0, NULL, NULL, NULL, NULL);
Box* rtn = new BoxedList();
while (true) {
Box* hasnext = callattr(iter, &_hasnext, true, 0, NULL, NULL, NULL, NULL);
bool hasnext_bool = nonzero(hasnext);
if (!hasnext_bool)
break;
Box* next = callattr(iter, &_next, true, 0, NULL, NULL, NULL, NULL);
Box* r = runtimeCall(f, 1, next, NULL, NULL, NULL);
listAppendInternal(rtn, r);
}
return rtn;
}
extern "C" const ObjectFlavor notimplemented_flavor(&boxGCHandler, NULL);
BoxedClass *notimplemented_cls;
BoxedModule* builtins_module;
......@@ -279,6 +304,7 @@ void setupBuiltins() {
open_obj = new BoxedFunction(open);
builtins_module->giveAttr("open", open_obj);
builtins_module->giveAttr("map", new BoxedFunction(boxRTFunction((void*)map2, LIST, 2, false)));
builtins_module->setattr("str", str_cls, NULL, NULL);
builtins_module->setattr("int", int_cls, NULL, NULL);
......
......@@ -15,6 +15,8 @@
#include <cstring>
#include <sstream>
#include "codegen/compvars.h"
#include "core/common.h"
#include "core/stats.h"
#include "core/types.h"
......@@ -64,6 +66,24 @@ Box* fileRead1(BoxedFile* self) {
return _fileRead(self, -1);
}
Box* fileReadline1(BoxedFile* self) {
assert(self->cls == file_cls);
std::ostringstream os("");
while (true) {
char c;
int nread = fread(&c, 1, 1, self->f);
if (nread == 0)
break;
os << c;
if (c == '\n')
break;
}
return boxString(os.str());
}
Box* fileRead2(BoxedFile* self, Box* size) {
assert(self->cls == file_cls);
if (size->cls != int_cls) {
......@@ -160,6 +180,9 @@ void setupFile() {
addRTFunction(read, (void*)fileRead2, NULL, 2, false);
file_cls->giveAttr("read", new BoxedFunction(read));
CLFunction *readline = boxRTFunction((void*)fileReadline1, STR, 1, false);
file_cls->giveAttr("readline", new BoxedFunction(readline));
file_cls->giveAttr("write", new BoxedFunction(boxRTFunction((void*)fileWrite, NULL, 2, false)));
file_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)fileClose, NULL, 1, false)));
......
......@@ -323,6 +323,22 @@ Box* strJoin(BoxedString* self, Box* rhs) {
}
}
Box* strSplit1(BoxedString* self) {
assert(self->cls == str_cls);
BoxedList* rtn = new BoxedList();
std::ostringstream os("");
for (char c : self->s) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') {
listAppendInternal(rtn, boxString(os.str()));
os.str("");
}
}
listAppendInternal(rtn, boxString(os.str()));
return rtn;
}
extern "C" Box* strGetitem(BoxedString* self, Box* slice) {
if (slice->cls == int_cls) {
BoxedInt* islice = static_cast<BoxedInt*>(slice);
......@@ -368,6 +384,7 @@ void setupStr() {
str_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)strGetitem, NULL, 2, false)));
str_cls->giveAttr("join", new BoxedFunction(boxRTFunction((void*)strJoin, NULL, 2, false)));
str_cls->giveAttr("split", new BoxedFunction(boxRTFunction((void*)strSplit1, LIST, 1, false)));
CLFunction *__new__ = boxRTFunction((void*)strNew1, NULL, 1, false);
addRTFunction(__new__, (void*)strNew2, NULL, 2, false);
......
def f(x):
print "f(%s)" % x
return -x
print map(f, range(10))
......@@ -4,20 +4,3 @@ import sys
sys.stdout.write("hello world\n")
print >>sys.stdout, "hello world"
class StringBuf(object):
def __init__(self):
self.s = ""
def write(self, s):
self.s += s
def getvalue(self):
return self.s
sys_stdout = sys.stdout
sys.stdout = StringBuf()
print "hello world"
print >>sys_stdout, "stringio contains:", repr(sys.stdout.getvalue())
# expected: fail
# prints without an explicit destination should go to sys.stdout, not to the real stdout,
# in case sys.stdout gets changed:
import sys
class StringBuf(object):
def __init__(self):
self.s = ""
def write(self, s):
self.s += s
def getvalue(self):
return self.s
sys_stdout = sys.stdout
sys.stdout = StringBuf()
print "hello world"
print >>sys_stdout, "stringio contains:", repr(sys.stdout.getvalue())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment