11import { WASM } from "latch" ;
22import { readFileSync } from "fs" ;
3+ import Value = WASM . Value ;
34
45interface Cursor {
56 value : number ;
67}
78
8- export function parseResult ( input : string ) : WASM . Value < bigint | number > | undefined {
9+ export function parseResult ( input : string ) : WASM . Value < WASM . Type > | undefined {
910 let cursor = 0 ;
1011 let delta : number = consume ( input , cursor , / \( / d) ;
1112 if ( delta === 0 ) {
@@ -14,27 +15,39 @@ export function parseResult(input: string): WASM.Value<bigint | number> | undefi
1415 cursor += delta ;
1516
1617 delta = consume ( input , cursor , / ^ [ ^ . ) ] * / d) ;
17- const type : WASM . Type = WASM . typing . get ( input . slice ( cursor , cursor + delta ) ) ?? WASM . Type . i64 ;
18+ let type : WASM . Type = WASM . typing . get ( input . slice ( cursor , cursor + delta ) ) ?? WASM . Integer . i64 ;
1819
1920 cursor += delta + consume ( input , cursor + delta ) ;
2021
21- let value ;
2222 delta = consume ( input , cursor , / ^ [ ^ ) ] * / d) ;
23- if ( type === WASM . Type . f32 || type === WASM . Type . f64 ) {
24- value = parseHexFloat ( input . slice ( cursor , cursor + delta ) ) ;
25- } else {
26- value = parseInteger ( input . slice ( cursor , cursor + delta ) ) ;
27- }
23+ return parseValue ( input . slice ( cursor , cursor + delta ) , type ) ;
24+ }
2825
29- if ( value === undefined ) {
30- return value ;
26+ function parseValue ( input : string , type : WASM . Type ) : Value < WASM . Type > | undefined {
27+ let value : number | bigint ;
28+ switch ( type ) {
29+ case WASM . Float . f32 :
30+ case WASM . Float . f64 :
31+ value = parseHexFloat ( input ) ;
32+ break ;
33+ case WASM . Integer . u32 :
34+ case WASM . Integer . i32 :
35+ case WASM . Integer . u64 :
36+ case WASM . Integer . i64 :
37+ value = parseInteger ( input ) ;
38+ break ;
39+ default :
40+ return undefined ;
3141 }
42+ type = typeof value !== 'bigint' && isNaN ( < number > value ) ? WASM . Special . nan
43+ : typeof value !== 'bigint' && < number > value === Infinity ? WASM . Special . infinity
44+ : type ;
3245
3346 return { type, value} ;
3447}
3548
36- export function parseArguments ( input : string , index : Cursor ) : WASM . Value < bigint | number > [ ] {
37- const args : WASM . Value < bigint | number > [ ] = [ ] ;
49+ export function parseArguments ( input : string , index : Cursor ) : WASM . Value < WASM . Type > [ ] {
50+ const args : WASM . Value < WASM . Type > [ ] = [ ] ;
3851
3952 let cursor : number = consume ( input , 0 , / i n v o k e " [ ^ " ] + " / d) ;
4053 while ( cursor < input . length ) {
@@ -45,19 +58,14 @@ export function parseArguments(input: string, index: Cursor): WASM.Value<bigint
4558 cursor += delta ;
4659
4760 delta = consume ( input , cursor , / ^ [ ^ . ) ] * / d) ;
48- const type : WASM . Type = WASM . typing . get ( input . slice ( cursor + delta - 3 , cursor + delta ) ) ?? WASM . Type . i64 ;
61+ const type : WASM . Type = WASM . typing . get ( input . slice ( cursor + delta - 3 , cursor + delta ) ) ?? WASM . Integer . i64 ;
4962
5063 cursor += delta + consume ( input , cursor + delta , / ^ [ ^ ) ] * c o n s t / d) ;
5164 delta = consume ( input , cursor , / ^ [ ^ ) ] * / d) ;
52- let maybe : bigint | number | undefined ;
53- if ( type === WASM . Type . f32 || type === WASM . Type . f64 ) {
54- maybe = parseHexFloat ( input . slice ( cursor , cursor + delta ) ) ;
55- } else {
56- maybe = parseInteger ( input . slice ( cursor , cursor + delta ) ) ;
57- }
65+ let maybe = parseValue ( input . slice ( cursor , cursor + delta ) , type ) ;
5866
5967 if ( maybe !== undefined ) {
60- args . push ( { type , value : maybe } ) ;
68+ args . push ( maybe ) ;
6169 }
6270
6371 cursor += consume ( input , cursor , / \) / d) ;
@@ -131,9 +139,10 @@ function parseHexFloat(input: string): number {
131139 return mantissa * Math . pow ( 2 , exponent ) ;
132140}
133141
134- function parseInteger ( hex : string , bytes : number = 4 ) : bigint {
142+ function parseInteger ( hex : string , bytes : number = 4 ) : bigint | number {
135143 if ( ! hex . includes ( '0x' ) ) {
136- return BigInt ( parseInt ( hex ) ) ;
144+ const n : number = parseInt ( hex ) ;
145+ return typeof n !== 'bigint' && isNaN ( n ) ? NaN : typeof n !== 'bigint' && n === Infinity ? Infinity : BigInt ( n ) ;
137146 }
138147 const mask = BigInt ( parseInt ( '0x80' + '00' . repeat ( bytes - 1 ) , 16 ) ) ;
139148 let integer = BigInt ( parseInt ( hex , 16 ) ) ;
0 commit comments