Skip to content

Commit

Permalink
fix(shader-ast): update allChildren(), add isTerm()
Browse files Browse the repository at this point in the history
- update allChildren() to descend into literals if value is a Term
  • Loading branch information
postspectacular committed Aug 8, 2020
1 parent 981e878 commit 267a0c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/shader-ast/src/ast/checks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { isPlainObject } from "@thi.ng/checks";
import type { Term } from "../api/nodes";

const RE_VEC = /^[iub]?vec[234]$/;
const RE_MAT = /^mat[234]$/;

/**
* Returns true if given `t` is a {@link Term}-like object.
*
* @param t
*/
export const isTerm = (t: any): t is Term<any> =>
isPlainObject(t) && !!t.tag && !t.type;

/**
* Returns true, if given term evaluates to a boolean value.
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/shader-ast/src/ast/scope.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Fn, Fn2 } from "@thi.ng/api";
import { isArray } from "@thi.ng/checks";
import { DGraph } from "@thi.ng/dgraph";
import { isMat, isVec } from "./checks";
import type { Fn, Fn2 } from "@thi.ng/api";
import type {
Assign,
Branch,
Expand All @@ -18,6 +17,7 @@ import type {
Ternary,
} from "../api/nodes";
import type { Type } from "../api/types";
import { isMat, isTerm, isVec } from "./checks";

/**
* Helper function for {@link walk}. Returns child nodes for any control
Expand Down Expand Up @@ -62,6 +62,8 @@ export const allChildren = (t: Term<any>) =>
? [(<Assign<any>>t).r]
: isVec(t) || isMat(t)
? (<Lit<any>>t).val
: isTerm((<Lit<any>>t).val)
? (<Lit<any>>t).val
: undefined);

/**
Expand Down

0 comments on commit 267a0c0

Please sign in to comment.