All Projects → mcilloni → fork

mcilloni / fork

Licence: MPL-2.0 license
A simple, self hosted, low level programming language.

Programming Languages

Makefile
30231 projects
c
50402 projects - #5 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to fork

mCaptcha
A no-nonsense CAPTCHA system with seamless UX | Backend component
Stars: ✭ 473 (+585.51%)
Mutual labels:  self-hosted
friendica
Friendica Communications Platform
Stars: ✭ 1,048 (+1418.84%)
Mutual labels:  self-hosted
alternative-frontends
🔐🌐 Privacy-respecting web frontends for popular services
Stars: ✭ 821 (+1089.86%)
Mutual labels:  self-hosted
docker-ttrss
Tiny Tiny RSS feed reader as a Docker image.
Stars: ✭ 55 (-20.29%)
Mutual labels:  self-hosted
zusam
Private groups to share messages, photos, videos, links with friends and family.
Stars: ✭ 79 (+14.49%)
Mutual labels:  self-hosted
delta
A modern file uploader + URL shortner written in node for your private cloud. low memory overhead + secure
Stars: ✭ 103 (+49.28%)
Mutual labels:  self-hosted
traefik-docker-ansible
🐳 🐹  This is an Ansible playbook to install multiple Web applications on a single Ubuntu server with Docker and Traefik.
Stars: ✭ 75 (+8.7%)
Mutual labels:  self-hosted
wp-cloud-deploy
WPCloudDeploy is a WordPress plugin that allows you to easily deploy and manage your own dedicated high-performance WordPress servers and sites at any cloud server provider.
Stars: ✭ 28 (-59.42%)
Mutual labels:  self-hosted
server
👨🏾‍🍳 Server for Ferdi that you can re-use to run your own
Stars: ✭ 26 (-62.32%)
Mutual labels:  self-hosted
mantle
📒 Easy and effective communication for any team or community.
Stars: ✭ 30 (-56.52%)
Mutual labels:  self-hosted
teslamateapi
TeslaMateApi is a RESTful API to get data collected by self-hosted data logger TeslaMate in JSON
Stars: ✭ 47 (-31.88%)
Mutual labels:  self-hosted
WorkGroup
Self-Hosted private Social Media-Intranet for Companies.
Stars: ✭ 21 (-69.57%)
Mutual labels:  self-hosted
stor
[DEPRECATED] STOR provides a JSON database with the power of HTTP requests 🚀
Stars: ✭ 18 (-73.91%)
Mutual labels:  self-hosted
jmal-cloud-view
JmalCloud 是一款私有云存储网盘项目,能够简单安全管理您的云端文件
Stars: ✭ 148 (+114.49%)
Mutual labels:  self-hosted
pawn-stdlib
The Pawn Standard Library Package, not including any files related to SA:MP - designed for the sampctl package management system.
Stars: ✭ 13 (-81.16%)
Mutual labels:  standard-library
ethibox
Open-source web apps hoster
Stars: ✭ 130 (+88.41%)
Mutual labels:  self-hosted
api
Mirror of vikunja from https://code.vikunja.io/api
Stars: ✭ 119 (+72.46%)
Mutual labels:  self-hosted
Tachidesk-Server
A rewrite of Tachiyomi for the Desktop
Stars: ✭ 1,052 (+1424.64%)
Mutual labels:  self-hosted
bumper
A standalone and self-hosted implementation of the central server used by Ecovacs vacuum robots.
Stars: ✭ 179 (+159.42%)
Mutual labels:  self-hosted
audiobookshelf
Self-hosted audiobook and podcast server
Stars: ✭ 1,316 (+1807.25%)
Mutual labels:  self-hosted

fork

fork is a compiled, procedural, imperative language and toolchain that I've developed in my spare time a few years ago (2014~2016) mostly as a toy, and as a way to tinker and learn how parsers and compilers work.

This repository contains a rudimental, but functioning, self hosting compiler and its runtime/library, which includes several modules implementing stuff such as basic I/O, containers (such as hashmaps, vectors and treemaps) and command-line argument parsers. Almost everything contained in this repository is written in Fork itself, including the runtime and the compiler; there are however a few C files still lingering around, mostly as glue code between fork and libc.

