Skip to content

schurhammer/gig

Repository files navigation

gig

Gig is a gleam compiler written in gleam.

How to use

Compile and run a sample file:

# clone the repository
git clone https://github.com/schurhammer/gig
cd gig

# compile a sample
gleam run samples/hello_world.gleam

# run the sample
samples/hello_world.exe
  • This will compile the file samples/hello_world.gleam to samples/hello_world.c and then use a c compiler to create the binary at samples/hello_world.exe.
  • Gig adds the .exe extension to avoid common naming conflicts with source directories.

Compile gig:

# compile gig into a binary
gleam run src/gig.gleam --gc --release

# compile a sample using the gig binary
src/gig.exe samples/hello_world.gleam

# run the sample
samples/hello_world.exe

Compile your project using the gig binary:

  • Add the gig executable to your path or copy it into your project.
  • Copy the patch directory into your project.
# you should be in your project root
cd <your project root>

# copy patch directory (adjust file paths to match your system)
cp -r ../gig/patch patch

# ensure dependencies are downloaded
gleam deps download

# compile your main module
gig src/<main module>.gleam

# run your main module
src/<main module>.exe
  • You should run gig from the root directory of the project.
  • Gig will include source files from the main module's directory, the patch directory, and each source directory of downloaded dependencies (i.e. build/packages/<package_name>/src).
  • Download dependencies using gleam deps download.
  • Your main module should be non-nested i.e. directly in the src directory.

Compile your project using gig as a path dependency

  • Add gig as a path dependency in your gleam.toml file gig = { path = "../gig" }.
  • Copy the patch directory into your project.
# you should be in your project root
cd <your project root>

# copy patch directory (adjust file paths to match your system)
cp -r ../gig/patch patch

# ensure dependencies are downloaded
gleam deps download

# compile your project using gig as a path dependency
gleam run -m gig src/<main module>.gleam

# run your main module
src/<main module>.exe

Optional flags:

  • --release: enable optimisation.
  • --gc: enable garbage collection (otherwise no garbage collection).
  • --debug: include debug symbols.
  • --compiler=name: the name/path of the c compiler.

Important

In the likely case you encounter stack overflows, increase your stack size. These often show up as segfaults.

ulimit -s unlimited

Required Dependencies:

  • C compiler (clang seems to work best)
  • Boehm GC is needed for --gc (aka libgc)

Standard Library

Since much of the standard library is implemented with @external calls, not all functions are available at this time. Some functions have already been re-implemented with patches, see the patch directory. The compiler will print a warning if an unimplemented function is used, and compilation will fail.

Patch System

Since gig is a third party project, most gleam libraries in the wild are unlikely to support it. For these situations we have a patch system that lets you override some modules that would otherwise not work. To do this simply create a module called x.patch.gleam where x is the name of the module you wish to patch. The patch will be merged with the original module, so you only need to implement the functions that are broken. Patches can be placed in the patch directory or any of the source directories.

FFI

You can use the @external(c, "", "function_name") annotation to call C functions. The compiler will then generate a header file with a function declaration that you can include and implement your function against. Your implementation C file should be named in the same way as the header file. Preferably your functions should be namespaced e.g. module_name_function_name().

Feature / Todo List

Basics

  • Bool
  • Int
  • Float
  • Number formats (other than decimal)
  • String
  • List
  • Equality
  • Assignments
  • Discard patterns
  • Type inference
  • Type annotations
  • Modules (note: modules are resolved relative to the target file)
  • Dependencies
  • Unqualified imports
  • Type aliases
  • Blocks
  • Constants
  • Memory Management (GC/RC)

Functions

  • Functions
  • Higher order functions
  • Anonymous functions
  • Function captures
  • Generic functions
  • Pipelines
  • Labeled arguments
  • Documentation comments (ignored)
  • Deprecations (ignored)

Flow control

  • Case expressions
  • Variable patterns
  • Constructor patterns
  • String patterns
  • List patterns
  • Recursion
  • Tail calls (note: the c compiler may do this for us)
  • Multiple subjects
  • Alternative patterns
  • Pattern aliases
  • Guards
  • Exhaustiveness checking

Other Data types

  • Tuples
  • Custom Types
  • Records
  • Record accessors
  • Record updates
  • Generic custom types
  • Results
  • Bit arrays (partial support)

Advanced features

  • Opaque types
  • Use
  • Todo
  • Panic
  • Let assert
  • Externals

Contributing

I am not accepting code contributions at this time. Feel free to make issues, suggestions, or discussions though.

About

A Gleam compiler written in Gleam

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published