Git Product home page Git Product logo

catseye / castile Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 183 KB

MIRROR of https://codeberg.org/catseye/Castile : A simple imperative language with union types (and a compiler for same, with multiple targets)

Home Page: https://catseye.tc/node/Castile

License: BSD 3-Clause "New" or "Revised" License

Python 99.07% Shell 0.93%
experimental-language compiler programming-language typecase flow-typing parse-dont-validate multiple-backends interpreter multiple-targets union-type

castile's Introduction

Castile

Version 0.5 | Entry @ catseye.tc | See also: EightebedDieter


This is the reference distribution for Castile, a simple imperative language with union types.

The design of Castile was influenced (in varying degrees) by C, Rust, Eightebed, Python, Ruby, and Erlang. More information on its roots can be found in doc/Design.md.

The reference implementation can both interpret Castile programs and compile them to a variety of targets — JavaScript, Ruby, C, and a generic stack-based VM (included in this distribution).

A rich test suite in Falderal format, which describes the language with many examples, can be found in tests/Castile.md.

Quick Start

Clone this repository, cd into the repo directory and run

bin/castile eg/hello.castile

Alternately, put the bin subdirectory on your executable search path, so that you can run castile from any directory on your system. castile has no dependencies besides Python (either Python 2 or Python 3.)

Motivating Example

Here are some functions for creating linked lists, written in Castile:

struct list {
  value: integer;
  next: list|void;
}

fun empty() {
  return null as list|void
}

fun cons(v: integer, l: list|void) {
  make list(value:v, next:l) as list|void
}

In this, list|void is a union type. In this case it is expressing the fact that the value can be either a list or void — the moral equivalent of "nullable". In order to access any of the concrete types of a union type, one must use typecase:

fun max(l: list|void) {
  u = l;
  v = u;
  n = 0;
  while true {
    typecase u is void {
      break;
    }
    typecase u is list {
      if u.value > n {
        n = u.value
      }
      v = u.next;
    }
    u = v;
  }
  return n
}

This retains type-safety; the code will never unexpectedly be presented with a null value.

Union types can also encourage the programmer follow a Parse, don't validate approach (which, despite the impression you might get from reading that article, is not restricted to Haskell or even to functional programming). In the above code, cons will never return a void, and max is not defined on empty lists. So ideally, we'd like to tighten their types to exclude those. And we can:

...

fun cons(v: integer, l: list) {
  make list(value:v, next:l as list|void)
}

fun singleton(v: integer) {
  make list(value:v, next:null as list|void)
}

fun max(l: list) {
  u = l as list|void;
  v = u;
  ...
}

Many more examples of Castile programs can be found in tests/Castile.md.

castile's People

Contributors

cpressey avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.