add some lexical rules

This commit is contained in:
Tan, Kian-ting 2023-09-22 00:08:53 +08:00
parent 757abafe4c
commit 5f0943539e
2 changed files with 90 additions and 42 deletions

View file

@ -96,7 +96,11 @@ exports.m1TType = m1TType;
*/ */
let tInt = m1TType(tk.TokenType.INT); let tInt = m1TType(tk.TokenType.INT);
let tAdd = m1TType(tk.TokenType.I_ADD); let tAdd = m1TType(tk.TokenType.I_ADD);
let tSub = m1TType(tk.TokenType.I_SUB);
let tMul = m1TType(tk.TokenType.I_MUL); 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) => { node_process_1.argv.forEach((val, index) => {
console.log(`${index}=${val}`); 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) => { let midfix = (f, signal) => (x) => {
var a = f(x); var a = f(x);
if (a._tag == "Some") { 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 ast_tail = slice(a.value.ast, a.value.ast.length - 3);
let new_ast = [ast_tail]; let new_ast = [ast_tail];
a.value.ast = new_ast; a.value.ast = new_ast;
@ -147,39 +156,50 @@ let midfix = (f, signal) => (x) => {
} }
return a; return a;
}; };
/** let circumfix = (f, signal) => (x) => {
* var a = f(x);
* fac1 = int MUL int if (a._tag == "Some") {
*/ let inner = a.value.ast[a.value.ast.length - 2];
//let fac1 = midfix((x : TokenMatcheePair)=> console.log("AST====" + repr(a.value.ast));
// thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt)); let ast_middle = [inner];
let fac1 = (x) => { let new_ast = [ast_middle];
let a = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt), "fac1")(x); a.value.ast = new_ast;
console.log("+" + signal + "+" + repr(a));
}
return a; return a;
}; };
/** /** fac1 = "(" expr ")" */
* let fac1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1");
* fac2 = int MUL int
*/
let fac2 = tInt; let fac2 = tInt;
/**
* fac = fac1 | fac2
*/
let fac = orDo(fac1, fac2); let fac = orDo(fac1, fac2);
/** /**
* *
* expr1 = fac ADD fac * term1 = fac (MUL | DIV) fac
*/ */
let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), tAdd), fac), "expr1"); let term1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), fac), orDo(tMul, tDiv)), fac), "term1");
/** /**
* expr2 = fac *
* term2 = int MUL int
*/ */
let expr2 = fac; let term2 = fac;
/**
* term = term1 | term2
*/
let term = orDo(term1, term2);
/**
*
* expr1 = term ADD term
*/
let expr1 = midfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), term), orDo(tAdd, tSub)), term), "expr1");
/**
* expr2 = term
*/
let expr2 = term;
/** /**
* expr = expr1 | expr2 * expr = expr1 | expr2
*/ */
let expr = orDo(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) => { let tokensFiltered = tokens.filter((x) => {
return (x.type != tk.TokenType.NL return (x.type != tk.TokenType.NL
&& x.type != tk.TokenType.SP); && x.type != tk.TokenType.SP);
@ -194,5 +214,4 @@ let beta = expr({
remained: tokensFiltered, remained: tokensFiltered,
ast: [] ast: []
}); });
console.log(repr(wrappedTokens));
console.log(repr(beta)); console.log(repr(beta));

View file

@ -95,7 +95,12 @@ export function m1TType(typ: tk.TokenType):
*/ */
let tInt = m1TType(tk.TokenType.INT); let tInt = m1TType(tk.TokenType.INT);
let tAdd = m1TType(tk.TokenType.I_ADD); let tAdd = m1TType(tk.TokenType.I_ADD);
let tSub = m1TType(tk.TokenType.I_SUB);
let tMul = m1TType(tk.TokenType.I_MUL); 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) => { 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)=>{ let midfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{
var a = f(x); var a = f(x);
if (a._tag == "Some"){ 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 ast_tail : tkTree[] = slice(a.value.ast,a.value.ast.length-3);
let new_ast = [ast_tail]; let new_ast = [ast_tail];
a.value.ast = new_ast; a.value.ast = new_ast;
@ -156,43 +166,63 @@ let midfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{
return a; return a;
} }
/** let circumfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{
* var a = f(x);
* fac1 = int MUL int if (a._tag == "Some"){
*/ let inner = a.value.ast[a.value.ast.length-2];
//let fac1 = midfix((x : TokenMatcheePair)=> console.log("AST===="+repr(a.value.ast));
// thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt)); let ast_middle : tkTree[] = [inner];
let new_ast = [ast_middle];
a.value.ast = new_ast;
let fac1 = (x : TokenMatcheePair) => { console.log("+"+signal+"+"+repr(a));
let a = midfix((x : TokenMatcheePair)=>
thenDo(thenDo(thenDo(tk.toSome(x), tInt), tMul), tInt), "fac1")(x);
}
return a; return a;
} }
/** fac1 = "(" expr ")" */
let fac1 = circumfix((x : TokenMatcheePair)=>
thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1");
/**
*
* fac2 = int MUL int
*/
let fac2 = tInt; let fac2 = tInt;
/**
* fac = fac1 | fac2
*/
let fac = orDo(fac1, fac2); let fac = orDo(fac1, fac2);
/** /**
* *
* expr1 = fac ADD fac * term1 = fac (MUL | DIV) fac
*/
let term1 = midfix((x : TokenMatcheePair)=>
thenDo(thenDo(thenDo(tk.toSome(x), fac), orDo(tMul,tDiv)), fac), "term1");
/**
*
* term2 = int MUL int
*/
let term2 = fac;
/**
* term = term1 | term2
*/
let term = orDo(term1, term2);
/**
*
* expr1 = term ADD term
*/ */
let expr1 = midfix((x : TokenMatcheePair)=> 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 * 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( let tokensFiltered = tokens.filter(
(x)=>{return (x.type != tk.TokenType.NL (x)=>{return (x.type != tk.TokenType.NL
&& x.type != tk.TokenType.SP)}); && x.type != tk.TokenType.SP)});
@ -218,7 +248,6 @@ let beta = expr({
remained : tokensFiltered, remained : tokensFiltered,
ast : []}); ast : []});
console.log(repr(wrappedTokens));
console.log(repr(beta)); console.log(repr(beta));