Skip to content

Commit

Permalink
_compat.bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Apr 5, 2024
1 parent 2dc6764 commit 3348ae1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
48 changes: 45 additions & 3 deletions aheui/_compat.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@

# coding: utf-8
from __future__ import absolute_import
import os


try:
from rpython.rlib import jit
from rpython.rlib.listsort import TimSort
TRACE_LIMIT = jit.PARAMETERS['trace_limit']

PYR = True
PY2 = False
PY3 = False
PY = -1

try:
USE_BIGINT = os.environ["RPAHEUI_BIGINT"]
except (KeyError, ValueError):
USE_BIGINT = ''

except ImportError:
"""Python compatibility."""
# Python compatibility

import sys

PY = sys.version_info.major
PYR = False
PY2 = PY == 2
PY3 = PY == 3

USE_BIGINT = False

def omnipotent(*args, **kw):
return args and args[0]

Expand Down Expand Up @@ -53,11 +75,31 @@ def ord(n):

try:
# rpython, python2
@jit.elidable
def _unicode(i):
return (b'%d' % i).decode('utf-8')
_unicode(0)

@jit.elidable
def _bytestr(i):
return b'%d' % i
except TypeError:
# python3
def _unicode(i):
return u'%d' % i
_unicode(0)

def _bytestr(i):
return b'%d' % i


try:
USE_BIGINT = os.environ["RPAHEUI_BIGINT"]
except (KeyError, ValueError):
USE_BIGINT = ''


if USE_BIGINT:
from aheui.int import bigint # Enable bigint in rpython build
else:
from aheui.int import smallint as bigint # noqa: F401 smallint or python support
9 changes: 4 additions & 5 deletions aheui/aheui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import os

from aheui import const as c
from aheui._compat import jit, unichr, ord, _unicode
from aheui._compat import jit, unichr, ord, _unicode, bigint
from aheui import compile
from aheui.int import smallint as bigint # import `bigint` to enable bigint
from aheui.option import process_options
from aheui.warning import WarningPool

Expand Down Expand Up @@ -286,7 +285,7 @@ def read_number(input_buffer=input_buffer):

@jit.dont_look_inside
def write_number(value):
os.write(outfp, _unicode(value).encode('utf-8'))
os.write(outfp, value)


@jit.dont_look_inside
Expand Down Expand Up @@ -415,8 +414,8 @@ def mainloop(program, debug):
stacksize=stacksize, storage=storage, selected=selected)
continue
elif op == c.OP_POPNUM:
r = selected.pop_longlong()
write_number(r)
r = selected.pop()
write_number(bigint.str(r))
elif op == c.OP_POPCHAR:
r = selected.pop_longlong()
write_utf8(warnings, r)
Expand Down
4 changes: 4 additions & 0 deletions aheui/int/bigint.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def tolonglong(big):
return big.tolonglong()


def str(big):
return big.str()


@jit.elidable
def add(r1, r2):
return r1.add(r2)
Expand Down
9 changes: 8 additions & 1 deletion aheui/int/smallint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import absolute_import

try:
import builtins
except ImportError:
except ImportError: # python2
builtins = __builtins__
from aheui._compat import _bytestr


Int = int
Expand All @@ -27,6 +30,10 @@ def tolonglong(v):
return v


def str(r):
return _bytestr(r)


def add(r1, r2):
return r1 + r2

Expand Down

0 comments on commit 3348ae1

Please sign in to comment.