Skip to content

Commit ae93246

Browse files
committed
added 'shift right logical' to shift node
1 parent b36e2e1 commit ae93246

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/components/DebugUI.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
PopoverTrigger,
2424
} from "@/components/ui/popover"
2525
import { ToggleGroup, ToggleGroupItem } from "./ui/toggle-group"
26-
import { makeShifter, type ShiftDirection } from "@/logic/nodeTypes/shift"
26+
import { makeShifter, type ShiftKind } from "@/logic/nodeTypes/shift"
2727
import { neg } from "@/logic/nodeTypes/neg"
2828
import type { NodeType } from "@/logic/simulation"
2929

@@ -34,7 +34,7 @@ type NodeInfo = {
3434
}
3535

3636
type NodeCreationParams = {
37-
dir: ShiftDirection
37+
kind: ShiftKind
3838
bits: number
3939
}
4040

@@ -50,13 +50,13 @@ const placeableNodes: NodeInfo[] = [
5050
{
5151
name: "Shift",
5252
params: true,
53-
node: (params) => makeShifter(params.dir, params.bits),
53+
node: (params) => makeShifter(params.kind, params.bits),
5454
},
5555
]
5656

5757
function AddNodePopup(props: { trigger: ReactNode }) {
5858
const [selectedNode, setSelectedNode] = useState<NodeInfo | undefined>()
59-
const [shiftDir, setShiftDir] = useState<ShiftDirection>("left")
59+
const [shiftKind, setShiftKind] = useState<ShiftKind>("left")
6060
const [shiftBits, setShiftBits] = useState("1")
6161

6262
return (
@@ -98,11 +98,14 @@ function AddNodePopup(props: { trigger: ReactNode }) {
9898
id="shift-direction"
9999
type="single"
100100
variant="outline"
101-
value={shiftDir}
102-
onValueChange={(v: ShiftDirection) => setShiftDir(v)}
101+
value={shiftKind}
102+
onValueChange={(v: ShiftKind) => setShiftKind(v)}
103103
>
104104
<ToggleGroupItem value="left">Left</ToggleGroupItem>
105105
<ToggleGroupItem value="right">Right</ToggleGroupItem>
106+
<ToggleGroupItem value="rightLogical">
107+
Right Logical
108+
</ToggleGroupItem>
106109
</ToggleGroup>
107110
</div>
108111

src/logic/nodeTypes/shift.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ const inputs = [
88

99
type Outputs = ["out"]
1010

11-
export type ShiftDirection = "left" | "right"
11+
export type ShiftKind = "left" | "right" | "rightLogical"
12+
13+
const shiftFuncs: Record<ShiftKind, (a: number, b: number) => number> = {
14+
left: (a, b) => a << b,
15+
right: (a, b) => a >> b,
16+
rightLogical: (a, b) => a >>> b,
17+
}
18+
19+
const shiftLabels: Record<ShiftKind, string> = {
20+
left: "Shift\nleft",
21+
right: "Shift\nright",
22+
rightLogical: "SRL",
23+
}
1224

1325
/**
1426
* Creates a node type that shifts its input.
15-
* @param dir The direction to shift into.
27+
* @param kind The kind of shift to perform.
1628
* @param bits The number of bits to shift by.
1729
*/
18-
export function makeShifter(
19-
dir: ShiftDirection,
20-
bits: number,
21-
): NodeType<Outputs> {
30+
export function makeShifter(kind: ShiftKind, bits: number): NodeType<Outputs> {
2231
return nodeType(
2332
inputs,
2433
(_, inputs) => ({
25-
out: dir == "left" ? inputs.in << bits : inputs.in >> bits,
34+
out: shiftFuncs[kind](inputs.in, bits),
2635
}),
2736
undefined,
28-
`Shift\n${dir} ${bits}`,
37+
`${shiftLabels[kind]} ${bits}`,
2938
)
3039
}

0 commit comments

Comments
 (0)