def _parse_trend_cond(token, lexer, symbol_table, engine, is_negated): is_inc = False symbol_id = None access_method = None if token.token_value != 'inc' and token.token_value != 'desc': raise ValueError('Either of [inc, desc] expected, none found, Sir') if token.token_value == 'inc': is_inc = True token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'currency': utils.expect_access_operator(lexer) symbol_id = symbol_table.get_currency(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) utils.expect_keyword(lexer, 'rate') access_method = engine.world.get_currency_rate elif token.token_type == TokenType.keyword and token.token_value == 'stock': utils.expect_access_operator(lexer) symbol_id = symbol_table.get_stock(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) utils.expect_keyword(lexer, 'value') access_method = engine.world.get_stock_price else: raise ValueError('Either stock or currency keywords expected, found: ' + str(token.token_value) + " ,Sir.") utils.expect_keyword(lexer, 'by') percent_growth = utils.expect_number(lexer) utils.expect_keyword(lexer, 'in') days_num = utils.expect_number(lexer) trendCond = TrendCondition(engine.world) trendCond.accessor_method = access_method trendCond.growth_percent = percent_growth trendCond.number_of_days = days_num trendCond.is_inc = is_inc trendCond.is_negated = is_negated return trendCond
def parse_action(lexer, symbol_table, engine): is_buy = False is_stock = False token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword and token.token_value not in ['buy', 'sell']: raise ValueError('Buy or sell expected to start action, found something else, Sir') if token.token_value == 'buy': is_buy = True token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword and token.token_value not in ['stock', 'currency']: raise ValueError('An action requires you to choose either stock or currency, nothing else, Sir') if token.token_value == 'stock': is_stock = True utils.expect_access_operator(lexer) symbol_name = utils.expect_given_name(lexer) symbol_id = -1 if is_stock: symbol_id = symbol_table.get_stock(symbol_name) else: symbol_id = symbol_table.get_currency(symbol_name) if not is_buy: token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'amount': token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.number: if token.token_value < 0: raise ValueError("You can only sell a positive amount, Sir") return build_action(engine, is_buy, is_stock, symbol_id, amount=token.token_value) elif token.token_type == TokenType.keyword and token.token_value == 'ALL': return build_action(engine, is_buy, is_stock, symbol_id, amount='ALL') else: raise ValueError("Expecting a number or ALL keyword, found neither of them, Sir") elif token.token_type == TokenType.keyword and token.token_value == 'part': amount = utils.expect_number(lexer) if not 1 <= amount <= 100: raise ValueError("Part must be 1 to 100 no more no less, Sir") return build_action(engine, is_buy, is_stock, symbol_id, part=amount) elif token.token_type == TokenType.keyword and token.token_value == 'for': curr_amount = utils.expect_number(lexer) if curr_amount < 0: raise ValueError("You can only sell for a positive price, Sir") return build_action(engine, is_buy, is_stock, symbol_id, curr_amount=curr_amount) else: raise ValueError("Expecting either amount, part, for, none of those were found, Sir") else: token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword or token.token_value != 'amount': raise ValueError("Expecting keyword amount, Sir") token = utils.get_token_skipping_whitespace(lexer) buy_amount = 0 if token.token_type == TokenType.keyword or token.token_value == 'MAX': buy_amount = 'MAX' elif token.token_type == TokenType.number: buy_amount = token.token_value if buy_amount < 0: raise ValueError("You can only buy a positive amount, Sir") else: raise ValueError("Unexpected Token, Sir") utils.expect_keyword(lexer, 'for') currency_used_id = None token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'OWN': currency_used_id = 'OWN' elif token.token_type == TokenType.keyword and token.token_value == 'ANY': currency_used_id = 'ANY' elif token.token_type == TokenType.keyword and token.token_value == 'currency': utils.expect_access_operator(lexer) currency_name = utils.expect_given_name(lexer) currency_used_id = symbol_table.get_currency(currency_name) else: raise ValueError("Unexpected token, Sir") return build_action(engine, is_buy, is_stock, symbol_id, buy_amount=buy_amount, curr_used=currency_used_id)