]> git.kianting.info Git - clo/blob - src/index.js
20230910 : add basic parser `CONST` rule, and add the grammar rule.
[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 * @description
33 * match one token type.
34 *
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`.
40 */
41 function m1TType(typ) {
42 return (m) => {
43 if (m.remained.length == 0) {
44 return { _tag: "None" };
45 }
46 /**
47 * token to be matched
48 * */
49 const ttbm = m.remained[0];
50 if (ttbm.type == typ) {
51 m.matched.push(ttbm);
52 return {
53 _tag: "Some", value: {
54 matched: m.matched,
55 remained: m.remained.slice(1)
56 }
57 };
58 }
59 else {
60 return { _tag: "None" };
61 }
62 };
63 }
64 exports.m1TType = m1TType;
65 ;
66 let toSome = tk.toSome;
67 let thenDo = tk.thenDo;
68 let orDo = tk.orDo;
69 node_process_1.argv.forEach((val, index) => {
70 console.log(`${index}=${val}`);
71 });
72 let commandInput = node_process_1.argv[2];
73 let commandInputTokenized = tk.tokenize(commandInput);
74 console.log(commandInputTokenized);
75 /**
76 * matchee pair of commandInputTokenized
77 */
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);
83 function tBool(x) {
84 let text = x.remained[0].text;
85 if (text == "true" || text == "false") {
86 return thenDo(toSome(x), m1TType(tk.TokenType.ID));
87 }
88 else {
89 return { _tag: "None" };
90 }
91 }
92 /**
93 * define the right hand side of a grammar
94 * eg. `LHS ::= a + b`
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.
99 */
100 function gramRHS(process, arrange) {
101 return (m) => {
102 let result = process(m);
103 console.log(`result ${result}`);
104 if (result._tag == "None") {
105 return result;
106 }
107 else {
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];
112 });
113 return return_array;
114 }
115 };
116 }
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 }));