]>
git.kianting.info Git - uann/blob - index.js
e18ebd5a1c4ff82a83542e067c0c39c7c233d41c
2 var __createBinding
= (this && this.__createBinding
) || (Object
.create
? (function(o
, m
, k
, k2
) {
3 if (k2
=== undefined) k2
= k
;
4 var desc
= Object
.getOwnPropertyDescriptor(m
, k
);
5 if (!desc
|| ("get" in desc
? !m
.__esModule
: desc
.writable
|| desc
.configurable
)) {
6 desc
= { enumerable
: true, get: function() { return m
[k
]; } };
8 Object
.defineProperty(o
, k2
, desc
);
9 }) : (function(o
, m
, k
, k2
) {
10 if (k2
=== undefined) k2
= k
;
13 var __setModuleDefault
= (this && this.__setModuleDefault
) || (Object
.create
? (function(o
, v
) {
14 Object
.defineProperty(o
, "default", { enumerable
: true, value
: v
});
18 var __importStar
= (this && this.__importStar
) || function (mod
) {
19 if (mod
&& mod
.__esModule
) return mod
;
21 if (mod
!= null) for (var k
in mod
) if (k
!== "default" && Object
.prototype.hasOwnProperty
.call(mod
, k
)) __createBinding(result
, mod
, k
);
22 __setModuleDefault(result
, mod
);
25 Object
.defineProperty(exports
, "__esModule", { value
: true });
26 exports
.m1TType
= void 0;
27 var fs
= require('fs');
28 const node_process_1
= require("node:process");
29 const tk
= __importStar(require("./tokenize.js"));
30 const util
= __importStar(require("util"));
33 * match one token type.
35 * it returns a function which test if the type of first token of the `remained` part of
36 * the argument of the function is `typ` , if it's true, update the `TokenMatcheePair` wrapped
37 * in `Some`. Otherwise, it returns `None`.
38 * * @param typ : the type to be test.
39 * @returns the updated `TokenMatcheePair` wrapped in `Some(x)` or `None`.
41 function m1TType(typ
) {
43 if (m
.remained
.length
== 0) {
44 return { _tag
: "None" };
49 const ttbm
= m
.remained
[0];
50 if (ttbm
.type
== typ
) {
53 _tag
: "Some", value
: {
55 remained
: m
.remained
.slice(1)
60 return { _tag
: "None" };
64 exports
.m1TType
= m1TType
;
66 let toSome
= tk
.toSome
;
67 let thenDo
= tk
.thenDo
;
69 node_process_1
.argv
.forEach((val
, index
) => {
70 console
.log(`${index}=${val}`);
72 let commandInput
= node_process_1
.argv
[2];
73 let commandInputTokenized
= tk
.tokenize(commandInput
);
74 console
.log(commandInputTokenized
);
76 * matchee pair of commandInputTokenized
78 let commandTPair
= { matched
: [],
79 remained
: commandInputTokenized
};
80 let tInt
= m1TType(tk
.TokenType
.INT
);
81 let tFlo
= m1TType(tk
.TokenType
.FLO
);
82 let tStr
= m1TType(tk
.TokenType
.STR
);
84 let text
= x
.remained
[0].text
;
85 if (text
== "true" || text
== "false") {
86 return thenDo(toSome(x
), m1TType(tk
.TokenType
.ID
));
89 return { _tag
: "None" };
93 * define the right hand side of a grammar
95 * @param process the right hand side processing : eg. `a + b` in `LHS`
96 * @param arrange define the order (0 starting) of the elements of the result.
97 * ast. : eg. `a + c` is `1 0 2` `(+ a c)`
98 * @returns the processed ast.
100 function gramRHS(process
, arrange
) {
102 let result
= process(m
);
103 console
.log(`result ${result}`);
104 if (result
._tag
== "None") {
108 let matched
= result
.value
.matched
;
109 let return_array
= Array(arrange
.length
);
110 arrange
.forEach((val
, index
) => {
111 return_array
[arrange
[index
]] = matched
[index
];
117 var constParser
= gramRHS((x
) => { return thenDo(toSome(x
), orDo(orDo(orDo(tInt
, tFlo
), tStr
), tBool
)); }, [0]);
118 let tree
= constParser(commandTPair
);
119 console
.log(util
.inspect(tree
, { showHidden
: true, depth
: null }));