Using Xojo to control and code in Xojo

AKA Xojo Recursion

I am having some fun writing program to write programs, that is, creating code generators.
So this begs the question: why not write code generators to generate Xojo code?
That is, write Xojo to write Xojo. Or maybe shell out and run a Python script that generates Xojo.
So now say I have some code running, and it runs this Python script, and now have some Xojo code in a string.
I want to add that to my project, or to “a project”. One might have, for instance, two versions of the same project. When the project runs, it copies itself. Then it edits the copy.

Writing the code generator is one thing, I know how to do that. What I don’t know is what tools are available within xojo for performing operations on a Xojo project. I assume a project cannot edit itself. Or can it?

Any ideas of how one would make a self-modifying Xojo program will be appreciated. :slight_smile:

Check out IDE scripting
But this only modifies SOURCE

Self modifying code - apps that alter themselves AT RUNTIME - would probably be flagged as viral / suspicious and are extremely difficult to write in Xojo (if you can write one at all)

You really want to read carefully the Xojo EULA to make sure what you do does not infringe.

Oh and there’s a bunch of existing code on my personal web site that can be used to write a code generator :stuck_out_tongue:
I wrote that about 10 years ago

I don’t think this would be a problem with the Xojo license, it is not for sale. It is just a matter of having some design patterns that I am typing in again and again. One could use cut-and-paste…or one could write an interpreter, or use buttons (say) to cause these design templates to be inserted.

For instance, how many times does one have to write
Dim i As Integer

for i = 0 to myArray.Ubound


Whether that is inserted by typing, cut-and-paste, or pressing a button…I don’t see the difference.

for i as Integer = 0 to myArray.Ubound


for each element as in myArray

There’s ways to accomplish this but this isn’t quite the same as what I posted
You want keyboard macros which are slightly different than “code generation”

Kem posted something on the forums about this
And you can attach an IDE script to a keyboard shortcut
Or use a keyboard recorder / macro program

Not really sure what your code generator is doing. Seems to generate an XML project. I am not so interested in creating a project as I am in inserting code into an existing project.

Hey that IDE scripting is cool. I’ve been using Xojo for a couple of years and didn’t know it was there!

You want something like TypeIt4Me then. It will save you from having to implement anything.
There’s also a snippets function in Dash.

Basically you would type forubound and it would expand to the code you’ve got there.
I use them, for example I have dlog( expand to system.debugLog() and the cursor is already inside the parentheses.

Edit: Norman beat me to the whole you need a macros thing.

Tim, that sounds very useful, I will look into it.

Though the thread is going a little off course. There is another application for code generation, that is, to translate code from another language. For instance, I have some C code, and I would like to have that in Xojo also. In this case, keyboard macros do not apply.

So say I write a program to translate the C into Xojo…what is the best way to programmatically insert that code into a Xojo project? Can it be cut-and-pasted in? There will be multiple functions, constant, properties…I don’t think this will work as a simple cut-and-paste operation.

[quote=257757:@Tom Dowad]
So say I write a program to translate the C into Xojo…what is the best way to programmatically insert that code into a Xojo project? Can it be cut-and-pasted in? There will be multiple functions, constant, properties…I don’t think this will work as a simple cut-and-paste operation.[/quote]
Thats where if you use the code gen classes I pointed out they write xojo classes modules etc
Then you add those to your project (drag & drop)

That Dash looks like a remarkable tool.
There is a multiplication of power that occurs when one tool can use another. I am finding Xojo to be the best tool I’ve ever had for creating applications in this way. For instance, launching other programs, running programs in the shell, loading web pages and scraping them, controlling other apps…you name it. Xojo is a beautiful thing.
Not to mention, putting a Youtube page up so I can listen to favourite tunes at the same time.
With Xojo, who needs the desktop. Write your own.

OK I will study those classes in your codegen project

Is there a way to call a function from a string?
For instance, enter a string into a textbox, then without using a lookup function, executing the text as a function?

you mean like “EXEC( string )” ?

not without you writing code - no

I just tried it with the Evaluator class from the examples…it just gives an error. I think I get the picture. It’s compiled code…you need to link to it.
So one then needs to make a table that maps from each string to a function call.

Perhaps one could put all the desired functions into a xojoscript module, then append to that the function to be called. Like in the example which draws the graphic…a module is defined, and after that, there is text to call it.

Something that isn’t obvious yet is whether one can generate xojoscript objects programmatically
(I think I’ve answered that…yes…just generate the text and assign it to a xojoscript object’s source property)

I suppose the LLVM compiler does not have access to the classes defined within Xojo. For instance, I would guess there is no way to create a Window object in XojoScript?

What is it you’re trying to accomplish ?

IDE scripting isn’t the same as Xojo script and how you use one vs the other is not the same although they share certain characteristics


What I want to do is this
I have some functions written that follow a naming convention
Now I want to be able to call a function from a command line, using an abbreviation for the name
Not that the fact of the abbreviation matters. I want to be able to type in a function call, and see the result.
I am assuming the only way to do this is to have a large Select statement, with a case for each function


Select Case inputText
Case “function1”
Case “function2”
End Select

In C i would make an array of functions, but don’t know that Xojo can deal with function pointers.