Skip to content
Neuron Teckid edited this page Mar 8, 2017 · 2 revisions

Compiler Usage

After install Flatscript from npm, you could use

flsc

to run it.

When executed without command line arguments, it reads Flatscript source from stdin and output JS to stdout.

There are some compiler options to change the behavior of the compiler.

Input/Output

Use -i/-o to specify the input/output file

flsc -i input.fls -o output.js

The compiler will read from "input.fls" and output to "output.js".

The equivalent to -i is --input-file, while the equivalent to -o is --output-file.

External or Pre-defined Symbols

Unlike Javascript, Flatscript will check name definition at compile-time. Any usage of undeclared name will cause a compile error.

The fundamental Javascript classes such as Array, String and utilities like console, eval is pre-defined in Flatscript. However, browser objects or Node functions are not. If you are going to use them, you ought to declare them.

The command line arguments approach is to use -e option.

flsc -e require

thus require is allowed during the compilation.

In the browser environment, JS files could reference variables in other JS files without something like require, so you could simply declare a bulk of name provided by other libraries and then use them in Flatscript.

To pre-define multiple names, you could use -e multiple times, or use colons (":") to join all the names you need, like

flsc -e window:document:jQuery

thus window, document, jQuery are allowed during the compilation.

The equivalent to -e is --externs.

Export Names to window

As the browser simply merge several JS modules together, without the awareness of namespace, it is possible that two modules have a same name declared and got conflicted.

Flatscript will hide all declaration in an anonymous function scope and you need to explicitly declare what name could be used in other module by using the export statement.

In the browser environment, they are by default exported to window, as you write

export foo: 10

it will result in window.foo = 10.

If you would like to make the export point deeper, use -p option with an identifier

flsc -p bar

and compile the above code, it will result in that

  • window.bar is an object
  • window.bar.foo = 10

The equivalent to -p is --export-point.

Debug Level

If you want some code that runs in the debug mode and eliminated at production, you could use the __debug__ macro.

To change the value of __debug__, use -D with an integer

flsc -D 1

This will set __debug__ to integral value 1.