From e2fd1c7ee759ac8870f21afe5fc4e36c8b808d25 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 20 Nov 2023 22:14:55 +0200 Subject: Do things --- asm/ast_compiler_bootstrap.atk16 | 1 - asm/atk16.py | 23 +++++----- asm/test_py_src.atk16 | 92 ++++++++++++++++++++-------------------- asm/test_py_src.py | 33 +++++++++----- src/ast_compiler.py | 14 ++++++ 5 files changed, 94 insertions(+), 69 deletions(-) diff --git a/asm/ast_compiler_bootstrap.atk16 b/asm/ast_compiler_bootstrap.atk16 index 18a345a..2e112eb 100644 --- a/asm/ast_compiler_bootstrap.atk16 +++ b/asm/ast_compiler_bootstrap.atk16 @@ -48,7 +48,6 @@ text_mem ; 0x19 @label hlt_isr - ldi 0x55 RE hlt @label keyboard_isr diff --git a/asm/atk16.py b/asm/atk16.py index 9a5921c..cdd9f64 100644 --- a/asm/atk16.py +++ b/asm/atk16.py @@ -1,26 +1,25 @@ -from typing import Literal, Any, TypeVar, NewType, Never +from typing import Literal, Any, TypeVar, NewType, Never, Callable, Generic, overload, cast Char = Literal['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f', '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87', '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f', '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97', '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f', '\xa0', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '\xad', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ'] -ConstInt = int +Void = Literal[0] +Word16 = int +ConstWord16 = int -def store(addr: int, value: int | Char): - return NotImplemented +def store(p: Word16, value: Word16) -> Void: + raise NotImplementedError -def load(addr: int) -> int: - return NotImplemented - -def put_char(c: int): - return NotImplemented +def load(p: Word16) -> Word16: + raise NotImplementedError T = TypeVar("T") def call_inline(expr: T) -> T: """Inline the function call `expr` at the callsite. This can lead to a larger output size but avoids having to do a subroutine call.""" - return NotImplemented + raise NotImplementedError def asm(asm: str): """Inject ATK16 assembly `asm` into the compiled output.""" - return NotImplemented + raise NotImplementedError def ord(char: Char) -> int: """Convert char to int""" - return NotImplemented + raise NotImplementedError diff --git a/asm/test_py_src.atk16 b/asm/test_py_src.atk16 index b605dad..c38d1ca 100644 --- a/asm/test_py_src.atk16 +++ b/asm/test_py_src.atk16 @@ -85,21 +85,21 @@ @label int_6 26 -; +; @label nth_letter ; stack frame with offsets ; n 0 -; -; -; BinOp lhs -; -; Builtin call ord [] -; +; +; +; BinOp lhs +; +; Builtin call ord [] +; ldi int_0 RC ldr RC RC ; int_0 = 65 spu RC -; BinOp rhs -; (n) +; BinOp rhs +; (n) ldi 0 RC add FP RC RC ldr RC RC @@ -120,12 +120,12 @@ ; text_mem_addr 1 ; i 2 addi SP 3 SP -; +; ; assigning graphics_mode_addr at FP + 0 ; evaluating value to be assigned -; -; Builtin call load [] -; (GRAPHICS_MODE_ADDR_P) +; +; Builtin call load [] +; (GRAPHICS_MODE_ADDR_P) ldi graphics_mode_addr_p RC ldr RC RC spu RC @@ -138,12 +138,12 @@ ldi 0 RB add RB FP RB str RA RB -; +; ; assigning text_mem_addr at FP + 1 ; evaluating value to be assigned -; -; Builtin call load [] -; (TEXT_MEM_ADDR_P) +; +; Builtin call load [] +; (TEXT_MEM_ADDR_P) ldi text_mem_addr_p RC ldr RC RC spu RC @@ -156,14 +156,14 @@ ldi 1 RB add RB FP RB str RA RB -; -; Builtin call store [, ] -; (graphics_mode_addr) +; +; Builtin call store [, ] +; (graphics_mode_addr) ldi 0 RA add FP RA RA ldr RA RA spu RA -; (TEXT_MODE) +; (TEXT_MODE) ldi text_mode RA ldr RA RA spu RA @@ -174,10 +174,10 @@ spu RA ; popping free-standing Expr result from stack spo RA -; +; ; assigning i at FP + 2 ; evaluating value to be assigned -; +; ldi 0 RC spu RC ; assigning stack address (FP + 2) := top of stack @@ -185,19 +185,19 @@ ldi 2 RB add RB FP RB str RA RB -; +; @label while_test_1 -; -; (i) +; +; (i) ldi 2 RB add FP RB RB ldr RB RB spu RB -; +; ldi int_6 RB ldr RB RB ; int_6 = 26 spu RB -; Comparing +; Comparing spo RC spo RB sub RB RC RB @@ -212,17 +212,17 @@ spo RA addi RA 0 RA bri zero While_else_2 -; -; Builtin call store [, ] -; -; BinOp lhs -; (text_mem_addr) +; +; Builtin call store [, ] +; +; BinOp lhs +; (text_mem_addr) ldi 1 RA add FP RA RA ldr RA RA spu RA -; BinOp rhs -; (i) +; BinOp rhs +; (i) ldi 2 RA add FP RA RA ldr RA RA @@ -231,12 +231,12 @@ spo RA add RA RB RA spu RA -; +; ; Call function nth_letter spu FP ldi 0 RA spu RA -; (i) +; (i) ldi 2 RA add FP RA RA ldr RA RA @@ -259,18 +259,18 @@ spu RA ; popping free-standing Expr result from stack spo RA -; +; ; assigning i at FP + 2 ; evaluating value to be assigned -; -; BinOp lhs -; (i) +; +; BinOp lhs +; (i) ldi 2 RC add FP RC RC ldr RC RC spu RC -; BinOp rhs -; +; BinOp rhs +; ldi 1 RC spu RC spo RD @@ -285,15 +285,15 @@ jpi While_test_1 @label while_else_2 @label while_end_3 -; +; @label while_test_7 -; +; ldi 1 RB spu RB spo RA addi RA 0 RA bri zero While_else_8 -; +; jpi While_test_7 @label while_else_8 @label while_end_9 diff --git a/asm/test_py_src.py b/asm/test_py_src.py index ec17ddc..676f8cd 100644 --- a/asm/test_py_src.py +++ b/asm/test_py_src.py @@ -1,21 +1,34 @@ -import atk16 +# TODO: actual imports, not just `import atk16` +from atk16 import * -TEXT_MODE: atk16.ConstInt = 1 -GRAPHICS_MODE_ADDR_P: atk16.ConstInt = 0x17 -TEXT_MEM_ADDR_P: atk16.ConstInt = 0x19 +GRAPHICS_NO_MODE: ConstWord16 = 0 +GRAPHICS_TEXT_MODE: ConstWord16 = 1 +GRAPHICS_SPRITE_MODE: ConstWord16 = 2 -graphics_mode_addr = atk16.load(GRAPHICS_MODE_ADDR_P) -text_mem_addr = atk16.load(TEXT_MEM_ADDR_P) +GRAPHICS_MODE_PP: ConstWord16 = 0x17 +TEXT_MEM_PP: ConstWord16 = 0x19 -atk16.store(graphics_mode_addr, TEXT_MODE) +# asm( +# "@label keyboard_isr" +# " spu RA" + +# " spo RA" +# ) + +# set_isr(0, "keyboard_isr") + +graphics_mode_p = load(GRAPHICS_MODE_PP) +text_mem_p = load(TEXT_MEM_PP) + +store(graphics_mode_p, GRAPHICS_TEXT_MODE) def nth_letter(n: int) -> int: - return atk16.ord('A') + n + return ord('A') + n i = 0 while i < 26: - atk16.store(text_mem_addr + i, nth_letter(i)) - i = i + 1 + store(text_mem_p + i, nth_letter(i)) + i += 1 while True: pass diff --git a/src/ast_compiler.py b/src/ast_compiler.py index 4dfcf8e..f0d0c79 100644 --- a/src/ast_compiler.py +++ b/src/ast_compiler.py @@ -643,6 +643,20 @@ class Compiler(ast.NodeVisitor): case other: raise Exception(f"Unsupported assign targets: {other}") + def visit_AugAssign(self, node: ast.AugAssign): + op = node.op + lhs = node.target + rhs = node.value + + if type(lhs) != ast.Name: + raise Exception("AugAssign to non-Name lhs not yet supported!") + + # construct bin op and assignment + bin_op = ast.BinOp(left=lhs, op=op, right=rhs) + assign = ast.Assign(targets=[lhs], value=bin_op) + + self.visit(assign) + def visit_Import(self, node: ast.Import) -> Any: match node.names: case [ast.alias(name="atk16")]: -- cgit v1.3