How to resolve the algorithm Execute Brain step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Execute Brain step by step in the V (Vlang) programming language

Table of Contents

Problem Statement

RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages. Below are links to each of the versions of RCBF. An implementation need only properly implement the following instructions: Any cell size is allowed,   EOF   (End-O-File)   support is optional, as is whether you have bounded or unbounded memory.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Execute Brain step by step in the V (Vlang) programming language

Source code in the brainfuc programming language

fn main() {
    // example program is current Brain**** solution to
    // Hello world/Text task.  only requires 10 bytes of data store!
    bf(10, '++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++
++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>>
>+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++.
<+++++++.--------.<<<<<+.<+++.---.')
}
 
fn bf(d_len int, code string) {
    mut ds := []u8{len: d_len} // data store
    mut dp := 0               // data pointer
    for ip := 0; ip < code.len; ip++ {
        match code[ip] {
            `>` {
                dp++
            }
            `<` {
                dp--
            }
            `+` {
                ds[dp]++
            }
            `-` {
                ds[dp]--
            }
            `.` {
                print(ds[dp].ascii_str())
            }
            `,` {
                //fmt.Scanf("%c", &ds[dp])
                ds[dp] = -1 //TODO
            }
            `[` {
                if ds[dp] == 0 {
                    for nc := 1; nc > 0; {
                        ip++
                        if code[ip] == `[` {
                            nc++
                        } else if code[ip] == `]` {
                            nc--
                        }
                    }
                }
            }
            `]` {
                if ds[dp] != 0 {
                    for nc := 1; nc > 0; {
                        ip--
                        if code[ip] == `]` {
                            nc++
                        } else if code[ip] == `[` {
                            nc--
                        }
                    }
                }
            }
            else {}
        }
    }
}

  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the REXX programming language
You may also check:How to resolve the algorithm Permutations step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Stream merge step by step in the Tcl programming language
You may also check:How to resolve the algorithm Input loop step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Scala programming language