2023-09-04 21:29:23 +08:00
|
|
|
var fs = require('fs');
|
|
|
|
|
2023-09-10 20:25:08 +08:00
|
|
|
import * as tk from './tokenize.js';
|
2023-09-05 23:57:57 +08:00
|
|
|
|
2023-09-04 21:29:23 +08:00
|
|
|
|
|
|
|
|
2023-09-10 20:25:08 +08:00
|
|
|
let b : Array<tk.Token> = tk.tokenize("2+2");
|
2023-09-07 00:01:15 +08:00
|
|
|
|
2023-09-10 20:25:08 +08:00
|
|
|
export interface TokenMatcheePair {
|
|
|
|
matched: tk.Token[]
|
|
|
|
remained: tk.Token[]
|
2023-09-06 00:43:00 +08:00
|
|
|
}
|
2023-09-04 21:29:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* it returns a function which test if the first char of the `remained` part of
|
|
|
|
* the argument of the function is `c`, if it's true, update the `MatchedPair` wrapped
|
|
|
|
* in `Some`. Otherwise, it returns `None`.
|
2023-09-10 20:25:08 +08:00
|
|
|
* * @param t : the char to be test.
|
2023-09-04 21:29:23 +08:00
|
|
|
* @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
|
|
|
|
*/
|
2023-09-10 20:25:08 +08:00
|
|
|
export function match1token(t: tk.Token): (m: TokenMatcheePair) => tk.Maybe<TokenMatcheePair> {
|
|
|
|
return (m: TokenMatcheePair) => {
|
|
|
|
if (m.remained.length == 0) {
|
2023-09-05 23:57:57 +08:00
|
|
|
return { _tag: "None" };
|
|
|
|
}
|
2023-09-10 20:25:08 +08:00
|
|
|
const tokenToBeMatched = m.remained[0];
|
|
|
|
if (tokenToBeMatched === t) {
|
|
|
|
m.matched.push(tokenToBeMatched);
|
|
|
|
return {
|
|
|
|
_tag: "Some", value: {
|
|
|
|
matched: m.matched,
|
|
|
|
remained: m.remained.slice(1)
|
2023-09-07 00:01:15 +08:00
|
|
|
}
|
2023-09-10 20:25:08 +08:00
|
|
|
};
|
2023-09-07 00:01:15 +08:00
|
|
|
}
|
2023-09-10 20:25:08 +08:00
|
|
|
else {
|
|
|
|
return { _tag: "None" };
|
2023-09-07 00:01:15 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-10 20:25:08 +08:00
|
|
|
};
|
2023-09-07 00:01:15 +08:00
|
|
|
|
2023-09-05 23:57:57 +08:00
|
|
|
|
2023-09-07 00:01:15 +08:00
|
|
|
|
2023-09-10 20:25:08 +08:00
|
|
|
let c = tk.toSome(b);
|
|
|
|
console.log(thenDo(c,match1token(tk.tokenize("+")[0])));
|