2

I have an idea to create scripting language which will make people to program easier doing a lot of macros, functions, simpler function names everything more simpler for simple person to work with. That code (custom scripting language) then should be translated to simple C language things. Like so:

Scripting:
IO[9].high
@include "lib"
for (int i=0 to 55)

end
C:
IO |= (1<<9);
#include "lib.h"
int i = 0;
for (i=0; i<55; i++) {

}

Is it possible somehow efficiently write this macro/scripting language which would nicely output to c code?

Aurimas Niekis
  • 227
  • 1
  • 14
  • 4
    Yes, it's possible. But it's not easy. – Oliver Charlesworth Jul 10 '12 at 11:48
  • 12
    Of course it is possible: after all, it's a simple conversion of text to text. Do you think The World needs yet another syntax to represent C programs, though? – Sergey Kalinichenko Jul 10 '12 at 11:48
  • It's possible to do this, but it's not usually the right solution. You would (possibly) be better off writing a front end for something like clang/llvm. [Example](https://donsbot.wordpress.com/2010/02/21/smoking-fast-haskell-code-using-ghcs-new-llvm-codegen/) – Flexo Jul 10 '12 at 11:49
  • @Flexo it's difficult to write front end because it's not x86 :D – Aurimas Niekis Jul 10 '12 at 11:53
  • 2
    Why would "simple people" want to program in C? Give them Python or something like that, and they can get simple jobs done easily. – Kerrek SB Jul 10 '12 at 12:03
  • 1
    Just a thought, but if you're going to create a C scripting language, you might not want to change the meaning of existing C syntax. – FatalError Jul 10 '12 at 12:03
  • @KerrekSB it's only embedded C or ASM is possible... – Aurimas Niekis Jul 10 '12 at 12:10

3 Answers3

3

Absolutely; some languages which compile (or have compiled) into C include Eiffel, Haskell (GHC), Vala, and Squeak. The earlier versions of C++ were implemented as a C++-to-C translator (CFront).

The general concept is an intermediate language. C is mentioned as being used as an intermediate language; by making use of a C compiler you gain binary compatibility with many languages and libraries and avoid having to write your own compiler; Ritchie describes C as being used as a "portable assembly language".

However, as with C++ you might quickly find that targeting C becomes overly restrictive; GHC are moving towards a dedicated intermediate language called C--, and you might find that using the LLVM toolchain (essentially, targeting the LLVM intermediate representation) is a better approach for multi-platform compatibility.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • The problem is that I developing this "language" for 8bit micro-controllers and I can't use llvm and other cool stuff I am very limited to avr-gcc. Now I think that only solution is preg_match many things and replace to C equivalents. – Aurimas Niekis Jul 10 '12 at 12:43
3

Thanks to PyPy, it is possible to translate a subset of Python code into c.

For further details, see the following reference : http://doc.pypy.org/en/latest/translation.html

See also the following question, which is basically your question specifying Python as the scripting language : Use Cython as Python to C Converter

Community
  • 1
  • 1
Nicolas Barbey
  • 6,639
  • 4
  • 28
  • 34
1

For example, Have you seen mib2c script from netsnmp?. It generates C source using script. You can refer to that for getting more ideas.

Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88