Skip to content

Commit 6ec4040

Browse files
committed
Allocate new data space every time CREATE is called.
1 parent 22513c2 commit 6ec4040

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

pkg/forth/builtin/02_core.f

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,23 @@
373373
DROP 0 \ drop the result, return 0
374374
;
375375

376-
0 BUFFER: DATASPACE \ create a buffer starting at size 0 to hold the data space
376+
VARIABLE DATASPACE \ create a variable to hold the currently allocated data space
377377
VARIABLE DATAPOINTER \ create a variable to keep track of the data space pointer
378-
DATASPACE DATAPOINTER ! \ set the data space pointer
378+
379+
: NEW-DATASPACE ( ) \ set up a new data space
380+
0 ALLOCATE DROP DUP ( addr addr )
381+
DATASPACE ! \ save this new address to the dataspace
382+
DATAPOINTER ! \ also to the pointer
383+
;
384+
NEW-DATASPACE \ run it right away
379385

380386
: HERE ( -- addr )
381-
DATAPOINTER @ \ return the dataspace pointer plus the size
387+
DATAPOINTER @ \ return the dataspace pointed address
382388
;
383389

384390
: DATASIZE
385-
DATAPOINTER @ DATASPACE - \ find the size difference
386-
0x7FFF AND
391+
DATAPOINTER @ DATASPACE @ - \ find the size difference
392+
0x7FFF AND \ remove any alignment bits
387393
;
388394

389395
: ALLOT ( n -- )
@@ -395,7 +401,7 @@
395401
EXIT
396402
THEN ( n newSize )
397403
SWAP DATAPOINTER +! ( newSize ) \ update the data space pointer
398-
DATASPACE SWAP ( dataspace newSize )
404+
DATASPACE @ SWAP ( dataspace newSize )
399405
RESIZE ( newaddress 0 ) \ resize the dataspace
400406
2DROP \ remove the extra values from the resize
401407
;
@@ -407,7 +413,7 @@
407413
;
408414

409415
: C, ( char -- )
410-
HERE DATASPACE - \ find the size difference
416+
HERE DATASPACE @ - \ find the size difference
411417
0< IF \ if the upper bit is set then we don't need to allocate
412418

413419
ELSE
@@ -450,7 +456,7 @@
450456
;
451457

452458
: CREATE
453-
ALIGN \ align the data space pointer
459+
NEW-DATASPACE \ create a fresh section of aligned data space
454460
HERE \ get the data space pointer
455461
: \ parse the next input, create a word with that name
456462
POSTPONE LITERAL \ create a literal of the previous data space pointer

pkg/forth/suite_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,23 @@ func TestSuite(t *testing.T) {
297297
`,
298298
},
299299
// CR does not have any tests
300-
// CREATE does not have any tests
300+
{
301+
name: "CREATE",
302+
setup: `
303+
T{ CREATE CR1 -> }T
304+
T{ : ADDR1 [ HERE ] LITERAL ; -> }T
305+
T{ 1 , -> }T
306+
T{ CREATE CR2 -> }T
307+
T{ : ADDR2 [ HERE ] LITERAL ; -> }T
308+
T{ FF , -> }T
309+
`,
310+
code: `
311+
T{ CR1 -> ADDR1 }T
312+
T{ CR2 -> ADDR2 }T
313+
T{ CR1 @ -> 1 }T
314+
T{ CR2 @ -> FF }T
315+
`,
316+
},
301317
{
302318
name: "C!", // not in test suite
303319
setup: "T{ VARIABLE c1 -> }T",

0 commit comments

Comments
 (0)