]> git.kianting.info Git - clo/blob - src/index.ts
temp snapshot
[clo] / src / index.ts
1 var fs = require('fs');
2
3 import * as tk from './tokenize.js';
4
5
6
7 let b : Array<tk.Token> = tk.tokenize("2+2");
8
9 export interface TokenMatcheePair {
10 matched: tk.Token[]
11 remained: tk.Token[]
12 }
13
14 /**
15 * @description
16 * it returns a function which test if the first char of the `remained` part of
17 * the argument of the function is `c`, if it's true, update the `MatchedPair` wrapped
18 * in `Some`. Otherwise, it returns `None`.
19 * * @param t : the char to be test.
20 * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
21 */
22 export function match1token(t: tk.Token): (m: TokenMatcheePair) => tk.Maybe<TokenMatcheePair> {
23 return (m: TokenMatcheePair) => {
24 if (m.remained.length == 0) {
25 return { _tag: "None" };
26 }
27 const tokenToBeMatched = m.remained[0];
28 if (tokenToBeMatched === t) {
29 m.matched.push(tokenToBeMatched);
30 return {
31 _tag: "Some", value: {
32 matched: m.matched,
33 remained: m.remained.slice(1)
34 }
35 };
36 }
37 else {
38 return { _tag: "None" };
39 }
40 }
41 };
42
43
44
45 let c = tk.toSome(b);
46 console.log(thenDo(c,match1token(tk.tokenize("+")[0])));