Fixed a bug in the multiHeadMatcher

This commit is contained in:
Rolf Martin Glomsrud 2024-05-28 15:46:10 +02:00
parent 6313ac889c
commit fc6a27e58f
8 changed files with 355 additions and 38 deletions

View file

@ -2,13 +2,13 @@ proposal awaitToPomise{
case single{
applicable to {
"let <<ident:Identifier>> = await <<awaitedExpr: Expression>>;
<<statements: (Statement && !ReturnStatement)+>>
<<statements: (Statement && !ReturnStatement && !ContinueStatement &&!BreakStatement)+>>
return <<returnExpr: Expression>>
"
}
transform to{
"return <<awaitedExpr>>.then((<<ident>>) => {
"return <<awaitedExpr>>.then(async <<ident>> => {
<<statements>>
return <<returnExpr>>
});"

View file

@ -0,0 +1,14 @@
proposal Pipeline{
case SingleArgument {
applicable to {
"<<someFunctionIdent:Identifier || MemberExpression>>(<<otherFunctionIdent:Identifier || MemberExpression>>(<<arggg:Identifier>>));"
}
transform to {
"(<<arggg>> |> <<otherFunctionIdent>>(%)) |> <<someFunctionIdent>>(%);"
}
}
}

View file

@ -1,8 +1,104 @@
async function lol() {
let a = 0;
return gc().then(async (b) => {
let c = 100;
let d = await l();
return 1;
});
}
return awaitedExpr.then(async _geometry => {
const _intersectPoint = /*@__PURE__*/new Vector3();
const _worldScale = /*@__PURE__*/new Vector3();
const _mvPosition = /*@__PURE__*/new Vector3();
const _alignedPosition = /*@__PURE__*/new Vector2();
const _rotatedPosition = /*@__PURE__*/new Vector2();
const _viewWorldMatrix = /*@__PURE__*/new Matrix4();
const _vA = /*@__PURE__*/new Vector3();
const _vB = /*@__PURE__*/new Vector3();
const _vC = /*@__PURE__*/new Vector3();
const _uvA = /*@__PURE__*/new Vector2();
const _uvB = /*@__PURE__*/new Vector2();
const _uvC = /*@__PURE__*/new Vector2();
class Sprite extends Object3D {
constructor(material = new SpriteMaterial()) {
super();
this.isSprite = true;
this.type = "Sprite";
if (_geometry === undefined) {
_geometry = new BufferGeometry();
const float32Array = new Float32Array([-0.5, -0.5, 0, 0, 0, 0.5, -0.5, 0, 1, 0, 0.5, 0.5, 0, 1, 1, -0.5, 0.5, 0, 0, 1]);
const interleavedBuffer = new InterleavedBuffer(float32Array, 5);
_geometry.setIndex([0, 1, 2, 0, 2, 3]);
_geometry.setAttribute("position", new InterleavedBufferAttribute(interleavedBuffer, 3, 0, false));
_geometry.setAttribute("uv", new InterleavedBufferAttribute(interleavedBuffer, 2, 3, false));
}
this.geometry = _geometry;
this.material = material;
this.center = new Vector2(0.5, 0.5);
}
raycast(raycaster, intersects) {
if (raycaster.camera === null) {
console.error('THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.');
}
_worldScale.setFromMatrixScale(this.matrixWorld);
_viewWorldMatrix.copy(raycaster.camera.matrixWorld);
this.modelViewMatrix.multiplyMatrices(raycaster.camera.matrixWorldInverse, this.matrixWorld);
_mvPosition.setFromMatrixPosition(this.modelViewMatrix);
if (raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false) {
_worldScale.multiplyScalar(-_mvPosition.z);
}
const rotation = this.material.rotation;
let sin, cos;
if (rotation !== 0) {
cos = Math.cos(rotation);
sin = Math.sin(rotation);
}
const center = this.center;
transformVertex(_vA.set(-0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos);
transformVertex(_vB.set(0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos);
transformVertex(_vC.set(0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos);
_uvA.set(0, 0);
_uvB.set(1, 0);
_uvC.set(1, 1);
// check first triangle
let intersect = raycaster.ray.intersectTriangle(_vA, _vB, _vC, false, _intersectPoint);
if (intersect === null) {
// check second triangle
transformVertex(_vB.set(-0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos);
_uvB.set(0, 1);
intersect = raycaster.ray.intersectTriangle(_vA, _vC, _vB, false, _intersectPoint);
if (intersect === null) {
return;
}
}
const distance = raycaster.ray.origin.distanceTo(_intersectPoint);
if (distance < raycaster.near || distance > raycaster.far) return;
intersects.push({
distance: distance,
point: _intersectPoint.clone(),
uv: Triangle.getInterpolation(_intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2()),
face: null,
object: this
});
}
copy(source, recursive) {
super.copy(source, recursive);
if (source.center !== undefined) this.center.copy(source.center);
this.material = source.material;
return this;
}
}
function transformVertex(vertexPosition, mvPosition, center, scale, sin, cos) {
// compute position in camera space
_alignedPosition.subVectors(vertexPosition, center).addScalar(0.5).multiply(scale);
// to check if rotation is not zero
if (sin !== undefined) {
_rotatedPosition.x = cos * _alignedPosition.x - sin * _alignedPosition.y;
_rotatedPosition.y = sin * _alignedPosition.x + cos * _alignedPosition.y;
} else {
_rotatedPosition.copy(_alignedPosition);
}
vertexPosition.copy(mvPosition);
vertexPosition.x += _rotatedPosition.x;
vertexPosition.y += _rotatedPosition.y;
// transform to world space
vertexPosition.applyMatrix4(_viewWorldMatrix);
}
export { Sprite };
return returnExpr;
});

View file

@ -12,7 +12,7 @@ import { parseJSTQL } from "./langium/langiumRunner";
const dir = "../prettier/src";
const path = "test_files/do_test.js";
const path = "test_files/test2.js";
const file = Bun.file(path);
const codeFromFile = await file.text();
const main = async () => {
@ -21,16 +21,16 @@ const main = async () => {
/*
console.log(codeFromFile);
const jstql_file =
"/home/rolfmg/Coding/Master/didactic-chainsaw/dsl_files/do.jstql";
"/home/rolfmg/Coding/Master/didactic-chainsaw/dsl_files/awaitToPromise.jstql";
const test_file = Bun.file(jstql_file);
const test_JSTQL = await test_file.text();
let proposals = await parseJSTQL(test_JSTQL);
let [code, count] = transform(proposals[0].cases, codeFromFile);
await Bun.write("output_files/output_do.js", code);
await Bun.write("output_files/test2.js", code);
return;
*/
let basepathExamplesJSFiles = "../react";
let basepathExamplesJSFiles = "../next.js";
let examples = (await readdir(basepathExamplesJSFiles, { recursive: true }))
.filter((x) => x.endsWith(".js"))
.map((x) => basepathExamplesJSFiles + "/" + x);
@ -54,7 +54,7 @@ const main = async () => {
for (let examplesFile of examples) {
try {
if (examplesFile.split("/").includes("compiled")) {
continue;
//continue;
}
console.log(examplesFile);
let script = await Bun.file(examplesFile).text();
@ -63,32 +63,25 @@ const main = async () => {
script
);
sum += matches;
console.log(matches);
if (matches > 0) {
//await Bun.write(
// "output_testing/" + count + examplesFile.split("/").at(-1),
//resultString
//);
await Bun.write(
"output_testing/" +
count +
examplesFile.split("/").join("_"),
resultString
);
count += 1;
}
filesSucceeded += 1;
} catch (e) {
failures += 1;
//console.log(e);
}
console.log("current sum", sum);
}
result.push(
"Total for " +
proposalFile +
" is " +
sum +
",failures " +
failures +
",succeeded " +
filesSucceeded +
", Files With Matches " +
count +
",totalJSFiles " +
examples.length
proposalFile + ", " + sum + ", " + count + ", " + filesSucceeded
);
}

View file

@ -226,7 +226,7 @@ export class Matcher {
let statements: TreeNode<PairedNodes>[] = [];
let aplToi = 0;
let codei = 0;
while (aplToi < aplTo.length) {
while (aplToi < aplTo.length && codei + y < code.length) {
let [paired, matchResult] = this.exactExprMatcher(
code[codei + y],
aplTo[aplToi]
@ -260,6 +260,9 @@ export class Matcher {
aplToi += 1;
codei += 1;
}
if (aplToi !== aplTo.length) {
fullMatch = false;
}
if (fullMatch) {
this.matches.push({ statements });
}
@ -317,6 +320,7 @@ export class Matcher {
i -= 1;
break;
}
pairedChild.element.codeNode.push(
...maybeChild.element.codeNode
);
@ -327,6 +331,10 @@ export class Matcher {
i += 1;
aplToi += 1;
}
// Verify it is a full match
if (aplToi < aplTo.children.length) {
return [undefined, MatchResult.NoMatch];
}
if (i < code.children.length) {
return [undefined, MatchResult.NoMatch];
}

View file

@ -1,7 +1,7 @@
async function something() {
let a = 100;
a *= 100000;
return fetch("https://uib.no").then(uib => {
return fetch("https://uib.no").then(async uib => {
a += 100000;
a -= 1000;
return [a, uib];

View file

@ -91,11 +91,11 @@ function transformSelfHosted(
throw new Error("This no worky LOL");
}
let matches = runMatch(codeTree, applicableToTree, internals);
console.log("We found", matches.length, "matches");
//showTreePaired(matches[0].statements[0]);
//console.log("We found", matches.length, "matches");
let outputAST = transformer(matches, transformToTree, codeAST, transformTo);
console.log("Finished transforming");
//console.log("Finished transforming");
return [outputAST, matches.length];
}

View file

@ -1,2 +1,208 @@
a(a(a(a(a(a(a(b)))))));
c(c(c(c(c(d, b), b), b), b), b);
let _geometry;
const _intersectPoint = /*@__PURE__*/ new Vector3();
const _worldScale = /*@__PURE__*/ new Vector3();
const _mvPosition = /*@__PURE__*/ new Vector3();
const _alignedPosition = /*@__PURE__*/ new Vector2();
const _rotatedPosition = /*@__PURE__*/ new Vector2();
const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
const _vA = /*@__PURE__*/ new Vector3();
const _vB = /*@__PURE__*/ new Vector3();
const _vC = /*@__PURE__*/ new Vector3();
const _uvA = /*@__PURE__*/ new Vector2();
const _uvB = /*@__PURE__*/ new Vector2();
const _uvC = /*@__PURE__*/ new Vector2();
class Sprite extends Object3D {
constructor(material = new SpriteMaterial()) {
super();
this.isSprite = true;
this.type = "Sprite";
if (_geometry === undefined) {
_geometry = new BufferGeometry();
const float32Array = new Float32Array([
-0.5, -0.5, 0, 0, 0, 0.5, -0.5, 0, 1, 0, 0.5, 0.5, 0, 1, 1,
-0.5, 0.5, 0, 0, 1,
]);
const interleavedBuffer = new InterleavedBuffer(float32Array, 5);
_geometry.setIndex([0, 1, 2, 0, 2, 3]);
_geometry.setAttribute(
"position",
new InterleavedBufferAttribute(interleavedBuffer, 3, 0, false)
);
_geometry.setAttribute(
"uv",
new InterleavedBufferAttribute(interleavedBuffer, 2, 3, false)
);
}
this.geometry = _geometry;
this.material = material;
this.center = new Vector2(0.5, 0.5);
}
raycast(raycaster, intersects) {
if (raycaster.camera === null) {
console.error(
'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.'
);
}
_worldScale.setFromMatrixScale(this.matrixWorld);
_viewWorldMatrix.copy(raycaster.camera.matrixWorld);
this.modelViewMatrix.multiplyMatrices(
raycaster.camera.matrixWorldInverse,
this.matrixWorld
);
_mvPosition.setFromMatrixPosition(this.modelViewMatrix);
if (
raycaster.camera.isPerspectiveCamera &&
this.material.sizeAttenuation === false
) {
_worldScale.multiplyScalar(-_mvPosition.z);
}
const rotation = this.material.rotation;
let sin, cos;
if (rotation !== 0) {
cos = Math.cos(rotation);
sin = Math.sin(rotation);
}
const center = this.center;
transformVertex(
_vA.set(-0.5, -0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
transformVertex(
_vB.set(0.5, -0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
transformVertex(
_vC.set(0.5, 0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
_uvA.set(0, 0);
_uvB.set(1, 0);
_uvC.set(1, 1);
// check first triangle
let intersect = raycaster.ray.intersectTriangle(
_vA,
_vB,
_vC,
false,
_intersectPoint
);
if (intersect === null) {
// check second triangle
transformVertex(
_vB.set(-0.5, 0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
_uvB.set(0, 1);
intersect = raycaster.ray.intersectTriangle(
_vA,
_vC,
_vB,
false,
_intersectPoint
);
if (intersect === null) {
return;
}
}
const distance = raycaster.ray.origin.distanceTo(_intersectPoint);
if (distance < raycaster.near || distance > raycaster.far) return;
intersects.push({
distance: distance,
point: _intersectPoint.clone(),
uv: Triangle.getInterpolation(
_intersectPoint,
_vA,
_vB,
_vC,
_uvA,
_uvB,
_uvC,
new Vector2()
),
face: null,
object: this,
});
}
copy(source, recursive) {
super.copy(source, recursive);
if (source.center !== undefined) this.center.copy(source.center);
this.material = source.material;
return this;
}
}
function transformVertex(vertexPosition, mvPosition, center, scale, sin, cos) {
// compute position in camera space
_alignedPosition
.subVectors(vertexPosition, center)
.addScalar(0.5)
.multiply(scale);
// to check if rotation is not zero
if (sin !== undefined) {
_rotatedPosition.x =
cos * _alignedPosition.x - sin * _alignedPosition.y;
_rotatedPosition.y =
sin * _alignedPosition.x + cos * _alignedPosition.y;
} else {
_rotatedPosition.copy(_alignedPosition);
}
vertexPosition.copy(mvPosition);
vertexPosition.x += _rotatedPosition.x;
vertexPosition.y += _rotatedPosition.y;
// transform to world space
vertexPosition.applyMatrix4(_viewWorldMatrix);
}
export { Sprite };