; LOOKUP - find ASCII string in symbol tree
; Mike Gabrielson   8/10/78
; accepts: BC = address of string to look for, terminated by NUL
;          DE = address of ROOT pointer
;          HL = contents of ROOT pointer (equals address of
;		first node if not grounded)
; returns: HL = address of tree node containing the string
;          carry set if a new node had to be grown
;          else carry cleared (symbol already in tree)
; MEMORY OVERFLOW IS NOT TESTED FOR!
;===============================================================

null	equ	0		; End of string character

	entry	lookup

	extrn	search		; SEARCH is not defined in this assembly
	extrn	first		; External pointer containing address of
				; First byte of available memory for
				; Growing new nodes

lookup:
	call	search		; Symbol already in tree?
	ret	nc		; Yes, all done
	ld	hl,(first)	; No, get address of node about to sprout
	push	hl		; Save address of new node for return
	ex	de,hl		; HL := address of last pointer SEARCHed
	ld	(hl),d		; Replace grounded pointer with address
	inc	hl		; Of new node
	ld	(hl),e
	xor	a		; Clear register A
	ld	(de),a		; And ground the two subtree pointers in the
	inc	de		; New node
	ld	(de),a
	inc	de
	ld	(de),a
	inc	de
	ld	(de),a
	inc	de
savnam:
	ld	a,(bc)		; Get next character from caller's string
	ld	(de),a		; Save in new node
	inc	bc		; Adjust pointers for next character
	inc	de
	cp	null		; Was that the end of the string?
	jr	nz,savnam	; No
	ex	de,hl		; Get address of next available byte into HL
	ld	(first),hl	; Save new FIRST
	pop	hl		; Yes, restore address of new node
	scf			; Indicate new node was grown for caller
	ret

	end

