def _parse_value(code: str) -> Tuple[data.Value, str]: if code.startswith('\''): value, rest_code = _parse_value(code[1:]) return _make_quote(value), rest_code if code.startswith('('): values, rest_code = _parse_list(code[1:]) rest_code = _parse_skip(rest_code) if not rest_code.startswith(')'): raise ParseError(rest_code) return data.from_native_list(values), rest_code[1:] match = _TOKEN_RE.search(code) if not match: raise ParseError(code) token = match.group() if _NUM_RE.search(token): value = data.Int(int(token)) elif token == '#f': value = data.Bool(False) elif token == '#t': value = data.Bool(True) else: value = data.Symbol(token) return value, code[match.end():]
def _builtin_mul(args: List[data.Value]) -> data.Value: assert args result = 1 for value in args: assert isinstance(value, data.Int), 'not integer' result *= value.raw_value return data.Int(result)
def _builtin_add(args: List[data.Value]) -> data.Value: assert args result = 0 for value in args: assert isinstance(value, data.Int) result += value.raw_value return data.Int(result)
def _builtin_div(args: List[data.Value]) -> data.Value: assert args first_value = args[0] assert isinstance(first_value, data.Int), 'not integer' result = first_value.raw_value for value in args[1:]: assert isinstance(value, data.Int), 'not integer' result //= value.raw_value return data.Int(result)