~aleteoryx/uxn

6ab0f101e72331adc13fcdbf03121f249ae387c0 — glenda 8 months ago 4a922c0
nanpa prefix parsing
1 files changed, 54 insertions(+), 29 deletions(-)

M nanpa.tal
M nanpa.tal => nanpa.tal +54 -29
@@ 1,34 1,49 @@
|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1

|0 @State &seenPrefix $1 &numType $1 &number $1 &err $1 &errChar $1
|0 @State &numType $1 &number $2 &err $1 &errChar $1
	@flag $1 @count $1

|0 @NumType &decimal $1 &octal $1 &hexcadecimal $1 &binary
|0 @NumType &decimal $1 &binary $1 &octal $1 &hexcadecimal $1


|100
@on-reset ( -> )
	#ffff testnum
	#efff testnum
	#8fff testnum
	#7fff testnum
	#3fff testnum
	#2710 testnum
	#0fff testnum
	#03e8 testnum
	#01f4 testnum
	#000b testnum
	;on-console
	.Console/type DEI ?{ ;on-prefix }
	.Console/vector DEO2

	BRK

@testnum ( num* -- )
	DUP2 printoctal
	#09 .Console/write DEO
	DUP2 printdecimal
	#09 .Console/write DEO
	SWP printhex printhex
	#0a .Console/write DEO
	JMP2r
@on-console ( -> )
	#63 .Console/write DEO
	.Console/type DEI #04 NEQ ?{ ( ignore args )
		;on-prefix .Console/vector DEO2
	} BRK

@on-prefix ( -> )
	[ LITr 00 ]
	.Console/read DEI 
	DUP #30 NEQ ?{ .NumType/octal .State/numType STZ BRK }

	DUP #62 NEQ ?{ .NumType/binary		!&matched }
	DUP #6f NEQ ?{ .NumType/octal		!&matched }
	DUP #64 NEQ ?{ .NumType/decimal		!&matched }
	    #78 NEQ ?{ .NumType/hexcadecimal	!&matched }
	.State/numType LDZ ( will fallthrough to decimal or octal )
	LITr 01

	&matched
	.State/numType STZ
	;on-digit .Console/vector DEO2

	STHr ?{ BRK } ( passthru if we didn't just match a prefix )

@on-digit ( -> )

	BRK


( NUMBER PRINTING ROUTINES )
@printhex ( num -- )
	#10 DIVk DUP printhexdigit
	MUL SUB printhexdigit


@@ 36,7 51,7 @@

@printdecimal ( num* -- )
	#01 .flag STZ
	#00 STH ( return stack is loop counter )
	[ LITr 00 ]
	#2710 DIV2k DUP2 NIP DUP #00 EQU ?{
		DUP printbcddigit
		#00 .flag STZ


@@ 55,7 70,7 @@

@printoctal ( num* -- )
	#01 .flag STZ
	#00 STH ( return stack is loop counter )
	[ LITr 00 ]
	DUP2 #0f SFT2 NIP #00 EQU ?{
		#31 .Console/write DEO
		#00 .flag STZ


@@ 72,6 87,16 @@
	( end ) ?&loop
	POPr POP2 JMP2r

@printbinary ( num -- )
	[ LITr 00 ]
	&loop
		DUP #07 SFT #30 ADD
		.Console/write DEO
		#10 SFT
	STHr #01 ADD DUP STH #08 NEQ
	( end ) ?&loop
	POPr POP JMP2r

@printhexdigit ( digit -- )
	#0f AND
	DUP #0a LTH ?{ #27 ADD } ( take top part, if 10-15 move to a-f )


@@ 83,16 108,16 @@
	JMP2r



@errors
( STRING DATA )
@Errors
	&prefix		={ "error 20 "at 20 "position 20 }
	&prefix2	={ 20 "with 20 "encoding 20 }
	&encoding	={ "character 20 "not 20 "valid 20 "in 20 "encoding }

@encodings
	&decimal	={ "decimal }
@Encodings
	&binary		={ "binary }
	&octal		={ "octal }
	&decimal	={ "decimal }
	&hexcadecimal	={ "hexcadecimal }
	&binary		={ "binary }
	&tab		=&decimal =&octal =&hexcadecimal =&binary
	&tab		=&decimal =&binary =&octal =&hexcadecimal