X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Findex.js;h=8fc8c3947bf805c304da93614435c5f90fd57147;hb=8d03cc503c747bb974c75d39f8b9c0678a9cc91f;hp=e18ebd5a1c4ff82a83542e067c0c39c7c233d41c;hpb=6f2e788329da7702ea96dc28ae04499917ec8152;p=clo diff --git a/src/index.js b/src/index.js index e18ebd5..8fc8c39 100644 --- a/src/index.js +++ b/src/index.js @@ -23,97 +23,97 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.m1TType = void 0; +exports.tkTreeToSExp = void 0; var fs = require('fs'); -const node_process_1 = require("node:process"); -const tk = __importStar(require("./tokenize.js")); const util = __importStar(require("util")); +const p = __importStar(require("typescript-parsec")); /** - * @description - * match one token type. * - * it returns a function which test if the type of first token of the `remained` part of - * the argument of the function is `typ` , if it's true, update the `TokenMatcheePair` wrapped - * in `Some`. Otherwise, it returns `None`. - * * @param typ : the type to be test. - * @returns the updated `TokenMatcheePair` wrapped in `Some(x)` or `None`. + * # REPRESENTATION */ -function m1TType(typ) { - return (m) => { - if (m.remained.length == 0) { - return { _tag: "None" }; - } - /** - * token to be matched - * */ - const ttbm = m.remained[0]; - if (ttbm.type == typ) { - m.matched.push(ttbm); - return { - _tag: "Some", value: { - matched: m.matched, - remained: m.remained.slice(1) - } - }; - } - else { - return { _tag: "None" }; - } - }; -} -exports.m1TType = m1TType; -; -let toSome = tk.toSome; -let thenDo = tk.thenDo; -let orDo = tk.orDo; -node_process_1.argv.forEach((val, index) => { - console.log(`${index}=${val}`); -}); -let commandInput = node_process_1.argv[2]; -let commandInputTokenized = tk.tokenize(commandInput); -console.log(commandInputTokenized); /** - * matchee pair of commandInputTokenized + * convert a `tkTree` AST to S-expr string + * @param t the `tkTree` + * @returns S-expr String */ -let commandTPair = { matched: [], - remained: commandInputTokenized }; -let tInt = m1TType(tk.TokenType.INT); -let tFlo = m1TType(tk.TokenType.FLO); -let tStr = m1TType(tk.TokenType.STR); -function tBool(x) { - let text = x.remained[0].text; - if (text == "true" || text == "false") { - return thenDo(toSome(x), m1TType(tk.TokenType.ID)); +function tkTreeToSExp(t) { + var str = ""; + if (Array.isArray(t)) { + let strArray = t.map((x) => tkTreeToSExp(x)); + str = "(" + strArray.join(" ") + ")"; } else { - return { _tag: "None" }; + if (t === undefined) { + str = "%undefined"; + } + else { + str = t; + } } + return str; } +exports.tkTreeToSExp = tkTreeToSExp; +/**inspect the inner of the representation. */ +let repr = (x) => { return util.inspect(x, { depth: null }); }; +var TokenKind; +(function (TokenKind) { + TokenKind[TokenKind["Seperator"] = 0] = "Seperator"; + TokenKind[TokenKind["Semicolon"] = 1] = "Semicolon"; + TokenKind[TokenKind["Number"] = 2] = "Number"; + TokenKind[TokenKind["Op"] = 3] = "Op"; + TokenKind[TokenKind["ExprMark"] = 4] = "ExprMark"; + TokenKind[TokenKind["Paren"] = 5] = "Paren"; + TokenKind[TokenKind["SpaceNL"] = 6] = "SpaceNL"; + TokenKind[TokenKind["Id"] = 7] = "Id"; + TokenKind[TokenKind["Str"] = 8] = "Str"; +})(TokenKind || (TokenKind = {})); /** - * define the right hand side of a grammar - * eg. `LHS ::= a + b` - * @param process the right hand side processing : eg. `a + b` in `LHS` - * @param arrange define the order (0 starting) of the elements of the result. - * ast. : eg. `a + c` is `1 0 2` `(+ a c)` - * @returns the processed ast. + * Parsing */ -function gramRHS(process, arrange) { - return (m) => { - let result = process(m); - console.log(`result ${result}`); - if (result._tag == "None") { - return result; - } - else { - let matched = result.value.matched; - let return_array = Array(arrange.length); - arrange.forEach((val, index) => { - return_array[arrange[index]] = matched[index]; - }); - return return_array; - } - }; +const lexer = p.buildLexer([ + [true, /^\d+(\.\d+)?/g, TokenKind.Number], + [true, /^\;/g, TokenKind.Semicolon], + [true, /^[-][-][-]/g, TokenKind.Seperator], + [true, /^[\+\-\*\/\&\|\!\^\<\>\~\=\?]+/g, TokenKind.Op], + [true, /^\@+/g, TokenKind.ExprMark], + [true, /^[()\[\]{}]/g, TokenKind.Paren], + [true, /^["]([\"]|[\\].)*["]/g, TokenKind.Str], + [true, /^[']([\']|[\\].)*[']/g, TokenKind.Str], + [true, /^[()\[\]{}]/g, TokenKind.Paren], + [true, /^[^\s\n\t\r;]+/g, TokenKind.Id], + [false, /^(\s|\n|\r|\t)+/g, TokenKind.SpaceNL] +]); +/** + * + * # TEST + */ +const inputTxt = `import ast; +--- +122`; +const PROG = p.rule(); +const UNIT = p.rule(); +const IMPORTS = p.rule(); +const SEMICOLON = p.rule(); +let doubleMinus = { type: 'Punctuator', value: '--' }; +let doubleMinus2 = p.str('--'); +const TERM = p.rule(); +function applyUnit(value) { + return value.text; +} +function applySemiColon(value) { + return value.text; +} +function applyParts(first, second) { + return ["%clo", first, second[1]]; } -var constParser = gramRHS((x) => { return thenDo(toSome(x), orDo(orDo(orDo(tInt, tFlo), tStr), tBool)); }, [0]); -let tree = constParser(commandTPair); -console.log(util.inspect(tree, { showHidden: true, depth: null })); +PROG.setPattern(p.lrec_sc(IMPORTS, p.seq(p.str('---'), UNIT), applyParts)); +function applyImports(input) { + let importTail = input[1].map(x => x.text); + return ["import"].concat(importTail); +} +; +IMPORTS.setPattern(p.apply(p.seq(p.str('import'), p.rep_sc(p.tok(TokenKind.Id)), SEMICOLON), applyImports)); +SEMICOLON.setPattern(p.apply(p.tok(TokenKind.Semicolon), applySemiColon)); +UNIT.setPattern(p.apply(p.tok(TokenKind.Number), applyUnit)); +let tree = p.expectSingleResult(p.expectEOF(PROG.parse(lexer.parse(inputTxt)))); +console.log("RESULT=" + tkTreeToSExp(tree));