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
}