UNPKG

p5

Version:

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

40 lines (39 loc) • 2.25 kB
import { BaseType, NodeType, OpCode } from "./ir_types.js"; import { userError } from "./strands_FES.js"; import { createNodeData, extractNodeTypeInfo, getOrCreateNode, propagateTypeToAssignOnUse } from "./ir_dag.js"; import { recordInBasicBlock } from "./ir_cfg.js"; import { T as createStrandsNode } from "../ir_builders-CVT9KlJB.js"; //#region src/strands/strands_ternary.js function buildTernary(strandsContext, condition, ifTrue, ifFalse) { const { dag, cfg, p5 } = strandsContext; const condNode = condition?.isStrandsNode ? condition : p5.strandsNode(condition); const trueNode = ifTrue?.isStrandsNode ? ifTrue : p5.strandsNode(ifTrue); const falseNode = ifFalse?.isStrandsNode ? ifFalse : p5.strandsNode(ifFalse); let trueType = extractNodeTypeInfo(dag, trueNode.id); let falseType = extractNodeTypeInfo(dag, falseNode.id); if (trueType.baseType === BaseType.ASSIGN_ON_USE && falseType.baseType !== BaseType.ASSIGN_ON_USE) { propagateTypeToAssignOnUse(dag, trueNode.id, falseType.baseType, falseType.dimension); trueType = extractNodeTypeInfo(dag, trueNode.id); } else if (falseType.baseType === BaseType.ASSIGN_ON_USE && trueType.baseType !== BaseType.ASSIGN_ON_USE) { propagateTypeToAssignOnUse(dag, falseNode.id, trueType.baseType, trueType.dimension); falseType = extractNodeTypeInfo(dag, falseNode.id); } if (trueType.baseType !== BaseType.ASSIGN_ON_USE && falseType.baseType !== BaseType.ASSIGN_ON_USE && (trueType.baseType !== falseType.baseType || trueType.dimension !== falseType.dimension)) userError("type error", `The true and false branches of a ternary expression must have the same type. Right now, the true branch is a ${trueType.baseType}${trueType.dimension}, and the false branch is a ${falseType.baseType}${falseType.dimension}.`); const resultType = trueType; const nodeData = createNodeData({ nodeType: NodeType.OPERATION, opCode: OpCode.Nary.TERNARY, dependsOn: [ condNode.id, trueNode.id, falseNode.id ], baseType: resultType.baseType, dimension: resultType.dimension }); const id = getOrCreateNode(dag, nodeData); recordInBasicBlock(cfg, cfg.currentBlock, id); return createStrandsNode(id, resultType.dimension, strandsContext); } //#endregion export { buildTernary };