char getNextCharacter(bool skipWhiteSpace) { /* Get the next character from the line buffer. */ inChar = *(FP->cp); /* If it is the EOL then read the next line from the input file */ if (!inChar) { /* We have used all of the characters on this line. Read the next * line of data */ if (getLine()) { /* Uh-oh, we are out of data! Just return some bogus value. */ inChar = '?'; } /* end if */ else { /* Otherwise, recurse to try again. */ return getNextCharacter(skipWhiteSpace); } /* end else */ } /* end if */ /* If it is a space and we have been told to skip spaces then consume * the input line until a non-space or the EOL is encountered. */ else if (skipWhiteSpace) { while ((isspace(inChar)) && (inChar)) { /* Skip over the space */ (FP->cp)++; /* A get the character after the space */ inChar = *(FP->cp); } /* end while */ /* If we hit the EOL while searching for the next non-space, then * recurse to try again on the next line */ if (!inChar) { return getNextCharacter(skipWhiteSpace); } } /* end else if */ return inChar; } /* end getNextCharacter */
bool XTeXTInputStream::getNextToken(InputToken *result) { int status, tokenLen; getNextToken_START: status = getNextCharacter(); tokenLen = 0; switch (status) { case '+': // proper word; put into even index address case '-': // annotation symbol; put into odd index address if (status == '-') sequenceNumber = (sequenceNumber | 1); else sequenceNumber = (sequenceNumber | 1) + 1; result->sequenceNumber = sequenceNumber; result->filePosition = filePosition - 1; while ((status = getNextCharacter()) != '\n') { if (status < 0) break; result->token[tokenLen++] = (byte)status; assert(tokenLen <= MAX_TOKEN_LENGTH); } result->token[tokenLen] = 0; return true; default: // text; skip until end of line while ((status = getNextCharacter()) != '\n') if (status < 0) return false; } goto getNextToken_START; return false; } // end of getNextToken(InputToken*)
bool Compiler::testNextCharacter(std::wistream &source, SourcePos &pos, wchar_t test, Token::Type tokenIfTrue) { if ((int)source.peek() == int(test)) { tokens.push_back(Token(tokenIfTrue, pos)); getNextCharacter(source, pos); return true; } return false; }
uint64_t readNumber1() { int const v = getNextCharacter(); if ( v < 0 ) { ::libmaus::exception::LibMausException ex; ex.getStream() << "Failed to read number in ::libmaus::aio::SocketFastReaderBase::readNumber1()."; ex.finish(); throw ex; } return v; }
std::pair < char const *, uint64_t > getLineRaw() { int c; cb.reset(); while ( (c=getNextCharacter()) >= 0 && c != '\n' ) cb.bufferPush(c); if ( cb.length == 0 && c == -1 ) return std::pair<char const *, uint64_t>(reinterpret_cast<char const *>(0),0); else return std::pair<char const *, uint64_t>(cb.buffer,cb.length); }
/*----------------------------------------------------------------------------*/ int initializeScanner(char *fileName) { /* open the input file */ file = fopen(fileName, "r"); if(file == NULL) { fprintf(stderr, "ERROR: Could not open %s for reading!\r\n", fileName); return EXIT_FAILURE; } /* initialize line and column number */ currentLine = 1; currentColumn = 0; currentCharacter = '\0'; getNextCharacter(); scanNextSymbol(); return EXIT_SUCCESS; }
static void writeText (uint16_t fileNumber) { exprType writeType; STYPE *wPtr; TRACE(lstFile, "[writeText]"); for (;;) { /* The general form is <expression>, <expression>, ... However, * there are a few unique things that must be handled as special * cases */ switch (token) { /* const strings -- either literal constants (tSTRING_CONST) * or defined string constant symbols (sSTRING_CONST) */ case tSTRING_CONST : { /* Add the literal string constant to the RO data section * and receive the offset to the data. */ uint32_t offset = poffAddRoDataString(poffHandle, tkn_strt); /* Set the offset and size on the stack (order is important) */ pas_GenerateDataOperation(opLAC, (uint16_t)offset); pas_GenerateDataOperation(opPUSH, strlen(tkn_strt)); pas_GenerateIoOperation(xWRITE_STRING, fileNumber); pas_GenerateDataOperation(opINDS, -(sPTR_SIZE + sINT_SIZE)); stringSP = tkn_strt; getToken(); } break; case sSTRING_CONST : pas_GenerateDataOperation(opLAC, (uint16_t)tknPtr->sParm.s.offset); pas_GenerateDataOperation(opPUSH, (uint16_t)tknPtr->sParm.s.size); pas_GenerateIoOperation(xWRITE_STRING, fileNumber); pas_GenerateDataOperation(opINDS, -(sPTR_SIZE + sINT_SIZE)); getToken(); break; /* Array of type CHAR without indexing */ case sARRAY : wPtr = tknPtr->sParm.v.parent; if (((wPtr) && (wPtr->sKind == sTYPE)) && (wPtr->sParm.t.type == sCHAR) && (getNextCharacter(true) != '[')) { pas_GenerateStackReference(opLAS, wPtr); pas_GenerateDataOperation(opPUSH, wPtr->sParm.v.size); pas_GenerateIoOperation(xWRITE_STRING, fileNumber); pas_GenerateDataOperation(opINDS, -(sPTR_SIZE + sINT_SIZE)); break; } /* end if */ /* Otherwise, we fall through to process the ARRAY like any */ /* expression */ default : writeType = expression(exprUnknown, NULL); switch (writeType) { case exprInteger : pas_GenerateIoOperation(xWRITE_INT, fileNumber); pas_GenerateDataOperation(opINDS, -sINT_SIZE); break; case exprBoolean : error(eNOTYET); break; case exprChar : pas_GenerateIoOperation(xWRITE_CHAR, fileNumber); pas_GenerateDataOperation(opINDS, -sINT_SIZE); break; case exprReal : pas_GenerateIoOperation(xWRITE_REAL, fileNumber); pas_GenerateDataOperation(opINDS, -sREAL_SIZE); break; case exprString : case exprStkString : pas_GenerateIoOperation(xWRITE_STRING, fileNumber); pas_GenerateDataOperation(opINDS, -sRSTRING_SIZE); break; default : error(eWRITEPARM); break; } /* end switch */ break; } /* end switch */ if (token == ',') getToken(); else return; } /* end for */ } /* end writeText */
static void readText (uint16_t fileNumber) { STYPE *rPtr; TRACE(lstFile, "[readText]"); /* The general form is <VAR parm>, <VAR parm>,... */ for (;;) { switch (token) { /* SPECIAL CASE: Array of type CHAR without indexing */ case sARRAY : rPtr = tknPtr->sParm.v.parent; if (((rPtr) && (rPtr->sKind == sTYPE)) && (rPtr->sParm.t.type == sCHAR) && (getNextCharacter(true) != '[')) { pas_GenerateStackReference(opLAS, rPtr); pas_GenerateDataOperation(opPUSH, rPtr->sParm.v.size); pas_GenerateIoOperation(xREAD_STRING, fileNumber); pas_GenerateDataOperation(opINDS, -(sPTR_SIZE+sINT_SIZE)); } /* end if */ /* Otherwise, we fall through to process the ARRAY like any */ /* expression */ default : switch (varParm(exprUnknown, NULL)) { case exprIntegerPtr : pas_GenerateIoOperation(xREAD_INT, fileNumber); pas_GenerateDataOperation(opINDS, -sPTR_SIZE); break; case exprCharPtr : pas_GenerateIoOperation(xREAD_CHAR, fileNumber); pas_GenerateDataOperation(opINDS, -sPTR_SIZE); break; case exprRealPtr : pas_GenerateIoOperation(xREAD_REAL, fileNumber); pas_GenerateDataOperation(opINDS, -sPTR_SIZE); break; default : error(eINVARG); break; } /* end switch */ break; } /* end switch */ if (token == ',') getToken(); else return; } /* end for */ } /* end readText */
/*----------------------------------------------------------------------------*/ int scanNextSymbol() { int i; /* remember current position */ currentSymbolLine = currentLine; currentSymbolColumn = currentColumn; /* check if the character is a newline symbol */ if((currentCharacter == '\r') || (currentCharacter == '\n')) { getNextCharacter(); currentSymbol = NEWLINE_SYM; return currentSymbol; } /* check if the end of file is reached */ if(currentCharacter == EOF) { currentSymbol = EOF_SYM; return currentSymbol; } /* check if the character is a terminal symbol */ currentSymbol=0; while((terminalSymList[currentSymbol] != currentCharacter) && (terminalSymList[currentSymbol] != '\0')) currentSymbol++; /* return the terminal symbol if one was found */ if(terminalSymList[currentSymbol] != '\0') { getNextCharacter(); return currentSymbol; } /* check if the character is a hex or dec number */ if(isdigit(currentCharacter)) { currentNumberValue = 0; /* if the first digit is a 0 it might be a hex number */ if(currentCharacter == '0') { /* if the next character is a 'x', */ /* a hex number was found */ getNextCharacter(); if(tolower(currentCharacter) == 'x') { /* check if the 'x' is followed by a hex digit*/ /* and otherwise return an unknown symbol */ getNextCharacter(); if(!isxdigit(currentCharacter)) { currentSymbol = UNKNOWN_SYM; return currentSymbol; } /* put together the hexadecimal number */ /* by using the horner scheme */ while(isxdigit(currentCharacter)) { if(isdigit(currentCharacter)) { /* add one of the digits 0-9 */ currentNumberValue *= 16; currentNumberValue += currentCharacter-'0'; } else { /* add one of the digits A-F */ currentNumberValue *= 16; currentNumberValue += currentCharacter-'A'+10; } getNextCharacter(); } currentSymbol = HEX_NUMBER_SYM; return currentSymbol; } } /* if no hex number was found and currentCharacter is a digit,*/ /* a decimal number was found */ if(isdigit(currentCharacter)) { /* put together the decimal number */ /* by using the horner scheme */ while(isdigit(currentCharacter)) { currentNumberValue *= 10; currentNumberValue += currentCharacter-'0'; getNextCharacter(); } } currentSymbol = DEC_NUMBER_SYM; return currentSymbol; } /* check if the character is part of an ident */ if(isalpha(currentCharacter)) { i=0; currentIdentName[i] = tolower(currentCharacter); getNextCharacter(); i++; /* put together ident */ while((i < MAX_IDENT_LENGTH - 1) && (isalnum(currentCharacter))) { currentIdentName[i] = tolower(currentCharacter); getNextCharacter(); i++; } currentIdentName[i] = '\0'; /* return IDENT_SYM if the maximum length hasn't been exceeded*/ if(!isalnum(currentCharacter)) { currentSymbol = IDENT_SYM; return currentSymbol; } } /* return UNKNOWN_SYM if an unexpected character or */ /* a too long ident was found */ getNextCharacter(); currentSymbol = UNKNOWN_SYM; return currentSymbol; }
/** * This internal function implements the state machine logic used when sending commands to the LCD. * It has both an initialization state machine and an update state machine, so the first few times * it will be called, it will run through the initialization portion. After that it enters the * update portion. It has been optimized so that only the necessary lines are updated depending on * what data has been changed since the last time it was called. */ static void runSM(void) { switch (lcdState) { // LCD initialization states case initStartState: SET_TIMER2_DELAY(LONG_DELAY); sendCommand(SetDisplayMode); lcdState = enableDisplayState; break; case enableDisplayState: SET_TIMER2_DELAY(SHORT_DELAY); sendCommand(EnableDisplay); lcdState = setEntryModeState; break; case setEntryModeState: SET_TIMER2_DELAY(SHORT_DELAY); sendCommand(SetEntryMode); lcdState = clearDisplayState; break; case clearDisplayState: SET_TIMER2_DELAY(LONG_DELAY); sendCommand(ClearDisplay); lcdState = waitForUpdateState; break; // LCD update states case waitForUpdateState: if (pendingUpdate) { SET_TIMER2_DELAY(SHORT_DELAY); lcdState = moveCursorLineState; currentLine = 0; pendingUpdate = false; } break; case moveCursorLineState: SET_TIMER2_DELAY(SHORT_DELAY); setLine(currentLine); lcdState = updateLineState; break; case updateLineState: { char tmp = getNextCharacter(); if (tmp != '\0') { SET_TIMER2_DELAY(SHORT_DELAY); sendData(tmp); } else { SET_TIMER2_DELAY(LONG_DELAY); currentLine++; if (currentLine >= LCD_LINE_TOTAL) { currentLine = 0; lcdState = waitForUpdateState; } else { lcdState = moveCursorLineState; } } } break; default: lcdState = initStartState; break; } }
int get() { return getNextCharacter(); }
bool getSimulatorConfiguration(struct simulatorStructure *simulator, const char *configurationFilePath) { // Initialize variables FILE *input; fpos_t cursor; unsigned int length; char *line; // Return failure if the file doesn't exist if((input = fopen(configurationFilePath, "r")) == NULL) return false; // Go through file while(getNextCharacter(input) != EOF) { // Get current position in file fgetpos(input, &cursor); // Get length of next part in file until a colon or newline is found for(length = 0; getNextCharacter(input) != '\n' && fgetc(input) != ':'; length++); // Check if the next character isn't a newline if(getNextCharacter(input) != '\n') { // Go back to last position in file fsetpos(input, &cursor); // Allocate memory for line including a space for a null terminator line = (char*)malloc(sizeof(char) * (length + 1)); // Read in line fread(line, sizeof(char), length, input); // Set last character of line to null terminator line[length] = '\0'; // Skip the colon and space in file fgetc(input); fgetc(input); // Get current position in file fgetpos(input, &cursor); // Get length of next part in file until a newline is found for(length = 0; fgetc(input) != '\n'; length++); // Go back to last position in file fsetpos(input, &cursor); // Check if line is version if(strcmp(line, "Version") == 0) { // Allocate memory for version including a space for a null terminator simulator->version = (char*)malloc(sizeof(char) * (length + 1)); // Read in version fread(simulator->version, sizeof(char), length, input); // Set last character of version to null terminator simulator->version[length] = '\0'; } // Otherwise check if line is quantum else if(strcmp(line, "Quantum (cycles)") == 0) // Read in quantum fscanf(input, "%u", &simulator->quantum); // Otherwise check if line is processor scheduling else if(strcmp(line, "Processor Scheduling") == 0) { // Allocate memory for processor scheduling including a space for a null terminator simulator->processorScheduling = (char*)malloc(sizeof(char) * (length + 1)); // Read in processor scheduling fread(simulator->processorScheduling, sizeof(char), length, input); // Set last character of processor scheduling to null terminator simulator->processorScheduling[length] = '\0'; // Set quantum to big number if FIFO if(strcmp( simulator->processorScheduling, "FIFO") == 0) simulator->quantum = 100000; } // Otherwise check if line is process file path else if(strcmp(line, "File Path") == 0) { // Allocate memory for process file path including a space for a null terminator simulator->processFilePath = (char*)malloc(sizeof(char) * (length + 1)); // Read in process file path fread(simulator->processFilePath, sizeof(char), length, input); // Set last character of process file path to null terminator simulator->processFilePath[length] = '\0'; } // Otherwise check if line is processor cycle time else if(strcmp(line, "Processor cycle time (msec)") == 0) // Read in processor cycle time fscanf(input, "%u", &simulator->processorCycleTime); // Otherwise check if line is monitor display time else if(strcmp(line, "Monitor display time (msec)") == 0) // Read in monitor display time fscanf(input, "%u", &simulator->monitorDisplayTime); // Otherwise check if line is hard drive cycle time else if(strcmp(line, "Hard drive cycle time (msec)") == 0) // Read in hard drive cycle time fscanf(input, "%u", &simulator->hardDriveCycleTime); // Otherwise check if line is printer cycle time else if(strcmp(line, "Printer cycle time (msec)") == 0) // Read in printer cycle time fscanf(input, "%u", &simulator->printerCycleTime); // Otherwise check if line is keyboard cycle time else if(strcmp(line, "Keyboard cycle time (msec)") == 0) // Read in keyboard cycle time fscanf(input, "%u", &simulator->keyboardCycleTime); // Otherwise check if line is memory type else if(strcmp(line, "Memory type") == 0) { // Allocate memory for memory type including a space for a null terminator simulator->memoryType = (char*)malloc(sizeof(char) * (length + 1)); // Read in memory type fread(simulator->memoryType, sizeof(char), length, input); // Set last character of memory type to null terminator simulator->memoryType[length] = '\0'; } // Otherwise check if line is log else if(strcmp(line, "Log") == 0) { // Allocate memory for log type including a space for a null terminator simulator->logType = (char*)malloc(sizeof(char) * (length + 1)); // Read in log type fread(simulator->logType, sizeof(char), length, input); // Set last character of log type to null terminator simulator->logType[length] = '\0'; } // Free memory free(line); } // Ignore newline character in file fgetc(input); } // Close file fclose(input); // Return success return true; }
//! Parse source and build tokens vector //! \param source source code void Compiler::tokenize(std::wistream& source) { tokens.clear(); SourcePos pos(0, 0, 0); const unsigned tabSize = 4; // tokenize text source while (source.good()) { wchar_t c = source.get(); if (source.eof()) break; pos.column++; pos.character++; switch (c) { // simple cases of one character case ' ': break; //case '\t': pos.column += tabSize - 1; break; case '\t': break; case '\n': pos.row++; pos.column = -1; break; // -1 so next call to pos.column++ result set 0 case '\r': pos.column = -1; break; // -1 so next call to pos.column++ result set 0 case '(': tokens.push_back(Token(Token::TOKEN_PAR_OPEN, pos)); break; case ')': tokens.push_back(Token(Token::TOKEN_PAR_CLOSE, pos)); break; case '[': tokens.push_back(Token(Token::TOKEN_BRACKET_OPEN, pos)); break; case ']': tokens.push_back(Token(Token::TOKEN_BRACKET_CLOSE, pos)); break; case ':': tokens.push_back(Token(Token::TOKEN_COLON, pos)); break; case ',': tokens.push_back(Token(Token::TOKEN_COMMA, pos)); break; // special case for comment case '#': { // check if it's a comment block #* ... *# if (source.peek() == '*') { // comment block // record position of the begining SourcePos begin(pos); // move forward by 2 characters then search for the end int step = 2; while ((step > 0) || (c != '*') || (source.peek() != '#')) { if (step) step--; if (c == '\t') pos.column += tabSize; else if (c == '\n') { pos.row++; pos.column = 0; } else pos.column++; c = source.get(); pos.character++; if (source.eof()) { // EOF -> unbalanced block throw TranslatableError(begin, ERROR_UNBALANCED_COMMENT_BLOCK); } } // fetch the # getNextCharacter(source, pos); } else { // simple comment while ((c != '\n') && (c != '\r') && (!source.eof())) { if (c == '\t') pos.column += tabSize; else pos.column++; c = source.get(); pos.character++; } if (c == '\n') { pos.row++; pos.column = 0; } else if (c == '\r') pos.column = 0; } } break; // cases that require one character look-ahead case '+': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_ADD_EQUAL)) break; if (testNextCharacter(source, pos, '+', Token::TOKEN_OP_PLUS_PLUS)) break; tokens.push_back(Token(Token::TOKEN_OP_ADD, pos)); break; case '-': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_NEG_EQUAL)) break; if (testNextCharacter(source, pos, '-', Token::TOKEN_OP_MINUS_MINUS)) break; tokens.push_back(Token(Token::TOKEN_OP_NEG, pos)); break; case '*': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_MULT_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_MULT, pos)); break; case '/': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_DIV_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_DIV, pos)); break; case '%': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_MOD_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_MOD, pos)); break; case '|': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_BIT_OR_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_BIT_OR, pos)); break; case '^': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_BIT_XOR_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_BIT_XOR, pos)); break; case '&': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_BIT_AND_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_BIT_AND, pos)); break; case '~': tokens.push_back(Token(Token::TOKEN_OP_BIT_NOT, pos)); break; case '!': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_NOT_EQUAL)) break; throw TranslatableError(pos, ERROR_SYNTAX); break; case '=': if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_ASSIGN, pos)); break; // cases that require two characters look-ahead case '<': if (source.peek() == '<') { // << getNextCharacter(source, pos); if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_SHIFT_LEFT_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_SHIFT_LEFT, pos)); break; } // < if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_SMALLER_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_SMALLER, pos)); break; case '>': if (source.peek() == '>') { // >> getNextCharacter(source, pos); if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_SHIFT_RIGHT_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_SHIFT_RIGHT, pos)); break; } // > if (testNextCharacter(source, pos, '=', Token::TOKEN_OP_BIGGER_EQUAL)) break; tokens.push_back(Token(Token::TOKEN_OP_BIGGER, pos)); break; // cases that require to look for a while default: { // check first character if (!std::iswalnum(c) && (c != '_')) throw TranslatableError(pos, ERROR_INVALID_IDENTIFIER).arg((unsigned)c, 0, 16); // get a string std::wstring s; s += c; wchar_t nextC = source.peek(); int posIncrement = 0; while ((source.good()) && (std::iswalnum(nextC) || (nextC == '_') || (nextC == '.'))) { s += nextC; source.get(); posIncrement++; nextC = source.peek(); } // we now have a string, let's check what it is if (std::iswdigit(s[0])) { // check if hex or binary if ((s.length() > 1) && (s[0] == '0') && (!std::iswdigit(s[1]))) { // check if we have a valid number if (s[1] == 'x') { for (unsigned i = 2; i < s.size(); i++) if (!std::iswxdigit(s[i])) throw TranslatableError(pos, ERROR_INVALID_HEXA_NUMBER); } else if (s[1] == 'b') { for (unsigned i = 2; i < s.size(); i++) if ((s[i] != '0') && (s[i] != '1')) throw TranslatableError(pos, ERROR_INVALID_BINARY_NUMBER); } else throw TranslatableError(pos, ERROR_NUMBER_INVALID_BASE); } else { // check if we have a valid number for (unsigned i = 1; i < s.size(); i++) if (!std::iswdigit(s[i])) throw TranslatableError(pos, ERROR_IN_NUMBER); } tokens.push_back(Token(Token::TOKEN_INT_LITERAL, pos, s)); } else { // check if it is a known keyword // FIXME: clean-up that with a table if (s == L"when") tokens.push_back(Token(Token::TOKEN_STR_when, pos)); else if (s == L"emit") tokens.push_back(Token(Token::TOKEN_STR_emit, pos)); else if (s == L"_emit") tokens.push_back(Token(Token::TOKEN_STR_hidden_emit, pos)); else if (s == L"for") tokens.push_back(Token(Token::TOKEN_STR_for, pos)); else if (s == L"in") tokens.push_back(Token(Token::TOKEN_STR_in, pos)); else if (s == L"step") tokens.push_back(Token(Token::TOKEN_STR_step, pos)); else if (s == L"while") tokens.push_back(Token(Token::TOKEN_STR_while, pos)); else if (s == L"do") tokens.push_back(Token(Token::TOKEN_STR_do, pos)); else if (s == L"if") tokens.push_back(Token(Token::TOKEN_STR_if, pos)); else if (s == L"then") tokens.push_back(Token(Token::TOKEN_STR_then, pos)); else if (s == L"else") tokens.push_back(Token(Token::TOKEN_STR_else, pos)); else if (s == L"elseif") tokens.push_back(Token(Token::TOKEN_STR_elseif, pos)); else if (s == L"end") tokens.push_back(Token(Token::TOKEN_STR_end, pos)); else if (s == L"var") tokens.push_back(Token(Token::TOKEN_STR_var, pos)); else if (s == L"const") tokens.push_back(Token(Token::TOKEN_STR_const, pos)); else if (s == L"call") tokens.push_back(Token(Token::TOKEN_STR_call, pos)); else if (s == L"sub") tokens.push_back(Token(Token::TOKEN_STR_sub, pos)); else if (s == L"callsub") tokens.push_back(Token(Token::TOKEN_STR_callsub, pos)); else if (s == L"onevent") tokens.push_back(Token(Token::TOKEN_STR_onevent, pos)); else if (s == L"abs") tokens.push_back(Token(Token::TOKEN_STR_abs, pos)); else if (s == L"return") tokens.push_back(Token(Token::TOKEN_STR_return, pos)); else if (s == L"or") tokens.push_back(Token(Token::TOKEN_OP_OR, pos)); else if (s == L"and") tokens.push_back(Token(Token::TOKEN_OP_AND, pos)); else if (s == L"not") tokens.push_back(Token(Token::TOKEN_OP_NOT, pos)); else tokens.push_back(Token(Token::TOKEN_STRING_LITERAL, pos, s)); } pos.column += posIncrement; pos.character += posIncrement; } break; } // switch (c) } // while (source.good()) tokens.push_back(Token(Token::TOKEN_END_OF_STREAM, pos)); }
bool XMLInputStream::getNextToken(InputToken *result) { // remember current sequence number; this allows us to call // FilteredInputStream::getNextToken(), which changes the sequence number uint32_t oldSequenceNumber = sequenceNumber; // if there are tokens in the queue, return them first before parsing any // input; make sure we are not at query time -- returning multiple tokens // with the same sequence number would confuse the query processor if ((termQueueLength > 0) && (!atQueryTime)) { memcpy(result, &termQueue[0], sizeof(InputToken)); termQueueLength--; for (int i = 0; i < termQueueLength; i++) termQueue[i] = termQueue[i + 1]; sequenceNumber = result->sequenceNumber + 1; return true; } getNextToken_START: while (true) { // skip over whitespace characters int nextChar; do { if (bufferPos < bufferSize) { filePosition++; nextChar = buffer[bufferPos++]; } else if ((nextChar = getNextCharacter()) < 0) return false; } while (isWhiteSpace[nextChar]); // Insert special handling for XML tags: we don't want to break them // up even if they contain underscores, slashes, etc. if (nextChar == '<') { result->filePosition = filePosition - 1; int len = 1; result->token[0] = '<'; while (len < MAX_TOKEN_LENGTH) { if (bufferPos < bufferSize) { filePosition++; nextChar = buffer[bufferPos++]; } else if ((nextChar = getNextCharacter()) < 0) break; if ((nextChar <= ' ') || ((len > 1) && (nextChar == '/'))) { putBackCharacter((byte)nextChar); break; } if ((nextChar >= 'A') && (nextChar <= 'Z')) nextChar += 32; result->token[len++] = (byte)nextChar; if (nextChar == '>') break; } result->token[len] = 0; result->canBeUsedAsLandmark = false; result->sequenceNumber = sequenceNumber = oldSequenceNumber; if (currentlyInComment) { int in, out; switch (xmlCommentMode) { case COMMENTS_PLAINTEXT: for (in = 0, out = 0; result->token[in] != 0; in++) { char c = (char)result->token[in]; if ((c != '<') && (c != '>')) result->token[out++] = (byte)c; } currentTag[0] = 0; if (out == 0) goto getNextToken_START; result->token[out] = 0; goto getNextToken_RETURN; case COMMENTS_IGNORE: currentTag[0] = 0; goto getNextToken_START; } result->canBeUsedAsLandmark = false; } // end if (currentlyInComment) // check whether this token is an unclosed opening XML tag; if so, add '>' // and put the new token into the term queue currentTag[0] = 0; if (result->token[len - 1] != '>') { if ((len < MAX_TOKEN_LENGTH) && (result->token[1] != '!')) { InputToken dummy; memcpy(&dummy, result, sizeof(InputToken)); dummy.token[len] = '>'; dummy.token[len + 1] = 0; if (result->token[1] != '/') addToTermQueue(&dummy); if (len < MAX_TOKEN_LENGTH - 2) strcpy(currentTag, (char*)&result->token[1]); else currentTag[0] = 0; } } // end if (result->token[len - 1] != '>') if (strcmp((char*)result->token, "<!--") == 0) { currentlyInComment = true; currentTag[0] = 0; if (xmlCommentMode == COMMENTS_IGNORE) strcpy((char*)result->token, "<!-->"); } getNextToken_RETURN: sequenceNumber++; return true; } // end if (nextChar == '<') // if we get here, that means that the current token is not the start // of an XML tag; put "nextChar" back into the read buffer if (bufferPos > 0) { buffer[--bufferPos] = (byte)nextChar; filePosition--; } else putBackCharacter((byte)nextChar); bool success = FilteredInputStream::getNextToken(result); if (!success) { sequenceNumber = oldSequenceNumber; return false; } byte *translated1, *translated2; byte *strg = result->token; bool specialChars = false; while (*strg != 0) { byte b = *strg; if ((b >= 128) || (b == '&') || (b == ';')) { specialChars = true; break; } else if ((b >= 'A') && (b <= 'Z')) *strg = b + 32; strg++; } // if there are special characters (entity references etc.) in the current // token, try to replace them according to the rules defined by the // "entityRefs" and "entityVals" arrays if (specialChars) { translated1 = XMLInputStream::replaceEntityReferences(result->token, tempString); translated2 = FilteredInputStream::replaceNonStandardChars(translated1, tempString2, true); // copy translated2 into output buffer and split into sub-tokens on-the-fly int tokenLen = 0; for (int i = 0; translated2[i] != 0; i++) { byte b = (byte)translated2[i]; if ((!isWhiteSpace[b]) && (tokenLen < MAX_TOKEN_LENGTH)) { result->token[tokenLen++] = (char)b; } else if (tokenLen > 0) { result->token[tokenLen] = 0; result->sequenceNumber = sequenceNumber++; addToTermQueue(result); tokenLen = 0; } } if (termQueueLength > 0) { if (tokenLen > 0) { result->token[tokenLen] = 0; result->sequenceNumber = sequenceNumber++; addToTermQueue(result); } return getNextToken(result); } } // end if (specialChars) else { translated1 = NULL; translated2 = NULL; } result->canBeUsedAsLandmark = true; if (translated2 != NULL) { int len = strlen((char*)translated2); if (len > MAX_TOKEN_LENGTH) translated2[MAX_TOKEN_LENGTH] = 0; strcpy((char*)result->token, (char*)translated2); } // special treatment for "-->" (end of comment) if (currentlyInComment) { bool leaveAsIs = false; if ((strcmp((char*)result->token, ">") == 0) && (currentTag[0] == 0)) { char prev[3]; getPreviousChars(prev, 3); if ((prev[0] == '-') && (prev[1] == '-') && (prev[2] == '>')) { if (xmlCommentMode == COMMENTS_IGNORE) strcpy((char*)result->token, "</!-->"); else { InputToken dummy; memcpy(&dummy, result, sizeof(InputToken)); strcpy((char*)dummy.token, "-->"); addToTermQueue(&dummy); } currentlyInComment = false; leaveAsIs = true; } } if (!leaveAsIs) { int in, out; switch (xmlCommentMode) { case COMMENTS_PLAINTEXT: for (in = 0, out = 0; result->token[in] != 0; in++) { char c = (char)result->token[in]; if ((c != '<') && (c != '>')) result->token[out++] = (byte)c; } if (out == 0) goto getNextToken_START; result->token[out] = 0; break; case COMMENTS_IGNORE: goto getNextToken_START; break; } result->canBeUsedAsLandmark = false; } } // end if (currentlyInComment) // special treatment for closing XML tags: put alternatives into the term queue // if necessary int len = strlen((char*)result->token); assert(len > 0); if ((result->token[len - 1] == '>') && (currentTag[0] != 0)) { // if this happens, we know that we are inside an XML tag which spans over multiple // tokens; add the corresponding attribute-less tag to the term queue InputToken dummy; memcpy(&dummy, result, sizeof(InputToken)); if (currentTag[0] == '/') { sprintf((char*)dummy.token, "<%s>", currentTag); addToTermQueue(&dummy); } else { char prev[2]; getPreviousChars(prev, 2); if (prev[0] == '/') { strcpy((char*)result->token, "/>"); sprintf((char*)dummy.token, "</%s>", currentTag); addToTermQueue(&dummy); } } currentTag[0] = 0; } // end if ((result->token[len - 1] == '>') && (currentTag[0] != 0)) result->sequenceNumber = sequenceNumber = oldSequenceNumber; sequenceNumber++; return true; } // end while (true) // if the above loop is ever left, this means there are no more tokens sequenceNumber = oldSequenceNumber; return false; } // end of getNextToken(InputToken*)
static void unsignedNumber(void) { /* This logic (along with with unsignedRealNumber, and * unsignedRealExponent) handles: * * FORM: integer-number = decimal-integer | hexadecimal-integer | * binary-integer * FORM: decimal-integer = digit-sequence * FORM: real-number = * digit-sequence '.' [digit-sequence] [ exponent scale-factor ] | * '.' digit-sequence [ exponent scale-factor ] | * digit-sequence exponent scale-factor * FORM: exponent = 'e' | 'E' * * When called, inChar is equal to the leading digit of a * digit-sequence. NOTE that the real-number form beginning with * '.' does not use this logic. */ /* Assume an integer type (might be real) */ token = tINT_CONST; /* Concatenate all digits until an non-digit is found */ do { *stringSP++ = inChar; getCharacter(); } while (isdigit(inChar)); /* If it is a digit-sequence followed by 'e' (or 'E'), then * continue processing this token as a real number. */ if ((inChar == 'e') || (inChar == 'E')) { unsignedExponent(); } /* If the digit-sequence is followed by '.' but not by ".." (i.e., * this is not a subrange), then switch we are parsing a real time. * Otherwise, convert the integer string to binary. */ else if ((inChar != '.') || (getNextCharacter(false) == '.')) { /* Terminate the integer string and convert it using sscanf */ *stringSP++ = '\0'; (void)sscanf(tkn_strt, "%ld", &tknInt); /* Remove the integer string from the character identifer stack */ stringSP = tkn_strt; } /* end if */ else { /* Its a real value! Now really get the next character and * after the decimal point (this will work whether or not * getNextCharacter() was called). Then process the real number. */ getCharacter(); unsignedRealNumber(); } /* end if */ }
bool createProcessQueue(struct processControlBlock **process, const char *processFilePath) { // Initialize variables FILE *input; fpos_t cursor; int tempValue = 0; unsigned int numberOfJobs, length, numberOfProcesses = 0; processControlBlock *tempProcess = NULL , *previousProcess = NULL; // Return failure if the file doesn't exist if((input = fopen(processFilePath, "r")) == NULL) return false; // Go through file while(getNextCharacter(input) != EOF) { // Check if next character in file is S if(getNextCharacter(input) == 'S') // Ignore character in file until next group while(getNextCharacter(input) != EOF && fgetc(input) != ' '); // Otherwise check if next character in file is A else if(getNextCharacter(input) == 'A') { // Allocate memory for new process tempProcess = (processControlBlock*)malloc(sizeof(processControlBlock)); // Check if this is the first process if(previousProcess == NULL) // Set first process *process = tempProcess; // Otherwise else // Set previous process's next process previousProcess->nextPCB = tempProcess; // Increment number of processes numberOfProcesses++; // Ignore character in file until next group while(fgetc(input) != ' '); // Get current position in file fgetpos(input, &cursor); // Skip until end of A process for(numberOfJobs = 0; getNextCharacter(input) != 'A'; numberOfJobs++) { while(fgetc(input) != ';'); fgetc(input); } // Go back to last position in file fsetpos(input, &cursor); // Allocate memory for jobs in the process tempProcess->jobs = (taskInfoBlock*)malloc(sizeof(taskInfoBlock) * (numberOfJobs)); // Set details of process tempProcess->numberOfJobs = numberOfJobs; tempProcess->currentJob = 0; tempProcess->pid = tempValue++; tempProcess->ioFinished = true; tempProcess->ioInterrupted = false; tempProcess->arrivalTime = 0; tempProcess->threadBeingCreated = false; // Go through all jobs in the process for(unsigned int i = 0; i < numberOfJobs; i++) { // Get job operation tempProcess->jobs[i].operation = fgetc(input); // Ignore nest character fgetc(input); // Get current position in file fgetpos(input, &cursor); // Get length of next part in file until a semicolon is found for(length = 0; fgetc(input) != ')'; length++); // Go back to last position in file fsetpos(input, &cursor); // Allocate memory for current jobs's name including space for a null terminator tempProcess->jobs[i].name = (char*)malloc(sizeof(char) * (length + 1)); // Read in current job fread(tempProcess->jobs[i].name, sizeof(char), length, input); // Set last character of current job to null terminator tempProcess->jobs[i].name[length] = '\0'; // Ignore next character fgetc(input); // Read in job cycles fscanf(input, "%u", &tempProcess->jobs[i].totalCycles); tempProcess->jobs[i].cyclesRemaining = tempProcess->jobs[i].totalCycles; // Ignore semicolon and space characters in file fgetc(input); fgetc(input); } // Ignore character in file until next group while(fgetc(input) != ' '); // Set process's time remaining for(unsigned int i = 0; i < numberOfJobs; i++) tempProcess->timeRemaining += tempProcess->jobs[i].totalCycles; // Set previous process to current process previousProcess = tempProcess; } } // Make process queue circular previousProcess->nextPCB = *process; // Set previous PCB for(unsigned int i = 0; i < numberOfProcesses; i++) { tempProcess = previousProcess; previousProcess = previousProcess->nextPCB; previousProcess->previousPCB = tempProcess; } // Close file fclose(input); // Return success return true; }
void main(void) { //***************** BEGINNING OF ETHERNET SETUP [DONT EDIT] *****************// EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[256]; //***************** END OF ETHERNET SETUP **********************************// //***************** BEGINNING OF WIRELESS SETUP [DONT EDIT] *****************// uint8_t channel = 6; //Set the Channel. 0 is default, 15 is max mrf.SetChannel(channel); //Start the timer timer.start(); //***************** END OF WIRELESS SETUP **********************************// raiseAllPins(); dropAllPins(); speedChecker.attach(&speedLogic,0.005); //sets ticker for interrupts dcOUT=0; // turns DC motor off initially int cycles = 0; int flush = 0; int startChar =0; while(1) { dcPWM.write(dutyCycle); cycles = cycles-1; if(needsInput==true) { //What MBED is receiving from client? if(flush) { int lengthBuffer = strlen(buffer); for(int i = 0; i <lengthBuffer; i++) { buffer[i]='\0'; } } printf("\nWait for packet...\n\r"); lightShow.drawChar('@'); //this should draw the character on the screen int n = server.receiveFrom(client, buffer, sizeof(buffer)); printf("\nReceived packet...\n\r"); printf("\nReceived integer n...%i\n\r",n); buffer[n]='\0'; printf("\nyour word is: %s\n\r",buffer); server.sendTo(client, buffer, n); //echo protocol needsInput=false; cycles=n; /*revive code*/ sendDelay = slowSpeed; //pins dutyCycle = slowMotor; //motor dcOUT = 1; //turn on motor off=false; slow = true; startChar=0; } if(cycles<=0) { needsInput= true; slow = false; off=true; dcOUT = 0; //turn motor off sendDelay = slowSpeed; //pins dutyCycle = slowMotor; //for whenever it turns on after you turned it off using REDUCE } if(!off) { char character = getNextCharacter(startChar, buffer); startChar=1; printf("\nchar: %c\n\r",character); int pinBinary = braille.matchCharacter(character); if(braille.isNumber(character)) { handleNumberCase(character); } led1=1; lightShow.drawChar(character); //this should draw the character on the screen led1=0; wait_ms(sendDelay); sendNumber(pinBinary); printf("Pinbinary: %i\n\r",pinBinary); //***** ACKNOWLEDGE CODE ******// int recLength = rf_receive(rxbuffer,128); while(recLength<=0) { led2=1; recLength = rf_receive(rxbuffer,128); } led2=0; //***** END ACKNOWLEDGE CODE ******// }//end if motor stopped code dropAllPins(); }//end while loop }//end all code