Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:49:55+00:00 2026-05-22T19:49:55+00:00

I want to make a mini programming language in mma. From a textfile to

  • 0

I want to make a mini programming language in mma. From a textfile to Module(s) in a Package. Ideally I should be able to generate the package and modules from within Mathematica by functions in another package.

Question:
Is this possible? I am looking for a reference or an example to get this started.

EDIT:
For example:

Imagine a memory bank with n integer typed registers.

The instructions are:

1 Z(n)

2 C(m,n)

3 J(m,n,q)

4 S(n)

Each line has an address. First line 1, second 2, etc.
Z(n) store 0 in register n.
C(m,n) store value of register m in register n.
J(m,n,q) if value register m equals value of register n then jump to line with address q.
S(n) add 1 to value in register n.

Then given two working programs P and Q I want to generate the concatenated program P+Q.

Then given two working programs P and Q I want to generate the substitution Q after P.

Finally I want to start experimenting with recursion… the purpose of this ‘mini-project’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:49:56+00:00Added an answer on May 22, 2026 at 7:49 pm

    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:

    SetAttributes[testSet, HoldFirst];
    SetAttributes[testIf, HoldRest];
    SetAttributes[testVar, HoldAll];
    SetAttributes[module, HoldAll];
    SetAttributes[{package, inContext}, HoldRest];
    testPlus[x_, y_] := Plus[x, y];
    testTimes[x_, y_] := Times[x, y];
    testDivide[x_, y_] := If[y == 0, Inf, Times[x, Power[y, -1]]];
    testPower[x_, y_] := If[x == 0 && y < 0, Inf, Power[x, y]];
    testSet[HoldPattern[testVar[x_]], expr_] := Set[x, expr];
    testVar[x_] := If[ValueQ[x], x, Throw[$Failed, {"varundef", x}]];
    testIf[cond_, expr_] := If[cond, expr];
    testIf[cond_, expr_, else_] := If[cond, expr, else];
    module[{vars__}, body_] := Module[{vars}, body];
    package[name_, code_] := (BeginPackage[name]; code; EndPackage[]);
    inContext[name_, code_] := (Begin[name]; code; End[]);
    

    Here is a small code snippet in this new language (wrapped in Hold):

    cd = 
    Hold[module[{a}, testSet[testVar[a],
      testPlus[testTimes[testTimes[testPlus[1, 2],
        testPower[testPlus[3, 4], -1]], testPlus[5, 6]], -7]]; testVar[a]]]
    

    It corresponds to this mma code:

    Module[{a},a = (1 + 2)/(3 + 4)*(5 + 6) - 7; a]
    

    Our code-generator is based on a very simple idea – we will repeatedly apply local rules to our held code. The local rules will be extracted from the definitions of our functions, like so:

    ClearAll[expansionRules];
    expansionRules[heads : {__Symbol}] := Flatten[DownValues /@ heads]
    

    We need to supply a list of heads for our language. I will do that manually, but it is easy to automate, by creating custom assignment operators.

    allHeadsToExpand[] := {testIf, testVar, testPlus, testTimes, testDivide, 
          testPower, testSet, testIf,module,package, inContext}
    

    Now, we generate our code:

    In[195]:= expanded = cd//.expansionRules[allHeadsToExpand[]]
    
    Out[195]= 
     Hold[Module[{a}, 
        a = ((1 + 2) If[3 + 4 == 0 && -1 < 0, Inf, 1/(3 + 4)]) (5 + 6) - 7; 
        If[ValueQ[a], a, Throw[$Failed, {"varundef", a}]]]]
    

    To execute it, you can simply use ReleaseHold:

    In[197]:= ReleaseHold[expanded]
    
    Out[197]= -(16/7)
    

    The advantage of our construction is that we can also execute our AST directly:

    In[198]:= ReleaseHold[cd]
    
    Out[198]= -(16/7)
    

    To save this to a package, you can simply use Put command. It is also easy to extend the language in any way you want. Of course, the way the code in this language looks is not pretty, since it is essentially the AST expressed as mma expression. To make it prettier, you’d need to introduce your own syntax and write a parser from it to mma AST, but that is another story.

    EDIT

    Regarding automating of code-generation and saving the generated code into a package: here are a couple of utilities to do that.

    Clear[generateCode];
    generateCode[code_Hold] :=
      code //. expansionRules[allHeadsToExpand[]] //.
       HoldPattern[
          CompoundExpression[left___, CompoundExpression[middle___], right___]] :> 
           (left; middle; right);
    
    Clear[formatCode];
    formatCode[code_Hold] :=
      StringReplace[Function[Null, ToString[Unevaluated[#], InputForm], HoldAll] @@ 
         code, ";" :> ";\n"];
    
    Clear[saveCode];
    saveCode[file_, generatedCode_] :=
     With[{result = BinaryWrite[file, formatCode@generatedCode]},
       Close[file];
       result];
    

    Here is the same example but placed in a package:

    cdp = Hold[
       package["myPackage`",
         inContext["`Private`",
           module[{a}, 
             testSet[testVar[a],
               testPlus[testTimes[testTimes[testPlus[1, 2],
                testPower[testPlus[3, 4], -1]], testPlus[5, 6]], -7]]; 
             testVar[a]]]]]
    

    We generate and save the code as follows:

    In[101]:= file = FileNameJoin[{"C:","Temp","myPackage.m"}]
    Out[101]= C:\Temp\myPackage.m
    
    In[106]:= saved =saveCode[file,generateCode[cdp]]
    Out[106]= C:\Temp\myPackage.m
    

    We can Import it to test:

    In[107]:= Import[file,"Text"]
    
    Out[107]= 
    BeginPackage["myPackage`"];
     Begin["`Private`"];
     Module[{a}, a = ((1 + 2)*If[3 + 4 == 0 && -1 < 0, Inf, (3 + 4)^(-1)])*(5 + 6) - 7;
      If[ValueQ[a], a, Throw[$Failed, {"varundef", a}]]];
     End[];
     EndPackage[]
    

    EDIT 2

    Regarding the way the code in your language will look, you can make this prettier without going all the way to create your own parser, by using the Notation package to alter the way you can input code and Format/FormatValues to control how it is rendered by the FrontEnd.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a 'timeline' containing all mini blog posts from a user,
I am new to OpenGL and want to make cricket stumps for my mini
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make a copy of an ActiveRecord object, changing a single field
I want to make an entity that has an autogenerated primary key, but also
I want to make a .NET Form as a TopMost Form for another external
I want to make sure people can't type the name of a PHP script
I want to make sure that a set of functions have the same signature
I want to make a JavaScript application that's not open source, and thus I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.