# Generate Haxe Source Files From Haxe?

**URL:** https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550
**Category:** Haxe
**Created:** [February 25, 2019, 3:53pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550 "2019-02-25T15:53:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![RMax](https://community.haxe.org/user_avatar/community.haxe.org/rmax/32/662_2.png) [@RMax](https://community.haxe.org/u/RMax)
#### Post date: [February 25, 2019, 3:53pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/1 "2019-02-25T15:53:27Z")

</div>

**Problem: Need to generate 1 or more Haxe source file(s) using a Haxe program.**  
Haxe sources generated should compile OK.  
**Dependencies of the generated files need to be included in typical Haxe fashion.**

**Approaches: A class name** (ex: GenMain) that **either already exists within the Haxe to Haxe generator** or **else is part of a physical file set.** GenMain is treated as the output Main class. So GenMain gets renamed and refactored as part of the generation steps.  
Refactoring as you might guess is where the EXCITEMENT happens.  
Not just the GenMain class but also ANY other class GenMain uses.

**Solution 1: Do this within my project (somewhat selfishly).**  
Likely approach is using a file template approach.  
**Right now my project does not come close to using all the features of Haxe.**  
**Pros:** I know what is needed for my project, and **my time is directly spent as needed.**  
**Cons: NO benefit for anyone else (at least for now).**

**Solution 2: Work on a Haxe Library that allows others to benefit.**  
**Pros: Community benefits.**  
**Cons:** Very likely more demanding than my project now. Maybe MUCH more.  
Difficult (for me as relatively new Haxe coder) to estimate how much effort is needed.  
Also Difficult as I do not know other Use Cases that add Requirements for more Haxe Features to support. **Also I have to balance working on my project vs this approach.**

**Solution 3: Either get buy in or start work on a new Haxe target language -haxe**  
If I may be allowed, I see this as actually a logical progression of what Haxe is all about.  
**"The generation of source files in programming languages that help with solving programming problems." What more help is possible than Haxe itself ?**  
Another way to state this is: **_Haxe: You got Expressions, Macros, and Haxe itself!_**  
**Pros: Community benefits.**  
**Cons:** same as for Solution 2

Hopefully I am foolishly missing something.  
**Perhaps there is another _Approach_ or _Solution_ out there ?**  
**Because of my own time constraints I would work on 1) now** and later refactor the limited solution to be a Haxe Library sometime in the future.

Wanted to forward the Haxe generating Haxe idea for your kind input 🙂  
**Thanks All !**

---

<div class="post-metadata">

### Author: ![mark.knol](https://community.haxe.org/user_avatar/community.haxe.org/mark.knol/32/50_2.png) [@mark.knol](https://community.haxe.org/u/mark.knol)
#### Post date: [February 25, 2019, 3:59pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/2 "2019-02-25T15:59:36Z")

</div>

> Problem: Need to generate 1 or more Haxe source file(s) using a Haxe program.

Why is this a problem? Or why do you even need this? 🙂

