LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Is it possible to execute strings?

Root / Programming Questions / [.]

Stefano_LassandroCreated:
I've created a string and stored in a varible: A$ = "Y=X*2" And defined the variables X and Y: Y = 0 X = 16 Now, is possible to execute the string A$? (The output of Y should be 32) Thanks :)

Yes and no. Let me explain: You can run code in strings with another slot using the PRG functions, but you won't be fully able to access the slot you're executing from without using VAR("slot:varname") Here's a simple solution which uses slot 1.
DEF CODE_EXEC S$
  PRGEDIT 1 'CHANGE THIS NUMBER TO USE ANOTHER SLOT
  PRGDEL -1
  PRGINS S$
  EXEC 1 'MAKE SURE THIS IS THE SAME AS THE PRGEDIT SLOT
END

I think you can with CALL, but I don't know. I'll play with that for a bit and let you know. EDIT: Ok, so CALL seems to work pretty well with all commands, though the guide claims that it can only be used with user-defined instructions. There are some weird ones, though, like PRINT/? make it say undefined function. Just plug in a string and CALL will call your function:
ST$=SPSET
CALL ST$,0,0
A sprite should appear.

Yeah, CALL works for calling instructions, but the OP originally wanted to execute strings of code, which sadly won't work with CALL. It's still a really powerful instruction and worth learning though. As SmileBASIC doesn't have an 'eval' construct, it cannot run code supplied from a string. Another "unrelated" issue for using CALL for running this kind of code is that you need to know all the parameters that the function accepts, since if you don't supply them it will error with 'Illegal function call'.

Yeah, CALL works for calling instructions, but the OP originally wanted to execute strings of code, which sadly won't work with CALL. It's still a really powerful instruction and worth learning though. As SmileBASIC doesn't have an 'eval' construct, it cannot run code supplied from a string. Another "unrelated" issue for using CALL for running this kind of code is that you need to know all the parameters that the function accepts, since if you don't supply them it will error with 'Illegal function call'.
yeah true

Ok, I understand. Tnx

Would setting up a CALL instruction (like below) be adequate?
VAR Y=0,X=16
VAR A$=“setY”

CALL A$

DEF setY
 Y=X*2
 ?Y
END
Do you need to execute strings or could you call to a function instead?

Would setting up a CALL instruction (like below) be adequate?
VAR Y=0,X=16
VAR A$=“setY”

CALL A$

DEF setY
 Y=X*2
 ?Y
END
Do you need to execute strings or could you call to a function instead?
I need to execute a string, so it cannot be executed using CALL... literally copying it into the interpreter