Note
slang
is experimental language.
This language was created while studying Haskell and PLT, so it may contain the wrong code.
Please, don't use it for anything important.
slang
is a simple ML dialect language with let-ploymorphic type system, written in Haskell.
- Let-polymorphic type inference
- Recursion
- First-class function
- GHCi-like REPL
- Command-line tool
You can build and run slang
using stack
:
$ stack build
$ stack run
$ stack test
You can see the commands available with the -h
(--help
) option.
$ stack run -- --help
SLang - a simple language
Usage: slang-exe [COMMAND]
Command line for SLang
Available options:
-h,--help Show this help text
Available commands:
interpret Interpret a SLang file
parse Parse a SLang file
type Type Inference a SLang file
repl Enter a REPL for SLang
Parse, type-check, and evaluate the given text and output a value result.
You can use the interpret
subcommand for interpreting standard input:
$ stack run -- interpret
let x = 1 in x
- : int = 1
and you can use -i
(--input
) option for interpreting a single file:
(* example.sl *)
let x = 1 in x
$ stack run -- interpret -i ./example.sl
- : int = 1
If you want to output the interpret result to a file, you can use the -o
(--output
) option.
$ stack run -- interpret -o ./example.out
let x = 1 in x
(* example.out *)
- : int = 1
It is also available with -i
and -o
options.
$ stack run -- interpret -i ./example.sl -o ./example.out
The -i
and -o
options are also used in the parse
and type
subcommands.
Parses the given text and output the AST result.
$ stack run -- parse
let x = 1 in x
ELet (LBVal "x" (EConst (CInt 1))) (EVar "x")
It is also available with -i
and -o
options.
Type check and infer the given text and output the type result of that text.
$ stack run -- type
let x = 1 in x
- : int
It is also available with -i
and -o
options.
You can use REPL with the repl
subcommand.
$ stack run -- repl
_____ _ Welcome to SLang REPL!
/ ____| |
| (___ | | __ _ _ __ __ _ Author: Seunguk Lee
\___ \| | / _` | '_ \ / _` | GitHub: https://github.com/seunguklee/slang
____) | |___| (_| | | | | (_| | Issues: https://github.com/seunguklee/issues
|_____/|______\__,_|_| |_|\__, | About: ML dialect language with let-ploymorphic type system
__/ | License: MIT
|___/
Type ':help' for available commands
sl>
Show list of commands
sl> :help
Load file and interpret
(* example.sl *)
let id a = a in id 3
sl> :load ./example.sl
- : int = 3
Parse the given text and show AST
sl> :parse let x = 1 in x
ELet (LBVal "x" (EConst (CInt 1))) (EVar "x")
Show the type of given text
sl> :type let x = 1 in x
let x = 1 in x : int
Exit REPL
sl> :quit
For example:
let add x y = x + y in
add 1 2
is semantically equivalent to:
let add = fun x -> fun y -> x + y in
add 1 2
and
let add = fun x y -> x + y in
add 1 2
let id a = a in
let const n = 10 in
(id 3) + (const 1) + (const true) + (const false)
let square x = x * x in
let squareFunc = square in
squareFunc 3
let op f a b = f a b in
let sum a b = a + b in
let mul a b = a * b in
op sum (op sum 1 3) (op mul 2 3)
let rec iter acc n =
if n == 0
then acc
else n + (iter acc (n - 1)) in
let sum n = iter 0 n in
sum 10
let add x y = x + y in
add 1 3
if 1 == 0
then true
else false
(* type error *)
if 1 == 0
then 1
else true
Sincere appreciation to the following repositories and courses for making the development of slang
possible.