vector<string> getTokens ( string& expression ) { vector<string> vec; const char* array = expression.c_str(); int length = expression.size(); int index = 0; string currentToken = ""; bool inQuote = false; bool value = false; while ( index < length ) { if ( ( array[index] == '\t'||array[index] == '\r' ) &&!inQuote ) { //do nothing when indent } else if ( array[index] == '\n'&&!inQuote ) { insertToken() currentToken = JASM_LINE_SEP; insertToken() } else if ( array[index] == '"' || array[index] == '\'') {
void createToken(char* lexeme, int type){ Token *tok = malloc(sizeof(Token)); char lexarray[20] = {'\0'}; int i; //Convert pointer array to char array for(i = 0; i < 20; i++){ lexarray[i] = *(lexeme + sizeof(char) * i); tok->lexeme[i] = lexarray[i]; } tok->type = type; insertToken(&tokenList, tok); free(tok); }
// // State transition matrix for a reader, based on PCSC state changes // void Reader::update(const PCSC::ReaderState &state) { // set new state unsigned long oldState = mState.state(); mState = state; mState.name(mName.c_str()); // (fix name pointer, unchanged) try { if (state.state(SCARD_STATE_UNAVAILABLE)) { // reader is unusable (probably being removed) secdebug("reader", "%p (%s) unavailable (0x%lx)", this, name().c_str(), state.state()); if (mToken) removeToken(); } else if (state.state(SCARD_STATE_EMPTY)) { // reader is empty (no token present) secdebug("reader", "%p (%s) empty (0x%lx)", this, name().c_str(), state.state()); if (mToken) removeToken(); } else if (state.state(SCARD_STATE_PRESENT)) { // reader has a token inserted secdebug("reader", "%p (%s) card present (0x%lx)", this, name().c_str(), state.state()); //@@@ is this hack worth it (with notifications in)?? if (mToken && CssmData(state) != CssmData(pcscState())) removeToken(); // incomplete but better than nothing //@@@ or should we call some verify-still-the-same function of tokend? //@@@ (I think pcsc will return an error if the card changed?) if (!mToken) insertToken(NULL); } else { secdebug("reader", "%p (%s) unexpected state change (0x%lx to 0x%lx)", this, name().c_str(), oldState, state.state()); } } catch (...) { secdebug("reader", "state update exception (ignored)"); } }
Value* tokenize(char* expression){ Value* head = NULL; char* temp = malloc(256 * sizeof(char)); temp = memset(temp, 0, (256 * sizeof(char))); int j = 0; int stringFlag = 0; int i; for (i=0; expression[i]; i++){ switch(expression[i]){ case '(': if (strlen(temp) == 0){ temp[0] = '('; head = insertToken(temp, head); clearTemp(temp, 1); } else { head = insertToken(temp, head); if (head == NULL){ printf("Untokenizable input: %s \n", temp); return NULL; } clearTemp(temp, j); temp[0] = '('; head = insertToken(temp, head); clearTemp(temp, 1); j=0; } break; case ')': if (strlen(temp) == 0){ temp[0] = ')'; head = insertToken(temp, head); clearTemp(temp, 1); } else { head = insertToken(temp, head); if (head == NULL){ printf("Untokenizable input: %s \n", temp); return NULL; } clearTemp(temp, j); temp[0] = ')'; head = insertToken(temp, head); clearTemp(temp, 1); j=0; } break; case ' ': if (stringFlag){ temp[j] = expression[i]; j++; break; } if (strlen(temp) > 0){ head = insertToken(temp, head); if (head == NULL){ printf("Untokenizable input: %s \n", temp); return NULL; } clearTemp(temp, j); j=0; } break; case '\t': if (stringFlag){ temp[j] = expression[i]; j++; break; } if (strlen(temp) > 0){ head = insertToken(temp, head); if (head == NULL){ printf("Untokenizable input: %s \n", temp); return NULL; } clearTemp(temp, j); j=0; } break; case '\n': if (strlen(temp) > 0){ head = insertToken(temp, head); if (head == NULL){ printf("Untokenizable input: %s \n", temp); return NULL; } clearTemp(temp, j); j=0; } break; case ';': return NULL; default: if (expression[i] == '\"') stringFlag = (stringFlag + 1) % 2; temp[j] = expression[i]; j++; break; } } // check if it's greater than one because it will have a '\n' from pressing the enter key. if (strlen(temp) > 1){ printf("Invalid input, boy!\n"); } free(temp); head = reverseList(head); return head; }
int main(int argc, char *argv[]) { char ch, *str, *strStart; int i; double number; struct String input = {NULL, 0, 0, 5}; /* Kick off scope */ struct Scope *currentScope = NULL; addScope(currentScope, ¤tScope); /* Read input from... */ if (argc == 1) { /* stdin */ while ((ch = getchar()) != '\n' && ch != EOF) { stringAddChar(&input, ch); } } else { /* args */ for (i = 1; i < argc; i++) { stringAddStr(&input, argv[i]); stringAddChar(&input, ' '); } } /* Tokenise */ str = input.str; while (*str != '\0') { strStart = str; if (isspace(*str)) { /* Space */ str++; } else if (*str == '(') { addScope(currentScope, ¤tScope); str++; } else if (*str == ')') { insertToken(currentScope, NUM, '\0', evaluateScope(currentScope, ¤tScope)); str++; } else if ((number = strtold(str, &str)) == 0.0L && (number = strtoconst(str, &str)) == 0.0L) { /* NaN */ insertToken(currentScope, OP, normalise(*str), 0); str++; } else { /* Number */ if ((*strStart == '+' || *strStart == '-') && currentScope->last != NULL && strcmp(currentScope->last->type, OP)) { //Assume this was supposed to be an operation insertToken(currentScope, OP, *strStart, 0); } insertToken(currentScope, NUM, '\0', number); } } /* * TODO: Increase precision * TODO: Allow precision to be specified */ printf("%.15g\n", evaluateScope(currentScope, ¤tScope)); free(input.str); return 0; }