void test_stringTrimLeft(void) { Text *text = textNew("SaJon "); String *str = stringNew(text); stringTrimLeft(str); TEST_ASSERT_EQUAL(0,str->start); TEST_ASSERT_EQUAL(6,str->length); }
/** * Top module to extract the whole string into final OpCode * * Input: * String instructions,with appropriate arguments * * Return OpCode in int type */ unsigned int interpret(String *instruction){ stringTrimLeft(instruction); String *instString = stringRemoveWordNotContaining(instruction," "); char *instChar = stringSubstringInChar(instString,0,instString->length); instructionTable inst = getInstruction(instChar); if(inst.type == FDA_TYPE) return inst.opCode | FDA(instruction); else if(inst.type == FBA_TYPE) return inst.opCode | FBA(instruction); else if(inst.type == FA_TYPE) return inst.opCode | FA(instruction); else if(inst.type == FSFD_TYPE) return inst.opCode | FSFD(instruction); else if(inst.type == N_TYPE) return inst.opCode | N(instruction); else if(inst.type == NS_TYPE) return inst.opCode | NS(instruction); else if(inst.type == S_TYPE) return inst.opCode | S(instruction); else if(inst.type == K_TYPE) return inst.opCode | K(instruction); else if(inst.type == FK_TYPE) return inst.opCode | FK(instruction); else Throw(ERR_ILLEGAL_ARGUMENT); }
/** * To extract the argument of address * * Input: * String the whole argument * * Return the value of the address * Throw if value is invalid */ int extractValue(String *arguments){ char *returnChar; int returnInt; String *string; stringTrimLeft(arguments); if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringCharAt(arguments,0) == ';' || stringLength(arguments) == 0) Throw(ERR_NO_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0) Throw(ERR_EMPTY_ARGUMENT); returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); free(string); return returnInt; } //pass to jason
void test_stringTrimLeft2(void) { Text *text = textNew(" HohnJon "); String *str = stringNew(text); str->start+=3; str->length-=3; stringTrimLeft(str); TEST_ASSERT_EQUAL(4,str->start); TEST_ASSERT_EQUAL(8,str->length); }
/** * To extract the argument of access or banked * * Input: * String the whole argument * * Return the value of access/banked * Throw if value is invalid */ int extractAccessBanked(String *arguments){ char location; char *returnChar; int returnInt; String *string; String *banked = stringNew(textNew("BANKED")); //1 String *access = stringNew(textNew("ACCESS")); //0 if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_NO_ARGUMENT); stringTrimLeft(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_EMPTY_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0){ if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); Throw(ERR_EMPTY_ARGUMENT); } else if(stringLength(string) == 6){ if(stringIsEqual(string,banked)) returnInt = 1; else if(stringIsEqual(string,access)) returnInt = 0; else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } } else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); free(string); return returnInt; }
/** * To extract the argument of location to save * * Input: * String the whole argument * * Return the value of the location * Throw if value is invalid */ int extractDestination(String *arguments){ char location; char *returnChar; int returnInt; String *string; if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); if(stringLength(arguments) == 0 ||stringCharAt(arguments,0) == ';') Throw(ERR_NO_ARGUMENT); stringTrimLeft(arguments); if(stringLength(arguments) == 0 || stringCharAt(arguments,0)== ';') Throw(ERR_EMPTY_ARGUMENT); string = stringRemoveWordNotContaining(arguments,",;"); stringTrim(string); if(stringLength(string) == 0){ if(stringCharAt(arguments,0) == ',') stringRemoveChar(arguments); Throw(ERR_EMPTY_ARGUMENT); } else if(stringLength(string) == 1){ location = stringCharAt(string,0); if(location == 'F') returnInt = 1; else if(location == 'W') returnInt = 0; else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } } else{ returnChar = stringSubstringInChar(string,0,string->length); returnInt = evaluate(returnChar); } free(string); return returnInt; }//if f, return 1, w is 0
static void getNextToken_Coord(char **s, int16_t *i, int16_t *j) { _currentTokenStr = stringTrimLeft(*s); if (_currentTokenStr && _currentTokenStr[0] == '(') { errno = 0; *i = strtol(&_currentTokenStr[1], 0, 0); if (errno == 0) { const char *subTokenStr = strchr(&_currentTokenStr[1], ','); if (subTokenStr) { errno = 0; *j = strtol(&subTokenStr[1], 0, 0); if (errno == 0) { const char *endTokenStr = strchr(&subTokenStr[1], ')'); if (endTokenStr) { *s = (char *)&endTokenStr[1]; return; } } } } } error("Parse error for coord '%s'", !_currentTokenStr ? "" : _currentTokenStr); }
/** * Return the one token from the String. The String is updated. * If the string is empty, NULL is return. * * Input: * str is the String object * * Possible returned token: * Number, Operator, and Identifier tokens */ Token *getToken(String *str) { //1 trim left //2 check first character // a -if a number then extract the number string // -convert into integer // -create number token // b -if alphabetSet then extract the identifier // -create identifier token // c -if operator set then extract operator symbol // -create operator token char charReturn[3]; Token *tokenReturn = NULL; String *strReturn = NULL; Number *number = NULL; Identifier *identifier = NULL; Operator *operator = NULL; stringTrimLeft(str); if(stringLength(str) == 0) return NULL; //Number if(stringIsCharAtInSet(str,0,numberSet)){ strReturn = stringRemoveWordContaining(str,numberSet); if(stringIsCharAtInSet(str,0,alphabetSet)){ Throw(ERR_NUMBER_NOT_WELL_FORMED); } else{ number = numberNew(stringToInteger(strReturn)); tokenReturn = (Token *)number; stringDel(strReturn); } } //Identifier else if(stringIsCharAtInSet(str,0,alphabetSet)){ strReturn = stringRemoveWordContaining(str,alphaNumericSet); identifier = identifierNew(stringSubstringInText(strReturn,0,strReturn->length)); tokenReturn = (Token *)identifier; stringDel(strReturn); } //Operator else if(stringIsCharAtInSet(str,0,operatorSet)){ charReturn[0] = (char )stringRemoveChar(str); charReturn[1] = 0; if(stringCharAt(str,0) == charReturn[0] && stringLength(str) != 0){ if(charReturn[0] == '&'){ charReturn[0] = '&'; charReturn[1] = '&'; charReturn[2] = 0; str->start++; str->length--; } else if(charReturn[0] == '|'){ charReturn[0] = '|'; charReturn[1] = '|'; charReturn[2] = 0; str->start++; str->length--; } } operator = operatorNewBySymbol(charReturn); tokenReturn = (Token *)operator; } else Throw(ERR_ILLEGAL_ARGUMENT); return tokenReturn; }
void stringTrim(String *string){ stringTrimLeft(string); stringTrimRight(string); }