Skip to content

egonSchiele/salty

Repository files navigation

Salty

Haskell CI

Salty is a language that compiles to PHP, JavaScript, and JSX.

Salty compiles to readable code, so there's no long term dependency on it.

Stop using Salty whenever you want, and you'll be left with the PHP or JavaScript code you'd have probably written instead anyway.

What can Salty do for you?

Salty is meant to be a pleasure to write and read, and that can be subjective, so see if Salty is right for you!

Here's a PHP function:

/**
 * @param int
 * @param int
 * @return int
 */
public function add(int $a, int $b) {
    return $a + $b;
}

Here's the same function in Salty:

add :: int -> int -> int
add a b := a + b

Here's a forEach loop in PHP:

$evens = [];
forEach($numbers as $number) {
    if (isEven($number)) {
        $evens []= $number;
    }
}

This is essentially a select, and Salty provides higher order functions so you can write it as a select:

evens = numbers.select(\number -> isEven(number))

Or for getting an array slice, instead of

array_slice($foo, 1)

You can do:

foo[1:]

These are simple examples, but you can find some longer examples here.

See list of features here: dot notation, implicit returns, guards, multi-assign, hash destructuring, and more.

Compiling to JSX

Salty provides an alternative to JSX that's similar to hyperscript-helpers.

Take this JSX fragment for example:

<TodoList title="My Todos">
    <ul>
        <li>todo one</li>
        <li>todo two</li>
        <li>todo three</li>
    </ul>
</TodoList>

You can write it in Salty like this:

TodoList.new({title: "My Todos"}) do
    ul do
        li "todo one"
        li "todo two"
        li "todo three"
    end
end

Getting Salty

Compile the code yourself or just copy one of the binaries in bin.

Compiling

Install Stack:

https://docs.haskellstack.org/en/stable/install_and_upgrade/

Then:

make install

Usage

transpiling to php

salty -p test.salt # outputs test.php
salty -p # reads from stdin and prints php to stdout

transpiling to js

salty -j test.salt # outputs test.js
salty -j # reads from stdin and prints js to stdout

debugging

Maybe salty isn't behaving how you expected? You can see the parsed AST using these commands

salty -d test.salt
salty -b test.salt

The difference is, -d will show the AST after the first step, whereas -b will show the AST after backtracks processed.

If you want Salty to print every single step as it tries to parse your code, use:

DEBUG=1 salty ...

Features

See list of features here.

Vim integration

Add these to your .vimrc:

autocmd BufNewFile,BufRead *.salt set filetype=salty
autocmd BufEnter *.salt

" all .salt files are automatically compiled to PHP on write.
autocmd BufWritePost *.salt silent !salty -f %

" leader-p will convert the selected salty code to php.
map <Leader>p :'<,'>!salty<CR>

Syntax highlighting: https://github.com/egonschiele/salt-vim

About

Language that transpiles to PHP, JavaScript, and JSX.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages