//Assign from a string. Rational& Rational::operator=(const string input) { int pos = input.find('/'); //Find the '/', or exit if invalid. if(pos > input.size()) { cout << "Invalid program string input. Creating whole number fraction.\n"; makeThis(parseStr(input), 1); return *this; } //Send the string chunks off for processing. makeThis( parseStr(input.substr( 0, pos )), parseStr(input.substr( (pos + 1), (input.size() - (pos + 1)) )) ); return *this; }
AstNode * makeObject() { AstNode *object; AstNode *equMeth, *construct, *destruct; AstNode *declator, *fparam; AstNode *ret, *eq; Literal *classLit, *oLit, *equalsLit; /* * Build the * int equals(Object o) { return this == o; } * method. */ classLit = makeLiteral(OBJECT_NAME, 0); oLit = makeLiteral("o", 0); fparam = makeFormalParam(makeNameId(classLit), makeNameId(oLit), 0); equalsLit = makeLiteral("equals", 0); declator = makeMethDeclator(makeNameId(equalsLit), makeSeq(fparam), 0); eq = makeBinaryOp(EQU_OP, makeThis(0), makeExprId(oLit), 0); ret = makeReturnSt(eq, 0); equMeth = makeMethDecl(makeInt(0), declator, makeSeq(ret), 0); /* * Build * Object() { return; } * constructor */ declator = makeConstDeclator(makeNameId(classLit), NULL, 0); construct = makeConstDecl(declator, makeSeq(makeReturnSt(NULL, 0)), 0); /* * Build * ~Object() { return; } * destructor */ destruct = makeDestructor(makeNameId(classLit), makeSeq(makeReturnSt(NULL, 0)), 0); /* * Link the class all together. */ object = makeClass(makeNameId(classLit), NULL, appendSeq(appendSeq(makeSeq(equMeth), makeSeq(construct)), makeSeq(destruct)), 0); return object; }
//Assign from an integer. Rational& Rational::operator=(const int rhs) { makeThis(rhs, 1); return *this; }
//Assign from another Rational. Rational& Rational::operator=(const Rational &rhs) { //Check for self-assignment if (this != &rhs) makeThis(rhs.num, rhs.dem); return *this; }
//Copy Constructor Rational::Rational(const Rational &input) { makeThis(input.num, input.dem); return; }
//Full Constructor Rational::Rational(int top, int bot) { makeThis(top, bot); return; }
//Half Constructor Rational::Rational(int top) { makeThis(top, 1); return; }
//Default Constructor Rational::Rational() { makeThis(0, 1); return; }