git @ Cat's Eye Technologies SixtyPical / f73a231
Add some failing tests for analyzing "for". Chris Pressey 3 years ago
2 changed file(s) with 62 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
00 # encoding: UTF-8
11
2 from sixtypical.ast import Program, Routine, Block, Instr, SingleOp, If, Repeat, WithInterruptsOff
2 from sixtypical.ast import Program, Routine, Block, Instr, SingleOp, If, Repeat, For, WithInterruptsOff
33 from sixtypical.model import (
44 TYPE_BYTE, TYPE_WORD,
55 TableType, BufferType, PointerType, VectorType, RoutineType,
328328 self.analyze_if(instr, context)
329329 elif isinstance(instr, Repeat):
330330 self.analyze_repeat(instr, context)
331 elif isinstance(instr, For):
332 self.analyze_for(instr, context)
331333 elif isinstance(instr, WithInterruptsOff):
332334 self.analyze_block(instr.block, context)
333335 else:
610612 self.analyze_block(instr.block, context)
611613 if instr.src is not None:
612614 context.assert_meaningful(instr.src)
615
616 def analyze_for(self, instr, context):
617 # TODO: find the range of the loop variable in context, make sure it fits
618
619 # TODO: set the loop variable as 'not writeable' in the context
620
621 self.analyze_block(instr.block, context)
622
623 # TODO: at the end of the loop, we know the new range of the loop variable
16581658 | }
16591659 = ok
16601660
1661 ### for ###
1662
1663 Basic "open-faced for" loop. We'll start with the "upto" variant.
1664
1665 In a "for" loop, we know the exact range the loop variable takes on.
1666
1667 | byte table[16] tab
1668 |
1669 | define foo routine inputs tab trashes a, x, c, z, v, n {
1670 | ld x, 0
1671 | for x upto 15 {
1672 | ld a, tab + x
1673 | }
1674 | }
1675 = ok
1676
1677 | byte table[15] tab
1678 |
1679 | define foo routine inputs tab trashes a, x, c, z, v, n {
1680 | ld x, 0
1681 | for x upto 15 {
1682 | ld a, tab + x
1683 | }
1684 | }
1685 ? RangeExceededError
1686
1687 You cannot modify the loop variable in a "for" loop.
1688
1689 | byte table[16] tab
1690 |
1691 | define foo routine inputs tab trashes a, x, c, z, v, n {
1692 | ld x, 0
1693 | for x upto 15 {
1694 | ld x, 0
1695 | }
1696 | }
1697 ? ForbiddenWriteError
1698
1699 If the range isn't known to be smaller than the final value, you can't go up to it.
1700
1701 | byte table[32] tab
1702 |
1703 | define foo routine inputs tab trashes a, x, c, z, v, n {
1704 | ld x, 16
1705 | for x upto 15 {
1706 | ld a, tab + x
1707 | }
1708 | }
1709 ? RangeExceededError
1710
16611711 ### copy ###
16621712
16631713 Can't `copy` from a memory location that isn't initialized.