Skip to content

Commit

Permalink
add target asm+comment (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
minacle authored and youknowone committed Mar 1, 2018
1 parent e7104bc commit 756eb70
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Options
- `run` target: This option is not availble and ignored.
- `bytecode` target: Default value is `.aheuic`
- `asm` target: Default value is `.aheuis`
- `asm+comment` target: Same as `asm` with comments.
- --cmd,-c: Program passed in as string
- --no-c: Do not generate `.aheuic` file automatically.
- Why `.aheuic` is useful: [https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35](https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ sys 0m0.035s
- --target=run: 이 옵션은 무시됩니다.
- --target=bytecode: 기본 값은 `.aheuic` 파일입니다.
- --target=asm: 기본 값은 `.aheuis` 파일입니다.
- --target=asm+comment: `asm`에 주석이 추가됩니다.
- --cmd,-c: 코드를 파일 대신 문자열로 받아 넘겨줍니다.
- --no-c: `.aheuic` 파일을 자동으로 생성하지않습니다.
- --no-c: `.aheuic` 파일을 자동으로 생성하지 않습니다.
- `.aheuic` 파일은 왜 생성되나요?: [https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35](https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35)

앟셈블리와 ahsembler
Expand Down
3 changes: 2 additions & 1 deletion aheui/_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ def parse_args(self, args):
\t1: Quickly resolve deadcode by rough stacksize emulation and merge constant operations.
\t2: Perfectly resolve deadcode by stacksize emulation, reserialize code chunks and merge constant operations.
""")
parser.add_argument('--source', '-S', default='auto', choices='auto,bytecode,asm,text', description='Set source filetype.', full_description="""\t- `auto`: Guess the source type. `bytecode` if `.aheuic` or `End of bytecode` pattern in source. `asm` is `.aheuis`. `text` if `.aheui`. `text` is default.
parser.add_argument('--source', '-S', default='auto', choices='auto,bytecode,asm,asm+comment,text', description='Set source filetype.', full_description="""\t- `auto`: Guess the source type. `bytecode` if `.aheuic` or `End of bytecode` pattern in source. `asm` is `.aheuis`. `text` if `.aheui`. `text` is default.
\t- `bytecode`: Aheui bytecode. (Bytecode representation of `ahsembly`.
\t- `asm`: See `ahsembly`.
\t- `asm+comment`: Same as `asm` with comments.
\t- usage: `--source=asm`, `-Sbytecode` or `-S text`
""")
parser.add_argument('--target', '-T', default='run', choices='run,bytecode,asm', description='Set target filetype.', full_description="""\t- `run`: Run given code.
Expand Down
14 changes: 8 additions & 6 deletions aheui/aheui.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,26 +489,28 @@ def open_r(filename):
aheuic_output = None

output = kwargs['output']
comment_aheuis = False
if output == '':
if target == 'bytecode':
output = filename
if output.endswith('.aheui'):
output += 'c'
else:
output += '.aheuic'
elif target == 'asm':
elif target in ['asm', 'asm+comment']:
output = filename
if output.endswith('.aheui'):
output += 's'
else:
output += '.aheuis'
comment_aheuis = target == 'asm+comment'
elif target == 'run':
output = '-'
else:
os.write(2, b'aheui: error: --target,-t must be one of "bytecode", "asm", "run"\n')
os.write(2, b'aheui: error: --target,-t must be one of "bytecode", "asm", "asm+comment", "run"\n')
raise SystemExit()

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


def open_w(filename):
Expand Down Expand Up @@ -549,7 +551,7 @@ def prepare_compiler(contents, opt_level=2, source='code', aheuic_output=None):

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

Expand All @@ -558,8 +560,8 @@ def entry_point(argv):
if target == 'run':
program = Program(compiler.lines, compiler.label_map)
exitcode = mainloop(program, compiler.debug)
elif target == 'asm':
asm = compiler.write_asm().encode('utf-8')
elif target in ['asm', 'asm+comment']:
asm = compiler.write_asm(commented=comment_aheuis).encode('utf-8')
os.write(outfp, asm)
os.close(outfp)
exitcode = 0
Expand Down
7 changes: 5 additions & 2 deletions aheui/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def read_bytecode(self, text):
if op in c.OP_JUMPS:
self.label_map[val] = val

def write_asm(self, fp=1):
def write_asm(self, fp=1, commented=True):
"""Write assembly representation with comments."""
codes = []
for i, (op, val) in enumerate(self.lines):
Expand All @@ -863,7 +863,10 @@ def write_asm(self, fp=1):
code_val = padding(code_val, 10)
comment = self.debug.comment(i) if self.debug else u''
sline = padding(_unicode(i), 3)
codes.append(u'%s ; L%s %s\n' % (code_val, sline, comment))
if commented:
codes.append(u'%s ; L%s %s\n' % (code_val, sline, comment))
else:
codes.append(u'%s\n' % code_val)
return u''.join(codes)

def read_asm(self, text):
Expand Down
2 changes: 1 addition & 1 deletion aheui/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '1.2.2-17-g49f1f4d'
VERSION = '1.2.2-20-gee8ab38'

0 comments on commit 756eb70

Please sign in to comment.