Commit 9a79b92c authored by Kevin Modzelewski's avatar Kevin Modzelewski

Start working on that gcj test

Added some string functions and fixed a couple bugs.
parent ac5c2a3c
......@@ -54,6 +54,8 @@ static ConcreteCompilerType* unboxedType(ConcreteCompilerType* t) {
return INT;
if (t == BOXED_FLOAT)
return FLOAT;
if (t == BOXED_BOOL)
return BOOL;
return t;
}
......
......@@ -1154,6 +1154,18 @@ public:
return new ConcreteCompilerVariable(other_type, boxed, true);
}
virtual CompilerType* getattrType(const std::string* attr, bool cls_only) {
return BOXED_BOOL->getattrType(attr, cls_only);
}
virtual CompilerVariable* getattr(IREmitter& emitter, const OpInfo& info, VAR* var, const std::string* attr,
bool cls_only) {
ConcreteCompilerVariable* converted = var->makeConverted(emitter, BOXED_BOOL);
CompilerVariable* rtn = converted->getattr(emitter, info, attr, cls_only);
converted->decvref(emitter);
return rtn;
}
virtual ConcreteCompilerType* getBoxType() { return BOXED_BOOL; }
};
ConcreteCompilerType* BOOL = new BoolType();
......
......@@ -572,6 +572,10 @@ private:
boxed_left->decvref(emitter);
boxed_right->decvref(emitter);
if (type == AST_TYPE::In || type == AST_TYPE::NotIn || type == AST_TYPE::Is || type == AST_TYPE::IsNot) {
return unboxVar(BOXED_BOOL, rtn, true);
}
return new ConcreteCompilerVariable(UNKNOWN, rtn, true);
}
......@@ -937,6 +941,11 @@ private:
ConcreteCompilerVariable* rtn = new ConcreteCompilerVariable(FLOAT, unboxed, true);
return rtn;
}
if (t == BOXED_BOOL) {
llvm::Value* unboxed = emitter.getBuilder()->CreateCall(g.funcs.unboxBool, v);
ConcreteCompilerVariable* rtn = new ConcreteCompilerVariable(BOOL, unboxed, true);
return rtn;
}
return new ConcreteCompilerVariable(t, v, grabbed);
}
......
......@@ -12,13 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/ast.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <stdint.h>
#include <cassert>
#include "core/ast.h"
#include "core/cfg.h"
#define FUTURE_DIVISION 0
......
......@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/cfg.h"
#include <algorithm>
#include <cstdio>
#include <cassert>
......@@ -20,7 +22,6 @@
#include "core/options.h"
#include "core/ast.h"
#include "core/cfg.h"
//#undef VERBOSITY
//#define VERBOSITY(x) 2
......@@ -1096,6 +1097,7 @@ public:
curblock = test_block;
AST_Branch* br = makeBranch(remapExpr(node->test));
CFGBlock* test_block_end = curblock;
push_back(br);
// We need a reference to this block early on so we can break to it,
......@@ -1107,7 +1109,8 @@ public:
CFGBlock* body = cfg->addBlock();
body->info = "while_body_start";
br->iftrue = body;
test_block->connectTo(body);
test_block_end->connectTo(body);
curblock = body;
for (int i = 0; i < node->body.size(); i++) {
node->body[i]->accept(this);
......@@ -1123,7 +1126,7 @@ public:
CFGBlock* orelse = cfg->addBlock();
orelse->info = "while_orelse_start";
br->iffalse = orelse;
test_block->connectTo(orelse);
test_block_end->connectTo(orelse);
curblock = orelse;
for (int i = 0; i < node->orelse.size(); i++) {
node->orelse[i]->accept(this);
......
......@@ -29,16 +29,13 @@
#include <vector>
#include "core/ast.h"
#include "core/common.h"
namespace pyston {
class AST_stmt;
namespace AST_TYPE {
enum AST_TYPE;
}
class CFG;
class CFGBlock {
private:
......
......@@ -138,9 +138,9 @@ int main(int argc, char** argv) {
m = parse(fn);
if (VERBOSITY() >= 1) {
fprintf(stderr, "Parsed code; ast:\n");
printf("Parsed code; ast:\n");
print_ast(m);
fprintf(stderr, "==============\n");
printf("==============\n");
}
try {
......
......@@ -383,6 +383,49 @@ Box* strSplit2(BoxedString* self, BoxedString* sep) {
}
}
Box* strStrip(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& s = self->s;
int n = s.size();
int strip_beginning = 0;
while (strip_beginning < n) {
char c = s[strip_beginning];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v')
strip_beginning++;
else
break;
}
if (strip_beginning == n)
return boxStrConstant("");
int strip_end = 0;
while (strip_end < n) {
char c = s[n - strip_end - 1];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v')
strip_end++;
else
break;
}
return new BoxedString(s.substr(strip_beginning, n - strip_beginning - strip_end));
}
Box* strContains(BoxedString* self, Box* elt) {
assert(self->cls == str_cls);
if (elt->cls != str_cls)
raiseExcHelper(TypeError, "'in <string>' requires string as left operand, not %s", getTypeName(elt)->c_str());
BoxedString* sub = static_cast<BoxedString*>(elt);
size_t found_idx = self->s.find(sub->s);
if (found_idx == std::string::npos)
return False;
return True;
}
extern "C" Box* strGetitem(BoxedString* self, Box* slice) {
if (slice->cls == int_cls) {
......@@ -419,6 +462,8 @@ void setupStr() {
str_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)strNonzero, NULL, 1, false)));
str_cls->giveAttr("lower", new BoxedFunction(boxRTFunction((void*)strLower, STR, 1, false)));
str_cls->giveAttr("strip", new BoxedFunction(boxRTFunction((void*)strStrip, STR, 1, false)));
str_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)strContains, BOXED_BOOL, 2, false)));
str_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)strAdd, NULL, 2, false)));
str_cls->giveAttr("__mod__", new BoxedFunction(boxRTFunction((void*)strMod, NULL, 2, false)));
......
......@@ -12,7 +12,8 @@ def compact(s):
i += 1
return s
class NotPossible(Exception):
# TODO This should be a subclass of Exception not object:
class NotPossible(object):
pass
P = 1000000007
......
......@@ -19,3 +19,8 @@ print map(bool, ["hello", "", "world"])
if "":
print "bad"
print repr(" \t\n\v\ftest \t\n\v\f".strip())
for pattern in ["hello", "o w", "nope"]:
print pattern in "hello world"
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