;;; Copyright (c) 2021-2022 Ufasoft  http://ufasoft.com  mailto:support@ufasoft.com,  Sergey Pavlov  mailto:dev@ufasoft.com
;;;
;;; SPDX-License-Identifier: MIT

.SBTTL	Symbol definitions

;.INCLUDE /config.inc/
.IIF EQ C$AMAL	.INCLUDE /macros.inc/

.GLOBL FIXEND

.MACRO	.RAD40	a, b, c
	.WORD	<c * 40 + b> * 40 + a
.ENDM

;; Encode name in RADIX-40/50 when possible
;;    difference with standard PDP-11 RADIX-50:
;;     33 -> '*'
;;     34 -> '-'
;;     35 -> '='
;;
.MACRO .NAM	name
	$good = 1
	.IRPC	c, ^|name|
		.IIF	GT, ''c-'Z, $good = 0
		.IIF	LT, ''c-'*, $good = 0

		.IF	NE ''c-'*
		.IF	NE ''c-'-
.IF NE C$RAD40
		.IF	NE ''c-'+
			.IIF	LT, ''c-'1, $good = 0
			.IF	GT ''c-'2
				.IIF	LT, ''c-'A, $good = 0
			.ENDC

		.ENDC
.IFF
			.IIF	LT, ''c-'0, $good = 0
			.IF	GT ''c-'9
				.IF	NE ''c-'=
					.IIF	LT, ''c-'A, $good = 0
				.ENDC
			.ENDC
.ENDC
		.ENDC
		.ENDC

		.IIF	EQ, $good, .MEXIT
	.ENDR

	.IF NE $good
		$ir = 0
		.NCHR	$cnt, ^|name|
		.IRPC	c, ^|name|
			$cn = 0
			.IIF	GE, ''c-'A, $cn = ''c - 'A + 1
			.IIF	EQ, ''c-'*, $cn = 33
			.IIF	EQ, ''c-'-, $cn = 34
			.IF NE C$RAD40
				.IIF	EQ, ''c-'+, $cn = 35
				.IIF	EQ, $cn, $cn = ''c-'1 + 36
			.IFF
				.IIF	EQ, ''c-'=, $cn = 35
				.IIF	EQ, $cn, $cn = ''c-'0 + 36
			.ENDC

			.IF EQ $ir - 2
				.IF NE C$RAD40
					.IIF	NE, $cnt - 1, $c0 = $c0	+ 100000	; set flag for non-last-word
                               		.RAD40	<$c0><$c1><$cn>
				.IFF
                               		.RAD50	<$c0><$c1><$cn>
				.ENDC
				$ir = 0
			.IFF
				.IIF	EQ, $ir - 0, $c0 = $cn
				.IIF	EQ, $ir - 1, $c1 = $cn
				$ir = $ir + 1
			.ENDC
			$cnt = $cnt - 1
		.ENDR

		.IF NE C$RAD40
			.IIF	EQ, $ir - 1, .RAD40	<$c0><0><0>
			.IIF	EQ, $ir - 2, .RAD40	<$c0><$c1><0>
		.IFF
			.IIF	EQ, $ir - 0, .WORD	0
			.IIF	EQ, $ir - 1, .RAD50	<0><0><$c0>
			.IIF	EQ, $ir - 2, .RAD50	<0><$c0><$c1>
		.ENDC
	.IFF
       		.WORD	0
       		.NCHR	$nlen, ^|name|
		.BYTE	$nlen
       		.ASCII	"name"
	.ENDC
.ENDM

;; Define symbol presentation
.MACRO	.SYM	subr, name, val, slab, $setf, var
	.IF	NB $setf
		.GLOBL	$setf
		.WORD	$setf, 0
		.WORD	V.SETF
		.WORD	.-6
		PLIST	= . - 4
	.IFF
		PLIST	= 0
	.ENDC

	.IF NB val
		.WORD	V.SPEC, . + 2, V.T, PLIST
		.WORD	V.SYVA, . + 2, val, . - 16
		PLIST	= . - 10
	.ENDC

	.IF	NB slab
		slab	= . + 1
		.GLOBL	slab
	.ENDC
	.WORD	PLIST

	.IIF	DIF, subr 0, .GLOBL	subr
       	.WORD	subr, S.PREV
       	S.PREV	= . - 5

	.IF	B ^|name|
		.NAM	<subr>
	.IFF
		.NAM	^|name|
	.ENDC

	ALGN4
