From eadf1eb4aed602cd758d3ad5f817817c9c82def0 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Thu, 29 Mar 2018 11:09:02 +0100 Subject: [PATCH] A `byte` or `word` table can be initialized with a list of constants. --- HISTORY.md | 5 +++-- src/sixtypical/compiler.py | 5 +++-- src/sixtypical/emitter.py | 13 +++++++------ src/sixtypical/parser.py | 4 ++-- tests/SixtyPical Compilation.md | 25 +++++++++++++++++++++---- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 10a7bf1..b4d98c9 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,10 +6,11 @@ History of SixtyPical * Symbolic constants can be defined with the `const` keyword, and can be used in most places where literal values can be used. -* Specifying multiple SixtyPical source files will produce a single - compiled result from their combination. * Added `nop` opcode, which compiles to `NOP` (mainly for timing.) * Accessing zero-page with `ld` and `st` generates zero-page opcodes. +* A `byte` or `word` table can be initialized with a list of constants. +* Specifying multiple SixtyPical source files will produce a single + compiled result from their combination. * Rudimentary support for Atari 2600 prelude in a 4K cartridge image, and start of an example program in `eg/atari2600` directory. diff --git a/src/sixtypical/compiler.py b/src/sixtypical/compiler.py index efeeb56..add19e9 100644 --- a/src/sixtypical/compiler.py +++ b/src/sixtypical/compiler.py @@ -122,8 +122,9 @@ class Compiler(object): elif type_ == TYPE_WORD: initial_data = Word(defn.initial) elif TableType.is_a_table_type(type_, TYPE_BYTE): - # FIXME convert defn.initial to a serializable type ... or when parsing. - initial_data = Table(defn.initial, type_.size) + initial_data = Table([Byte(i) for i in defn.initial], type_.size) + elif TableType.is_a_table_type(type_, TYPE_WORD): + initial_data = Table([Word(i) for i in defn.initial], type_.size) else: raise NotImplementedError(type_) label.set_length(initial_data.size()) diff --git a/src/sixtypical/emitter.py b/src/sixtypical/emitter.py index 78dbf95..a1b962c 100644 --- a/src/sixtypical/emitter.py +++ b/src/sixtypical/emitter.py @@ -13,6 +13,8 @@ class Emittable(object): class Byte(Emittable): def __init__(self, value): + if isinstance(value, basestring): + value = ord(value) if value < -127 or value > 255: raise IndexError(value) if value < 0: @@ -49,6 +51,7 @@ class Word(Emittable): class Table(Emittable): def __init__(self, value, size): + """`value` should be an iterable of Emittables.""" # TODO: range-checking self.value = value self._size = size @@ -57,12 +60,10 @@ class Table(Emittable): return self._size def serialize(self, addr=None): - bytes = [] - for b in self.value: - bytes.append(chr(ord(b))) - while len(bytes) < self.size(): - bytes.append(chr(0)) - return ''.join(bytes) + buf = ''.join([emittable.serialize() for emittable in self.value]) + while len(buf) < self.size(): + buf += chr(0) + return buf def __repr__(self): return "%s()" % (self.__class__.__name__) diff --git a/src/sixtypical/parser.py b/src/sixtypical/parser.py index 851002e..82ba3eb 100644 --- a/src/sixtypical/parser.py +++ b/src/sixtypical/parser.py @@ -144,9 +144,9 @@ class Parser(object): self.scanner.scan() else: initial = [] - initial.append(self.const()) + initial.append(self.const().value) while self.scanner.consume(','): - initial.append(self.const()) + initial.append(self.const().value) else: initial = self.const().value diff --git a/tests/SixtyPical Compilation.md b/tests/SixtyPical Compilation.md index 862c22f..511ed5c 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -198,14 +198,31 @@ Initialized byte table, initialized with list of byte values. = $080D LDX #$00 = $080F LDA $0813,X = $0812 RTS - = $0813 .byte $57 - = $0814 PHA - = $0815 EOR ($54,X) - = $0817 .byte $3F + = $0813 .byte $FF + = $0814 BRK + = $0815 STA ($80,X) + = $0817 .byte $7F = $0818 BRK = $0819 BRK = $081A BRK +Initialized word table, initialized with list of word values. + + | word table[8] message : 65535, 0, 127 + | + | routine main + | { + | } + = $080D RTS + = $080E .byte $FF + = $080F .byte $FF + = $0810 BRK + = $0811 BRK + = $0812 .byte $7F + = $0813 BRK + = $0814 BRK + = $0815 BRK + Some instructions. | byte foo