int read_cmd(char* in, char* param[]){ char *tempCmd; if(!cflg) tempCmd = readline(in); int paraNum; /* Handle the non-input in cmd */ if(!cflg){ free(in); in = tempCmd; } if( in == NULL || strlen(in) == 0){ param[0] = in; return 0; } if( strcpy(cmd,in) == NULL) stdErr("err in strycpy()"); int length = sperateCMD(cmd); if( length < 0){ cmd[0] = '\0'; } else{ int i=0, index=1; for(i=0; i< length; i++) if(cmd[i]!='\0'){ param[0] = &cmd[i]; break; } /* skip the space before the first char in cmd*/ int firstItem = i; for(i = firstItem+1; i< length; i++){ if(cmd[i] == '>' || cmd[i] == '<' || cmd[i] == '|' || cmd[i] == '&'){ if(cmd[i+1] == '>' && cmd[i] == '>'){ param[index++] = makeOperator('d'); cmd[i+1] = '\0';cmd[i]='\0'; } else{ param[index++] = makeOperator(cmd[i]); cmd[i] = '\0'; } } else if(cmd[i] != '\0' && cmd[i-1] == '\0') param[index++] = &cmd[firstItem]+i; } paraNum = index; } return paraNum; }
// castExpr ::= 'int' '(' expr ')' // | 'string' '(' expr ')' // | 'bool' '(' expr ')' std::unique_ptr<Expr> FlowParser::castExpr() { FNTRACE(); FlowLocation sloc(location()); FlowToken targetTypeToken = token(); nextToken(); if (!consume(FlowToken::RndOpen)) return nullptr; std::unique_ptr<Expr> e(expr()); if (!consume(FlowToken::RndClose)) return nullptr; if (!e) return nullptr; Opcode targetType = makeOperator(targetTypeToken, e.get()); if (targetType == Opcode::EXIT) { reportError("Type cast error. No cast implementation found for requested cast from %s to %s.", tos(e->getType()).c_str(), targetTypeToken.c_str()); return nullptr; } if (targetType == Opcode::NOP) { return e; } printf("Type cast from %s to %s: %s\n", tos(e->getType()).c_str(), targetTypeToken.c_str(), mnemonic(targetType)); return std::make_unique<UnaryExpr>(targetType, std::move(e), sloc.update(end())); }
std::unique_ptr<Expr> FlowParser::powExpr() { // powExpr ::= primaryExpr ('**' powExpr)* FNTRACE(); FlowLocation sloc(location()); std::unique_ptr<Expr> left = primaryExpr(); if (!left) return nullptr; while (token() == FlowToken::Pow) { nextToken(); std::unique_ptr<Expr> right = powExpr(); if (!right) return nullptr; auto opc = makeOperator(FlowToken::Pow, left.get(), right.get()); if (opc == Opcode::EXIT) { reportError("Type error in binary expression (%s versus %s).", tos(left->getType()).c_str(), tos(right->getType()).c_str()); return nullptr; } left = std::make_unique<BinaryExpr>(opc, std::move(left), std::move(right)); } return left; }
// rhsExpr ::= (BIN_OP primaryExpr)* std::unique_ptr<Expr> FlowParser::rhsExpr(std::unique_ptr<Expr> lhs, int lastPrecedence) { FNTRACE(); for (;;) { // quit if this is not a binOp *or* its binOp-precedence is lower than the // minimal-binOp-requirement of our caller int thisPrecedence = binopPrecedence(token()); if (thisPrecedence <= lastPrecedence) return lhs; FlowToken binaryOperator = token(); nextToken(); std::unique_ptr<Expr> rhs = powExpr(); if (!rhs) return nullptr; int nextPrecedence = binopPrecedence(token()); if (thisPrecedence < nextPrecedence) { rhs = rhsExpr(std::move(rhs), thisPrecedence + 0); if (!rhs) return nullptr; } Opcode opc = makeOperator(binaryOperator, lhs.get(), rhs.get()); if (opc == Opcode::EXIT) { reportError("Type error in binary expression (%s versus %s).", tos(lhs->getType()).c_str(), tos(rhs->getType()).c_str()); return nullptr; } lhs = std::make_unique<BinaryExpr>(opc, std::move(lhs), std::move(rhs)); } }
std::unique_ptr<Expr> asString(std::unique_ptr<Expr>&& expr) { FlowType baseType = expr->getType(); if (baseType == FlowType::String) return std::move(expr); Opcode opc = makeOperator(FlowToken::StringType, expr.get()); if (opc == Opcode::EXIT) return nullptr; // cast error return std::make_unique<UnaryExpr>(opc, std::move(expr), expr->location()); }
std::unique_ptr<Stmt> FlowParser::postscriptStmt(std::unique_ptr<Stmt> baseStmt) { FNTRACE(); if (token() == FlowToken::Semicolon) { nextToken(); return baseStmt; } if (baseStmt->location().end.line != lexer_->line()) return baseStmt; FlowToken op = token(); switch (op) { case FlowToken::If: case FlowToken::Unless: break; default: return baseStmt; } // STMT ['if' EXPR] ';' // STMT ['unless' EXPR] ';' FlowLocation sloc = location(); nextToken(); // 'if' | 'unless' std::unique_ptr<Expr> condExpr = expr(); if (!condExpr) return nullptr; consumeIf(FlowToken::Semicolon); if (op == FlowToken::Unless) { auto opc = makeOperator(FlowToken::Not, condExpr.get()); if (opc == Opcode::EXIT) { reportError("Type cast error. No cast implementation found for requested cast from %s to %s.", tos(condExpr->getType()).c_str(), "bool"); return nullptr; } condExpr = std::make_unique<UnaryExpr>(opc, std::move(condExpr), sloc); } return std::make_unique<CondStmt>(std::move(condExpr), std::move(baseStmt), nullptr, sloc.update(end())); }