Intermediate language selection

I am currently playing with programming languages. I spent some time creating parsers and interpreters in high-level languages ​​(primarily haXe).

I had some results, which, in my opinion, are actually quite pleasant, but now I would like to make them quick.

My idea was to translate the input language to C.
My C knowledge is limited to what you study at university. Besides some exercises, I never wrote real programs in C. But I'm sure I can make it work.

Of course, I could try writing an interface for LLVM or creating MSIL or JVM bytecode. But I feel that there is too much to learn right now, and I do not see much benefit in fact. In addition, C is human-readable, so if I messed up, it’s much easier to understand why. And C, after all, is a high level. I can really translate concepts from the input language without excessive intelligence. I should have something working and working within a reasonable time, and then optimize it as I see fit.

So: Are there any flaws in using C? Can you recommend an alternative?
Thank you for your understanding:)


Edit: Some clarification

  • The reason I want to go all the way is because I am writing a language with OOP support, and I want to actually implement my sending method manually, because I have something very specific.
  • The main area of ​​use would be to write HTTP services, but I could add bindings to the GUI library (possibly wxWidgets) or something else.
+6
source share
4 answers

C is a good and fairly popular choice for what you are trying to do.

However, take a look at the intermediate language LLVM (IR). It's pretty readable, and I think it's cleaner and easier to generate and analyze than C. LLVM comes with a pretty large collection of tools to work with it. You can generate your own code for various platforms (as for C, but with a bit more control over the output) or for virtual machines. The ability to compile JIT is also a plus.

See Open Source Application Architecture, Chapter 11, for an LLVM approach and some IR snippets.


What is your target environment? This can help us give you a better answer.

+6
source

C is actually a pretty good choice for the target language for a small or experimental compiler - it is widely available on many platforms, so your compiler becomes immediately useful in many environments. The main drawback is dealing with things that are poorly supported in C or not defined in the C specification. For example, if you want to create dynamic code generation (JIT compilation), C is problematic. Things like splitting and flipping a stack are complex in C (although setjmp / longjmp and careful use of structures for which you create layout descriptions can do a lot). Things like word size, small or small end, and arithmetic accuracy differ between C compilers, so you should be aware of this, but this is what you need to deal with if you want to support multiple target machines anyway.

Other languages ​​can be used - the main advantage of C is its ubiquity.

+2
source

You might consider a C-- , a C-like language designed for a better purpose for generating code than C.

+2
source

C is a good choice, IMHO. Unlike many languages, C is generally considered "elegant" because you have only 32 keywords and very simple constructs (sequence, selection, iteration), with a very simple and consistent collection of tokens and operators.

Since the syntax is very consistent inside C (brackets and braces, blocks and expressions, using expressions), you do not go into the unlimited world of language expansion. C is a mature language, it has withstood the times, and now it is a "known amount" (which is very difficult to say about many other languages, even "mature").

0
source

Source: https://habr.com/ru/post/890518/


All Articles