// ----------------------------------------------------------------------------- // CWimToken::DoGetInterface() // Returns a valid interface and KErrNone, or interface = NULL and // KErrNotSupported if it isn't supported by this token // ----------------------------------------------------------------------------- // void CWimToken::DoGetInterface( TUid aRequiredInterface, MCTTokenInterface*& aReturnedInterface, TRequestStatus& aStatus ) { _WIMTRACE ( _L( "CWimToken::DoGetInterface()" ) ); if ( TokenRemoved() ) { Release(); TRequestStatus* status = &aStatus; User::RequestComplete( status, KErrHardwareNotAvailable ); return; } TInt error = KErrNone; if ( TokenType().Type().iUid == WIM_IMPLEMENTATION_UID ) { TRAP( error, MakeInterfaceL( aRequiredInterface, aReturnedInterface ) ); } else { error = KErrArgument; } TRequestStatus* status = &aStatus; User::RequestComplete( status, error ); // If something went wrong, this interface must be released, // because framework has just before incremented references if ( error != KErrNone ) { Release(); } }
/* * Returns true if current token is a register, 5 * is number of registers */ int IsRegister() { int i; for(i = 0;i < 5;i++) { if(TokenType(RegList[i]) == 1) return 1; } return 0; }
/* * Returns true if current token is an operator */ int IsOperator() { int i; for(i = 0;i<10;i++) { if(TokenType(OpList[i]) == 1) return 1; } return 0; }
struct TokenStruct NextToken (char *c) { *c = FindToken (*c); struct TokenStruct ret;// = malloc (sizeof (struct TokenStruct)); ret.Token = malloc (sizeof (char) * 2); ret.Token[0] = *c; ret.type = TokenType (*c); ret.priority = OpPriority (*c); ret.index = line_index; int length = 1; if (!ret.type) { if (*c == '+' || *c == '-' || *c == '*' || *c == '/' || *c == '%' || *c == '^' || *c == '=' || *c == ')' || *c == '(' || *c == ',') { // Grab next character if we have a valid token *c = FindToken (0); } ret.Token[1] = '\0'; #ifdef DEBUG if (ret.Token[0] != '\n') { printf (" Op/Invalid Token %i: '%s';\n", ret.index, ret.Token); } else { printf (" Op/Invalid Token %i: '\\n';\n", ret.index); } sleep (1); #endif return ret; } *c = getchar (); AddSpace (*c); while (TokenType (*c) == ret.type) { ret.Token[length++] = *c; if (ECHO) { putchar (*c); } ret.Token = realloc (ret.Token, sizeof (char) * (length+1)); *c = getchar (); AddSpace (*c); } if (ECHO) { putchar (*c); } //line_index += length; ret.Token[length] = '\0'; *c = FindToken (*c); #ifdef DEBUG printf (" Var/Num Token %i: %s;\n", ret.index, ret.Token); sleep (1); #endif return ret; }
/* * Register operations happen here * */ void Operation(Operators op) { GetToken(); int i; if(IsRegister()) { for(i = 0;i < 5;i++) { if(TokenType(RegList[i])) break; } } else { printf("Expected register, not %s or %d\n", Look->value.string, Look->value.number); exit(1); } GetToken(); if(IsNumber()) { switch(op) { case ADD: Register[i] += Look->value.number; break; case SUB: Register[i] -= Look->value.number; break; case SHL: Register[i] = Register[i] << Look->value.number; break; case SHR: Register[i] = Register[i] >> Look->value.number; break; case SET: Register[i] = Look->value.number; break; case AND: Register[i] = Register[i] & Look->value.number; break; case OR: Register[i] = Register[i] | Look->value.number; break; default: printf(" opcode: %d",op); } } else if(IsRegister())
Token get_token(istream* input, bool ignoreWhitespace) { char ch = 0; if(ignoreWhitespace) { for(;;) { if(!input->get(ch)) return Token(TOK_END); if(!(isspace(ch) && ch != '\n')) break; } } else if (!input->get(ch)) return Token(TOK_END); if(isspace(ch) && ch != '\n') { while(input->get(ch) && isspace(ch) && ch != '\n') {}; input->putback(ch); return Token(TOK_WHITESPACE); } switch(ch) { case '\n': return Token(TOK_NEWLINE, ch); case TOK_POUND: case TOK_LT: case TOK_GT: case TOK_QUOT: return Token(TokenType(ch), ch); case '*': case '/': char c; input->get(c); if(ch == '*' && c == '/') return Token(TOK_COMMENT_END, "*/"); if(ch == '/' && c == '/') return Token(TOK_COMMENT, "//"); if(ch == '/' && c == '*') return Token(TOK_COMMENT_START, "/*"); input->putback(c); // Intended fall-through. default: if (isalnum(ch)) { string s; s.push_back(ch); while(input->get(ch) && isalnum(ch)) s.push_back(ch); input->putback(ch); return Token(TOK_WORD, s); } return Token(TOK_OTHER, ch); } }
double EvalSubStatement (char *status, char *look, struct Variable *Vars) { #ifdef DEBUG printf ("EvalSubStatement in; status: %i, look: %c;\n", *status, *look); #endif char oldstatus = *status; *status = -1; // pass on substatement status struct TokenStruct tmp; // tmp.Token = malloc (sizeof (char) * 2); tmp.Token = "+"; tmp.type = TokenType ('+'); tmp.priority = OpPriority ('+'); double ret = Evaluate (0, tmp, status, look, Vars); if (!*status) { *status = oldstatus; } // if no error occured during substatement evaluation, reset status return ret; }
TokenType TokenScanner::getTokenType(const std::string& token) const { if (token.empty()) { return TokenType(EOF); } char ch = token[0]; if (isspace(ch)) { return SEPARATOR; } else if (ch == '"' || (ch == '\'' && token.length() > 1)) { return STRING; } else if (isdigit(ch)) { return NUMBER; } else if (isWordCharacter(ch)) { return WORD; } else { return OPERATOR; } }
Parser::Parser(Lexer* lexer, const char* outfile, const char* variabletypesfile) { bytecode = fopen(outfile, "w"); if(bytecode == NULL) { printf("[Parser Error] Unable to open bytecode file for writing!\n"); return; } FILE *vartypes = fopen(variabletypesfile, "r"); if(vartypes == NULL) { printf("[Parser Error] Unable to open variable types file!\n"); return; } variabletypes = new BinarySearchTree(); int i = 0; while(!feof(vartypes)) { if(feof(vartypes)) break; Keyword* keyword = (Keyword*) malloc(sizeof(Keyword)); fscanf(vartypes, "%s", keyword->text); keyword->type = TokenType(i); variabletypes->insert(variabletypes->root, keyword); i++; } fclose(vartypes); buffer = new TokenBuffer(lexer, 3); variables = new VariableStoreTree(); in = new Queue<int>(INT_MAX); out = new Queue<int>(INT_MAX); uniform = new Queue<int>(INT_MAX); blockList = new Queue<Block*>(INT_MAX); functions = new FunctionStoreTree(); currentFunction = 0; }
double EvalFunction (struct TokenStruct val, char *status, char *look, struct Variable *Vars) { #ifdef DEBUG printf ("EvalFunction in; Val: %s, status: %i, look: %c;\n", val.Token, *status, *look); #endif // parse args double *Args = 0; int nargs = 0; char oldstatus = *status; struct TokenStruct tmp; //tmp.Token = malloc (sizeof (char) * 2); tmp.Token = "+"; tmp.type = TokenType ('+'); tmp.priority = OpPriority ('+'); // tmp.index = 0; *status = -2; // set status to indicate argument evaluation if (*look != ')') { // make sure there are more than zero arguments while (*status == -2) { Args = realloc (Args, sizeof (double) * ++nargs); // make room for the next argument Args[nargs-1] = Evaluate (0.0, tmp, status, look, Vars); // and add it to the argument vector } if (*status) { // argument(s) invalid; return. Error in Evaluate, let it handle the error free (Args); return; } } else { NextToken (look); } *status = oldstatus; // resolve function double ret = GetFunction (val, Args, nargs, status); if (Args) { free (Args); } // if (*status > 0) { HandleError (val, *status, look); } // Let Evaluate or Parse handle errors return ret; }
Token* lex(const char* string,Uptr stringLength,LineInfo*& outLineInfo) { static StaticData staticData; Timing::Timer timer; if(stringLength > UINT32_MAX) { Errors::fatalf("cannot lex strings with more than %u characters",UINT32_MAX); } // Allocate enough memory up front for a token and newline for each character in the input string. Token* tokens = (Token*)malloc(sizeof(Token) * (stringLength + 1)); U32* lineStarts = (U32*)malloc(sizeof(U32) * (stringLength + 2)); Token* nextToken = tokens; U32* nextLineStart = lineStarts; *nextLineStart++ = 0; const char* nextChar = string; while(true) { // Skip whitespace and comments (keeping track of newlines). while(true) { switch(*nextChar) { // Single line comments. case ';': if(nextChar[1] != ';') { goto doneSkippingWhitespace; } else { nextChar += 2; while(*nextChar) { if(*nextChar == '\n') { // Emit a line start for the newline. *nextLineStart++ = U32(nextChar - string + 1); ++nextChar; break; } ++nextChar; }; } break; // Delimited (possibly multi-line) comments. case '(': if(nextChar[1] != ';') { goto doneSkippingWhitespace; } else { const char* firstCommentChar = nextChar; nextChar += 2; U32 commentDepth = 1; while(commentDepth) { if(nextChar[0] == ';' && nextChar[1] == ')') { --commentDepth; nextChar += 2; } else if(nextChar[0] == '(' && nextChar[1] == ';') { ++commentDepth; nextChar += 2; } else if(nextChar == string + stringLength) { // Emit an unterminated comment token. nextToken->type = t_unterminatedComment; nextToken->begin = U32(firstCommentChar - string); ++nextToken; goto doneSkippingWhitespace; } else { if(*nextChar == '\n') { // Emit a line start for the newline. *nextLineStart++ = U32(nextChar - string); } ++nextChar; } }; } break; // Whitespace. case '\n': *nextLineStart++ = U32(nextChar - string + 1); ++nextChar; break; case ' ': case '\t': case '\r': case '\f': ++nextChar; break; default: goto doneSkippingWhitespace; }; } doneSkippingWhitespace: // Once we reach a non-whitespace, non-comment character, feed characters into the NFA until it reaches a terminal state. nextToken->begin = U32(nextChar - string); NFA::StateIndex terminalState = staticData.nfaMachine.feed(nextChar); if(terminalState != NFA::unmatchedCharacterTerminal) { nextToken->type = TokenType(NFA::maximumTerminalStateIndex - (NFA::StateIndex)terminalState); ++nextToken; } else { if(nextToken->begin < stringLength) { // Emit an unrecognized token. nextToken->type = t_unrecognized; ++nextToken; // Advance until a recovery point. while(!isRecoveryPointChar(*nextChar)) { ++nextChar; } } else { break; } } } // Emit an end token to mark the end of the token stream. nextToken->type = t_eof; ++nextToken; // Emit an extra line start for the end of the file, so you can find the end of a line with lineStarts[line + 1]. *nextLineStart++ = U32(nextChar - string) + 1; // Shrink the line start and token arrays to the final number of tokens/lines. const Uptr numLineStarts = nextLineStart - lineStarts; const Uptr numTokens = nextToken - tokens; lineStarts = (U32*)realloc(lineStarts,sizeof(U32) * numLineStarts); tokens = (Token*)realloc(tokens,sizeof(Token) * numTokens); // Create the LineInfo object that encapsulates the line start information. outLineInfo = new LineInfo {lineStarts,U32(numLineStarts)}; Timing::logRatePerSecond("lexed WAST file",timer,stringLength/1024.0/1024.0,"MB"); Log::printf(Log::Category::metrics,"lexer produced %u tokens (%.1fMB)\n",numTokens,numTokens*sizeof(Token)/1024.0/1024.0); return tokens; }
/*! \brief \fn Parser::GetToken \param ignoreSign \return const Parser::TokenType */ const Parser::TokenType Parser::GetToken (const bool ignoreSign) { word_.erase (0, std::string::npos); // skip spaces while (*pWord_ && isspace (*pWord_)) ++pWord_; pWordStart_ = pWord_; // remember where word_ starts *now* // look out for unterminated statements and things if (*pWord_ == 0 && // we have EOF type_ == END) // after already detecting it { //throw std::runtime_error ("Unexpected end of expression."); } unsigned char cFirstCharacter = *pWord_; // first character in new word_ if (cFirstCharacter == 0) // stop at end of file { word_ = "<end of expression>"; return type_ = END; } unsigned char cNextCharacter = *(pWord_ + 1); // 2nd character in new word_ // look for number // can be: + or - followed by a decimal point // or: + or - followed by a digit // or: starting with a digit // or: decimal point followed by a digit if ((!ignoreSign && (cFirstCharacter == '+' || cFirstCharacter == '-') && (isdigit (cNextCharacter) || cNextCharacter == '.') ) || isdigit (cFirstCharacter) // allow decimal numbers without a leading 0. e.g. ".5" // Dennis Jones 01-30-2009 || (cFirstCharacter == '.' && isdigit (cNextCharacter)) ) { // skip sign for now if ((cFirstCharacter == '+' || cFirstCharacter == '-')) pWord_++; while (isdigit (*pWord_) || *pWord_ == '.') pWord_++; // allow for 1.53158e+15 if (*pWord_ == 'e' || *pWord_ == 'E') { pWord_++; // skip 'e' if ((*pWord_ == '+' || *pWord_ == '-')) pWord_++; // skip sign after e while (isdigit (*pWord_)) // now digits after e pWord_++; } word_ = std::string (pWordStart_, pWord_ - pWordStart_); std::istringstream is (word_); // parse std::string into double value is >> value_; if (is.fail () || !is.eof ()) { // throw std::runtime_error ("Bad numeric literal: " + word_); } return type_ = NUMBER; } // end of number found // special test for 2-character sequences: <= >= == != // also +=, -=, /=, *= if (cNextCharacter == '=') { switch (cFirstCharacter) { // comparisons case '=': type_ = EQ; break; case '<': type_ = LE; break; case '>': type_ = GE; break; case '!': type_ = NE; break; // assignments case '+': type_ = ASSIGN_ADD; break; case '-': type_ = ASSIGN_SUB; break; case '*': type_ = ASSIGN_MUL; break; case '/': type_ = ASSIGN_DIV; break; // none of the above default: type_ = NONE; break; } // end of switch on cFirstCharacter if (type_ != NONE) { word_ = std::string (pWordStart_, 2); pWord_ += 2; // skip both characters return type_; } // end of found one } // end of *= switch (cFirstCharacter) { case '&': if (cNextCharacter == '&') // && { word_ = std::string (pWordStart_, 2); pWord_ += 2; // skip both characters return type_ = AND; } else { word_ = std::string (pWordStart_, 1); ++pWord_; // skip it return type_ = TokenType (cFirstCharacter); } break; case '|': if (cNextCharacter == '|') // || { word_ = std::string (pWordStart_, 2); pWord_ += 2; // skip both characters return type_ = OR; } else { word_ = std::string (pWordStart_, 1); ++pWord_; // skip it return type_ = TokenType (cFirstCharacter); } break; // single-character symboles case '=': case '<': case '>': case '+': case '-': case '/': case '*': case '(': case ')': case ',': case '!': word_ = std::string (pWordStart_, 1); ++pWord_; // skip it return type_ = TokenType (cFirstCharacter); } // end of switch on cFirstCharacter if (!isalpha (cFirstCharacter)) { if (cFirstCharacter < ' ') { std::ostringstream s; s << "Unexpected character (decimal " << int (cFirstCharacter) << ")"; //throw std::runtime_error (s.str ()); } //else //throw std::runtime_error ("Unexpected character: " + std::string (1, cFirstCharacter)); } // we have a word (starting with A-Z) - pull it out while (isalnum (*pWord_) || *pWord_ == '_') ++pWord_; word_ = std::string (pWordStart_, pWord_ - pWordStart_); return type_ = NAME; } // end of Parser::GetToken
char Parse (struct Variable *Vars) { // grabs first two tokens to see if they are an assign statment // Symbol-specified functions: // = (Assign), + (Add), - (Subtract), * (Multiply), / (Divide), % (Modulus), ^ (Power) // Future symbols: ! (Factorial / Gamma function), // Unused symbols: ~, `, @, #, $, &, [, ], {, }, |, \, ;, :, ', ", ?, ,, <, >, line_index = 0; char look = 0, status = 0; printf ("> "); fflush (0); struct TokenStruct /*Oper*/and = NextToken (&look); // check for calculator commands if (!strcmp (and.Token, "DISPLAY")) { // display variable tree char *disp = 0; DisplayVars (Vars, disp, 0); HandleError (and, 0, &look); return 0; } else if (!strcmp (and.Token, "ECHO")) { // toggle input echo mode ECHO = 1 - ECHO; if (ECHO) { puts ("Input echo on"); } else { puts ("Input echo off"); } HandleError (and, 0, &look); return 0; } else if (!strcmp (and.Token, "HELP")) { // display help screen puts ("Basic Calculator V 0.9"); puts ("--------------------------------------------------------------------------------"); puts ("This program is meant to be used as a general purpose calculator, akin to bc. Users can input arithmitic expressions of arbitrary length, assign and retrieve variables, and calculate common math functions. If the expression input isn't valid, the program displays an error message, indicates where in the line the problem occured, clears the input and resumes operation. In addition, users can work in interactive mode or redirect input scripts to execute a list of operations."); puts ("--------------------------------------------------------------------------------"); puts ("COMMANDS: (case sensitive)"); puts (" DISPLAY : Display defined variables and their associated values."); puts (" ECHO : Echo user input on next line. Useful when using an input script."); puts (" HELP : Display this help screen."); puts (" QUIT : Quit this calculator program."); puts ("--------------------------------------------------------------------------------"); puts ("MATH FUNCTIONS: (not case sensitive)\n"); puts ("ABS / ABSOLUTE :\n return the absolute value of the argument. Accepts one argument."); puts ("ASIN / ARCSINE :\n return the arc sine (inverse sine function) of the argument. Accepts one argument."); puts ("ACOS / ARCCOSINE :\n return the arc cosine (inverse cosine function) of the argument. Accepts one argument."); puts ("ATAN / ARCTANGENT :\n return the arc tangent (inverse tangent function) of the argument. Accepts one argument."); puts ("COS / COSINE :\n return the cosine of the argument. Accepts one argument."); puts ("DIST / DISTANCE / HYPOTENUSE :\n return the pythangorian distance (hypotenuse) between two values. Accepts two arguments."); puts ("E :\n return the value of e. Accepts no arguments."); puts ("EXP :\n return the exponent of the argument (e ^ arg). Accepts one argument."); puts ("LN / NATURAL_LOG :\n return the natural log of the argument (arg = e ^ x). Accepts one argument."); puts ("LOG / LOGARITHM :\n return the natural log of an argument OR the log of one argument in terms of another (log A / log b). Accepts one or two arguments."); puts ("MOD / MODULUS :\n return the remainder of one argument divided by another. Accepts two arguments."); puts ("PI :\n return the value of pi. Accepts no arguments."); puts ("POW / POWER :\n return the value of one argument raised to the power of the other. Accepts two arguments."); puts ("PROD / PRODUCT :\n return the product of the arguments. Accepts one or more arguments."); puts ("SIN / SINE :\n return the sine of the argument. Accepts one argument."); puts ("SQRT / SQUARE_ROOT :\n return the square root of the argument. Accepts one argument."); puts ("SUM / SUMMATION :\n return the sum of the arguments. Accepts one or more arguments."); puts ("TAN / TANGENT :\n return the tangent of the argument. Accepts one argument."); HandleError (and, 0, &look); return 0; } else if (!strcmp (and.Token, "QUIT")) { // quit calculator HandleError (and, 0, &look); return 1; } struct TokenStruct /*Oper*/ator; struct TokenStruct tmp; tmp.Token = malloc (sizeof (char) * 2); tmp.Token = "+"; tmp.type = TokenType ('+'); tmp.priority = OpPriority ('+'); if (!and.priority) { return 0; } // no operand; blank line double result = 0.0; double sign = 1.0; if (and.Token[0] == '-') { // negative value sign = -1.0; and = NextToken (&look); } if (and.Token[0] == '(') { result = sign * EvalSubStatement (&status, &look, Vars); } // substatement if (status) { return 0; } ator = NextToken (&look); // must be determined *after* substatement, and *before* function, assignment, or variable // function, assignment, or variable; need operator to decide if (ator.Token[0] == '(') { // function result = sign * EvalFunction (and, &status, &look, Vars); if (status) { if (status == 5 || status == 9) { HandleError (and, status, &look); } return 0; } ator = NextToken (&look); result = Evaluate (result, ator, &status, &look, Vars); } else if (and.type == 1 && ator.Token[0] == '=') { result = Evaluate (0.0, tmp, &status, &look, Vars); } // assignment; don't need to check status here since it will be checked as soon as this statement is finished else { // variable, number, substatement, or invalid if (and.Token[0] != '(') { result = sign * Resolve (and, &status, Vars); if (status) { HandleError (and, status, &look); return 0; } } result = Evaluate (result, ator, &status, &look, Vars); } if (status) { return 0; } else if (ator.Token[0] == '=') { AssignValue (and.Token, result, Vars); printf ("%s ", and.Token); } printf ("= %f;\n", result); return 0; }
// ----------------------------------------------------------------------------- // TCTTokenHandle CWimToken::Handle() // Returns the token's handle. TCTTokenHandle defines a handle to a subclass // of the MCTToken class // WimSecModule()->TokenNumber retrieves the token actually exists. // Values 0..7 are for hardware tokens. Value 255 is for SoftId-token. // ----------------------------------------------------------------------------- // TCTTokenHandle CWimToken::Handle() { _WIMTRACE ( _L( "CWimToken::Handle()" ) ); const TInt tokenNumber = WimSecModule()->TokenNumber(); return TCTTokenHandle( TokenType().Type(), tokenNumber ); }