def load(self): self.waysOfSaying.update({ obj.Number(bound=True, value=1): "1", obj.Number(bound=True, value=2): "2", obj.Number(bound=True, value=3): "3", acts.YN(value=True): "Yes", acts.YN(value=False): "No" }) for k, v in self.waysOfSaying.items(): self.waysOfSaying[v] = k
def __request__(self): if not self['person']['bound'] and not self['num']['bound']: raise exc.InvalidRequestException() if not self['num']['bound']: # I suppose this should be contextual. # I'll worry about that later. do = "does" name = str(self['person']) have = "have" return { "formulation": "How many brothers {do} {name} {have}?".format(**locals()), "expectation": obj.Number, "meaning": NumBrothers(person=self['person'], num=obj.Number(bound=False)) } else: return { "formulation": "I have no idea how to ask this... Hmmm...", "expectation": None, "meaning": acts.DontKnowHowToAsk() }
def start_to_end(args, context): start = args["start"] end = args["end"] if not start.is_integer(): return err("$start in `$start to $end` must be an integer") if not end.is_integer(): return err("$end in `$start to $end` must be an integer") s_val = int(start.value) e_val = int(end.value) if e_val < s_val: result = obj.Array([obj.Number(e + 1) for e in range(e_val, s_val)]) result.elements.reverse() return result elif e_val > s_val: return obj.Array([obj.Number(e) for e in range(s_val, e_val)]) else: return start
def eval_number_infix(op, left, right): l = left.value r = right.value if op == "+": return obj.Number(l + r) if op == "-": return obj.Number(l - r) if op == "*": return obj.Number(l * r) if op == "/": return obj.Number(l / r) if op == "&": return obj.Number(int(l) & int(r)) if op == "|": return obj.Number(int(l) | int(r)) if op == "**": return obj.Number(l**r) if op == "//": return obj.Number(l // r) if op == "%": return obj.Number(l % r) if op == "<": return bool_obj(l < r) if op == ">": return bool_obj(l > r) if op == "<=": return bool_obj(l <= r) if op == ">=": return bool_obj(l >= r) return err("unknown operator: %s %s %s" % (left.type, op, right.type))
def eval_minus_prefix(right): if right.type != obj.NUMBER: return err("unknown operator: -%s" % right.type) return obj.Number(-right.value)
def evaluate(node, ctx): t = type(node) # Constructs if t == ast.Program: return eval_program(node, ctx) if t == ast.BlockStatement: return eval_block_stmt(node, ctx) if t == ast.ExpressionStatement: return evaluate(node.expr, ctx) if t == ast.IfExpression: return eval_if(node, ctx) if t == ast.WhileLoop: return eval_while_loop(node, ctx) if t == ast.ForLoop: return eval_for_loop(node, ctx) # Literals if t == ast.Null: return NULL if t == ast.Number: return obj.Number(node.value) if t == ast.String: return obj.String(node.value) if t == ast.Char: return obj.Char(node.value) if t == ast.Boolean: return bool_obj(node.value) if t == ast.Identifier: return eval_id(node, ctx) if t == ast.BlockLiteral: return eval_block(node, ctx) if t == ast.NextStatement: return NEXT if t == ast.BreakStatement: return BREAK # Functions if t == ast.FunctionDefinition: return eval_function_def(node, ctx) if t == ast.FunctionCall: return eval_function_call(node, ctx) if t == ast.Array: elements = eval_exprs(node.elements, ctx) if len(elements) == 1 and is_err(elements[0]): return elements[0] return obj.Array(elements) if t == ast.Object: keys = eval_exprs(node.pairs.keys(), ctx) if len(keys) == 1 and is_err(keys[0]): return keys[0] values = eval_exprs(node.pairs.values(), ctx) if len(values) == 1 and is_err(values[0]): return values[0] return obj.Object(list(zip(keys, values))) if t == ast.Tuple: elements = eval_exprs(node.value, ctx) if len(elements) == 1 and is_err(elements[0]): return elements[0] return obj.Tuple(elements) # More complex nodes if t == ast.ReturnStatement: if node.value == None: return obj.ReturnValue(NULL) val = evaluate(node.value, ctx) return val if is_err(val) else obj.ReturnValue(val) if t == ast.PrefixExpression: right = evaluate(node.right, ctx) return right if is_err(right) else eval_prefix(node.operator, right) if t == ast.InfixExpression: left = evaluate(node.left, ctx) if is_err(left): return left right = evaluate(node.right, ctx) if is_err(right): return right return eval_infix(node.operator, left, right, ctx) if t == ast.AssignExpression: right = evaluate(node.value, ctx) return right if is_err(right) else eval_assign(node.name, right, ctx) if t == ast.DeclareExpression: right = evaluate(node.value, ctx) return right if is_err(right) else eval_declare(node.name, right, ctx) return err("evaluation for %s not yet implemented" % t)
def nth_root_of_num(args, context): return obj.Number(args["num"].value**(1 / args["root"].value))
def square_root_of_num(args, context): return obj.Number(math.sqrt(args["num"].value))
def indices_of_arr(args, context): collection = args["collection"].get_elements() result = [obj.Number(i) for i in range(len(collection))] return obj.Array(result)
def askAboutBrother(): you = obj.Person(name="Alec", bound=True) numbrothers = obj.Number(bound=False) req = acts.Request(stmt.NumBrothers(person=you, num=numbrothers)) g.initiateAct(req)
def round_n(args, context): n = args["n"] return obj.Number(round(n.value))