Short answer: There are already thoughts/ideas for haxe-in-haxe [https://github.com/HaxeFoundation/haxe/issues/6843](https://github.com/HaxeFoundation/haxe/issues/6843)

BTW you are using **a lot of bold text**

---

<div class="post-metadata">

### Author: ![Simn](https://community.haxe.org/user_avatar/community.haxe.org/simn/32/388_2.png) [@Simn](https://community.haxe.org/u/Simn)
#### Post date: [February 25, 2019, 4:21pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/3 "2019-02-25T16:21:16Z")

</div>

I’m sure you’re a nice guy but the way you format your posts gives me cancer.

On a more constructive note, you might want to look at haxe/macro/Printer.hx.

---

<div class="post-metadata">

### Author: ![RMax](https://community.haxe.org/user_avatar/community.haxe.org/rmax/32/662_2.png) [@RMax](https://community.haxe.org/u/RMax)
#### Post date: [February 25, 2019, 5:26pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/4 "2019-02-25T17:26:46Z")

</div>

@mark.knol@Simn  
Thanks for the info.  
I will try for less _Verbose_ and _Bold_  
Capitalization for Emphasis may still escape my fingers 🙂

_Why is this a problem? Or why do you even need this?_

Project allows programming/syntax that is more like a pseudo language than a Programming Language style/syntax. Users/Programmers output of their programs to Haxe sources would be very valuable as they could also learn about the various Haxe target languages and Haxe itself.

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [February 25, 2019, 5:50pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/5 "2019-02-25T17:50:52Z")

</div>

If you want to make a programming language that targets haxe you’ll need to make a compiler.  
This library can help you make a lexer/parser for it: [GitHub - Simn/hxparse: haxe Lexer/Parser library](https://github.com/Simn/hxparse)

---

<div class="post-metadata">

### Author: ![RMax](https://community.haxe.org/user_avatar/community.haxe.org/rmax/32/662_2.png) [@RMax](https://community.haxe.org/u/RMax)
#### Post date: [February 25, 2019, 6:20pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/6 "2019-02-25T18:20:51Z")

</div>

_Thanks_ for the info!  
Not sure I need a compiler right now, but may be better for performance later.

---

<div class="post-metadata">

### Author: ![sundialservices](https://avatars.discourse-cdn.com/v2/letter/s/9e8a1a/32.png) [@sundialservices](https://community.haxe.org/u/sundialservices)
#### Post date: [February 27, 2019, 2:55pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/7 "2019-02-27T14:55:49Z")

</div>

Even if you aren’t “making a compiler,” lexers and parsers – and, they are legion – are a very good technology to be familiar with. You can easily handle very-complex inputs of any sort, by _describing_ that input (as a “grammar”) and thereby pushing “all the niggling character-juggling” off to _somebody else’s_ code. 🙂

> (Show of hands, please … does _anybody_ like to juggle characters? … Anybody? …)

“It’s a well-known and well-studied problem,” and “everybody needs to do it at some time or another,” and this broad class of tools “does it very well.”

---

<div class="post-metadata">

### Author: ![RMax](https://community.haxe.org/user_avatar/community.haxe.org/rmax/32/662_2.png) [@RMax](https://community.haxe.org/u/RMax)
#### Post date: [February 27, 2019, 5:42pm UTC](https://community.haxe.org/t/generate-haxe-source-files-from-haxe/1550/8 "2019-02-27T17:42:51Z")

</div>

@sundialservices, All,

**Summary for the Impatient:**  
This is all background and Please **skip if your Verbose setting is Low**

Agreed. _Compilers_ are awesome tools. Been using them since mid 80s.  
I spent more than a little time looking into using an existing Compiler/Interpreter.  
Finally decided to write my own Interpreter.

**_Why you ask?_**  
The project is (right now) my own somewhat _skewed_ **_\*Hobby_**.  
I am learning from my mistakes.  
This is my first Interpreter.  
I found Haxe and wanted to give it a try.  
I like a lot of what Haxe offers.

_Technically_ the project allows nearly any mix of _Infix_, _Prefix_ or _Postfix_ notation.  
You realize that nearly All programming languages have the concepts of **LHS** and **RHS**?  
And will be adding support for **Parsing RIGHT to LEFT** (Ex: Hebrew, Arabic)  
The project has 3 types of Assignment statements:

1. What other programming languages use (this is resolved by Parser/Interpreter)  
**=** Assign to Left the value/expression evaluation of the Right  
**x = 42 .**
2. Assign to Left (similar to **=** but always deterministic)  
**:=** where you see the Colon **:** is the side that gets changed  
**x := 7 \* 6 .** Or **x from 42 .**
3. Assign to Right  
**45 radians sin into x .** Or **sin( radians( 45 ) ) =: x .**

3 different Assignments are to allow easier learning of Computer Programming coming from a non English and possibly not deeply technical background.  
The _ **from** _ and **_into_** allow a logical way to express relationships at the Natural Language level but are alias for the internal **:=** and **=:**

Also Semantically the ordinary **=** Assignment can be extended to something like:  
**x \* y = 5 - z .**  
Which conventional programming languages do not handle but in the Future the project may allow for more of a **_Symbolic_** approach to Systems of Equations.  
Like 4 Equations and 4 Unknowns for example.

Some of you may recognize a **Forth** like syntax above.  
The Interpreter implements a Stack based approach that is more verbose than Forth.  
You may note that a simple period is used to end a statement. **, ; :** may also be used.  
Project uses period as example as it fits Natural Language syntax better (at least for statements in European languages).

Looked at some **BNF** and **EBNF** and **PEG** tools.  
I found 1 tool that was flexible enough but was big $ and proprietary.  
None of the other tools offered a solution. Would need to patch tools together at Best.  
At worst I would be Debugging assumptions about LHS and RHS Grammers/Parsers, etc.

Earlier said I was working on a rough _Prototype_.  
Right now, I do not care about Performance only _Correctness_.  
If I really cared about performance now, I likely would have finished version 0.1 in **C++**  
and used **LLVM** or various other compilers for distribution to what platform(s). Similar to options allowed kindly by hxcpp.

**But I wanted to learn to be dangerous with _JavaScript_** as well.  
I have the entire Interpreter running as a Web Worker with a primitive UI in HTML.  
_No CSS, No JavaScript libraries._  
Need more work on Web side to do same as Console apps in **C++, C#, Java, Python**

**_(Show of hands, please … does anybody like to juggle characters? … Anybody? …)_**

Imagine me holding up Both hands. 😄  
First full time Programming job was 8086 ASM on original IBM PC using **Edlin** and **MASM** and **Debug** (my humble **_SKEWED_** beginnings, so this is Easier!)

**Approaches**  
User/Programmer defines words in **simplified** form of their **own natural language** in a Dictionary. I use _ **.toml** _ style as Dictionary Input/Output.  
**Verbs** , **Nouns** , **Operators** , **Built In Verbs** are provided.  
**Verbs** are like Code. Now most examples are **Imperative**.  
In future more **Declarative** is planned.  
**Nouns** are like Data  
Nouns can be declared **within a block in a Verb** and those act like **local variables**.  
**Operators** do well known actions. Some use Data, some produce Data, some both.  
**Built In Verbs** do well known actions in a Declarative way. **Show** or **Repeat** for example.  
**Constants like Numbers or Strings or Bools** can be sprinkled in for extra flavor.

**Abstract/Concrete Syntax Array** and **not Tree**  
This allows easy tracking of Reading order which I think is important for both Users/Programmers and my own sanity. Also not sure how to support well Reading order within a Tree representation. Also (perhaps) less complexity than a Tree. Also suspect either 3 Tree traversal approaches or 3 different trees to handle the various prefix/infix/postfix combinations. Also actually maps much closer to ASM than a Tree does. Perhaps output to WASM or LLVM IRL sometime later.

**Paradigms**  
**_Concatenative_**  
User/Programmer sees a Data stack as part of learning.  
Data stack is passed in to a called Verb. Verb returns values by adding to Data stack.  
_Why?_  
Because every general purpose CPU and many embedded CPU have Stack Registers/Segments. I felt that knowing a little about a Data stack is useful.

**_Call by Syntax_**  
Supported (in progress today) as part of _ **Export** _ the flexible syntax to more conventional programming languages like Haxe  
Haxe to learn programming, **_WOW !_**  
Haxe to learn programming in about a dozen programming languages **_WOW squared!_**

**_Declarative_**  
Limited examples now like **Show** or **Repeat**  
Future direction. Consider this as applying the SQL approach to databases in a way as a DSL to programming languages or a Programming Design Language. There are many examples of various DSL making programming easier and less error prone.

**_Functional_**  
Future direction. Thinking about integration with **Haskell** or **F#** or joy or ?

**_Programming by Logic_**  
Future direction. Thinking about integration with **ProLog** or similar.  
A lot of AI now started with ProLog and a fair amount still use ProLog.

**_Code Generation_**  
Future. Likely do 1) easily and quickly.  
Add support to Generate new Verbs in 2 different ways:

1. **Change a Noun definition until it looks like a Verb.**  
Use a reserved keyword like to\_Verb to take the Noun and make a new Verb.  
Run the new Verb as would run any other.
2. **_Create 1 or more Verbs from embedded Comments_**  
Comments may use a specific format to enable/encourage some extra parsing.  
Extra parsing may be more on Declarative side than Imperative.  
Two sources of idea for 2)… current Database engine research allows for Declarative description of Data (like SQL logical layer) and then research engine figures out details of Data Structures and Algorithms. Found from the **Daily Paper** site. Other idea is my epiphany that the Semantic contents of Comments can act as place holders to do a similar approach for not just Databases (_“a well-known and well-studied problem”_) but more generally for other programming patterns.