.ENDM ; .SYM

;; Define Keyword presentation
.MACRO	.KEYW	name, slab
	PLIST	= 0

	.WORD	V.SPEC, . + 2, V.T, PLIST
	.WORD	V.SYVA, . + 2, slab, . - 16
	PLIST	= . - 10

	.IF	NB slab
		slab	= . + 1
		.GLOBL	slab
	.ENDC
	.WORD	PLIST

	.IIF	DIF, subr 0, .GLOBL	subr
       	.WORD	subr, K.PREV
       	K.PREV	= . - 5

	.NAM	^|name|

	ALGN4
.ENDM ; .SYM

.IF NE C$BK11			; system area in 042600
.ASECT
	. = 42604
	.WORD	ASTKUP		; Start address

	. = 42700 		; End of system area
.ENDC


.IF NE C$AMAL
	. = . + 37 & 177740
	HEAP:
.IFF
	.ASECT
	.	= HEAP
.ENDC ; C$AMAL

S.PREV	= 0
K.PREV	= 0
;;--- T must be second in memory after symbol with lame length==4, to define Bit.2 of V.T. Used by AND/OR
	.SYM	QUIT,,, V.QUIT		; must be first, referenced in P.SYMB
	.SYM	0, <T>, V.T, V.T
;;----------------------
	;; Rarely used symbols at the end of OBLIST for faster parsing
	.SYM	QUOTE,,, V.QUOT
	.SYM	TAGBODY,,, V.TAGB
	.SYM	GO,,, V.GO
	.SYM	BLOCK,,, V.BLOCK
	.SYM	RETUFR, <RETURN-FROM>
	.SYM	RETU, <RETURN>
	.SYM	LVER, <LISP-IMPLEMENTATION-VERSION>
	.SYM	LTYP, <LISP-IMPLEMENTATION-TYPE>
	.SYM	MVER, <MACHINE-VERSION>
	.SYM	MTYP, <MACHINE-TYPE>
	.SYM	CLOSE
.IF NE C$RT11
	.SYM	GTUTIM, <GET-UNIVERSAL-TIME>
	.SYM	ECUTIM, <ENCODE-UNIVERSAL-TIME>
	.SYM	GDCTIM, <GET-DECODED-TIME>
	.SYM	DCUTIM, <DECODE-UNIVERSAL-TIME>

	.SYM	SVER, <SOFTWARE-VERSION>
	.SYM	STYP, <SOFTWARE-TYPE>
	.SYM	OPEN
	.SYM	DELETF, <DELETE-FILE>
	.SYM	PROBEF, <PROBE-FILE>
	.SYM	RENAMF, <RENAME-FILE>
	.SYM	FLENGTH, <FILE-LENGTH>
	.SYM	FPOSIT, <FILE-POSITION>
	.KEYW	<DIRECTION>, KW.DIR
	.KEYW	<OUTPUT>, KW.OUT				; This is the last symbol in the KWLIST

	.SYM	0, <*ARGS*>, 0, V.ARGS

	.IIF NE C$FEXT	.SYM	FDATE, <FILE-WRITE-DATE>
.ENDC ; C$RT11
.IF NE C$MDAR
	.SYM	0, <ARRAY-RANK-LIMIT>, V.8
	.SYM	0, <ARRAY-DIMENSION-LIMIT>, FIXEND-1
	.SYM	0, <ARRAY-TOTAL-SIZE-LIMIT>, FIXEND-1
	.SYM	ARRAYP
	.SYM	ARTSIZ, <ARRAY-TOTAL-SIZE>
	.SYM	ARMI, <ARRAY-ROW-MAJOR-INDEX>
	.SYM	RMAR, <ROW-MAJOR-AREF>,,, X.RMAR
	.SYM	AREF,,,, X.AREF
	.SYM	ARRANK, <ARRAY-RANK>
	.SYM	ARDIM, <ARRAY-DIMENSION>
	.SYM	ARDIMS, <ARRAY-DIMENSIONS>
	.SYM	ARIBP, <ARRAY-IN-BOUNDS-P>
