Skip to main content
Clarify what OP's asking. Include information in question's text which one may have to otherwise figure out by understanding the code.
Source Link

Basically, I want to know how to simulate while and if if I'm handling the control flow myself through an array of instructions. 

The while loop can be simulated by if, as seen with assembly branching with je and such. But the question is, if ancan a if statement can be simulated somehow, perhaps by simulating a program counter or instruction pointer in a loop.?

By simulated I mean replicated without using any control-flow primitives directly, other than the while (true) { ... } loop to loop through instructions.

For example:

var pointer = 0

var instructions = [
  doa,
  dob,
  dob2,
  dob3,
  doc,
  dod
]

var a = 1
var b = 2
var c = 3

while (true) {
  var instruction = instructions[pointer]
  instruction()
}

function doa() {
  a = 10
  pointer = 1
}

function dob() {
  doif(a == 10, dob2)
  doif(a != 10, dob3)
}

function dob2() {
  b = 20
  pointer = 4
}

function dob3() {
  b = 2
  pointer = 4
}

function doc() {
  c = 30
  pointer = 5
}

function dod() {
  console.log(a, b, c)
}

function doif(a, b) {
  // How to remove this:
  if (a) b()
}

At the bottom is a doif. Wondering if there is any way to change its implementation so it doesn't use the built-in if, as in if (a) b(). Somehow maybe it dynamically adds an instruction to the stack, and perhaps sets the instructions pointer, then pops it off somehow. But I don't see how to do that without resorting to if somewhere.

AlsoAs mentioned already, I want to do it without using any control-flow primitives direct. So I know there is a way to simulate an if statement, like a && b(), but I am wanting to avoid that because it's just syntactic shortcut. Also using a while loop as in while (a) { b(); break } is same thing., and so is using the ternary operator a? b() : null;

Any thoughts would be appreciated. Thank you.

Basically, how to simulate while and if. The while loop can be simulated by if, as seen with assembly branching with je and such. But the question is, if an if statement can be simulated somehow, perhaps by simulating a program counter or instruction pointer in a loop. By simulated I mean replicated without using any control-flow primitives directly, other than the while (true) { ... } loop to loop through instructions.

For example:

var pointer = 0

var instructions = [
  doa,
  dob,
  dob2,
  dob3,
  doc,
  dod
]

var a = 1
var b = 2
var c = 3

while (true) {
  var instruction = instructions[pointer]
  instruction()
}

function doa() {
  a = 10
  pointer = 1
}

function dob() {
  doif(a == 10, dob2)
  doif(a != 10, dob3)
}

function dob2() {
  b = 20
  pointer = 4
}

function dob3() {
  b = 2
  pointer = 4
}

function doc() {
  c = 30
  pointer = 5
}

function dod() {
  console.log(a, b, c)
}

function doif(a, b) {
  // How to remove this:
  if (a) b()
}

At the bottom is a doif. Wondering if there is any way to change its implementation so it doesn't use the built-in if, as in if (a) b(). Somehow maybe it dynamically adds an instruction to the stack, and perhaps sets the instructions pointer, then pops it off somehow. But I don't see how to do that without resorting to if somewhere.

Also there is a way to simulate an if statement, like a && b(), but I am wanting to avoid that because it's just syntactic shortcut. Also using a while loop as in while (a) { b(); break } same thing.

Any thoughts would be appreciated. Thank you.

Basically, I want to know how to simulate while and if if I'm handling the control flow myself through an array of instructions. 

The while loop can be simulated by if, as seen with assembly branching with je and such. But the question is, can a if statement be simulated somehow, perhaps by simulating a program counter or instruction pointer in a loop?

By simulated I mean replicated without using any control-flow primitives directly, other than the while (true) { ... } loop to loop through instructions.

For example:

var pointer = 0

var instructions = [
  doa,
  dob,
  dob2,
  dob3,
  doc,
  dod
]

var a = 1
var b = 2
var c = 3

while (true) {
  var instruction = instructions[pointer]
  instruction()
}

function doa() {
  a = 10
  pointer = 1
}

function dob() {
  doif(a == 10, dob2)
  doif(a != 10, dob3)
}

function dob2() {
  b = 20
  pointer = 4
}

function dob3() {
  b = 2
  pointer = 4
}

function doc() {
  c = 30
  pointer = 5
}

function dod() {
  console.log(a, b, c)
}

function doif(a, b) {
  // How to remove this:
  if (a) b()
}

At the bottom is a doif. Wondering if there is any way to change its implementation so it doesn't use the built-in if, as in if (a) b(). Somehow maybe it dynamically adds an instruction to the stack, and perhaps sets the instructions pointer, then pops it off somehow. But I don't see how to do that without resorting to if somewhere.

As mentioned already, I want to do it without using any control-flow primitives direct. So I know there is a way to simulate an if statement, like a && b(), but I am wanting to avoid that because it's just syntactic shortcut. Also using a while loop as in while (a) { b(); break } is same thing, and so is using the ternary operator a? b() : null;

Any thoughts would be appreciated. Thank you.

Source Link
Lance Pollard
  • 2.8k
  • 1
  • 23
  • 41

How to Simulate Control-Flow without using Control-Flow Primitives

Basically, how to simulate while and if. The while loop can be simulated by if, as seen with assembly branching with je and such. But the question is, if an if statement can be simulated somehow, perhaps by simulating a program counter or instruction pointer in a loop. By simulated I mean replicated without using any control-flow primitives directly, other than the while (true) { ... } loop to loop through instructions.

For example:

var pointer = 0

var instructions = [
  doa,
  dob,
  dob2,
  dob3,
  doc,
  dod
]

var a = 1
var b = 2
var c = 3

while (true) {
  var instruction = instructions[pointer]
  instruction()
}

function doa() {
  a = 10
  pointer = 1
}

function dob() {
  doif(a == 10, dob2)
  doif(a != 10, dob3)
}

function dob2() {
  b = 20
  pointer = 4
}

function dob3() {
  b = 2
  pointer = 4
}

function doc() {
  c = 30
  pointer = 5
}

function dod() {
  console.log(a, b, c)
}

function doif(a, b) {
  // How to remove this:
  if (a) b()
}

At the bottom is a doif. Wondering if there is any way to change its implementation so it doesn't use the built-in if, as in if (a) b(). Somehow maybe it dynamically adds an instruction to the stack, and perhaps sets the instructions pointer, then pops it off somehow. But I don't see how to do that without resorting to if somewhere.

Also there is a way to simulate an if statement, like a && b(), but I am wanting to avoid that because it's just syntactic shortcut. Also using a while loop as in while (a) { b(); break } same thing.

Any thoughts would be appreciated. Thank you.