Skip to content

Commit

Permalink
WarningPool 추가 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone authored Apr 4, 2024
1 parent 42be3ea commit 868e7e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions aheui/_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,6 @@ def parse_args(self, args):
""")
parser.add_argument('--cmd', '-c', default='', description='Program passed in as string')
parser.add_argument('--no-c', '--no-c', narg='0', default='no', description='Do not generate `.aheuic` file automatically.', full_description='\tWhat is .aheuic? https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35\n')
parser.add_argument('--warning-limit', '--warning-limit', default='3', description='Set repetitive warning limit. 0 means no warning. -1 means no limit.')
parser.add_argument('--version', '-v', narg='-1', default='no', description='Show program version', message=VERSION)
parser.add_argument('--help', '-h', narg='-1', default='no', description='Show this help text')
16 changes: 10 additions & 6 deletions aheui/aheui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from aheui import _argparse
from aheui import compile
from aheui.int import smallint as bigint # import `bigint` to enable bigint
from aheui.warning import WarningPool


def get_location(pc, stackok, is_queue, program):
Expand Down Expand Up @@ -288,10 +289,9 @@ def write_number(value):


@jit.dont_look_inside
def write_utf8(value):
def write_utf8(warnings, value):
if not (0 <= value < 0x110000):
msg = b'[Warning] Undefined behavior: unicode %x out of range\n' % value
os.write(errfp, msg)
warnings.warn(b'write-utf8-range', value)
value = 0xfffd
os.write(outfp, unichr(value).encode('utf-8'))

Expand Down Expand Up @@ -324,6 +324,7 @@ def get_label(self, pc):

outfp = 1
errfp = 2
warnings = WarningPool()


def mainloop(program, debug):
Expand Down Expand Up @@ -417,7 +418,7 @@ def mainloop(program, debug):
write_number(r)
elif op == c.OP_POPCHAR:
r = selected.pop_longlong()
write_utf8(r)
write_utf8(warnings, r)
elif op == c.OP_PUSHNUM:
num = read_number()
selected.push(num)
Expand Down Expand Up @@ -518,7 +519,9 @@ def open_r(filename):
os.write(2, b'aheui: error: --target,-t must be one of "bytecode", "asm", "asm+comment", "run"\n') # noqa: E501
raise SystemExit()

return cmd, source, contents, opt_level, target, aheuic_output, comment_aheuis, output
warning_limit = int(kwargs['warning-limit'])

return cmd, source, contents, opt_level, target, aheuic_output, comment_aheuis, output, warning_limit


def open_w(filename):
Expand Down Expand Up @@ -559,9 +562,10 @@ def prepare_compiler(contents, opt_level=2, source='code', aheuic_output=None, a

def entry_point(argv):
try:
cmd, source, contents, str_opt_level, target, aheuic_output, comment_aheuis, output = process_opt(argv)
cmd, source, contents, str_opt_level, target, aheuic_output, comment_aheuis, output, warning_limit = process_opt(argv)
except SystemExit:
return 1
warnings.limit = warning_limit

add_debug_info = DEBUG or target != 'run' # debug flag for user program
compiler = prepare_compiler(contents, int(str_opt_level), source, aheuic_output, add_debug_info)
Expand Down
39 changes: 39 additions & 0 deletions aheui/warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import absolute_import

import os
from aheui._compat import jit


class Warning(object):
def __init__(self, name, message):
self.name = name
self.message = message

def format(self, *args):
return self.message % args


WARNING_LIST = [
Warning(b'write-utf8-range', b'[Warning:UndefinedBehavior:write-utf8-range] value %x is out of unicode codepoint range.'),
]


class WarningPool(object):
def __init__(self):
self.limit = -1
self.warnings = {}
self.counters = {}
for w in WARNING_LIST:
self.warnings[w.name] = w
self.counters[w.name] = 0

@jit.dont_look_inside
def warn(self, name, *args):
warning = self.warnings[name]
if self.limit != -1 and self.limit <= self.counters[name]:
return
self.counters[name] = self.counters[name] + 1
os.write(2, warning.format(*args))
os.write(2, b'\n')
if self.limit != -1 and self.limit <= self.counters[name]:
os.write(2, b"[Warning:Meta] The warning '%s' has reached the limit %d and will be suppressed\n" % (warning.name, self.limit))

0 comments on commit 868e7e6

Please sign in to comment.