How to dynamically generate mathematica code?

Your question has several parts. First, if you want to use some non-mma syntax for your language, you need to make a parser from your language to mma expression (AST of your code). I will leave this one out (since this is a separate topic) and assume that you are willing to use mma syntax or have means to transfer your program to some mma expression.

Your question has several parts. First, if you want to use some non-mma syntax for your language, you need to make a parser from your language to mma expression (AST of your code). I will leave this one out (since this is a separate topic) and assume that you are willing to use mma syntax or have means to transfer your program to some mma expression.

Regarding the mma code generation, Mathematica is very well suited for it since it embraces the code-is-data paradigm. The hardest part here is the evaluation control - we want to make sure that none of our generated code pieces evaluates during the code-generation process. The standard techniques of evaluation control can be successfully used for that, but this will generally make things rather complicated.

I will illustrate one technique of mma code generation, which is not the best/most powerful one, but the easiest. Consider a toy language created by these definitions: SetAttributestestSet, HoldFirst; SetAttributestestIf, HoldRest; SetAttributestestVar, HoldAll; SetAttributesmodule, HoldAll; SetAttributes{package, inContext}, HoldRest; testPlusx_, y_ := Plusx, y; testTimesx_, y_ := Timesx, y; testDividex_, y_ := Ify == 0, Inf, Timesx, Powery, -1; testPowerx_, y_ := Ifx == 0 && y (left; middle; right); ClearformatCode; formatCodecode_Hold := StringReplaceFunctionNull, ToStringUnevaluated#, InputForm, HoldAll @@ code, ";" :> ";\n"; ClearsaveCode; saveCodefile_, generatedCode_ := With{result = BinaryWritefile, formatCode@generatedCode}, Closefile; result; Here is the same example but placed in a package: cdp = Hold package"myPackage`", inContext"`Private`", module{a}, testSettestVara, testPlustestTimestestTimestestPlus1, 2, testPowertestPlus3, 4, -1, testPlus5, 6, -7; testVara We generate and save the code as follows: In101:= file = FileNameJoin{"C:","Temp","myPackage. M"} Out101= C:\Temp\myPackage.

M In106:= saved =saveCodefile,generateCodecdp Out106= C:\Temp\myPackage. M We can Import it to test: In107:= Importfile,"Text" Out107= BeginPackage"myPackage`"; Begin"`Private`"; Module{a}, a = ((1 + 2)*If3 + 4 == 0 && -1.

2 Leonid's answer /. {AST->"Abstract Syntax Tree"} – belisarius Jun 2 at 13:55 @belisarius Thanks! I will probably have to edit this and add more explanations.Oops... It took me a while to understand the deep code-generating nature of your comment :) – Leonid Shifrin Jun 2 at 13:56 2 I didn't even bother answering this question, as I knew you'd be by to post something much better.

– Mr.Wizard Jun 2 at 15:48 1 @Mr.Wizard Thanks. This is the topic of real interest to me. – Leonid Shifrin Jun 2 at 16:39 2 @Leonid: I cannot unsee this: Hold package"myPackage`", inContext"`Private`"... ಠ_ಠ... If that was intentional, you're a frickin' genius!

– yoda Jun 27 at 2:50.

It is tangential to the question, but you may find important utility in the setting CellEvaluationFunction as described in a post by WReach.

Thank you @Mr.Wizard. Very interesting. – nilo de roock Jun 4 at 18:09 That's one of my favorites from the 'tool bag'.

I really wish something like that were covered in the documentation. (+1 for spreading the info) – telefunkenvf14 Jun 30 at 18:33.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions