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?

7
  • 4
    Yes, it's possible. But it's not easy. Commented Jul 10, 2012 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? Commented Jul 10, 2012 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 Commented Jul 10, 2012 at 11:49
  • 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. Commented Jul 10, 2012 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. Commented Jul 10, 2012 at 12:03

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.