Skip to content

Latest commit

 

History

History
272 lines (205 loc) · 5.87 KB

getting-started.md

File metadata and controls

272 lines (205 loc) · 5.87 KB

Getting started with Fusion

Installing fut

The command-line transpiler fut runs on Windows, macOS and Linux.

Download the release or build from sources.

Syntax highlighting

To install Fusion syntax highlighting in your IDE or text editor, follow the instructions.

Hello, world!

Now you are ready to try out your first Fusion code:

public static class HelloFu
{
    /// Returns a greeting message.
    public static string GetMessage()
    {
        return "Hello, world!";
    }
}

Save the above in hello.fu, then issue this command:

fut -o hello.c,cpp,cs,d,java,js,py,swift,ts,cl hello.fu

This will translate the Fusion code to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C. The fut command accepts one or more Fusion source files (here, just hello.fu) and outputs the transpiled source files as specified by the mandatory -o option. Here we specified several languages, comma-separated. In a real-world scenario, you would integrate the fut command into your build system.

Now let's look at the outputs. hello.cs:

public static class HelloFu
{

	/// <summary>Returns a greeting message.</summary>
	public static string GetMessage()
	{
		return "Hello, world!";
	}
}

This is virtually identical to the Fusion source, with just documentation comment markup added.

The Java source file is named HelloFu.java, after the public class it defines. This is a requirement of the Java programming language.

public final class HelloFu
{
	private HelloFu()
	{
	}

	/**
	 * Returns a greeting message.
	 */
	public static String getMessage()
	{
		return "Hello, world!";
	}
}

Since Java has no concept of static classes, it's emulated with final and a private constructor. The documentation comment is in JavaDoc syntax and String is spelled with an uppercase S.

The C++ translation consists of two files: hello.hpp defines the class and declares the method:

#pragma once
#include <string_view>
class HelloFu;

class HelloFu
{
public:
	/**
	 * Returns a greeting message.
	 */
	static std::string_view getMessage();
private:
	HelloFu() = delete;
};

while hello.cpp defines the method:

#include "hello.hpp"

std::string_view HelloFu::getMessage()
{
	return "Hello, world!";
}

The class is static because of the deleted constructor.

Similarly, the C output consists of the header file hello.h:

#pragma once
#ifdef __cplusplus
extern "C" {
#endif

/**
 * Returns a greeting message.
 */
const char *HelloFu_GetMessage(void);

#ifdef __cplusplus
}
#endif

and the implementation file hello.c:

#include <stdlib.h>
#include "hello.h"

const char *HelloFu_GetMessage(void)
{
	return "Hello, world!";
}

hello.swift is in the Apple-centric language Swift:

public class HelloFu
{

	/// Returns a greeting message.
	public static func getMessage() -> String
	{
		return "Hello, world!"
	}
}

Note the different placement of the return type and the lack of semicolon.

The JavaScript output hello.js does not specify the return type:

"use strict";

class HelloFu
{

	/**
	 * Returns a greeting message.
	 */
	static getMessage()
	{
		return "Hello, world!";
	}
}

TypeScript is a JavaScript derivative, with explicit types and visibility control:

export class HelloFu
{
	private constructor()
	{
	}

	/**
	 * Returns a greeting message.
	 */
	public static getMessage(): string
	{
		return "Hello, world!";
	}
}

hello.py in Python:

class HelloFu:

	@staticmethod
	def get_message() -> str:
		"""Returns a greeting message."""
		return "Hello, world!"

Finally, there's OpenCL code that can run on a GPU:

/**
 * Returns a greeting message.
 */
constant char *HelloFu_GetMessage(void);

constant char *HelloFu_GetMessage(void)
{
	return "Hello, world!";
}

As you can see, fut simply rewrites your code in different languages.

Now, you may wonder why the "Hello, world" code does not print the message in the console? That's because Fusion was never intended to be used to write complete programs. What you write are reusable components aka libraries. In this minimal example we have a class with one method that returns a string. All the languages mentioned above can easily call this method. For example, this is how you could use it from C:

#include <stdio.h>
#include "hello.h"

int main()
{
    puts(HelloFu_GetMessage());
}

In Java, you could display the message in a message box. In Python, you could emit the message to a website. In C++, you could display the message on an embedded display. The point is, Fusion abstracts from the user interfaces.

Language documentation

Fusion is explained in depth in its reference documentation.

Example projects

It's always good to study the language by looking at projects written in it - starting with really small ones:

Then there's a very portable chiptune player:

ASAP architecture

and a decoder of 500+ retro image formats:

RECOIL architecture

Last, but not least, fut itself is implemented in Fusion.

Community

Please join our Discussions and submit Issues and Pull requests on GitHub!