.ENDC ; C$MDAR
	.SYM	MKAR	, <MAKE-ARRAY>
	.SYM	0, <PI>, S.PI+2, V.PI
	.SYM	SINH
	.SYM	COSH
	.SYM	TANH
	.SYM	ATAN
	.SYM	ASIN
	.SYM	ACOS
	.SYM	SIN
	.SYM	COS
	.SYM	TAN
	.SYM	EXPT
	.SYM	EXP
	.SYM	LOG
	.SYM	ISQRT
	.SYM	SQRT
	;;---------NON COMMON LISP SYMBOLS------
	.SYM	LISTIFY
	.SYM	SAVE, <SAVE>
	;;------ BK-0010 SPECIFIC--------
	.SYM	PEEK
	.SYM	POKE
	.SYM	INKEY
	.SYM	EXTROOM
	;;-------------------------------
	.SYM	ROOM
	.SYM	LOAD
	.SYM	LOGAC1, <LOGANDC1>
	.SYM	LOGAC2, <LOGANDC2>
	.SYM	LOGOC1, <LOGORC1>
	.SYM	LOGOC2, <LOGORC2>
	.SYM	LOGNAND
	.SYM	LOGTEST
	.SYM	LOGAND
	.SYM	LOGIOR
	.SYM	LOGXOR
	.SYM	LOGEQV
	.SYM	LOGNOT
	.SYM	LOGNOR
	.SYM	YORNP, <Y-OR-N-P>
	.SYM	ASH
	.SYM	0, <*STANDARD-INPUT*>, STDIN+1, V.STDIN				; also *STANDARD-OUTPUT*
	.SYM	0, <*STANDARD-OUTPUT*>, STDIN+1, V.STDOUT
	.SYM	0, <INTERNAL-TIME-UNITS-PER-SECOND>, 366.+FIXOFF, V.FREQ		; 3 MHz / 128 / 16 / 14
	.SYM	GETTIM, <GET-INTERNAL-REAL-TIME>
	.SYM	SLEEP
	.SYM	ROTATEF

;;-------------- Used by INTERP. Order does matter
	.SYM	L.REST, <&REST>,, V.REST
.IF NE C$FULL
	.SYM	L.KEY, <&KEY>,, V.KEY
	.SYM	L.AUX, <&AUX>,, V.AUX
.ENDC ; C$FULL
	.SYM	L.OPT, <&OPTIONAL>,, V.OPT
;;-------------------
	.SYM	CPALIS, <COPY-ALIST>
	.SYM	CPLIST, <COPY-LIST>
	.SYM	CPTREE, <COPY-TREE>
	.SYM	CPSEQ, <COPY-SEQ>
	.SYM	REDUCE
	.SYM	REM
	.SYM	SUBST
	.SYM	SETDIF, <SET-DIFFERENCE>
	.SYM	INTERSECTION
	.SYM	SUBSETP
	.SYM	NREVERSE
	.SYM	REVERSE
	.SYM	ADJOIN
	.SYM	COERCE
	.SYM	MEMBER
	.SYM	REMOVE
	.SYM	RASSOC
	.SYM	ASSOC
	.SYM	UNION
	.SYM	TAILP
	.SYM	SORT
	.SYM	TYPEOF, <TYPE-OF>
;;-------------- Used by TYPE-OF. Order does matter: NULL is the middle
.IIF NE C$FILE, .SYM	0, <FILE-STREAM>,, V.FILE
.IIF NE C$MDAR, .SYM	0, <ARRAY>,, V.AR
	.SYM	0, <SPECIAL-OPERATOR>,, V.SPOP
	.SYM	FUNCTION,,, V.FUN
	.SYM	0, <SYSTEM-FUNCTION>,, V.BIFU
	.SYM	VECTOR,,, V.VEC
	.SYM	STRING,,, V.STR
	.SYM	0, <STREAM>,, V.STM
	.SYM	FLOAT,,, V.FLT
	.SYM	0, <SYMBOL>,, V.SYM
	.SYM	CONS,,, V.CONS
	.SYM	CHARACTER,,, V.CHAR
	.SYM	0, <INTEGER>,, V.INT
	.SYM	NULL,,, V.NULL

	.SYM	SYVAL, <SYMBOL-VALUE>,,V.SYVA, SET
