How to change output generated based on input?

I’m new to Kotlin language and I am currently attempting to write some code that would take a user created opcode, match it to a template and change the result of the output based on the parameters set in the opcode. An example would be an assignment of a number to a variable with the opcode looking like BN(=A=1); the result of this opcode would be a string that contains A = 1. However if I have multiple variables and numbers I would need to do multiple if statements to account for that and it would not be efficient. For example instead of the previous example if I was now given the opcode BN(=X=55) I would like to be able to assign X = 55. Is there anyway to do this? So far I have a function that takes the opcode and outputs some code in Python as a string which I then write to a file. Any assistance is appreciated.

fun lookUp(Opcode:String): String
    {
        var code = ""
        if (Opcode != null) {
            if (Opcode == "SS;") {
                code = "#!/usr/bin/env pybricks-micropython\n" +
                        "from pybricks.hubs import EV3Brick\n" +
                        "from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,\n" +
                        "                                 InfraredSensor, UltrasonicSensor, GyroSensor)\n" +
                        "from pybricks.parameters import Port, Stop, Direction, Button, Color\n" +
                        "from pybricks.tools import wait, StopWatch, DataLog\n" +
                        "from pybricks.robotics import DriveBase\n" +
                        "from pybricks.media.ev3dev import SoundFile, ImageFile\n" +
                        "import math\n" +
                        "motor1 = Motor(Port.B)\n" +
                        "motor2 = Motor(Port.C)\n"
            }
            if (Opcode == "BN(=X=5);") {
                code = "x = 5\n"
            }
            if (Opcode == "BN(=Y=7);") {
                code = "y = 7\n"
            }
            if (Opcode == "CS0507(=Y);") {
                code = "if"
            }
            if (Opcode == "BH(=5=7)Y;") {
                code = "(y>=x):\n"
            }
            if (Opcode == "BL(=A=360);") {
                code = "motor1.run(360)\n"
            }
            if (Opcode == "BL(=B=360);") {
                code = "motor2.run(360)\n"
            }
            if (Opcode == "CQ(=10);") {
                code = "wait(10000)\n"
            }
            if (Opcode == "CG(=A);") {
                code = "motor1.stop()\n"
            }
            if (Opcode == "CG(=B);") {
                code = "motor2.stop()\n"
            }
            if (Opcode == "XX;") {
                code = "#Program Terminates"
            }
        }
        return code
    }

I’m not sure if I understand your problem fully, but it seems you look for a simple map data structure (dictionary in Python). Just keep your variables in the map and not as local variables.

I am trying to convert a user made code which I refer to as an ‘opcode’ to python code. The python code is stored in a string to be written to a file. There are many opcodes which each represents a different code in python. The issue I have is that because of how many variations in the opcodes there are, I wanted to figure out a way to to change the python code based on what the opcode represents. In the example I gave above the opcode BN(=A=1); is a opcode for assigning a number to a variable in python. Where =A represents the variable you want to use and =1 is the integer you want to assign to that variable. This opcode can now be varied as you can use any variable and any integer. Is there a way to vary what is written in the string based on what the parameters of the opcode are?

Ahh, ok, now I get it. You can either parse the opcode string manually using indexOf(), substring(), etc. or use regular expressions:

val regex = Regex("""(\w+)(?:|\((?:=(\w+))?(?:=(\w+))?\))(\w+)?;""")
val m = regex.matchEntire("BN(=X=5)Y;") ?: error("Invalid opcode")
println(m.groupValues[1]) // BN
println(m.groupValues[2]) // X
println(m.groupValues[3]) // 5
println(m.groupValues[4]) // Y

Above regex catches at most 2 arguments inside parentheses.

1 Like

Hey thanks for the response, I knew I’d probably need to use Regex to do it but I wanted to see if there was a simpler way. I ended up implementing it last night but with a simpler pattern and using the substring to split up the string into the data I wanted and joined them afterwards.