Skip to content

Commit

Permalink
Adding helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRayCode committed Jul 8, 2024
1 parent f773cfc commit 7cd8753
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ import { renameBindings, renameFunctions, renameTypes } from '@shaderfrog/glsl-p
// ... parse an ast...

// Suffix top level variables with _x
// TODO UPDATE THIS
renameBindings(ast.scopes[0], (name, node) => `${name}_x`);
// Suffix function names with _x
renameFunctions(ast.scopes[0], (name, node) => `${name}_x`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"engines": {
"node": ">=16"
},
"version": "5.0.0-beta.2",
"version": "5.0.0-beta.3",
"type": "module",
"description": "A GLSL ES 1.0 and 3.0 parser and preprocessor that can preserve whitespace and comments",
"scripts": {
Expand Down
21 changes: 0 additions & 21 deletions src/parser/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,27 +361,6 @@ StructName_x main(in StructName_x x, StructName_x[3] y) {
expect(Object.keys(ast.scopes[2].types)).toEqual(['StructName']);
});

test('shangus', () => {
const ast = c.parseSrc(`
struct MyStruct { float y; };
attribute vec3 position;
vec3 func() {}`);

ast.scopes[0].bindings = renameBindings(
ast.scopes[0].bindings,
(name) => `${name}_x`
);
ast.scopes[0].functions = renameFunctions(
ast.scopes[0].functions,
(name) => `${name}_y`
);
ast.scopes[0].types = renameTypes(ast.scopes[0].types, (name) => `${name}_z`);

expect(Object.keys(ast.scopes[0].bindings)).toEqual(['position_x']);
expect(Object.keys(ast.scopes[0].functions)).toEqual(['func_y']);
expect(Object.keys(ast.scopes[0].types)).toEqual(['MyStruct_z']);
});

test('fn args shadowing global scope identified as separate bindings', () => {
const ast = c.parseSrc(`
attribute vec3 position;
Expand Down
31 changes: 0 additions & 31 deletions src/parser/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,6 @@ export const buildParser = () => {
// }
// };

export const debugEntry = (bindings: ScopeIndex) => {
return Object.entries(bindings).map(
([k, v]) =>
`${k}: (${v.references.length} references, ${
v.declaration ? '' : 'un'
}declared): ${v.references.map((r) => r.type).join(', ')}`
);
};
export const debugFunctionEntry = (bindings: FunctionScopeIndex) =>
Object.entries(bindings).flatMap(([name, overloads]) =>
Object.entries(overloads).map(
([signature, overload]) =>
`${name} (${signature}): (${overload.references.length} references, ${
overload.declaration ? '' : 'un'
}declared): ${overload.references.map((r) => r.type).join(', ')}`
)
);

export const debugScopes = (astOrScopes: Program | Scope[]) =>
console.log(
'Scopes:',
'scopes' in astOrScopes
? astOrScopes.scopes
: astOrScopes.map((s) => ({
name: s.name,
types: debugEntry(s.types),
bindings: debugEntry(s.bindings),
functions: debugFunctionEntry(s.functions),
}))
);

const middle = /\/\* start \*\/((.|[\r\n])+)(\/\* end \*\/)?/m;

type ParseSrc = (src: string, options?: ParserOptions) => Program;
Expand Down
33 changes: 33 additions & 0 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Program } from '../ast/ast-types.js';
import {
FunctionOverloadIndex,
FunctionScopeIndex,
Scope,
ScopeEntry,
ScopeIndex,
TypeScopeEntry,
Expand Down Expand Up @@ -123,3 +125,34 @@ export const renameFunctions = (
);

export const xor = (a: any, b: any): boolean => (a || b) && !(a && b);

export const debugEntry = (bindings: ScopeIndex) => {
return Object.entries(bindings).map(
([k, v]) =>
`${k}: (${v.references.length} references, ${
v.declaration ? '' : 'un'
}declared): ${v.references.map((r) => r.type).join(', ')}`
);
};
export const debugFunctionEntry = (bindings: FunctionScopeIndex) =>
Object.entries(bindings).flatMap(([name, overloads]) =>
Object.entries(overloads).map(
([signature, overload]) =>
`${name} (${signature}): (${overload.references.length} references, ${
overload.declaration ? '' : 'un'
}declared): ${overload.references.map((r) => r.type).join(', ')}`
)
);

export const debugScopes = (astOrScopes: Program | Scope[]) =>
console.log(
'Scopes:',
'scopes' in astOrScopes
? astOrScopes.scopes
: astOrScopes.map((s) => ({
name: s.name,
types: debugEntry(s.types),
bindings: debugEntry(s.bindings),
functions: debugFunctionEntry(s.functions),
}))
);

0 comments on commit 7cd8753

Please sign in to comment.