;;--------------
	.SYM	LIST,,, V.LIST
	.SYM	LISTA, <LIST*>
	.SYM	NUMP, <NUMBERP>
	.SYM	SPECOP, <SPECIAL-OPERATOR-P>
	.SYM	SVECP, <SIMPLE-VECTOR-P>
	.SYM	VECP, <VECTORP>
	.SYM	FUNP, <FUNCTIONP>
	.SYM	INTP, <INTEGERP>
	.SYM	SYMP, <SYMBOLP>
	.SYM	STRP, <STRINGP>
	.SYM	FLOATP
	.SYM	REALP
	.SYM	STREQ, <STRING=>
	.SYM	STRNE, <STRING/=>
	.SYM	STREQU, <STRING-EQUAL>
	.SYM	SYFUN, <SYMBOL-FUNCTION>,,, X.SYFU
	.SYM	SET
	.SYM	0, <SPECIAL>,,V.SPEC
	.SYM	SYPLS, <SYMBOL-PLIST>
	.SYM	SYNAM, <SYMBOL-NAME>
	.SYM	FINSY, <FIND-SYMBOL>
	.SYM	0, <*RANDOM-STATE*>, C.RND, V.RNDS
	.SYM	MKRNDS, <MAKE-RANDOM-STATE>
	.SYM	RANDOM
	.SYM	GRACHP, <GRAPHIC-CHAR-P>
	.SYM	ALNUMP, <ALPHANUMERICP>
	.SYM	ALCHP, <ALPHA-CHAR-P>
	.SYM	DIGCHP, <DIGIT-CHAR-P>
	.SYM	DIGICH, <DIGIT-CHAR>
	.SYM	CHARP, <CHARACTERP>
	.SYM	CHNAME, <CHAR-NAME>
	.SYM	NAMECH, <NAME-CHAR>
	.SYM	CODECH, <CODE-CHAR>
	.SYM	CHCODE, <CHAR-CODE>
	.SYM	CHCODE, <CHAR-INT>
	.SYM	NSTRDN, <NSTRING-DOWNCASE>
	.SYM	STRDN, <STRING-DOWNCASE>
	.SYM	NSTRUP, <NSTRING-UPCASE>
	.SYM	STRUP, <STRING-UPCASE>
	.SYM	MKSEQ, <MAKE-SEQUENCE>
	.SYM	CHRDN, <CHAR-DOWNCASE>
	.SYM	CHRUP, <CHAR-UPCASE>
	.SYM	CHEQ, <CHAR=>
	.SYM	CHEQUA, <CHAR-EQUAL>
	.SYM	MKLIST, <MAKE-LIST>
	.SYM	MKSTR, <MAKE-STRING>
	.SYM	MKSYM, <MAKE-SYMBOL>
	.SYM	FDEFINITION
	.SYM	FMAKUNBOUND
	.SYM	MAKUNBOUND
	.SYM	PAIRLIS
	.SYM	BUTLAST
	.SYM	REMPROP
	.SYM	FBOUNDP
	.SYM	BOUNDP
	.SYM	NTHCDR
	.SYM	NTH,,,, X.NTH
	.SYM	FIRST,,,, X.CAR
	.SYM	SECOND,,,, X.SECOND
	.SYM	THIRD,,,, X.THIRD
.IF NE C$FULL
	.SYM	FOURTH,,,, X.FOURTH
	.SYM	FIFTH,,,, X.FIFTH
	.SYM	SIXTH,,,, X.SIXTH
	.SYM	SEVENTH,,,, X.SEVENTH
	.SYM	EIGHTH,,,, X.EIGTH
	.SYM	NINTH,,,, X.NINTH
	.SYM	TENTH,,,, X.TENTH
	.SYM	CAAAR
	.SYM	CAADR
	.SYM	REST,,,, X.CDR

	.SYM	FRADIX, <FLOAT-RADIX>
	.SYM	SCALEF, <SCALE-FLOAT>
	.SYM	0, <LAMBDA-PARAMETERS-LIMIT>, 50.+FIXOFF
	.SYM	0, <CALL-ARGUMENTS-LIMIT>, 50.+FIXOFF
	.SYM	KEYWORDP
	.SYM	0, <KEYWORD>,, V.KEYW
	.SYM	TRACE
	.SYM	UNTRACE
	.SYM	TRACER, <T*R>,, V.TRC
