Self-hosting compiler: Difference between revisions

Added FreeBASIC
(Added Algol 68)
(Added FreeBASIC)
 
(22 intermediate revisions by 14 users not shown)
Line 1:
{{difficulty}}
{{draft task}}
 
Implement a self-hosting compiler for your language i.e. create a compiler that is implemented in the language it is supposed to compile. Extra credit for any solution that can compile in an online compiler such as Ideone. If this is not feasible due to language complexity, link to external projects that achieve this task.
 
Implement a self-hosting compiler for your language, i.e.:   create a compiler that is implemented in the language it is's supposed to compile. Extra credit for any solution that can compile in an online compiler such as Ideone. If this is not feasible due to language complexity, link to external projects that achieve this task.
 
Extra credit for any solution that can compile in an online compiler such as Ideone.
 
If this isn't feasible due to language complexity, link to external projects that achieve this task.
<br><br>
 
=={{header|ALGOL 68}}==
 
The Algol 68 to C compiler available here: [httphttps://wwwgithub.poenikatu.co.ukcom/srcsdolim/] or here:algol68toc [https://github.com/NevilleDNZsdolim/algol68toc] is written in ALGOL 68 and compiles itself. It is derived from the ALGOL 68RS compiler.
 
=={{header|ATS}}==
ATS2 is ''not'' self-hosting, but ATS1 (in which ATS2 is written) is. The source repository is here: [https://github.com/githwxi/ATS-Anairiats https://github.com/githwxi/ATS-Anairiats]
 
=={{header|BaCon}}==
BaCon is a BASIC to C converter and it is implemented in itself. Also it can compile itself since version 0.110. Source code can be downloaded from [https://basic-converter.org/ here].
 
=={{header|C sharp}}==
The official (Microsoft) C# compiler became self-hosting in 2011 and can be found (along with the self-hosting VB.NET compiler) at [https://github.com/dotnet/roslyn].
 
The older Mono C# compiler, currently backed by the .NET Foundation, can be found at [https://github.com/mono/mono]. There is also a (much newer) Mono VB compiler.
 
=={{header|FreeBASIC}}==
Essentially, FreeBASIC consists of two parts:
 
1. The FreeBASIC compiler, written in FreeBASIC (self-hosting). Compiling this requires a working FreeBASIC installation.
2. The FreeBASIC runtime libraries, written in C. Compiling this requires a C compiler such as gcc, the GNU C compiler (Native gcc on Linux, MinGW on Windows, DJGPP for DOS).
 
FreeBASIC is a self-hosting compiler, being roughly 120,000 lines of code (compiler core only, not including libraries).
 
Source code can be downloaded from [https://freebasic.net/wiki/DevBuild https://freebasic.net/wiki/DevBuild].
 
=={{header|Go}}==
The principal Go compiler was originally written in C but from version 1.5 became entirely self-hosting (with a little assembler).
 
The source code for the current version can be viewed [https://go.googlesource.com/go here].
 
There is also a second compiler 'gccgo' which is part of the GNU Compiler Collection. However, this compiler is written in C++.
 
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">do =: ".
STDIN =: 1
do read STDIN
Line 16 ⟶ 51:
".1!:1[1
((,~}:)~|.)'Go hang a salami '
Go hang a salami imalas a gnah oG</langsyntaxhighlight>
 
{{omit from|Julia}}
 
=={{header|Kotlin}}==
Line 25 ⟶ 62:
=={{header|Lingo}}==
Propably not how the task was meant by the OP, but if "compiling code at runtime" counts, here Lingo's capabilities to do this:
<langsyntaxhighlight lang="lingo">m = new(#script)
 
-- automatically triggers compilation to byte code
Line 42 ⟶ 79:
-- for direct execution of simple code there is also the do() function
do("put _system.milliseconds")
-- 22476580</langsyntaxhighlight>
 
=={{header|Perl 6Nim}}==
Nim compiler is written in Nim and can be found here: https://github.com/nim-lang/Nim.
 
<lang perl6>use MONKEY-SEE-NO-EVAL;
EVAL slurp</lang>
 
=={{header|Phix}}==
Phix is self hosted. Run "p -c p" and it rebuilds itself in about 15 seconds. One of my favourite parlour tricks is to then run "p p p p p p p p -cp" which stacks seven interpreted copies of itself on top of each other with the last recompiling itself, and apart from a slightly longer startup time, no slower than the shorter command.
 
=={{header|Plain English}}==
Plain English is self-hosted. The Osmosian Order of Plain English Programmers claim that Plain English can recompile itself in 3 seconds. Instructions for recompiling Plain English can be found in the instructions.pdf.
 
=={{header|Python}}==
The [http://pypy.org/ PyPy] project has implemented Python using the Python language. The most popular Python interpreter is CPython, which is implemented in C.
 
=={{header|Quackery}}==
 
The Quackery language is an extensible assembler for a Quackery Engine. The Quackery Engine has a memory model based on dynamic arrays and bignums, so presumes comprehensive hardware support for these features.
 
Program execution consists of the Quackery Processor traversing directed tree-like graphs built from dynamic arrays ("Nests" in the Quackery nomenclature) containing Operators (op-codes), Numbers (pointers to bignums) and pointers to Nests. The Quackery processor is stack-based rather than register-based.
 
Programming in Quackery consists of extending the predefined graphs that constitute the Quackery environment.
 
The [https://github.com/GordonCharlton/Quackery Python 3 implementation at Github] of a virtual Quackery Engine uses Python lists as Nests, Python functions as Operators and Python ints as Numbers.
 
The Quackery processor and a basic Quackery compiler are coded in Python 3, and the Python Quackery compiler is used to compile the Quackery environment, which is written in Quackery and includes a more fully featured (and extensible) Quackery compiler, which is available to the Quackery programmer.
 
That the Quackery language has similarities to Forth (also an extensible assembler for a stack processor), that it leans on Python for support for dynamic arrays and bignums, and that the majority of Quackery is written in Quackery all make for a very compact implementation, under 48k of source code. The downsides are that it is rather slow by modern standards, and that it is by no means "fully featured".
 
(In its defence it is possible to understand the entirety of Quackery in short order, and, once the hurdle of Reverse Polish Notation has been passed, program development with the interactive environment (the Quackery Shell) is quick and rewarding. Quackery is intended primarily for recreational and educational programming, and is a relatively painless introduction to the concatenative programming paradigm.)
 
'''The relevant file at GitHub is [https://github.com/GordonCharlton/Quackery/blob/main/quackery.py quackery.py]''' [https://github.com/GordonCharlton/Quackery/blob/main/quackery.py]
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>use MONKEY-SEE-NO-EVAL;
EVAL slurp</langsyntaxhighlight>
 
=={{header|Rust}}==
[https://web.archive.org/web/20140815054745/http://blog.mozilla.org/graydon/2010/10/02/rust-progress/ Since 2010], the [https://github.com/rust-lang/rust/ Rust compiler] has consisted of a self-hosting compiler frontend built on top of LLVM.
 
A code generator named [https://github.com/CraneStation/cranelift Cranelift], written in Rust, is currently under development with one of its goals being to serve as an alternative to LLVM for debug builds which can provide a better balance between compile time and runtime performance.
 
=={{header|Scala}}==
The Scala compiler is totally written in Scala and compiles itself. The code is sourced and maintained in [https://github.com/scala/scala GitHub] repsitory.
 
=={{header|Visual Basic .NET}}==
The official (Microsoft) VB.NET compiler became self-hosting in 2011 and can be found (along with the self-hosting C# compiler) at [https://github.com/dotnet/roslyn].
 
.NET Mono also has a relatively new VB.NET compiler, which can be found at [https://github.com/mono/mono-basic]. There is also an older Mono C# compiler.
 
=={{header|Wren}}==
Although Wren is an interpreted language, source code is not interpreted directly. Instead a single pass compiler first converts the source code into a simple stack-based bytecode which is then interpreted by the virtual machine (VM).
 
The advantage of this is that syntax errors are flagged before the script even runs and (in unison with several other factors) subsequent interpretation is faster than it would otherwise have been.
 
The VM (which includes the bytecode compiler) and the command line interpreter (which enables Wren scripts to be run independently) are written in C. Most of the core library is also written in C though parts of it are written in Wren itself. Frankly, in the interests of speed and because Wren is primarily intended as an embedded scripting language, self-hosting is never going to happen.
 
Note, however, that Wren can compile Wren source code ''at runtime'' using the Meta.compile method.
 
=={{header|ZED}}==
Source ->
Compiled -> http://ideone.com/UHMQco
<langsyntaxhighlight lang="zed">(*) number1 number2
comment:
#true
Line 1,328 ⟶ 1,412:
comment:
#true
(zed->scheme!) "value" nil</langsyntaxhighlight>
 
=={{header|zkl}}==
2,122

edits