From 96cf8f7c61baa987f3f047ff3602becd4719f3ba Mon Sep 17 00:00:00 2001 From: Lawrence Kesteloot Date: Mon, 6 Aug 2018 11:31:44 -0700 Subject: [PATCH] Fix array allocation and access. --- Makefile | 2 +- examples/stars.bas | 78 +++++++++++++++++++++++----------------------- examples/stars.py | 7 +++-- main.c | 2 ++ runtime.c | 3 ++ 5 files changed, 49 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index acff00c..b73dc24 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ endif CC65 ?= $(TREES)/cc65/bin APPLE2E ?= $(TREES)/apple2e/apple2e -CPU = 65C02 +CPU = 6502 ROM = apple2a.rom LIB = apple2rom.lib diff --git a/examples/stars.bas b/examples/stars.bas index e71b1b8..c5b2880 100644 --- a/examples/stars.bas +++ b/examples/stars.bas @@ -1,46 +1,46 @@ 10 GR 20 TS = 40 30 DIM DX(9),DY(9),C(9),X(9),Y(9),T(9) -1000 DX(0) = 12 -1005 DY(0) = 15 -1010 C(0) = 6 -1015 T(0) = 10 -1100 DX(1) = 19 -1105 DY(1) = 0 +1000 DX(0) = -1 +1005 DY(0) = -19 +1010 C(0) = 1 +1015 T(0) = 26 +1100 DX(1) = -14 +1105 DY(1) = -13 1110 C(1) = 5 -1115 T(1) = 9 -1200 DX(2) = -19 -1205 DY(2) = -1 -1210 C(2) = 9 -1215 T(2) = 9 -1300 DX(3) = -19 -1305 DY(3) = 3 -1310 C(3) = 1 -1315 T(3) = 32 -1400 DX(4) = -1 -1405 DY(4) = 19 -1410 C(4) = 7 -1415 T(4) = 27 -1500 DX(5) = -12 -1505 DY(5) = -15 -1510 C(5) = 12 -1515 T(5) = 3 -1600 DX(6) = -19 -1605 DY(6) = -2 -1610 C(6) = 7 -1615 T(6) = 18 -1700 DX(7) = 17 -1705 DY(7) = 9 -1710 C(7) = 13 -1715 T(7) = 7 -1800 DX(8) = -19 -1805 DY(8) = 1 -1810 C(8) = 10 -1815 T(8) = 5 -1900 DX(9) = 18 -1905 DY(9) = 8 -1910 C(9) = 1 -1915 T(9) = 9 +1115 T(1) = 23 +1200 DX(2) = 12 +1205 DY(2) = -15 +1210 C(2) = 7 +1215 T(2) = 3 +1300 DX(3) = 19 +1305 DY(3) = 2 +1310 C(3) = 9 +1315 T(3) = 10 +1400 DX(4) = 17 +1405 DY(4) = -10 +1410 C(4) = 12 +1415 T(4) = 4 +1500 DX(5) = 17 +1505 DY(5) = -9 +1510 C(5) = 9 +1515 T(5) = 5 +1600 DX(6) = 15 +1605 DY(6) = 12 +1610 C(6) = 4 +1615 T(6) = 22 +1700 DX(7) = -18 +1705 DY(7) = -8 +1710 C(7) = 5 +1715 T(7) = 23 +1800 DX(8) = -2 +1805 DY(8) = -19 +1810 C(8) = 1 +1815 T(8) = 29 +1900 DX(9) = -7 +1905 DY(9) = 18 +1910 C(9) = 10 +1915 T(9) = 28 2200 I = 0 2210 OX = X(I) : OY = Y(I) 2220 X(I) = 20 + DX(I) * T(I) / 40 diff --git a/examples/stars.py b/examples/stars.py index 001453a..d31dab0 100644 --- a/examples/stars.py +++ b/examples/stars.py @@ -6,13 +6,14 @@ str = """%(init)d00 DX(%(which)d) = %(x)d %(init)d10 C(%(which)d) = %(c)d %(init)d15 T(%(which)d) = %(t)d""" +num_stars = 10 ts = 40 print "10 GR" print "20 TS = %d" % ts -print "30 DIM DX(9),DY(9),C(9),X(9),Y(9),T(9)" +print "30 DIM DX(%(n)d),DY(%(n)d),C(%(n)d),X(%(n)d),Y(%(n)d),T(%(n)d)" % {'n' : num_stars - 1 } -for i in range(0,10) : +for i in range(0,num_stars) : angle = random.uniform(0,.999) x = int(20 * math.cos(angle * 3.14159 * 2)) y = int(20 * math.sin(angle * 3.14159 * 2)) @@ -27,5 +28,5 @@ print "2230 Y(I) = 20 + DY(I) * T(I) / %d" % ts print "2235 T(I) = T(I) + 1 : IF T(I) = TS THEN T(I) = 0" print "2240 COLOR=0 : PLOT OX, OY" print "2250 COLOR=C(I) : PLOT X(I), Y(I)" -print "2260 I = I + 1 : IF I < 10 GOTO 2210" +print "2260 I = I + 1 : IF I < %d GOTO 2210" % num_stars print "2300 GOTO 2200" diff --git a/main.c b/main.c index f0d96b0..654e66c 100644 --- a/main.c +++ b/main.c @@ -617,6 +617,8 @@ static void push_operator_stack(uint8_t op) { // All our operators are left-associative, so no special check for the case // of equal precedence. while (g_op_stack_size > 0 && + op != OP_OPEN_PARENS && + op != OP_ARRAY_DEREF && (top_op = g_op_stack[g_op_stack_size - 1]) != OP_OPEN_PARENS && top_op != OP_ARRAY_DEREF && OP_PRECEDENCE(top_op) >= OP_PRECEDENCE(op)) { diff --git a/runtime.c b/runtime.c index 0286edc..f60f57a 100644 --- a/runtime.c +++ b/runtime.c @@ -501,6 +501,9 @@ uint16_t next_statement(uint16_t line_number, uint16_t var_address) { * the variable that will store the array location. */ void allocate_array(uint16_t size, uint16_t var_addr) { + // Actual size is one more. DIM X(10) allocates 11-entry array. + size += 1; + // Check for overflow. if (g_arrays_size + size > MAX_ARRAY_WORDS) { print("Too many arrays.\n");