]>
git.kianting.info Git - uann/blob - index.ts
a8d103b5d550e29e63fe0e707a9b36b3b10bd090
1 var fs
= require('fs');
2 import { argv
} from
'node:process';
3 import * as tk from
'./tokenize.js';
4 import * as util from
'util';
9 type tkTree
= tk
.Token
[] | tk
.Token
11 export interface TokenMatcheePair
{
18 * match one token type.
20 * it returns a function which test if the type of first token of the `remained` part of
21 * the argument of the function is `typ` , if it's true, update the `TokenMatcheePair` wrapped
22 * in `Some`. Otherwise, it returns `None`.
23 * * @param typ : the type to be test.
24 * @returns the updated `TokenMatcheePair` wrapped in `Some(x)` or `None`.
26 export function m1TType(typ
: tk
.TokenType
):
27 (m
: TokenMatcheePair
) => tk
.Maybe
<TokenMatcheePair
> {
28 return (m
: TokenMatcheePair
) => {
29 if (m
.remained
.length
== 0) {
30 return { _tag
: "None" };
35 const ttbm
= m
.remained
[0];
37 if (ttbm
.type == typ
) {
40 _tag
: "Some", value
: {
42 remained
: m
.remained
.slice(1)
47 return { _tag
: "None" };
52 let toSome
= tk
.toSome
;
53 let thenDo
= tk
.thenDo
;
57 argv
.forEach((val
, index
) => {
58 console
.log(`${index}=${val}`);
61 let commandInput
= argv
[2];
62 let commandInputTokenized
= tk
.tokenize(commandInput
);
63 console
.log(commandInputTokenized
);
66 * matchee pair of commandInputTokenized
68 let commandTPair
: TokenMatcheePair
= {matched
:[],
69 remained
: commandInputTokenized
};
72 let tInt
= m1TType(tk
.TokenType
.INT
);
73 let tFlo
= m1TType(tk
.TokenType
.FLO
);
74 let tStr
= m1TType(tk
.TokenType
.STR
);
75 function tBool (x
: TokenMatcheePair
) :tk
.Maybe
<TokenMatcheePair
> {
76 let text
= x
.remained
[0].text
77 if (text
== "true" || text
== "false"){
78 return thenDo(toSome(x
), m1TType(tk
.TokenType
.ID
));
80 return {_tag
: "None"};
85 * define the right hand side of a grammar
87 * @param process the right hand side processing : eg. `a + b` in `LHS`
88 * @param arrange define the order (0 starting) of the elements of the result.
89 * ast. : eg. `a + c` is `1 0 2` `(+ a c)`
90 * @returns the processed ast.
92 function gramRHS (process
: Function, arrange
: number[]){
93 return (m
: TokenMatcheePair
)=>{
95 let result
: tk
.Maybe
<TokenMatcheePair
> = process(m
);
96 console
.log(`result ${result}`)
97 if (result
._tag
== "None"){
101 let matched
= result
.value
.matched
;
102 let return_array
: tkTree
[] = Array(arrange
.length
);
104 arrange
.forEach((val
, index
) => {
105 return_array
[arrange
[index
]] = matched
[index
];
114 * CONST ::= INT | STR | FLO | BOOL
116 var constParser
= gramRHS((x
: TokenMatcheePair
)=>
117 {return thenDo(toSome(x
),orDo(orDo(orDo(tInt
,tFlo
),tStr
),tBool
))}, [0]);
119 let tree
= constParser(commandTPair
);
120 console
.log(util
.inspect(tree
, { showHidden
: true, depth
: null }));