1
0
Fork 0

correctly parse multi-digit numbers

This commit is contained in:
Sean Sube 2022-09-30 22:28:36 -05:00
parent 45a6f2d034
commit ea6bbc4462
2 changed files with 13 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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);
}