Skip to content

Commit

Permalink
feat(shader-ast): add/update AST node predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 13, 2021
1 parent 8d6390c commit 8a4855e
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions packages/shader-ast/src/ast/checks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isPlainObject } from "@thi.ng/checks";
import type { Term } from "../api/nodes";
import { isArrayLike, isNumber, isPlainObject } from "@thi.ng/checks";
import type { Lit, Term } from "../api/nodes";
import type { BoolTerm, FloatTerm, IntTerm, UintTerm } from "../api/terms";
import type { BVec, IVec, UVec, Vec } from "../api/types";

const RE_VEC = /^[iub]?vec[234]$/;
const RE_MAT = /^mat[234]$/;
Expand All @@ -15,44 +17,68 @@ export const isTerm = (t: any): t is Term<any> =>
/**
* Returns true, if given term evaluates to a boolean value.
*/
export const isBool = (t: Term<any>) => t.type === "bool";
export const isBool = (t: Term<any>): t is BoolTerm => t.type === "bool";

/**
* Returns true, if given term evaluates to a float value.
*/
export const isFloat = (t: Term<any>) => t.type === "float";
export const isFloat = (t: Term<any>): t is FloatTerm => t.type === "float";

/**
* Returns true, if given term evaluates to a signed integer value.
*/
export const isInt = (t: Term<any>) => t.type === "int";
export const isInt = (t: Term<any>): t is IntTerm => t.type === "int";

/**
* Returns true, if given term evaluates to an unsigned integer value.
*/
export const isUint = (t: Term<any>) => t.type === "uint";
export const isUint = (t: Term<any>): t is UintTerm => t.type === "uint";

/**
* Returns true, if given term is a literal.
*/
export const isLit = (t: Term<any>) => t.tag === "lit";
export const isLit = (t: Term<any>): t is Lit<any> => t.tag === "lit";

/**
* Returns true, if given term is a float literal.
*/
export const isLitFloat = (t: Term<any>) => isLit(t) && isFloat(t);
export const isLitFloat = (t: Term<any>): t is Lit<"float"> =>
isLit(t) && isFloat(t);

/**
* Returns true, if given term is a signed integer literal.
*/
export const isLitInt = (t: Term<any>) => isLit(t) && isInt(t);
export const isLitInt = (t: Term<any>): t is Lit<"int"> => isLit(t) && isInt(t);

/**
* Returns true, if given term is a numeric literal (float, int, uint).
*/
export const isLitNumeric = (t: Term<any>) =>
export const isLitNumeric = (
t: Term<any>
): t is Lit<"float" | "int" | "uint"> =>
isLit(t) && (isFloat(t) || isInt(t) || isUint(t));

/**
* Returns true if t is a numeric literal with a JS number as value (not an
* expression).
*
* @param t
*/
export const isLitNumericConst = (
t: Term<any>
): t is Lit<"float" | "int" | "uint"> => isLit(t) && isNumber(t.val);

/**
* Returns true if t is a vector literal with a JS array as value (not an
* expression).
*
* @param t
*/
export const isLitVecConst = (
t: Term<any>
): t is Lit<Vec | IVec | UVec | BVec> =>
isLit(t) && isVec(t) && isArrayLike(t.val);

/**
* Returns true, if given term evaluates to a vector value (vec, ivec, bvec).
*/
Expand Down

0 comments on commit 8a4855e

Please sign in to comment.