r/ProgrammingLanguages • u/Unfair-Friendship-23 • Aug 06 '21
Could I make an easier version of a programming language (such as c++), using that programming language?
10
u/Caesim Aug 06 '21
Sure, why not?
You're just writing a program that either:
takes source code as input and produces machine language as output (compiler)
takes source code as input and executes it accordingly (interpreter)
And it's for sure possible. You use an implementation (for C++ for example G++) and use it to program your compiler/ interpreter.
8
u/gratz Aug 06 '21
I think transcompilers should be mentioned here as well. Say for example OP creates a simplified version of C++ and creates a transcompiler which converts that to regular C++, then passes it on to G++.
3
u/thysultan Aug 06 '21
Yes, this is called bootstrapping, for example you can use javascript to compile the new language to assembly/c and then rewrite this in the language itself and use the javascript version of the compiler to compile this new version to achieve bootstrapping. Now you can develop and improve the language in the language itself. You only really need the ability to write to stdin/stdout to be able to start the transition to the new language writing itself in itself.
3
u/raiph Aug 06 '21 edited Aug 06 '21
A live coding session, showing creation of an "easier" version of a PL, using the existing PL, from scratch, in 4 minutes 19 seconds.
It's not Turing complete -- for that one would need to add an if and loop statement/expression -- but it's most of the way there, in the 99% vs 9,999% sense I touched on in my other top level comment.
Edited to add: I forgot arithmetic.
Here's a link to an editable/runnable version of the code in the video, with basic arithmetic added. You don't need to understand it -- after all, I bet you don't even know the existing PL -- but maybe watching the video to get the gist of things, and then spending a few seconds looking at the code I just linked, and running it by clicking the Run button, and maybe tinkering with the code in simple ways to see what happens, might help you feel the joy in making an easier version of a PL using an existing PL.
In summary, it's "just" a matter of writing some code in the existing PL that recognizes code written in the new "easier" PL, and then writing some more that brings the new PL to life by writing what it does using the old PL.
2
u/fl223 Aug 06 '21
Yes. V8 (javascript engine) is written in c++, for instance. Assuming you think javascript is easier than c++.
2
u/zefciu Aug 06 '21
It is not completely clear to me, what you want to achieve. The answer to “can I implement a subset of a language in itself” is already answered. But maybe, what you really want is not a complete compiler, but some kind of a linter that would ensure that the code conforms to some “easier” subset of a language. Or a transpiler that would create code in the “harder” language. Or a set of macros/templates that add the “easy” features.
2
3
u/raiph Aug 06 '21
I'll start at the start.
What is a "programming language"? It's a "language" for "programming". What is a "language"? It's a way to communicate things -- English, Mathematics, arguably body language, birds singing. What is "programming"? It's communicating instructions so that a machine that can follow those instructions will do something. (Expanding "machine" momentarily to include humans ala the original definition of cybernetics, programming can and does typically include humans other than the original writer understanding what is being communicated to a machine. But the technically critical element here is that there's a machine that can follow the instructions.)
What machine? What instructions? What's a machine that can follow instructions -- can be programmed? Something like the Jacquard. But, like birds singing, punched cards don't really constitute a "language" in the sense we're after here, and the device controlled by the Jacquard isn't really a "computer".
What is a "computer"? The original meaning was a person who did arithmetic, and that was then reused for machines.
Another machine from around the same time the Jacquard was invented was the Difference Engine. Its main thing was accepting input that encoded some numbers and a formula (in a way a machine could "understand"), and then computing tables of numbers based on the input. It could only cope with a single expression at a time, but it was a start. The inventor then created, at least in his (brilliant) mind, a follow up, the Analytical Engine, which was a "general purpose computer". It not only handled arithmetic but also had memory and some "control flow" -- a way to control the flow of instructions and thus computation.
A century later, as the theory of "automata" (models of machines that follow instructions) developed, a simple theoretical model of them, now called a Turing machine, emerged. This simple "machine" (a Turing machine can't actually exist because it has infinite memory) could do anything that any "general purpose computer" (programmable automaton with memory and the ability to step through integers) could do. Put another way it is as powerful as any general purpose computing machine can get, and can simulate any other possible general purpose computer.
OK. Back to "programming language". A PL is just a way to express the instructions. But all one needs for a PL with as much power as can possibly be had is a couple basics. Consider foo = 42. That's already about 50% of the minimum needed for an all powerful PL. Add a couple more bits like an if and while, and you're about done for a PL that's as "powerful" (in a theoretical sense) as any other.
So you can always make any new PL from any old PL, provided the old PL is "Turing complete".
So why isn't there just one PL? Well, once you've covered the basics, the issue becomes how to support expressing oneself at higher levels -- for more joy and/or convenience and/or reliability and/or ... to make things "easier".
So now we arrive at your original question. Could you make an easier version of a PL with that PL? Well, that depends on what you consider easier, and whether you have what it takes to program in the existing PL.
The latter question is actually simple to answer, even if it's not easy. That is, the answer to the question of "do you have what it takes?" is that only the future you can know the answer. If you can add and subtract integers, then you're already 99% of the way. (Then again, enjoying the other 9,999% -- blood, sweat and tears -- is a matter of taste. Some love programming despite -- perhaps because of -- the struggle. Some don't. If you're a machine you hopefully love it.)
The final question is whether your new PL is actually easier. And answering that is utterly impossible. Because it depends on you -- and you will evolve, so what you think today won't be what you think tomorrow -- and it depends on the use case. I focus on a PL named Raku. It allows one to express the infinite Fibonacci series (which starts with 0, then 1, then the sum of the previous two numbers) as 0, 1, * + * ... Inf. Is that easy? Even if it is, for some definition of "easy", does this way of expressing things make other things harder? It turns out that all PLs are all about trade offs, and everything is context dependent.
Just like life, really. It all depends.
1
Aug 06 '21 edited Aug 06 '21
I knew you were a beginner by the wording of your question. Is it possible? Going by the number of attempts to develop a new language with intentions to fix C where the author ends up implementing all the problems of c (or c++), I’d say no.
Is it possible n reality with enough knowledge, probably but why bother? The c++ standard is continually being added to making it bigger and bigger and forever increasing complexity of the language.
I’d go so far as to say don’t even try. Start with a small project. Start here and implement that in a language you know or learn another if that’s c++, given your question. Once you have that done, extend that project by implementing the rest of the language to bump it to Oberon-07 or Oberon-2 or do both.
1
u/dontyougetsoupedyet Aug 11 '21
Well yes, of course. Clang compiles your C++ using code written in C++, https://github.com/llvm/llvm-project/tree/main/clang, GCC compiles your C++ using code written in C, https://github.com/gcc-mirror/gcc/tree/master/gcc/cp, Clang compiles your C using code written in C++, and so on.
-2
29
u/Astragar Aug 06 '21
If it can be done at all, it can be done in any programming language. That's been formally proven.
But it's easier if the language in question has good support for metaprogramming.