L.TRAC::
	.WORD	V.LAMB, L.TRAC+4, 1$, L.TRAC+10, 2$, 0			; (LAMBDA (&REST VALUES) (T*R VALUES #\A #\B))
1$:	.WORD	V.REST, 1$+4, V.VALS, 0					; (&REST VALUES)
2$:	.WORD	V.TRC, 2$+4, V.VALS, 2$+10, V.C.A, 2$+14, V.C.B, 0	; (T*R VALUES #\A #\B)
E.TRAC:
.ENDC ; C$FULL
	.SYM	ACONS
	.SYM	TAILP
	.SYM	LDIFF
	.SYM	SIGNUM
	.SYM	MINUSP
	.SYM	PLUSP
	.SYM	GETF
	.SYM	GET,,,, X.GET
	.SYM	TRUNCATE
	.SYM	FLOOR
	.SYM	EVENP
	.SYM	ODDP
	.SYM	ABS
	.SYM	INCR, <1+>
	.SYM	DECR, <1->
	.SYM	MIN
	.SYM	MAX
	.SYM	ELT,,,, X.ELT
	.SYM	CHAR,,,, X.CHAR
	.SYM	SVREF,,,, X.SVRE
	.SYM	NCONC
	.SYM	FUNCALL,,, V.FUNCALL
	.SYM	APPLY
	.SYM	EVAL
	.SYM	IDENTITY
	.SYM	LSTLEN, <LIST-LENGTH>
	.SYM	APPEND
	.SYM	LENGTH
	.SYM	FILL
	.SYM	R.DELI, <READ-DELIMITED-LIST>
	.SYM	UNR.CH, <UNREAD-CHAR>
	.SYM	PEEKCH, <PEEK-CHAR>
	.SYM	R.CHAR, <READ-CHAR>
	.SYM	READ
	.SYM	0, <*PRINT-ESCAPE*>, V.T, V.PESC
	.SYM	0, <*PRINT-RADIX*>, 0, V.PRAD
	.SYM	0, <*PRINT-BASE*>, V.10, V.PBAS
	.SYM	WRITE
	.SYM	W.STR, <WRITE-STRING>
	.SYM	W.CHAR, <WRITE-CHAR>
	.SYM	W.LINE, <WRITE-LINE>
	.SYM	FRESHL, <FRESH-LINE>
	.SYM	MKSOS,	<MAKE-STRING-OUTPUT-STREAM>
	.SYM	MKSSTM, <MAKE-STRING-INPUT-STREAM>
	.SYM	GETOSS, <GET-OUTPUT-STREAM-STRING>
	.SYM	ISTMP, <INPUT-STREAM-P>
	.SYM	STMP, <STREAMP>
	.SYM	OPENP, <OPEN-STREAM-P>
	.SYM	FORMAT,,, V.FORM
	.SYM	0, <*GENSYM-COUNTER*>, V.0, V.GCNT
	.SYM	GENSYM
	.SYM	UNINTERN
	.SYM	INTERN
	.SYM	MAPC
	.SYM	MAPCAR
	.SYM	ERROR,,, V.ERR
;	.SYM	RETUR, <RETURN>
	.SYM	DEFVAR
	.SYM	DEFPARAMETER
	.SYM	PROG1
	.SYM	PROG2
	.SYM	PROGV
	.SYM	PROGN,,,V.PROGN
	.SYM	TIME
	.SYM	TERPRI
	.SYM	PRIN1
	.SYM	PRINC
	.SYM	PRINT
	.SYM	DOLIST
	.SYM	DOTIMES
	.SYM	DOA, <DO*>
	.SYM	DO
	.SYM	INCF
	.SYM	DECF
	.SYM	S.PUSH, <PUSH>
	.SYM	S.POP, <POP>
	.SYM	CHRNE, <CHAR/=>
	;;------------ Order does matter. Used in DO/DO*
	.SYM	SETQ,,, V.SETQ
	.SYM	LETA, <LET*>,, V.LETA
	.SYM	PSETQ,,, V.PSETQ
	.SYM	LET,,, V.LET
	;;--------------------------
	.SYM	SETF,,, V.SETF
	.SYM	ENDP
	.SYM	ATOM
	.SYM	ZEROP
	.SYM	NOT
	.SYM	CONSP
	.SYM	LISTP
	.SYM	LAMBDA,,,V.LAMB
	.SYM	DEFUN
	.SYM	NUMLT, ^/</
	.SYM	NUMGT, ^/>/
	.SYM	NUMEQ, <=>
	.SYM	NUMNE, </=>
	.SYM	NUMLE, ^/<=/
	.SYM	NUMGE, ^/>=/
	.SYM	COND
	.SYM	AND
	.SYM	OR
	.SYM	EQUALP
	.SYM	EQUAL
	.SYM	EQL
	.SYM	EQ
	.SYM	WHEN
	.SYM	UNLESS,,, V.UNLESS
	.SYM	IF,,, V.IF
	.SYM	RPLACA
	.SYM	RPLACD
	.SYM	CAAR
	.SYM	CADR
	.SYM	CDAR
	.SYM	CDDR
	.SYM	CDR,,,, X.CDR
	.SYM	CAR,,,, X.CAR
	;; -------------------------------------------
	;; Order of these symbols does matter for REPL
	.SYM	S.SUB, <->, 0, V.S
	.SYM	S.ADD, <+>, 0, V.P
.IIF NE C$FULL	.SYM	V.U, <++>, 0, V.PP
.IIF NE C$FULL	.SYM	V.U, <+++>, 0, V.PPP
	.SYM	S.MUL, <*>, 0, V.M
.IIF NE C$FULL	.SYM	V.U, <**>, 0, V.MM
.IIF NE C$FULL	.SYM	V.U, <***>, 0, V.MMM
	.SYM	S.DIV, </>, 0, V.D
.IF NE C$MVAL
	.SYM	V.U, <//>, 0, V.DD
	.SYM	V.U, <///>, 0, V.DDD
	;;--------------------------------------------
	.SYM	VALUES,,, V.VALS
	.SYM	VALLS, <VALUES-LIST>
	.SYM	MVALLS, <MULTIPLE-VALUE-LIST>
	.SYM	NTHVAL, <NTH-VALUE>
	.SYM	0, <MULTIPLE-VALUES-LIMIT>, VALLIM+FIXOFF
.ENDC
	.SYM	LAST,,, V.LAST			; This is the last symbol in the OBLIST

S.END:

.IF NE C$FULL
;; List (/// // / *** ** * +++ ++ +) for rotating in REPL
L.PREV::
	.WORD	V.P, 0, V.PP, L.PREV, V.PPP, L.PREV+4, V.M, L.PREV+10, V.MM, L.PREV+14
	.WORD	V.MMM, L.PREV+20
.IIF NE C$MVAL,	.WORD	V.D, L.PREV+24, V.DD, L.PREV+30, V.DDD, L.PREV+34
	.WORD	V.S, . - 6
A.PREV::
.ENDC ; C$FULL

S.PI::	.FLT4	3.1415926535897932384626433832795
S.RND:	.FLT4	0.94360820238906328860650774004171		; Initial *random-state*'S III
S.ZERO:	.WORD	0, 0, 0, 0
C.RND:	.WORD	S.RND+2, S.ZERO+2				; (CONS S.RND S.ZERO)
;S.INI::	.BYTE	<E.INI - S.INI - 1>
;E.INI::
	ALGN4
;S.ISTM::	.WORD	V.STM, S.INI + 3, 0, E.INI - S.INI - 1		; Stream for init load
STDIN::	.WORD	V.STM, V.0, 0, V.C.NL


ALGN4
HEAPFRE::
	.WORD	HIMEM-.			; Size
	.WORD	0                       ; Next

.IIF EQ C$RT11	.INCLUDE /init.mac/

.IIF NE C$RT11	HIMEM:
A.END::


.IIF EQ C$AMAL	.END
