37

Psyche: A domain specific language designed for creating WebAssembly modules

 4 years ago
source link: https://www.tuicool.com/articles/IfUFZvV
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Psyche

A WASM friendly lightweight programming language implemented in OCaml

For more details about the language specification, see wiki .

Made with :heart: in Japan

Usage

psyche <command> [<args>]

Commands:

  • make : Compile a source file specified by the command line argument.

make sub command

example.psy :

pub fn main() {
  fib(7)  (* => 13 *)
}

fn fib(n) {
  if n == 0 || n == 1
    then n
    else fib(n - 1) + fib(n - 2)
}

Compile command :

psyche make example.psy  # produces out.wasm

In order to install or to build the Psyche compiler, ensure that you have OPAM installed.

Installing

opam install psyche

Building

Clone a copy of the repo :

git clone https://github.com/0918nobita/psyche

Install dev dependencies :

opam install . --deps-only

Build the compiler :

make  # produces compiler/_build/psyche (executable file)

Testing

In order to run tests, ensure that you have WABT installed.

make test

EBNF

comment = "(*", <STRING?>, "*)"

non_zero_digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

zero = "0"

digit = zero | non_zero_digit

integer = ["+" | "-"], (zero | (non_zero_digit, { digit }))

letter = "a" | "b" | "c" | ... | "z" | "A" | "B" | "C" | ... | "Z"

identifier = letter, { letter | digit }

if_expr = "if", logical_expr_or, "then", logical_expr_or, "else", logical_expr_or

let_expr = "let", identifier, "=", logical_expr_or, "in", logical_expr_or

funcall = identifier, "(", [ logical_expr_or, { ",", logical_expr_or } ], ")"

list_literal = "[", [ logical_expr_or, { ";", logical_expr_or } ], "]"

factor1 = integer | funcall | list_literal | ("(", logical_expr_or, ")") | if_expr | let_expr | identifier

factor2 = factor1, [ ".", "(", logical_expr_or, ")" ]

term = factor2, { ("*" | "/"), factor2 }

arithmetic_expr = term, { ("+" | "-"), term }

comparison_expr = arithmetic_expr, { ("==" | "!=" | "<" | "<=" | "=>" | ">"), arithmetic_expr }

logical_expr_and = comparison_expr, { "&&", comparison_expr }

logical_expr_or = logical_expr_and, { "||", logical_expr_and }

func_def = ["pub"], "fn", identifier, "(", [ identifier, { ",", identifier } ], ")", "{", logical_expr_or, "}"

program = { func_def }

Reference

License

This software is released under the MIT License, see LICENSE.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK