ParScript

From Esolang
Jump to navigation Jump to search

ParScript is an esoteric programming language designed for the purpose of code golf. An interpreter is in the process of being made.

Description

ParScript is a simple stack-based language that enables writing of concise (and unreadable) code. It has a wide selection of library functions for manipulation of data. A notable feature is that arithmetic operations work on scalars as well as lists (like in APL).

Syntax

Here is a simplified (BNF-like) syntax.

Prog  ::= Exp*
Exp   ::= Lit | Id | Sp
Lit   ::= Num | Str | Blk | Sym
Str   ::= "(" .* ")"
    with match parens
Sym   ::= "'" .
Blk   ::= "[" Prog "]"
Num   ::= Digit+ ["." Digit+] ["e" ["+"|"-"] Digit+]
Id    ::= Alpha+ ["+"|"*"|"'"]
Sp    ::= regex \s+
Digit ::= regex \d
Alpha ::= regex [a-zA-Z]


Example programs

A few example programs are given below.

Hello world program

(Hello, world!
)p
(or, if you wish, you can use the library function)`
H

nCr program

(usage: N1 N2 ncr; assume N1,N2 >0)

(The ncr function is given as the number of ways to select
 r objects from a set of n unique objects if order is not
 important. It is given as ncr(n,r)=n!/r!(n-r)! where ! is
 the factorial function.)`
(The factorial function is not defined in the standard
 library, though it is easily defined as [i f*].)`
(ncr1)[\\-i@i@i@&/f*]d
(ncr2)[\i@\~-i~i&/f*]d


Library functions

A list of library functions is given below. This is by no means an exhaustive list.

Stack manipulation

    `  pop     ( a -- )           ~  swap      ( a b -- b a )
    $  dup     ( a -- a a )       \  over      ( a b -- a b a )
    @  rot     ( a b c -- c a b ) :  nil       ( -- : )

List manipulation

    .  cons    ( y x -- x:y)      ,  uncons    ( x:y -- y x )
    &  append  ( l1 l2 -- l3 )    #  index     ( l n -- x )
    {  begin   ( -- { )           }  end       ( { ... -- l )
    S  sort    ( l -- l )         J  jumble    ( l -- l )
    l  length  ( l -- n )

Control flow

    ?  ifthen  ( c f1 f2 -- )     I  if        ( c f1 -- )
    w  while   ( c f -- )         !  call/eval ( x -- ??? )

Higher-order functions

    m  map     ( l f -- l' )      g  grep      ( l f -- l' )
    f  fold    ( l x f -- x' )    A  all       ( l f -- b  )
    O  any     ( l f -- b )

Arithmetic

Arithmetic functions are versatile, they either work on pairs of numbers (the usual interpretation), on one number and one list (a map()) or two lists (a zipWith()). For example:

1 2 *              ==> 2
1 {2 3 4}*         ==> {2 3 4}
{1 2 3} 4*         ==> {4 8 12}
{1 2 3} {4 5 6}*   ==> {4 10 18}
{1 2}   {4 5 6}*   ==> {4 10}   (truncated due to lack of elements)

The arithmetic functions are:

    +  add      ( a b -- a+b )     -  subtract  ( a b -- a-b )
    *  multiply ( a b -- a*b )     /  divide    ( a b -- a/b )
    ^  power    ( a b -- a^b )     %  mod       ( a b -- a%b )
    _  negate   ( a   -- -a  )

Turing-completeness

The author suspects that this language can be Turing-complete by reduction from Brainfuck, but has not yet come up with a suitable reduction.