]> git.kianting.info Git - clo/blob - src/index.js
add some lexical rules
[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 tSub = m1TType(tk.TokenType.I_SUB);
100 let tMul = m1TType(tk.TokenType.I_MUL);
101 let tDiv = m1TType(tk.TokenType.I_DIV);
102 let tLParen = m1TType(tk.TokenType.L_PAREN);
103 let tRParen = m1TType(tk.TokenType.R_PAREN);
104 node_process_1.argv.forEach((val, index) => {
105 console.log(`${index}=${val}`);
106 });
107 /**
108 * like `m ==> f` in ocaml
109 * @param m matchee wrapped
110 * @param f matching function
111 * @returns wrapped result
112 */
113 function thenDo(m, f) {
114 if (m._tag == "None") {
115 return m;
116 }
117 else {
118 var a = f(m.value);
119 if (a._tag == "Some") {
120 a.value.ast = concat(m.value.ast, a.value.ast);
121 }
122 return a;
123 }
124 }
125 /**
126 * like `f1 | f2` in regex
127 * @param f1 the first tried function
128 * @param f2 the second tried function
129 * @returns wrapped result
130 */
131 function orDo(f1, f2) {
132 return (x) => {
133 let res1 = f1(x);
134 if (res1._tag == "Some") {
135 return res1;
136 }
137 else {
138 let res2 = f2(x);
139 return res2;
140 }
141 };
142 }
143 /**
144 * aux function for midfix operator
145 * @param f function
146 * @param signal the rule name
147 * @returns
148 */
149 let midfix = (f, signal) => (x) => {
150 var a = f(x);
151 if (a._tag == "Some") {
152 let ast_tail = slice(a.value.ast, a.value.ast.length - 3);
153 let new_ast = [ast_tail];
154 a.value.ast = new_ast;
155 console.log("+" + signal + "+" + repr(a));
156 }
157 return a;
158 };
159 let circumfix = (f, signal) => (x) => {
160 var a = f(x);
161 if (a._tag == "Some") {
162 let inner = a.value.ast[a.value.ast.length - 2];
163 console.log("AST====" + repr(a.value.ast));
164 let ast_middle = [inner];
165 let new_ast = [ast_middle];
166 a.value.ast = new_ast;
167 console.log("+" + signal + "+" + repr(a));
168 }
169 return a;
170 };
171 /** fac1 = "(" expr ")" */
172 let fac1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1");
173 let fac2 = tInt;
174 let fac = orDo(fac1, fac2);
175 /**
176 *
177 * term1 = fac (MUL | DIV) fac
178 */
179 let term1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), orDo(tMul, tDiv)), fac), "term1");
180 /**
181 *
182 * term2 = int MUL int
183 */
184 let term2 = fac;
185 /**
186 * term = term1 | term2
187 */
188 let term = orDo(term1, term2);
189 /**
190 *
191 * expr1 = term ADD term
192 */
193 let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), term), orDo(tAdd, tSub)), term), "expr1");
194 /**
195 * expr2 = term
196 */
197 let expr2 = term;
198 /**
199 * expr = expr1 | expr2
200 */
201 let expr = orDo(expr1, expr2);
202 let tokens = tk.tokenize("(4-(3/4))"); //tk.tokenize(argv[2]);
203 let tokensFiltered = tokens.filter((x) => {
204 return (x.type != tk.TokenType.NL
205 && x.type != tk.TokenType.SP);
206 });
207 let wrappedTokens = tk.toSome({
208 matched: [],
209 remained: tokensFiltered,
210 ast: []
211 });
212 let beta = expr({
213 matched: [],
214 remained: tokensFiltered,
215 ast: []
216 });
217 console.log(repr(beta));