Sadly, only POSIX-compliant, 64-bit operating systems work at the moment, due to some unfortunate assumptions present in the code and the lack of a Windows implementation for several basic functions.

This is obviously super experimental, and was never meant to ever be nothing but a playground for tinkering around, so it might crash horribly, eat your babies and destroy whatever's left of your hopes and dreams.

The language

Fork is a "low level" statically typed, imperative, procedural language that provides an experience quite close to C (with which shares the memory model, same ABI and concepts). Given its nature as an experiment and a way to learn, its syntax has been designed to feel purposely "different" than other commonly used programming languages:

  • Blocks are delimited by <keyword> /<keyword> identifiers, i.e.,

    func deinit(kv ptr KVList)
        kv.freeContents()
    
        hash:deinit(ptr kv'hash)
        list:deinitAll(ptr kv'list, ptr pair:free)
    /func
    

    The supported control flow blocks are limited to just if//if and while//while; no fancy stuff. There is although support for a ternary operator with the syntax condition => expression || expression.

  • Statements and expressions are terminated by newlines; it's possibile to write multiple-line statements by escaping a newline with \ (such as in shell scripting or Python).

  • The operator ' is used to access structure fields, and works pretty much like the operator . in Go or Rust, dereferencing a pointer when needed.

  • Modules are defined as a collection of files contained in the same directory and marked with a module <name> statement. The : operator is used to access functions, variables and types belonging to other modules, after importing them with import.

  • It is possible to either define stand-alone functions (using func) or methods, which can be attached to any arbitrary in-scope named type (including foreign types):

    func strnew(cap uintptr) ptr KVList
        return new(cap, ptr txt:strhash)
    /func
    
    method KVList.clone() ptr KVList
        mut ret = new(me'hash'buckets'cap, me'hash'hf)
    
        mut len = me.len()
        mut i uintptr = 0
    
        while i < len
            mut elem = me.getAt(i)
            ret.put(elem'key, elem'value)
    
            i++
        /while
    
        return ret
    /method
    

    A method is equivalent to defining a func receiving a pointer to an instance of the associated type as its first argument (available in the method body using the me keyword), and can be invoked on value or pointer types using the . operator (which will automatically reference the type if necessary).

  • The entry point of an application is always by an entry block contained an anonymous module. Command line arguments are captured by rt.fork, and are exposed via libfork proc:args() function.

  • (Almost) all of the usual arithmetic and bitwise operations are supported (including exponentiation, using **), respecting the same precedence and behaviour of C. Logical operations are implemented by the and,or and not keywords, while pointer ref/deref are implemented using ptr and val; casts can be defined using the <Type>(expr) syntax.

  • The ? is a unary operator which returns false if the expression at its left is null. The similar binary operator ?? checks its left side expression, returning its right-hand one if null.

  • New types can be created using the alias keyword, which aliases an existing type (such as an anonymous structure type) to an identifier. Aliased types can then be imported from other modules, or may have methods defined on them.

  • Fork supports anonymous structures using {}, which can be returned by functions (implementing multiple return values) and assigned to variables; an anonymous struct ({} or struct()) can be assigned to a named type if structurally compatible. Returned structures are always subjected to return value optimization (RVO)

  • Only mutable variables are supported, using the keyword mut. Types are optional, because they are in general automatically infered by their initialising expressions:

    mut i uintptr = 0
    mut ret = false
    
  • Memory is managed manually; no fancy refcounting, GC or borrow-checking is provided.

  • Builtin types consist in signed and unsigned integers (no floating point support has ever been implemented), a boolean type, plus pointers (ptr T).
    data is a type that corresponds to C's void*, which is handled as a special case by the compiler; every pointer type can be downcasted to data implicitely; upcasting is never automatic, and always requires type casting.

Implementation

Fork is transpiled to C using transmod (see tools/transmod), which uses libforkparse to parse, verify and translate a module to a C file using libctrans. This operation also creates a .ford file containing a binary dump of an AST containing the functions, types and variables declared by a module.
A .ford module must be available to transmod while compiling a module which imports it; these are automatically discovered and picked up by transmod using the FORDPATH variable.

Libraries

The parser and AST-to-C translator are respectively implemented by libforkparse and libctrans.

libfork includes the main runtime for the language (rt.fork) and its standard library. Libfork includes several submodules, which implement fundamental structures and routines, such as:

  • args: command-line argument parser, like getopt, supporting optional arguments and both short/long parameters;
  • dyn: a wrapper for dlopen,dlsym, ...
  • fs: provides support for IO from files, including path handling and browsing directories;
  • hash: a simple hash map;
  • io: generic interfaces for IO;
  • kv: implementation of a "keyvalue list", i.e. an insertion-order preserving map.
  • list: implementation of a doubly-linked list, using raw pointers;
  • map: a simple red-black treemap;
  • mem: a wrapper for malloc, free, ...
  • proc: provides functions to access the environment of the current process, including argv and getenv.
  • rt: the definition of the compiler runtime for fork, which must be linked into every Fork binary.
  • set: implementation of an hash set.
  • tty: wraps C stdio to provide functions to write to stdout and stderr.
  • txt: provides string-related functions, such as string concatenation (using heap) and a tokenizer. This module also provides a rudimental string buffer which is widely used thorough the codebase.
  • vect: implements a dynamic-expansion vector similar to C++'s std::vector, Java ArrayList and Rust's Vec. Like list, this structure only supports data pointers and integers.

Lexer and Parser

The parser is a simple recursive-descent parser, which uses a lexer to build an AST top-down; parsing an AST and checking its semantical correctness are done in two different, separated steps. It's also simple to use libforkparse as a stand-alone library, outside of the compiler.

Generated C code

Generated C files are generally unreadable; the code is generated for the sake of simplicity in an "SSA-like" form, which can result in very huge binaries if compiled with -O0. This is generally not an issue, given that any decent optimizing compiler (i.e. gcc, clang, icc, cl.exe, ..) will elide most if not all of the useless stack allocations and assignments in a release build (i.e., with -O1 or better). The only mandatory requirement for any C compiler to be used with transmod is to support to dollar signs in identifiers (which are used extensively to scope package functions and methods), which is the case for every relevant compiler (except tcc and pcc).

Every module is compiled down to a single .c file, containing the translated code and the extern and typedef declaration extracted by every imported .ford.

Debugging with gdb/lldb

Debugging with gdb or lldb is supported out of the box; every single line of C generated by libctrans is marked using preprocessor #line directives, allowing to step inside of the original fork files (instead of following the C gibberish generated by the compiler). Just compile every C source with -O0/-Og and -g.

How to build fork

Use GNU make to bootstrap fork. You need an already existing binary release of fork with transmod to build this (this is a self-hosting compiler, after all), and it should be in your path (you can otherwise use the TRNS env var to specify which transmod the Makefile should use). See releases or tags on Github/Gitlab to find one for your platform.

$ make # or gmake on FreeBSD

The makefile will compile transmod three times, using the output of the previous stage as the compiler for the next stage; the finished compiler can be found in build/stage3.

If either a prebuilt for your platform is not available, or you are scared of random binaries from the internet (which you definitely should), there should be a cfiles tarball (which contains the C output of a previous compilation, plus a Makefile) available, which can be used to compile a barebone transmod. This can then be used to bootstrap the full source code on your system:

$ env TRNS=/path/to/transmod make

Usage

The FORDPATHS environment variable sets the search paths for .ford files.

.ford files contain a binary representation of the structures defined in already built modules, and are created by transmod at compile time.

You can build a fork module (contained into a single directory) with transmod:

$ env FORDPATHS=$MY_FORD_PATH transmod -fo $OUT_DIR_FOR_FORDS -co $OUT_DIR_FOR_C_FILES -n $FILE_NAME moduledir

If not specified, the module name is either the one specified by the files of the module itself, or main if this is a main module.

Compile then all the files with a C99 compiler (GCC, ICC and Clang are currently supported):

$ cc -c -w -g -std=c99 file.c
$ # in case this is a program and not a library
$ cc -o prog file.o $PATH_TO_LIBFORK/libfork.a $PATH_TO_RT_O/rt.o

Remember to link rt.o: it contains the entry point of a Fork application. libforkparse.a and libctrans.a are part of the compiler and are generally not very useful if not hacking on the compiler itself.

License

The entirety of code in this repository is licensed under the MPL 2.0. See LICENSE for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].