Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- def sign(x):
- return '-' if x < 0 else '+'
- def txt2forth(src, dst, xoffset: int, yoffset: int, comment: str):
- lines = src.readlines()
- rows = len(lines)
- cols = max(map(len, lines))
- dst.write(': logo ( x y -- ) \\ {comment} ({rows} rows x {cols} columns)\n\n'.format(comment=comment,
- rows=rows, cols=cols))
- dst.write('\t{xabs} {xsign} swap {yabs} {ysign} swap\n\n'.format(xabs=abs(xoffset), xsign=sign(xoffset),
- yabs=abs(yoffset), ysign=sign(yoffset)))
- for line in lines[:-1]:
- dst.write('\t2dup at-xy ." {}" 1+\n'.format(line.replace('\n', '')))
- dst.write( '\t at-xy ." {}"\n\n'.format(lines[-1].replace('\n', '')))
- dst.write( '\t\\ Put the cursor back at the bottom\n\t0 25 at-xy\n;\n')
- def main():
- import sys
- if len(sys.argv) != 4:
- print("usage: {} xoffset yoffset comment")
- print(" Reads ASCII art from stdin, writes Forth to stdout")
- exit(1)
- txt2forth(src = sys.stdin,
- dst = sys.stdout,
- xoffset = int(sys.argv[1]),
- yoffset = int(sys.argv[2]),
- comment = sys.argv[3])
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment