Hello World

gal is a small programming language that compiles to six others. In this chapter, we will write the "Hello World" program in gal. Then we will compile it into Python, JavaScript, Go, Java, MUMPS, and C++. Every version will print the same thing. At the end of this chapter, you will have seen the whole pipeline in action. gal stands for general abstract language.
Hello World in gal
module Hello
{
main
{
say "Hello, World!";
}
}
This is the "Hello World" program in gal. The syntax is deliberately simple, because the compiler has to understand it well enough to rewrite it in other languages. The module statement creates a module called "Hello". A module is a kind of program. The main statement defines the main thing that will happen when we run the program. The say statement causes the program to say "Hello, World!".
Python
module Hello
{
main
{
say "Hello, World!";
}
}
Let's compile this gal program into Python.
$ galpy hello.gal hello.py
The galpy command compiles a gal program into Python. You have to install gal for that to work. This is a simple program, so we can use galpy directly. The galpy command didn't print any output, which means there were no errors. Let's see what the new Python program looks like.
$ cat hello.py
# module: Hello
if __name__ == '__main__':
print('Hello, World!')
# module: Hello
if __name__ == '__main__':
print('Hello, World!')
This is the same program, in the Python syntax. Python has a different way of handling modules, defining the main part of the program, and printing output. The syntax is different, but the meaning is the same. Now let's run the program to see if it works.
$ python3 hello.py
Hello, World!
So we ran the program and it printed, "Hello, World!". Exactly what we expected. You have to install Python for that to work.
JavaScript
module Hello
{
main
{
say "Hello, World!";
}
}
Let's compile this gal program into JavaScript.
$ galjs hello.gal hello.js
The galjs command compiles a gal program into JavaScript. It doesn't produce output, so it worked. Let's see what the new JavaScript program looks like.
$ cat hello.js
// module: Hello
// main program
console.log('Hello, World!');
// module: Hello
// main program
console.log('Hello, World!');
This is the same program, in JavaScript. It has comments about module and main, and it logs "Hello, World!" to the console. Once again, let's run the program to see if it works.
$ node hello.js
Hello, World!
We ran it and it printed, "Hello, World!". Just as expected. You have to install nodejs for that to work.
Go
module Hello
{
main
{
say "Hello, World!";
}
}
Now let's compile this gal program into Go.
$ galgo hello.gal hello.go
The galgo command compiles a gal program into Go. Let's see how it looks.
$ cat hello.go
package main
import "fmt"
// module: Hello
func main() {
fmt.Println("Hello, World!")
}
package main
import "fmt"
// module: Hello
func main() {
fmt.Println("Hello, World!")
}
So in Go, a module becomes a package. It has to import fmt. There is a function named main. And it prints Hello World.
$ go build hello.go
We use 'go build' to compile the program. You have to install Go for that to work. It doesn't print any output, so it compiled without errors. Let's run it.
$ ./hello
Hello, World!
And it worked.
Java
module Hello
{
main
{
say "Hello, World!";
}
}
Now let's compile this gal program into Java.
$ galjava hello.gal Hello.java
The galjava command compiles a gal program into Java. Notice that I capitalized the filename 'Hello.java'. Java has expectations about the file names that you use. Let's see how it looks.
$ cat Hello.java
@SuppressWarnings({"unchecked", "cast", "serial", "rawtypes", "this-escape", "deprecation", "removal"})
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
@SuppressWarnings({"unchecked", "cast", "serial", "rawtypes", "this-escape", "deprecation", "removal"})
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
So in Java, a module becomes a public class. There is a static function named main. And it prints Hello World.
$ javac Hello.java
We use 'javac' to compile the program. You have to install Java for that to work. As usual, no news is good news. Now I'll try to run it.
$ java Hello
Hello, World!
It worked!
MUMPS
module Hello
{
main
{
say "Hello, World!";
}
}
Now let's compile this gal program into MUMPS. MUMPS is the oldest language on this list, and the strangest. It was born in 1966, in a Massachusetts hospital. It has no objects, one data structure, and a database instead of a heap. If gal can produce MUMPS, it can produce anything.
$ galmumps hello.gal Hello.m
The galmumps command compiles a gal program into a MUMPS routine. Let's see what it looks like.
$ cat Hello.m
;;;ROUTINE Hello
Hello ; gal ; Hello module.
do init^galrt
do main()
quit
main() ; main program
new %etin set %etin=$etrap
new $etrap,$estack set $etrap=%etin
use $principal write "Hello, World!",!
quit
;;;ROUTINE Hello
Hello ; gal ; Hello module.
do init^galrt
do main()
quit
main() ; main program
new %etin set %etin=$etrap
new $etrap,$estack set $etrap=%etin
use $principal write "Hello, World!",!
quit
So in MUMPS, the module becomes a routine. The first line names the routine Hello. The do init^galrt line calls a tiny runtime that every generated MUMPS program needs. And the write statement prints Hello World, with an exclamation mark for the new line. MUMPS is a line-oriented language, so most lines start with a command word like do, set, use, write, or quit.
Running MUMPS takes one more step than the others. We use the YottaDB implementation of MUMPS. First we load its environment settings, and tell it to look for routines in the current directory. Then we run the routine with mumps -run Hello.
$ . /usr/local/etc/ydb_env_set; export ydb_routines="$PWD $ydb_routines"; mumps -run Hello
Hello, World!
And it worked. You have to install YottaDB for that to work, and that is the last piece.
C++
module Hello
{
main
{
say "Hello, World!";
}
}
Finally, let's compile this gal program into C++.
$ galcpp hello.gal hello.cpp
The galcpp command compiles a gal program into C++. Let's see how it looks.
$ cat hello.cpp
#include <iostream>
// module: Hello
int main() {
std::cout << "Hello, World!\n";
return 0;
}
#include <iostream>
// module: Hello
int main() {
std::cout << "Hello, World!\n";
return 0;
}
So in C++, a module is just a comment. There is a function named main. It has to include iostream to print something to standard output. And it prints Hello World. And it returns 0, indicating that no error occurred.
$ g++ hello.cpp -o hello2
We use 'g++' to compile the program. You have to install g++ for that to work. I named it 'hello2' to avoid confusion with the earlier program we created. Once again, no news is good news. Let's run it.
$ ./hello2
Hello, World!
It worked!
The six outputs agree
We compiled the same gal program into Python, JavaScript, Go, Java, MUMPS, and C++, and every one of them printed "Hello, World!". Six run commands, six runtimes, one line of output, and it was always the same line. gal's oracle checks this for every example program, byte for byte.
Python to gal to JavaScript
$ cat hello.py
# module: Hello
if __name__ == '__main__':
print('Hello, World!')
# module: Hello
if __name__ == '__main__':
print('Hello, World!')
Remember the Python version of Hello World that we created? Now I want to turn the Python version into JavaScript. gal can help us with that too, with its experimental Python reader.
$ pygal hello.py hello_new.gal
pygal OK, gal3 program written to hello_new.gal
pygal is an experimental program in gal version 3. It can read a Python program and translate it into gal. The program is translated into a special Python dialect of gal.
$ cat hello_new.gal
flowerbox 'translated from python by pygal';
module Compiler
{
class Base
{
constructor [variant A [null], variant B [null], variant C [null]];
}
main
{
python print 'Hello, World!';
}
}
This program is not quite the same as the original 'hello.gal' program we created. It has a couple extra things added to it. And it uses a 'python print' statement instead of a 'say' statement. Because the translator needs to faithfully represent what the Python program is doing.
flowerbox 'translated from python by pygal';
module Compiler
{
class Base
{
constructor [variant A [null], variant B [null], variant C [null]];
}
main
{
python print 'Hello, World!';
}
}
$ fallback hello_new.gal hello_new.fallback
This program isn't simple enough to turn directly into JavaScript. So we use the fallback gal compiler. Basically, the fallback compiler rewrites a gal program into a simpler gal program. Let's see what the result is.
$ cat hello_new.fallback
comment "***********************************" [line] '* translated from python by pygal *' [line] "***********************************";
module Compiler
{
class Base
{
constructor [variant A [null]] [variant B [null]] [variant C [null]];
}
main
{
say 'Hello, World!';
}
}
There are a couple of things that changed with the fallback version of the program. The most noticeable is that the python print statement is now a say statement. Also, the flowerbox statement is now a comment statement. Now that we simplified the program with the fallback compiler, we are ready to turn it into JavaScript.
comment "***********************************" [line] '* translated from python by pygal *' [line] "***********************************";
module Compiler
{
class Base
{
constructor [variant A [null]] [variant B [null]] [variant C [null]];
}
main
{
say 'Hello, World!';
}
}
$ galjs hello_new.fallback hello_new.js
The galjs compiler worked. Let's look at the result.
$ cat hello_new.js
// ***********************************
// * translated from python by pygal *
// ***********************************
// module: Compiler
class Base
{
constructor(A=null, B=null, C=null)
{
}
}
// main program
console.log('Hello, World!');
// ***********************************
// * translated from python by pygal *
// ***********************************
// module: Compiler
class Base
{
constructor(A=null, B=null, C=null)
{
}
}
// main program
console.log('Hello, World!');
So we have a JavaScript hello world program with a couple of extra things, that were inserted by pygal and carried through two more compilers to make it into JavaScript.
$ node hello_new.js
Hello, World!
So once again, it worked. The Python reader is experimental, and reading other languages into gal is the main project of gal version 4.
Conclusion

In this video, we compiled from gal into Python, JavaScript, Go, Java, MUMPS and C++. We read a Python program into gal, and carried it through the fallback compiler into JavaScript. And every program we ran printed the same thing. You can get gal at generalabstractlanguage.com And you can keep watching this channel for more information about gal.