about summary refs log tree commit diff stats
path: root/subx/examples/ex6
Commit message (Expand)AuthorAgeFilesLines
* 4661Kartik Agaram2018-10-041-0/+0
* 4541Kartik Agaram2018-09-111-0/+0
* 4529 - move examples to a sub-directoryKartik Agaram2018-09-011-0/+0
#n38'>38 39 40 41 42 43 44
# Test proper list construction (3 2 1)
# Building the list in proper order: car points to value, cdr points to next pair

# Start with empty list
PUSH_CONST NIL:           # [nil]
PRINT                     # Print nil

# Build (1 . nil)
PUSH_CONST NIL:          # [nil]
PUSH_CONST N:1          # [nil 1]
SWAP                    # [1 nil]
CONS                    # [(1 . nil)]
DUP
PRINT                   # Print (1 . nil)

# Build (2 . (1 . nil))
PUSH_CONST N:2         # [(1.nil) 2]
SWAP                   # [2 (1.nil)]
CONS                   # [(2 . (1.nil))]
DUP
PRINT                  # Print (2 . (1.nil))

# Build (3 . (2 . (1 . nil)))
PUSH_CONST N:3        # [(2.(1.nil)) 3]
SWAP                  # [3 (2.(1.nil))]
CONS                  # [(3 . (2.(1.nil)))]
DUP
PRINT                 # Print full structure

# Test CAR/CDR operations
DUP                   # Keep a copy of the list for later
DUP                   # Another copy for CAR
CAR                   # Get first element (3)
PRINT                 # Should print 3

SWAP                  # Bring back our spare list copy
CDR                   # Get rest of list ((2 . (1 . nil)))
DUP
PRINT                 # Print rest of list

CAR                   # Get first of rest (2)
PRINT                 # Should print 2

HALT