Skip to content

Commit

Permalink
replace === check with startsWith in atom case of matchExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Masom committed Jun 11, 2024
1 parent 823059c commit 04dae22
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/logic/wordgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const matchQuantifier = (g: Grammar, expr: Expr, min: number, max: number, s: st
const matchExpr = (g: Grammar, e: Expr, s: string): number[] | null => {
switch (e.tag) {
case 'Atom':
return s === e.value || e.value === "" ? [e.value.length] : null
return s.startsWith(e.value) ? [e.value.length] : null
case 'Ref': {
const rule = g.rules.find(r => r.name === e.rule);
if (!rule) throw new Error(`Rule ${e.rule} not found`)
Expand Down Expand Up @@ -159,24 +159,22 @@ const applyRuleRewrites = (g: Grammar, rule: Rule, gen: string): string => {
for (const [match, replace] of rule.rewrites) {

for (let i = 0; i < result.length; i++) {
for (let j = i + 1; j <= result.length; j++) {
const s = result.slice(i, j);
const exprMatch = matchExpr(g, match, s);
if (exprMatch !== null) {
let matchEnd = 0;
const matches = exprMatch.map(l => {
const newMatchEnd = matchEnd + l;
const match = s.slice(matchEnd, newMatchEnd);
matchEnd = newMatchEnd;
return match;
});
console.log(matches);
const head = result.slice(0, i);
const tail = result.slice(i + matchEnd);
const gen = generateExpr(g, replace, matches);

result = head + gen + tail;
}
const s = result.slice(i);
const exprMatch = matchExpr(g, match, s);
if (exprMatch !== null) {
let matchEnd = 0;
const matches = exprMatch.map(l => {
const newMatchEnd = matchEnd + l;
const match = s.slice(matchEnd, newMatchEnd);
matchEnd = newMatchEnd;
return match;
});
console.log(matches);
const head = result.slice(0, i);
const tail = result.slice(i + matchEnd);
const gen = generateExpr(g, replace, matches);

result = head + gen + tail;
}
}
}
Expand Down

0 comments on commit 04dae22

Please sign in to comment.