void BytecodeVisitor::visitUnaryOpNode(UnaryOpNode *node) { LOG_Visitor("visitUnaryOpNode"); node->operand()->visit(this); switch (node->kind()) { case tSUB: { if (!isNumericType(topOfStackType)) { throw TranslationError(string("Incorrect type for SUB inside unary-node: ") + typeToName(topOfStackType), node->position()); } bc()->addInsn(topOfStackType == VT_DOUBLE ? BC_DNEG : BC_INEG); break; } case tNOT: { if (topOfStackType != VT_INT && topOfStackType != VT_STRING) { throw TranslationError(string("Incorrect type for NOT inside unary-node: ") + typeToName(topOfStackType), node->position()); } cast(VT_INT, node, "cast in unary op node"); bc()->addInsn(BC_ILOAD0); bc()->addInsn(BC_ICMP); break; } default: throw TranslationError(string("Unknown unary operation token: ") + tokenStr(node->kind()), node->position()); } }
bool ParseFoodItem(std::ifstream & infile, FoodItem & item) { if (true == infile.eof()) { return false; } const int MAX_TOKEN = 256; std::string line; std::string tokenBuf(MAX_TOKEN, '\0'); const char * delim = ",\n"; do { float calories = 0; // read line std::getline(infile, line); // grab first token char * token = nullptr; char * nextToken = nullptr; token = strtok_s(&line[0], delim, &nextToken); std::string tokenStr(token); //trim_str(tokenStr); String nameToken(ConvertStringToWString(token)); // read name, 1st token // next token plz token = strtok_s(nullptr, delim, &nextToken); if (token == nullptr) { return false; } // read calories, 2nd token calories = (float)atof(token); if (calories <= 0) { return false; } // read description std::string desc; std::getline(infile, desc); String measurements(ConvertStringToWString(desc)); item = FoodItem(nameToken, measurements, calories); return true; } while (false == infile.eof()); return false; }
void BytecodeVisitor::visitForNode(ForNode *node) { LOG_Visitor("visitForNode"); VariableInContextDescriptor variableDescriptor = context->getVariableDescriptor(node->var()->name()); BinaryOpNode *innerExpression = (BinaryOpNode *) node->inExpr(); if (innerExpression->kind() != tRANGE) { throw TranslationError(string("Incorrect binary operation in for-expression. Exptected: RANGE, got: ") + tokenStr(innerExpression->kind()), node->position()); } if (node->var()->type() != VT_INT) { throw TranslationError(string("Incorrect type of for-variable. Exptected: INT, got: ") + typeToName(node->var()->type()), node->position()); } innerExpression->left()->visit(this); bc()->addInsn(BC_ILOADM1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); Label begin(bc()); Label end(bc()); { bc()->bind(begin); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_ILOAD1); bc()->addInsn(BC_IADD); storeVariable(variableDescriptor, innerExpression); //condition innerExpression->right()->visit(this); loadVariable(variableDescriptor, innerExpression); bc()->addInsn(BC_SWAP); // goto end if greater bc()->addBranch(BC_IFICMPG, end); node->body()->visit(this); bc()->addBranch(BC_JA, begin); } bc()->bind(end); }
AstNode* Parser::parseUnary() { if (isUnaryOp(currentToken())) { TokenKind op = currentToken(); consumeToken(); return new UnaryOpNode(_currentTokenIndex, op, parseUnary()); } else if (currentToken() == tIDENT && lookaheadToken(1) == tLPAREN) { AstNode* expr = parseCall(); return expr; } else if (currentToken() == tIDENT) { AstVar* var = _currentScope->lookupVariable(currentTokenValue()); if (var == 0) { error("undeclared variable: %s", currentTokenValue().c_str()); } LoadNode* result = new LoadNode(_currentTokenIndex, var); consumeToken(); return result; } else if (currentToken() == tDOUBLE) { DoubleLiteralNode* result = new DoubleLiteralNode(_currentTokenIndex, parseDouble(currentTokenValue())); consumeToken(); return result; } else if (currentToken() == tINT) { IntLiteralNode* result = new IntLiteralNode(_currentTokenIndex, parseInt(currentTokenValue())); consumeToken(); return result; } else if (currentToken() == tSTRING) { StringLiteralNode* result = new StringLiteralNode(_currentTokenIndex, currentTokenValue()); consumeToken(); return result; } else if (currentToken() == tLPAREN) { consumeToken(); AstNode* expr = parseExpression(); ensureToken(tRPAREN); return expr; } else { error("Unexpected token: %s", tokenStr(currentToken())); return 0; } }
nsresult GetSlotWithMechanism(uint32_t aMechanism, nsIInterfaceRequestor* m_ctx, PK11SlotInfo** aSlot, nsNSSShutDownPreventionLock& /*proofOfLock*/) { PK11SlotList * slotList = nullptr; char16_t** tokenNameList = nullptr; nsITokenDialogs * dialogs; char16_t *unicodeTokenChosen; PK11SlotListElement *slotElement, *tmpSlot; uint32_t numSlots = 0, i = 0; bool canceled; nsresult rv = NS_OK; *aSlot = nullptr; // Get the slot slotList = PK11_GetAllTokens(MapGenMechToAlgoMech(aMechanism), true, true, m_ctx); if (!slotList || !slotList->head) { rv = NS_ERROR_FAILURE; goto loser; } if (!slotList->head->next) { /* only one slot available, just return it */ *aSlot = slotList->head->slot; } else { // Gerenate a list of slots and ask the user to choose // tmpSlot = slotList->head; while (tmpSlot) { numSlots++; tmpSlot = tmpSlot->next; } // Allocate the slot name buffer // tokenNameList = static_cast<char16_t**>(moz_xmalloc(sizeof(char16_t *) * numSlots)); if (!tokenNameList) { rv = NS_ERROR_OUT_OF_MEMORY; goto loser; } i = 0; slotElement = PK11_GetFirstSafe(slotList); while (slotElement) { tokenNameList[i] = UTF8ToNewUnicode(nsDependentCString(PK11_GetTokenName(slotElement->slot))); slotElement = PK11_GetNextSafe(slotList, slotElement, false); if (tokenNameList[i]) i++; else { // OOM. adjust numSlots so we don't free unallocated memory. numSlots = i; PK11_FreeSlotListElement(slotList, slotElement); rv = NS_ERROR_OUT_OF_MEMORY; goto loser; } } /* Throw up the token list dialog and get back the token */ rv = getNSSDialogs((void**)&dialogs, NS_GET_IID(nsITokenDialogs), NS_TOKENDIALOGS_CONTRACTID); if (NS_FAILED(rv)) goto loser; if (!tokenNameList || !*tokenNameList) { rv = NS_ERROR_OUT_OF_MEMORY; } else { rv = dialogs->ChooseToken(m_ctx, (const char16_t**)tokenNameList, numSlots, &unicodeTokenChosen, &canceled); } NS_RELEASE(dialogs); if (NS_FAILED(rv)) goto loser; if (canceled) { rv = NS_ERROR_NOT_AVAILABLE; goto loser; } // Get the slot // slotElement = PK11_GetFirstSafe(slotList); nsAutoString tokenStr(unicodeTokenChosen); while (slotElement) { if (tokenStr.Equals(NS_ConvertUTF8toUTF16(PK11_GetTokenName(slotElement->slot)))) { *aSlot = slotElement->slot; PK11_FreeSlotListElement(slotList, slotElement); break; } slotElement = PK11_GetNextSafe(slotList, slotElement, false); } if(!(*aSlot)) { rv = NS_ERROR_FAILURE; goto loser; } } // Get a reference to the slot // PK11_ReferenceSlot(*aSlot); loser: if (slotList) { PK11_FreeSlotList(slotList); } if (tokenNameList) { NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(numSlots, tokenNameList); } return rv; }
void BytecodeVisitor::visitBinaryOpNode(BinaryOpNode *node) { LOG_Visitor("visitBinaryOpNode"); TokenKind tokenKind = node->kind(); if (tokenKind == tAND || tokenKind == tOR) { node->left()->visit(this); bc()->addInsn(BC_ILOAD0); Label exitWithResult(bc()); bc()->addBranch(node->kind() == tAND ? BC_IFICMPE : BC_IFICMPNE, exitWithResult); node->right()->visit(this); Label exit(bc()); bc()->addBranch(BC_JA, exit); bc()->bind(exitWithResult); bc()->addInsn(node->kind() == tAND ? BC_ILOAD0 : BC_ILOAD1); bc()->bind(exit); return; } node->left()->visit(this); VarType leftType = topOfStackType; node->right()->visit(this); VarType rightType = topOfStackType; switch (tokenKind) { case tAOR: case tAAND: case tAXOR: case tMOD: { switch (tokenKind) { case tAAND: bc()->addInsn(BC_IAAND); break; case tAOR: bc()->addInsn(BC_IAOR); break; case tAXOR: bc()->addInsn(BC_IAXOR); break; case tMOD: bc()->addInsn(BC_SWAP); bc()->addInsn(BC_IMOD); break; default:; } topOfStackType = VT_INT; break; } case tEQ: case tNEQ: case tGT: case tGE: case tLT: case tLE: { VarType varType = equateTypes(leftType, rightType, node); if (!isNumericType(varType)) { throw TranslationError(string("Incorrect type for COMPARE inside binary-node: ") + typeToName(varType), node->position()); } if (varType == VT_DOUBLE) { bc()->addInsn(BC_DCMP); bc()->addInsn(BC_ILOAD0); } Label _else(bc()); Label end(bc()); switch (tokenKind) { case tEQ: bc()->addBranch(BC_IFICMPE, _else); break; case tNEQ: bc()->addBranch(BC_IFICMPNE, _else); break; case tGT: bc()->addBranch(BC_IFICMPG, _else); break; case tGE: bc()->addBranch(BC_IFICMPGE, _else); break; case tLT: bc()->addBranch(BC_IFICMPL, _else); break; case tLE: bc()->addBranch(BC_IFICMPLE, _else); break; default:; } bc()->addInsn(BC_ILOAD0); bc()->addBranch(BC_JA, end); bc()->bind(_else); bc()->addInsn(BC_ILOAD1); bc()->bind(end); topOfStackType = VT_INT; break; } case tINCRSET: case tDECRSET: break; case tADD: case tSUB: case tMUL: case tDIV: { VarType varType = equateTypes(leftType, rightType, node); if (!isNumericType(varType)) { throw TranslationError(string("Incorrect type for ARITHMETIC inside binary-node: ") + typeToName(varType), node->position()); } switch (tokenKind) { case tADD: bc()->addInsn(varType == VT_DOUBLE ? BC_DADD : BC_IADD); break; case tSUB: bc()->addInsn(BC_SWAP); bc()->addInsn(varType == VT_DOUBLE ? BC_DSUB : BC_ISUB); break; case tMUL: bc()->addInsn(varType == VT_DOUBLE ? BC_DMUL : BC_IMUL); break; case tDIV: bc()->addInsn(BC_SWAP); bc()->addInsn(varType == VT_DOUBLE ? BC_DDIV : BC_IDIV); break; default: assert(false); } topOfStackType = varType; break; } default: throw TranslationError(string("Unknown binary operation token: ") + tokenStr(tokenKind), node->position()); } }
bool ParseText::parseEx(const CastFun &castFun) { //CHILA_LIB_MISC__SHOW(40, tokenStr()); if (outside) { auto end = boost::find(_range, '\''); _token = boost::make_iterator_range(_range.begin(), end); //CHILA_LIB_MISC__SHOW(40, "here"); outside = end == _range.end(); if (_range.empty()) return false; //CHILA_LIB_MISC__SHOW(40, "here"); _range = boost::make_iterator_range(end == _range.end() ? end : end + 1, _range.end()); _foundNode = nullptr; } else { auto end = boost::find_first_of(_range, std::array<char, 3>{{'\'', '\n', ' '}}); _token = boost::make_iterator_range(_range.begin(), end); //CHILA_LIB_MISC__SHOW(40, "here"); outside = true; if (_range.empty()) BOOST_THROW_EXCEPTION(DescriptionParseError()); if (end == _range.end() || *end == ' ' || *end == '\n') { _range = boost::make_iterator_range(end, _range.end()); _token = boost::make_iterator_range(_token.begin() - 1, _token.end()); //CHILA_LIB_MISC__SHOW(40, "here"); return end != _range.end(); } //CHILA_LIB_MISC__SHOW(40, "here"); _range = boost::make_iterator_range(end + 1, _range.end()); //CHILA_LIB_MISC__SHOW(40, "here"); auto path = clMisc::Path(tokenStr()); //CHILA_LIB_MISC__SHOW(40, "here"); if (path.size() != 2) // abort(); BOOST_THROW_EXCEPTION(DescriptionParseInvalidReference() << ErrorInfo::DescToken(tokenStr())); auto typeStr = *path.begin(); auto name = *(path.begin() + 1); //CHILA_LIB_MISC__SHOW(40, "here"); _foundNode = castFun(typeStr, name); //CHILA_LIB_MISC__SHOW(40, "here"); if (!_foundNode) BOOST_THROW_EXCEPTION(DescriptionParseInvalidReference() << ErrorInfo::DescToken(tokenStr())); } return true; }
void Parser::ensureToken(TokenKind token) { if (currentToken() != token) { error("'%s' expected, seen %s", tokenStr(token), tokenStr(currentToken())); } consumeToken(); }
FunctionNode* Parser::parseFunction() { uint32_t tokenIndex = _currentTokenIndex; ensureKeyword("function"); if (currentToken() != tIDENT) { error("identifier expected"); } const string& returnTypeName = currentTokenValue(); VarType returnType = nameToType(returnTypeName); if (returnType == VT_INVALID) { error("wrong return type"); } consumeToken(); if (currentToken() != tIDENT) { error("name expected"); } const string& name = currentTokenValue(); consumeToken(); Signature signature; signature.push_back(SignatureElement(returnType, "return")); ensureToken(tLPAREN); while (currentToken() != tRPAREN) { const string& parameterTypeName = currentTokenValue(); VarType parameterType = nameToType(parameterTypeName); if (parameterType == VT_INVALID) { error("wrong parameter type"); } consumeToken(); const string& parameterName = currentTokenValue(); if (currentToken() != tIDENT) { error("identifier expected"); } consumeToken(); signature.push_back(SignatureElement(parameterType, parameterName)); if (currentToken() == tCOMMA) { consumeToken(); } } ensureToken(tRPAREN); BlockNode* body = 0; pushScope(); for (uint32_t i = 1; i < signature.size(); i++) { const string& name = signature[i].second; VarType type = signature[i].first; if (!_currentScope->declareVariable(name, type)) { error("Formal \"%s\" already declared", name.c_str()); } } if ((currentToken() == tIDENT) && currentTokenValue() == "native") { consumeToken(); if (currentToken() != tSTRING) { error("Native name expected, got %s", tokenStr(currentToken())); } pushScope(); body = new BlockNode(_currentTokenIndex, _currentScope); body->add(new NativeCallNode(tokenIndex, currentTokenValue(), signature)); consumeToken(); ensureToken(tSEMICOLON); body->add(new ReturnNode(0, 0)); popScope(); } else { body = parseBlock(true, false); if (body->nodes() == 0 || !(body->nodeAt(body->nodes() - 1)->isReturnNode())) { body->add(new ReturnNode(0, defaultReturnExpr(returnType))); } } popScope(); if (_currentScope->lookupFunction(name) != 0) { error("Function %s already defined", name.c_str()); } FunctionNode* result = new FunctionNode(tokenIndex, name, signature, body); _currentScope->declareFunction(result); // We don't add function node into AST. return 0; }
void DwDispositionType::Parse() { mIsModified = 0; mDispositionType = DwMime::kDispTypeNull; mDispositionTypeStr = ""; if(mFirstParameter) { DeleteParameterList(); } if(mString.length() == 0) return; DwRfc1521Tokenizer tokenizer(mString); int found = 0; while(!found && tokenizer.Type() != eTkNull) { if(tokenizer.Type() == eTkToken) { mDispositionTypeStr = tokenizer.Token(); found = 1; } ++tokenizer; } // Get parameters DwTokenString tokenStr(mString); while(1) { // Get ';' found = 0; while(!found && tokenizer.Type() != eTkNull) { if(tokenizer.Type() == eTkTspecial && tokenizer.Token()[0] == ';') { found = 1; } ++tokenizer; } if(tokenizer.Type() == eTkNull) { // No more parameters break; } tokenStr.SetFirst(tokenizer); // Get attribute DwString attrib; int attribFound = 0; while(!attribFound && tokenizer.Type() != eTkNull) { if(tokenizer.Type() == eTkToken) { attrib = tokenizer.Token(); attribFound = 1; } ++tokenizer; } // Get '=' found = 0; while(!found && tokenizer.Type() != eTkNull) { if(tokenizer.Type() == eTkTspecial && tokenizer.Token()[0] == '=') { found = 1; } ++tokenizer; } // Get value int valueFound = 0; while(!valueFound && tokenizer.Type() != eTkNull) { if(tokenizer.Type() == eTkToken || tokenizer.Type() == eTkQuotedString) { valueFound = 1; } ++tokenizer; } if(attribFound && valueFound) { tokenStr.ExtendTo(tokenizer); DwParameter *param = DwParameter::NewParameter(tokenStr.Tokens(), this); param->Parse(); _AddParameter(param); } } StrToEnum(); }