Commit 7a44bcaf authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #48 from undingen/backtick

Implement backtick (repr)
parents 3580fc81 f2d5277e
...@@ -189,6 +189,7 @@ class NameCollectorVisitor : public ASTVisitor { ...@@ -189,6 +189,7 @@ class NameCollectorVisitor : public ASTVisitor {
virtual bool visit_num(AST_Num *node) { return false; } virtual bool visit_num(AST_Num *node) { return false; }
virtual bool visit_pass(AST_Pass *node) { return false; } virtual bool visit_pass(AST_Pass *node) { return false; }
virtual bool visit_print(AST_Print *node) { return false; } virtual bool visit_print(AST_Print *node) { return false; }
virtual bool visit_repr(AST_Repr *node) { return false; }
virtual bool visit_return(AST_Return *node) { return false; } virtual bool visit_return(AST_Return *node) { return false; }
virtual bool visit_slice(AST_Slice *node) { return false; } virtual bool visit_slice(AST_Slice *node) { return false; }
virtual bool visit_str(AST_Str *node) { return false; } virtual bool visit_str(AST_Str *node) { return false; }
......
...@@ -358,6 +358,10 @@ class BasicBlockTypePropagator : public ExprVisitor, public StmtVisitor { ...@@ -358,6 +358,10 @@ class BasicBlockTypePropagator : public ExprVisitor, public StmtVisitor {
abort(); abort();
} }
virtual void* visit_repr(AST_Repr *node) {
return STR;
}
virtual void* visit_slice(AST_Slice *node) { virtual void* visit_slice(AST_Slice *node) {
return SLICE; return SLICE;
} }
......
...@@ -707,6 +707,20 @@ class IRGeneratorImpl : public IRGenerator { ...@@ -707,6 +707,20 @@ class IRGeneratorImpl : public IRGenerator {
RELEASE_ASSERT(0, ""); RELEASE_ASSERT(0, "");
} }
CompilerVariable* evalRepr(AST_Repr *node) {
assert(state != PARTIAL);
CompilerVariable *var = evalExpr(node->value);
ConcreteCompilerVariable *cvar = var->makeConverted(emitter, var->getBoxType());
var->decvref(emitter);
std::vector<llvm::Value*> args{cvar->getValue()};
llvm::Value* rtn = emitter.getBuilder()->CreateCall(g.funcs.repr, args);
cvar->decvref(emitter);
return new ConcreteCompilerVariable(STR, rtn, true);
}
CompilerVariable* evalSlice(AST_Slice *node) { CompilerVariable* evalSlice(AST_Slice *node) {
assert(state != PARTIAL); assert(state != PARTIAL);
...@@ -848,6 +862,9 @@ class IRGeneratorImpl : public IRGenerator { ...@@ -848,6 +862,9 @@ class IRGeneratorImpl : public IRGenerator {
case AST_TYPE::Num: case AST_TYPE::Num:
rtn = evalNum(ast_cast<AST_Num>(node)); rtn = evalNum(ast_cast<AST_Num>(node));
break; break;
case AST_TYPE::Repr:
rtn = evalRepr(ast_cast<AST_Repr>(node));
break;
case AST_TYPE::Slice: case AST_TYPE::Slice:
rtn = evalSlice(ast_cast<AST_Slice>(node)); rtn = evalSlice(ast_cast<AST_Slice>(node));
break; break;
...@@ -867,7 +884,7 @@ class IRGeneratorImpl : public IRGenerator { ...@@ -867,7 +884,7 @@ class IRGeneratorImpl : public IRGenerator {
rtn = evalClsAttribute(ast_cast<AST_ClsAttribute>(node)); rtn = evalClsAttribute(ast_cast<AST_ClsAttribute>(node));
break; break;
default: default:
printf("Unhandled expr type: %d (irgen.cpp:" STRINGIFY(__LINE__) ")\n", node->type); printf("Unhandled expr type: %d (irgenerator.cpp:" STRINGIFY(__LINE__) ")\n", node->type);
exit(1); exit(1);
} }
......
...@@ -493,6 +493,15 @@ AST_Num* read_num(BufferedReader *reader) { ...@@ -493,6 +493,15 @@ AST_Num* read_num(BufferedReader *reader) {
return rtn; return rtn;
} }
AST_Repr* read_repr(BufferedReader *reader) {
AST_Repr *rtn = new AST_Repr();
rtn->col_offset = readColOffset(reader);
rtn->lineno = reader->readULL();
rtn->value = readASTExpr(reader);
return rtn;
}
AST_Pass* read_pass(BufferedReader *reader) { AST_Pass* read_pass(BufferedReader *reader) {
AST_Pass *rtn = new AST_Pass(); AST_Pass *rtn = new AST_Pass();
...@@ -636,6 +645,8 @@ AST_expr* readASTExpr(BufferedReader *reader) { ...@@ -636,6 +645,8 @@ AST_expr* readASTExpr(BufferedReader *reader) {
return read_name(reader); return read_name(reader);
case AST_TYPE::Num: case AST_TYPE::Num:
return read_num(reader); return read_num(reader);
case AST_TYPE::Repr:
return read_repr(reader);
case AST_TYPE::Slice: case AST_TYPE::Slice:
return read_slice(reader); return read_slice(reader);
case AST_TYPE::Str: case AST_TYPE::Str:
......
...@@ -159,6 +159,7 @@ void initGlobalFuncs(GlobalState &g) { ...@@ -159,6 +159,7 @@ void initGlobalFuncs(GlobalState &g) {
GET(getclsattr); GET(getclsattr);
GET(unaryop); GET(unaryop);
GET(import); GET(import);
GET(repr);
GET(checkUnpackingLength); GET(checkUnpackingLength);
GET(raiseAttributeError); GET(raiseAttributeError);
......
...@@ -21,7 +21,7 @@ struct GlobalFuncs { ...@@ -21,7 +21,7 @@ struct GlobalFuncs {
llvm::Value *printf, *my_assert, *malloc, *free; llvm::Value *printf, *my_assert, *malloc, *free;
llvm::Value *boxInt, *unboxInt, *boxFloat, *unboxFloat, *boxStringPtr, *boxCLFunction, *unboxCLFunction, *boxInstanceMethod, *boxBool, *unboxBool, *createTuple, *createDict, *createList, *createSlice, *createClass; llvm::Value *boxInt, *unboxInt, *boxFloat, *unboxFloat, *boxStringPtr, *boxCLFunction, *unboxCLFunction, *boxInstanceMethod, *boxBool, *unboxBool, *createTuple, *createDict, *createList, *createSlice, *createClass;
llvm::Value *getattr, *setattr, *print, *nonzero, *binop, *compare, *augbinop, *unboxedLen, *getitem, *getclsattr, *getGlobal, *setitem, *unaryop, *import; llvm::Value *getattr, *setattr, *print, *nonzero, *binop, *compare, *augbinop, *unboxedLen, *getitem, *getclsattr, *getGlobal, *setitem, *unaryop, *import, *repr;
llvm::Value *checkUnpackingLength, *raiseAttributeError, *raiseAttributeErrorStr, *raiseNotIterableError, *assertNameDefined, *assertFail; llvm::Value *checkUnpackingLength, *raiseAttributeError, *raiseAttributeErrorStr, *raiseNotIterableError, *assertNameDefined, *assertFail;
llvm::Value *printFloat, *listAppendInternal; llvm::Value *printFloat, *listAppendInternal;
llvm::Value *dump; llvm::Value *dump;
......
...@@ -529,6 +529,15 @@ void* AST_Num::accept_expr(ExprVisitor *v) { ...@@ -529,6 +529,15 @@ void* AST_Num::accept_expr(ExprVisitor *v) {
return v->visit_num(this); return v->visit_num(this);
} }
void AST_Repr::accept(ASTVisitor *v) {
bool skip = v->visit_repr(this);
}
void* AST_Repr::accept_expr(ExprVisitor *v) {
return v->visit_repr(this);
}
void AST_Pass::accept(ASTVisitor *v) { void AST_Pass::accept(ASTVisitor *v) {
bool skip = v->visit_pass(this); bool skip = v->visit_pass(this);
} }
...@@ -1104,6 +1113,13 @@ bool PrintVisitor::visit_print(AST_Print *node) { ...@@ -1104,6 +1113,13 @@ bool PrintVisitor::visit_print(AST_Print *node) {
return true; return true;
} }
bool PrintVisitor::visit_repr(AST_Repr *node) {
printf("`");
node->value->accept(this);
printf("`");
return true;
}
bool PrintVisitor::visit_return(AST_Return *node) { bool PrintVisitor::visit_return(AST_Return *node) {
printf("return "); printf("return ");
return false; return false;
...@@ -1280,6 +1296,7 @@ class FlattenVisitor : public ASTVisitor { ...@@ -1280,6 +1296,7 @@ class FlattenVisitor : public ASTVisitor {
virtual bool visit_num(AST_Num *node) { output->push_back(node); return false; } virtual bool visit_num(AST_Num *node) { output->push_back(node); return false; }
virtual bool visit_pass(AST_Pass *node) { output->push_back(node); return false; } virtual bool visit_pass(AST_Pass *node) { output->push_back(node); return false; }
virtual bool visit_print(AST_Print *node) { output->push_back(node); return false; } virtual bool visit_print(AST_Print *node) { output->push_back(node); return false; }
virtual bool visit_repr(AST_Repr *node) { output->push_back(node); return false; }
virtual bool visit_return(AST_Return *node) { output->push_back(node); return false; } virtual bool visit_return(AST_Return *node) { output->push_back(node); return false; }
virtual bool visit_slice(AST_Slice *node) { output->push_back(node); return false; } virtual bool visit_slice(AST_Slice *node) { output->push_back(node); return false; }
virtual bool visit_str(AST_Str *node) { output->push_back(node); return false; } virtual bool visit_str(AST_Str *node) { output->push_back(node); return false; }
......
...@@ -560,6 +560,18 @@ class AST_Num : public AST_expr { ...@@ -560,6 +560,18 @@ class AST_Num : public AST_expr {
static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Num; static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Num;
}; };
class AST_Repr : public AST_expr {
public:
AST_expr* value;
virtual void accept(ASTVisitor *v);
virtual void* accept_expr(ExprVisitor *v);
AST_Repr() : AST_expr(AST_TYPE::Repr) {}
static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Repr;
};
class AST_Pass : public AST_stmt { class AST_Pass : public AST_stmt {
public: public:
virtual void accept(ASTVisitor *v); virtual void accept(ASTVisitor *v);
...@@ -776,6 +788,7 @@ class ASTVisitor { ...@@ -776,6 +788,7 @@ class ASTVisitor {
virtual bool visit_num(AST_Num *node) { assert(0); abort(); } virtual bool visit_num(AST_Num *node) { assert(0); abort(); }
virtual bool visit_pass(AST_Pass *node) { assert(0); abort(); } virtual bool visit_pass(AST_Pass *node) { assert(0); abort(); }
virtual bool visit_print(AST_Print *node) { assert(0); abort(); } virtual bool visit_print(AST_Print *node) { assert(0); abort(); }
virtual bool visit_repr(AST_Repr *node) { assert(0); abort(); }
virtual bool visit_return(AST_Return *node) { assert(0); abort(); } virtual bool visit_return(AST_Return *node) { assert(0); abort(); }
virtual bool visit_slice(AST_Slice *node) { assert(0); abort(); } virtual bool visit_slice(AST_Slice *node) { assert(0); abort(); }
virtual bool visit_str(AST_Str *node) { assert(0); abort(); } virtual bool visit_str(AST_Str *node) { assert(0); abort(); }
...@@ -828,6 +841,7 @@ class NoopASTVisitor : public ASTVisitor { ...@@ -828,6 +841,7 @@ class NoopASTVisitor : public ASTVisitor {
virtual bool visit_num(AST_Num *node) { return false; } virtual bool visit_num(AST_Num *node) { return false; }
virtual bool visit_pass(AST_Pass *node) { return false; } virtual bool visit_pass(AST_Pass *node) { return false; }
virtual bool visit_print(AST_Print *node) { return false; } virtual bool visit_print(AST_Print *node) { return false; }
virtual bool visit_repr(AST_Repr *node) { return false; }
virtual bool visit_return(AST_Return *node) { return false; } virtual bool visit_return(AST_Return *node) { return false; }
virtual bool visit_slice(AST_Slice *node) { return false; } virtual bool visit_slice(AST_Slice *node) { return false; }
virtual bool visit_str(AST_Str *node) { return false; } virtual bool visit_str(AST_Str *node) { return false; }
...@@ -860,6 +874,7 @@ class ExprVisitor { ...@@ -860,6 +874,7 @@ class ExprVisitor {
virtual void* visit_listcomp(AST_ListComp *node) { assert(0); abort(); } virtual void* visit_listcomp(AST_ListComp *node) { assert(0); abort(); }
virtual void* visit_name(AST_Name *node) { assert(0); abort(); } virtual void* visit_name(AST_Name *node) { assert(0); abort(); }
virtual void* visit_num(AST_Num *node) { assert(0); abort(); } virtual void* visit_num(AST_Num *node) { assert(0); abort(); }
virtual void* visit_repr(AST_Repr *node) { assert(0); abort(); }
virtual void* visit_slice(AST_Slice *node) { assert(0); abort(); } virtual void* visit_slice(AST_Slice *node) { assert(0); abort(); }
virtual void* visit_str(AST_Str *node) { assert(0); abort(); } virtual void* visit_str(AST_Str *node) { assert(0); abort(); }
virtual void* visit_subscript(AST_Subscript *node) { assert(0); abort(); } virtual void* visit_subscript(AST_Subscript *node) { assert(0); abort(); }
...@@ -938,6 +953,7 @@ class PrintVisitor : public ASTVisitor { ...@@ -938,6 +953,7 @@ class PrintVisitor : public ASTVisitor {
virtual bool visit_num(AST_Num *node); virtual bool visit_num(AST_Num *node);
virtual bool visit_pass(AST_Pass *node); virtual bool visit_pass(AST_Pass *node);
virtual bool visit_print(AST_Print *node); virtual bool visit_print(AST_Print *node);
virtual bool visit_repr(AST_Repr *node);
virtual bool visit_return(AST_Return *node); virtual bool visit_return(AST_Return *node);
virtual bool visit_slice(AST_Slice *node); virtual bool visit_slice(AST_Slice *node);
virtual bool visit_str(AST_Str *node); virtual bool visit_str(AST_Str *node);
......
...@@ -542,6 +542,14 @@ class CFGVisitor : public ASTVisitor { ...@@ -542,6 +542,14 @@ class CFGVisitor : public ASTVisitor {
return makeName(rtn_name, AST_TYPE::Load); return makeName(rtn_name, AST_TYPE::Load);
}; };
AST_expr* remapRepr(AST_Repr* node) {
AST_Repr *rtn = new AST_Repr();
rtn->lineno = node->lineno;
rtn->col_offset = node->col_offset;
rtn->value = remapExpr(node->value);
return rtn;
}
AST_expr* remapSlice(AST_Slice* node) { AST_expr* remapSlice(AST_Slice* node) {
AST_Slice *rtn = new AST_Slice(); AST_Slice *rtn = new AST_Slice();
rtn->lineno = node->lineno; rtn->lineno = node->lineno;
...@@ -627,6 +635,9 @@ class CFGVisitor : public ASTVisitor { ...@@ -627,6 +635,9 @@ class CFGVisitor : public ASTVisitor {
return node; return node;
case AST_TYPE::Num: case AST_TYPE::Num:
return node; return node;
case AST_TYPE::Repr:
rtn = remapRepr(ast_cast<AST_Repr>(node));
break;
case AST_TYPE::Slice: case AST_TYPE::Slice:
rtn = remapSlice(ast_cast<AST_Slice>(node)); rtn = remapSlice(ast_cast<AST_Slice>(node));
break; break;
......
...@@ -34,8 +34,8 @@ Box* dictRepr(BoxedDict* self) { ...@@ -34,8 +34,8 @@ Box* dictRepr(BoxedDict* self) {
} }
first = false; first = false;
BoxedString *k = repr(p.first); BoxedString *k = static_cast<BoxedString*>(repr(p.first));
BoxedString *v = repr(p.second); BoxedString *v = static_cast<BoxedString*>(repr(p.second));
chars.insert(chars.end(), k->s.begin(), k->s.end()); chars.insert(chars.end(), k->s.begin(), k->s.end());
chars.push_back(':'); chars.push_back(':');
chars.push_back(' '); chars.push_back(' ');
...@@ -79,7 +79,7 @@ Box* dictGetitem(BoxedDict* self, Box* k) { ...@@ -79,7 +79,7 @@ Box* dictGetitem(BoxedDict* self, Box* k) {
Box* &pos = self->d[k]; Box* &pos = self->d[k];
if (pos == NULL) { if (pos == NULL) {
BoxedString *s = repr(k); BoxedString *s = static_cast<BoxedString*>(repr(k));
fprintf(stderr, "KeyError: %s\n", s->s.c_str()); fprintf(stderr, "KeyError: %s\n", s->s.c_str());
raiseExc(); raiseExc();
} }
......
...@@ -70,6 +70,7 @@ void force() { ...@@ -70,6 +70,7 @@ void force() {
FORCE(setitem); FORCE(setitem);
FORCE(unaryop); FORCE(unaryop);
FORCE(import); FORCE(import);
FORCE(repr);
FORCE(checkUnpackingLength); FORCE(checkUnpackingLength);
FORCE(raiseAttributeError); FORCE(raiseAttributeError);
......
...@@ -41,7 +41,7 @@ extern "C" Box* listRepr(BoxedList* self) { ...@@ -41,7 +41,7 @@ extern "C" Box* listRepr(BoxedList* self) {
if (i > 0) if (i > 0)
os << ", "; os << ", ";
BoxedString *s = repr(self->elts->elts[i]); BoxedString *s = static_cast<BoxedString*>(repr(self->elts->elts[i]));
os << s->s; os << s->s;
} }
os << ']'; os << ']';
......
...@@ -949,7 +949,7 @@ extern "C" BoxedString* str(Box* obj) { ...@@ -949,7 +949,7 @@ extern "C" BoxedString* str(Box* obj) {
return static_cast<BoxedString*>(obj); return static_cast<BoxedString*>(obj);
} }
extern "C" BoxedString* repr(Box* obj) { extern "C" Box* repr(Box* obj) {
static StatCounter slowpath_repr("slowpath_repr"); static StatCounter slowpath_repr("slowpath_repr");
slowpath_repr.log(); slowpath_repr.log();
......
...@@ -39,7 +39,7 @@ extern "C" bool nonzero(Box* obj); ...@@ -39,7 +39,7 @@ extern "C" bool nonzero(Box* obj);
extern "C" Box* runtimeCall(Box*, int64_t, Box*, Box*, Box*, Box**); extern "C" Box* runtimeCall(Box*, int64_t, Box*, Box*, Box*, Box**);
extern "C" Box* callattr(Box*, std::string*, bool, int64_t, Box*, Box*, Box*, Box**); extern "C" Box* callattr(Box*, std::string*, bool, int64_t, Box*, Box*, Box*, Box**);
extern "C" BoxedString* str(Box* obj); extern "C" BoxedString* str(Box* obj);
extern "C" BoxedString* repr(Box* obj); extern "C" Box* repr(Box* obj);
extern "C" BoxedInt* hash(Box* obj); extern "C" BoxedInt* hash(Box* obj);
//extern "C" Box* abs_(Box* obj); //extern "C" Box* abs_(Box* obj);
//extern "C" Box* min_(Box* o0, Box* o1); //extern "C" Box* min_(Box* o0, Box* o1);
......
...@@ -107,7 +107,7 @@ Box* setRepr(BoxedSet* self) { ...@@ -107,7 +107,7 @@ Box* setRepr(BoxedSet* self) {
if (!first) { if (!first) {
os << ", "; os << ", ";
} }
os << repr(elt)->s; os << static_cast<BoxedString*>(repr(elt))->s;
first = false; first = false;
} }
os << "])"; os << "])";
......
...@@ -72,7 +72,7 @@ Box* tupleRepr(BoxedTuple *t) { ...@@ -72,7 +72,7 @@ Box* tupleRepr(BoxedTuple *t) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (i) os << ", "; if (i) os << ", ";
BoxedString *elt_repr =repr(t->elts[i]); BoxedString *elt_repr = static_cast<BoxedString*>(repr(t->elts[i]));
os << elt_repr->s; os << elt_repr->s;
} }
if (n == 1) os << ","; if (n == 1) os << ",";
......
...@@ -274,9 +274,9 @@ Box* instancemethodRepr(BoxedInstanceMethod* self) { ...@@ -274,9 +274,9 @@ Box* instancemethodRepr(BoxedInstanceMethod* self) {
} }
Box* sliceRepr(BoxedSlice* self) { Box* sliceRepr(BoxedSlice* self) {
BoxedString *start = repr(self->start); BoxedString *start = static_cast<BoxedString*>(repr(self->start));
BoxedString *stop = repr(self->stop); BoxedString *stop = static_cast<BoxedString*>(repr(self->stop));
BoxedString *step = repr(self->step); BoxedString *step = static_cast<BoxedString*>(repr(self->step));
std::string s = "slice(" + start->s + ", " + stop->s + ", " + step->s + ")"; std::string s = "slice(" + start->s + ", " + stop->s + ", " + step->s + ")";
return new BoxedString(s); return new BoxedString(s);
} }
......
print `42`
print `int`
print `2+3`
i = 23
print `i`
s = 'test'
print `s`
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