]> git.kianting.info Git - clo/blob - src/index.js
rebuild parser
[clo] / src / index.js
1 "use strict";
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]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9 }) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12 }));
13 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15 }) : function(o, v) {
16 o["default"] = v;
17 });
18 var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
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);
23 return result;
24 };
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"));
31 /**
32 * debug reprensenting
33 */
34 let repr = (x) => { return util.inspect(x, { depth: null }); };
35 /**
36 * concated 2 `tkTree`s
37 * @param x the array to be concated
38 * @param y the item or array to ve concated
39 * @returns concated tkTree array, or thrown error if can't be concated.
40 */
41 function concat(x, y) {
42 if (Array.isArray(x)) {
43 return x.concat(y);
44 }
45 else {
46 throw new Error("the tkTree can't be concated, because it's not an array.");
47 }
48 }
49 function slice(x, index, end) {
50 if (Array.isArray(x)) {
51 return x.slice(index, end);
52 }
53 else {
54 throw new Error("the tkTree can't be concated, because it's not an array.");
55 }
56 }
57 /**
58 * @description
59 * match one token type.
60 *
61 * it returns a function which test if the type of first token of the `remained` part of
62 * the argument of the function is `typ` , if it's true, update the `TokenMatcheePair` wrapped
63 * in `Some`. Otherwise, it returns `None`.
64 * * @param typ : the type to be test.
65 * @returns the updated `TokenMatcheePair` wrapped in `Some(x)` or `None`.
66 */
67 function m1TType(typ) {
68 return (m) => {
69 if (m.remained.length == 0) {
70 return { _tag: "None" };
71 }
72 /**
73 * token to be matched
74 * */
75 const ttbm = m.remained[0];
76 if (ttbm.type == typ) {
77 let new_matched = m.matched.concat(ttbm);
78 let result = {
79 _tag: "Some", value: {
80 matched: new_matched,
81 remained: m.remained.slice(1),
82 ast: ([ttbm]),
83 }
84 };
85 return result;
86 }
87 else {
88 return { _tag: "None" };
89 }
90 };
91 }
92 exports.m1TType = m1TType;
93 ;
94 /**
95 * type int
96 */
97 let tInt = m1TType(tk.TokenType.INT);
98 let tAdd = m1TType(tk.TokenType.I_ADD);
99 let tMul = m1TType(tk.TokenType.I_MUL);
100 node_process_1.argv.forEach((val, index) => {
101 console.log(`${index}=${val}`);
102 });
103 /**
104 * like `m ==> f` in ocaml
105 * @param m matchee wrapped
106 * @param f matching function
107 * @returns wrapped result
108 */
109 function thenDo(m, f) {
110 if (m._tag == "None") {
111 return m;
112 }
113 else {
114 var a = f(m.value);
115 if (a._tag == "Some") {
116 a.value.ast = concat(m.value.ast, a.value.ast);
117 }
118 return a;
119 }
120 }
121 /**
122 * like `f1 | f2` in regex
123 * @param f1 the first tried function
124 * @param f2 the second tried function
125 * @returns wrapped result
126 */
127 function orDo(f1, f2) {
128 return (x) => {
129 let res1 = f1(x);
130 if (res1._tag == "Some") {
131 return res1;
132 }
133 else {
134 let res2 = f2(x);
135 return res2;
136 }
137 };
138 }
139 let midfix = (f, signal) => (x) => {
140 var a = f(x);
141 if (a._tag == "Some") {
142 let ast_head = slice(a.value.ast, 0, a.value.ast.length - 3);
143 let ast_tail = slice(a.value.ast, a.value.ast.length - 3);
144 let new_ast = [ast_tail];
145 a.value.ast = new_ast;
146 console.log("+" + signal + "+" + repr(a));
147 }
148 return a;
149 };
150 /**
151 *
152 * fac1 = int MUL int
153 */
154 //let fac1 = midfix((x : TokenMatcheePair)=>
155 // thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt));
156 let fac1 = (x) => {
157 let a = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt), "fac1")(x);
158 return a;
159 };
160 /**
161 *
162 * fac2 = int MUL int
163 */
164 let fac2 = tInt;
165 /**
166 * fac = fac1 | fac2
167 */
168 let fac = orDo(fac1, fac2);
169 /**
170 *
171 * expr1 = fac ADD fac
172 */
173 let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), tAdd), fac), "expr1");
174 /**
175 * expr2 = fac
176 */
177 let expr2 = fac;
178 /**
179 * expr = expr1 | expr2
180 */
181 let expr = orDo(expr1, expr2);
182 let tokens = tk.tokenize("2+3"); //tk.tokenize(argv[2]);
183 let tokensFiltered = tokens.filter((x) => {
184 return (x.type != tk.TokenType.NL
185 && x.type != tk.TokenType.SP);
186 });
187 let wrappedTokens = tk.toSome({
188 matched: [],
189 remained: tokensFiltered,
190 ast: []
191 });
192 let beta = expr({
193 matched: [],
194 remained: tokensFiltered,
195 ast: []
196 });
197 console.log(repr(wrappedTokens));
198 console.log(repr(beta));