Beispiel #1
0
 def apply(self, x):
     base = x[0]
     exp = x[1]
     if isinstance(base, Real) \
        or isinstance(exp, Real) \
        or isinstance(exp, Rational):
         return Real(base.real_value()**exp.real_value())
     else:
         r = Integer(1)
         for i in range(exp.int_value()):
             r *= base
         return r
Beispiel #2
0
 def apply(self, x):
     return Real(math.cos(x.real_value()))
Beispiel #3
0
 def apply(self, x):
     return Real(math.log10(x.real_value()))
Beispiel #4
0
 def apply(self, x):
     mean = x.get(0).get_real_value()
     std = x.get(1).get_real_value()
     return Real(r.normalvariate(mean, std))
Beispiel #5
0
 def apply(self, x):
     return Real(r.random())
Beispiel #6
0
                            assert Rational(a_n*b_d - ((a_n*b_d) % (a_d*b_n)),
                                            a_d*b_n) == Rational(a_n, a_d) \
                                // Rational(b_n, b_d)

assert (Integer(2) + Integer(3) == Integer(5))
assert (Integer(2) - Integer(3) == Integer(-1))
assert (Integer(2) * Integer(3) == Integer(6))
assert (Integer(2) // Integer(3) == Integer(0))

assert (Integer(2) + Rational(2, 5) == Rational(12, 5))
assert (Integer(2) - Rational(12, 5) == Rational(-2, 5))
assert (Integer(2) * Rational(2, 5) == Rational(4, 5))
assert (Integer(2) / Rational(1, 5) == Rational(10, 1))
assert (Integer(2) // Rational(3, 5) == Integer(3))

assert (Integer(2) + Real(2.5) == Real(4.5))
assert (Integer(2) - Real(2.5) == Real(-0.5))
assert (Integer(2) * Real(2.5) == Real(5.0))
assert (Integer(2) / Real(0.5) == Real(4.0))
assert (Integer(2) // Real(1.5) == Integer(1))

assert (Integer(2) + Infinity.pos() == Infinity.pos())
assert (Integer(2) - Infinity.pos() == Infinity.neg())
assert (Integer(2) * Infinity.pos() == Infinity.pos())
assert (Integer(0) * Infinity.pos() == Integer(0))
assert (Integer(2) / Infinity.pos() == Integer(0))
assert (Integer(2) // Infinity.pos() == Integer(0))

assert (Integer(0) >= Integer(0))
assert (Integer(0) < Integer(1))
assert (Integer(2) < Integer(3))
Beispiel #7
0
def parse_string(s, aiddl_paths, freg):
    str_id = 0
    str_lookup = {}

    bs = False
    current_str_start = None
    slices = []
    s_new = ""
    for i in range(len(s)):
        c = s[i]
        if c == '"' and not bs and current_str_start is not None:
            str_lookup[str_id] = s[current_str_start:i]
            slices.append((current_str_start, i, str(str_id)))
            s_new += ' "%d"' % (str_id)
            current_str_start = None
            str_id += 1
        elif c == '"' and not bs:
            current_str_start = i + 1
        elif current_str_start is None:
            s_new += c

        if c == '\\':
            bs = True
        else:
            bs = False

    # for sl in slices:
    #     s = s[:sl[0]] + sl[2] + s[sl[1]:]

    s = s_new

    s = re.sub(r";;.*\n", "", s)
    s = s.replace("\n", " ")
    s = s.replace("\t", " ")
    for token in SPECIAL:
        s = s.replace(token, " " + token + " ")
    while "  " in s:
        s = s.replace("  ", " ")
    s = s.strip()
    stack = []
    module_name = None
    self_ref = None
    local_refs = {}
    tokens = s.split(" ")
    tuple_depth = 0
    for token in tokens:
        token = token.strip()
        if token == CLIST:
            assembled_list = []
            current = pop(stack)
            while current != OLIST:
                assembled_list.append(current)
                current = pop(stack)
            assembled_list.reverse()
            term = List(assembled_list)
            # push(stack, term)
        elif token == CTUPLE:
            tuple_depth -= 1
            assembled_list = []
            current = pop(stack)
            while current != OTUPLE:
                assembled_list.append(current)
                current = pop(stack)
            assembled_list.reverse()
            term = Tuple(assembled_list)
            # push(stack, term)
            if tuple_depth == 0:
                if term.get(0) == MOD:
                    self_ref = term.get(1)
                    module_name = term.get(2)
                elif term.get(0) == REQ or \
                     term.get(0) == NAMES or \
                     term.get(0) == NAMES_ALT:
                    local_refs[term.get(1)] = term.get(2)
                    if isinstance(term.get(2), String):
                        fname = term.get(2).get_string_value()
                        req_mod_n = get_mod_name_from_file(fname, aiddl_paths)
                        local_refs[term.get(1)] = req_mod_n
        elif token == CSET:
            assembled_set = []
            current = pop(stack)
            while current != OSET:
                assembled_set.append(current)
                current = pop(stack)
            term = Set(assembled_set)
        elif token in SPECIAL:
            push(stack, token)
            if token == OTUPLE:
                tuple_depth += 1
            continue
        else:
            term = None
            if token[0] == "?":
                term = Variable(name=token)
            elif token == "_":
                term = Variable()
            elif token[0] == '"':
                string = str_lookup[int(token[1:-1])]
                term = String(string)
            elif "/" in token:
                n = token.split("/")[0]
                d = token.split("/")[1]
                term = Rational(int(n), int(d))
            elif is_float(token):
                term = Real(float(token))
            elif is_int(token):
                term = Integer(int(token))
            elif is_bin(token):
                term = Integer(int(token[2:], 2))
            elif is_oct(token):
                term = Integer(int(token[2:], 8))
            elif is_hex(token):
                term = Integer(int(token[2:], 16))
            elif token in INFINITY:
                if "-" in token:
                    term = Infinity.neg()
                else:
                    term = Infinity.pos()
            else:
                term = Symbolic(token)
        if len(stack) > 0:
            back_resolve = True
            while back_resolve and len(stack) > 0:
                back_resolve = False
                if peek(stack) == SREF:
                    pop(stack)
                    term = Reference(term, module_name)
                    back_resolve = True
                elif peek(stack) == REF:
                    pop(stack)
                    name = pop(stack)
                    if not isinstance(name, FunctionReference):
                        if term == self_ref:
                            # print("Pointing to %s in module %s (aka %s)"
                            #       % (str(name), str(module_name), str(term)))
                            term = Reference(name, module_name, alias=term)
                        else:
                            # print("Pointing to %s in module %s (aka %s)"
                            #       % (str(name), str(module_name), str(term)))
                            term = Reference(name,
                                             local_refs[term],
                                             alias=term)
                    else:
                        bad_fref = name.get_fref()
                        if term == self_ref:
                            term = FunctionReference(bad_fref,
                                                     freg,
                                                     module=module_name)
                        else:
                            term = FunctionReference(bad_fref,
                                                     freg,
                                                     module=module_name)
                    back_resolve = True
                elif peek(stack) == FREF:
                    pop(stack)
                    term = FunctionReference(term, freg)
                    back_resolve = True
                elif peek(stack) == ASSOC:
                    pop(stack)
                    key = pop(stack)
                    if isinstance(key, KeyValue):
                        term = KeyValue(key._key, KeyValue(key._value, term))
                    else:
                        term = KeyValue(key, term)
                    back_resolve = True
        push(stack, term)
    return (stack, module_name, self_ref, local_refs)