**_Library Interface_**  
Future. Leaning toward using _ **.toml** _ file to declare a **_IDL_** style interface.  
I like IDL idea as it implements the **_iUnknown_** interface so that existing Libraries will work and new Libraries may be added with new features without breaking old interfaces.  
Also allows runtime Documentation from actual interfaces.  
May allow Dynamic Libraries that do not consume Resources when not being used.

_ **Target Platforms** _  
Phones, Smart Phones, Tablets, Laptops, Desktops, etc.  
Project is intended to not consume large resources of the platform.

_A problem being well known and well studied does NOT mean it is well **Implemented**._  
Current claim is at **_Prototype_** set of expectations, Implementation internally is not significant. Only significance is Correctness of Results.

My assertion is that it only takes 1 counter example to show another way as an effective approach. Have read on the order of 500 papers/articles & several books. On the order of 100+ related to the problem/design/solution/implementation domains of the project.

Mid 90s wrote a Windows or NetWare multithreaded database engine that was anywhere from 2x to 10x faster than SQL DB on same HW/OS. Also easy to fall into Sorting too late trap. “When Sorting Needs To Be Faster”.

_\ ***Hobby** _ now defined as 10+ K lines of Haxe with 3+ K lines of Comments in about a dozen source files, all Haxe except for 1 HTML and 1 JavaScript for UI front end of Web Interpreter.  
**Having Fun, Hope You Are Also !**
