From: Tan Kian-ting Date: Thu, 21 Sep 2023 16:08:53 +0000 (+0800) Subject: add some lexical rules X-Git-Url: https://git.kianting.info/?a=commitdiff_plain;h=5f0943539e27b3e0d08e70559ea39017e6df330e;p=clo add some lexical rules --- diff --git a/src/index.js b/src/index.js index bd62a48..0e3686c 100644 --- a/src/index.js +++ b/src/index.js @@ -96,7 +96,11 @@ exports.m1TType = m1TType; */ let tInt = m1TType(tk.TokenType.INT); let tAdd = m1TType(tk.TokenType.I_ADD); +let tSub = m1TType(tk.TokenType.I_SUB); let tMul = m1TType(tk.TokenType.I_MUL); +let tDiv = m1TType(tk.TokenType.I_DIV); +let tLParen = m1TType(tk.TokenType.L_PAREN); +let tRParen = m1TType(tk.TokenType.R_PAREN); node_process_1.argv.forEach((val, index) => { console.log(`${index}=${val}`); }); @@ -136,10 +140,15 @@ function orDo(f1, f2) { } }; } +/** + * aux function for midfix operator + * @param f function + * @param signal the rule name + * @returns + */ let midfix = (f, signal) => (x) => { var a = f(x); if (a._tag == "Some") { - let ast_head = slice(a.value.ast, 0, a.value.ast.length - 3); let ast_tail = slice(a.value.ast, a.value.ast.length - 3); let new_ast = [ast_tail]; a.value.ast = new_ast; @@ -147,39 +156,50 @@ let midfix = (f, signal) => (x) => { } return a; }; +let circumfix = (f, signal) => (x) => { + var a = f(x); + if (a._tag == "Some") { + let inner = a.value.ast[a.value.ast.length - 2]; + console.log("AST====" + repr(a.value.ast)); + let ast_middle = [inner]; + let new_ast = [ast_middle]; + a.value.ast = new_ast; + console.log("+" + signal + "+" + repr(a)); + } + return a; +}; +/** fac1 = "(" expr ")" */ +let fac1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); +let fac2 = tInt; +let fac = orDo(fac1, fac2); /** * - * fac1 = int MUL int + * term1 = fac (MUL | DIV) fac */ -//let fac1 = midfix((x : TokenMatcheePair)=> -// thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt)); -let fac1 = (x) => { - let a = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt), "fac1")(x); - return a; -}; +let term1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), orDo(tMul, tDiv)), fac), "term1"); /** * - * fac2 = int MUL int + * term2 = int MUL int */ -let fac2 = tInt; +let term2 = fac; /** - * fac = fac1 | fac2 + * term = term1 | term2 */ -let fac = orDo(fac1, fac2); +let term = orDo(term1, term2); /** * - * expr1 = fac ADD fac + * expr1 = term ADD term */ -let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), tAdd), fac), "expr1"); +let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), term), orDo(tAdd, tSub)), term), "expr1"); /** - * expr2 = fac + * expr2 = term */ -let expr2 = fac; +let expr2 = term; /** * expr = expr1 | expr2 */ let expr = orDo(expr1, expr2); -let tokens = tk.tokenize("2+3"); //tk.tokenize(argv[2]); +let tokens = tk.tokenize("(4-(3/4))"); //tk.tokenize(argv[2]); let tokensFiltered = tokens.filter((x) => { return (x.type != tk.TokenType.NL && x.type != tk.TokenType.SP); @@ -194,5 +214,4 @@ let beta = expr({ remained: tokensFiltered, ast: [] }); -console.log(repr(wrappedTokens)); console.log(repr(beta)); diff --git a/src/index.ts b/src/index.ts index 4c1ce91..acaa74e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,7 +95,12 @@ export function m1TType(typ: tk.TokenType): */ let tInt = m1TType(tk.TokenType.INT); let tAdd = m1TType(tk.TokenType.I_ADD); +let tSub = m1TType(tk.TokenType.I_SUB); let tMul = m1TType(tk.TokenType.I_MUL); +let tDiv = m1TType(tk.TokenType.I_DIV); +let tLParen = m1TType(tk.TokenType.L_PAREN); +let tRParen = m1TType(tk.TokenType.R_PAREN); + argv.forEach((val, index) => { @@ -141,10 +146,15 @@ function orDo(f1 : Function, f2 : Function){ } +/** + * aux function for midfix operator + * @param f function + * @param signal the rule name + * @returns + */ let midfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{ var a = f(x); if (a._tag == "Some"){ - let ast_head : tkTree[] = slice(a.value.ast,0,a.value.ast.length-3); let ast_tail : tkTree[] = slice(a.value.ast,a.value.ast.length-3); let new_ast = [ast_tail]; a.value.ast = new_ast; @@ -156,43 +166,63 @@ let midfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{ return a; } -/** - * - * fac1 = int MUL int - */ -//let fac1 = midfix((x : TokenMatcheePair)=> -// thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt)); +let circumfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{ + var a = f(x); + if (a._tag == "Some"){ + let inner = a.value.ast[a.value.ast.length-2]; + console.log("AST===="+repr(a.value.ast)); + let ast_middle : tkTree[] = [inner]; + let new_ast = [ast_middle]; + a.value.ast = new_ast; -let fac1 = (x : TokenMatcheePair) => { - let a = midfix((x : TokenMatcheePair)=> - thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt), "fac1")(x); + console.log("+"+signal+"+"+repr(a)); + + } return a; } +/** fac1 = "(" expr ")" */ +let fac1 = circumfix((x : TokenMatcheePair)=> +thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); + +let fac2 = tInt; + +let fac = orDo(fac1, fac2); + + + +/** + * + * term1 = fac (MUL | DIV) fac + */ + +let term1 = midfix((x : TokenMatcheePair)=> + thenDo(thenDo(thenDo(tk.toSome(x), fac), orDo(tMul,tDiv)), fac), "term1"); + /** * - * fac2 = int MUL int + * term2 = int MUL int */ -let fac2 = tInt; +let term2 = fac; /** - * fac = fac1 | fac2 + * term = term1 | term2 */ -let fac = orDo(fac1, fac2); +let term = orDo(term1, term2); /** * - * expr1 = fac ADD fac + * expr1 = term ADD term */ let expr1 = midfix((x : TokenMatcheePair)=> - thenDo(thenDo(thenDo(tk.toSome(x), fac), tAdd), fac), "expr1"); + thenDo(thenDo(thenDo(tk.toSome(x), term), orDo(tAdd,tSub)), term), "expr1"); /** - * expr2 = fac + * expr2 = term */ -let expr2 = fac; +let expr2 = term; /** * expr = expr1 | expr2 @@ -202,7 +232,7 @@ let expr = orDo(expr1, expr2); -let tokens = tk.tokenize("2+3*4");//tk.tokenize(argv[2]); +let tokens = tk.tokenize("(4-(3/4))");//tk.tokenize(argv[2]); let tokensFiltered = tokens.filter( (x)=>{return (x.type != tk.TokenType.NL && x.type != tk.TokenType.SP)}); @@ -218,7 +248,6 @@ let beta = expr({ remained : tokensFiltered, ast : []}); -console.log(repr(wrappedTokens)); console.log(repr(beta));