correctly parse multi-digit numbers
This commit is contained in:
parent
45a6f2d034
commit
ea6bbc4462
|
@ -40,3 +40,11 @@ export function mustExist<T>(m: T | undefined): T {
|
|||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
export function defaultTo<T>(d: T, m: Maybe<T>): T {
|
||||
if (isJust(m)) {
|
||||
return m[SymbolJust];
|
||||
} else {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isJust, isNothing, Just, just, Maybe, mustExist, Nothing, nothing, SymbolJust } from './Maybe.js';
|
||||
import { defaultTo, isJust, isNothing, Just, just, Maybe, mustExist, Nothing, nothing, SymbolJust } from './Maybe.js';
|
||||
import { map, primStringToList, split } from './Util.js';
|
||||
|
||||
export type Token = {
|
||||
|
@ -155,10 +155,11 @@ export function parseNat(a: Maybe<number>, cs: ReadonlyArray<string>): Result<nu
|
|||
}
|
||||
|
||||
const [x, ...xs] = cs;
|
||||
const t = parseChar(x);
|
||||
const n = parseChar(x);
|
||||
|
||||
if (t.type === 'digit') {
|
||||
return emitCont(t.val, xs)
|
||||
if (n.type === 'digit') {
|
||||
const na = (defaultTo(0, a) * 10) + n.val;
|
||||
return parseNat(just(na), xs);
|
||||
} else {
|
||||
return emitBack(cs);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue