From Esolang
Jump to navigation Jump to search

Fun is an esoteric dialect of JavaScript, which uses only function calls.

To be more specific, a Fun program is a function call, where a function call is an identifier, followed by an open parenthesis, followed by zero or more function calls separated by commas, followed by a closing parenthesis.

Fun itself does not have an interpreter, we use the host language (JavaScript) to interpret Fun code. There is a standard library, where the functions are defined. There may also be a parser/validator which checks syntax.

Standard Library

Note that standard Fun functions, with the exception of RUN, do not immediately perform the operation described below, rather they return a function which performs that operation.

Standard Library
Name Parameters Description
RUN program runs program
D0..D9 - primitive values 0..9
A..E (value) or none if no parameters, then return the a global variable associated with the letter, if one parameter then set the variable to that value
NUM (d1, d2 .. dN) evaluate parameters, then concatenate them and return the result as a decimal number, for example NUM(D1(), D(0)) returns 10.
PRINT value evaluates value and prints it.
SEQUENCE step1, step2 ... stepN evaluates each parameter in turn, returning the value of the last one.
READ index evaluates index, and returns the value of the global variable at that index.
WRITE index, value evaluates index, and sets the value of the global variable at that index to value
READ_PARAM index evaluates index, and returns the function parameter at that index.
READ_LOCAL index like READ, except it returns a local variable at that index.
WRITE_LOCAL index, value like WRITE except that it sets a local variable.
IF condition, action1, action2 if condition evaluates to true, then evaluate action1, otherwise evaluate action2
WHILE condition, action while condition evaluates to true, evaluate action
NOT x evaluates x, and returns NOT x
EQ a, b evaluates a and b and returns true if they are equal
LT a, b evaluates a and b and returns true if a < b
GT a, b evaluates a and b and returns true if a > b
ADD a, b evaluates a and b and returns a + b
MULTIPLY a, b evaluates a and b and returns a * b
SUBTRACT a, b evaluates a and b and returns a - b
MOD a, b evaluates a and b and returns a % b
FUNCTION fn returns fn unevaluated
CALL fn, arg1, arg2 ... argN evaluates fn, and the provided arguments, binds the arguments to the function and calls it.

Example Programs

Print 5

RUN( PRINT( D5() ) )

Print factorial 5

        EQ(READ_PARAM(D0()), D1()), 
            SUBTRACT( READ_PARAM(D0()), D1() )
    PRINT( CALL(A(), D5()) )

Print primes from 1 through 100

        A( D1() ),   
        B( D2() ),  
        WHILE( NOT(GT( B(), SUBTRACT( READ_PARAM(D0()), D1() ) ) ), 
            IF( EQ(MOD(READ_PARAM(D0()), B()), D0()), 
              A( D0() ) 
            B( ADD( B(), D1() ) ) 
    C( D2() ),  
    WHILE(LT(C(), NUM(D1(), D0(), D0())),
        IF( EQ( CALL(D(), C()), D1() ), 
        C( ADD(C(), D1()))

Implementation Guidelines

As indicated above, an implementation of fun is a library containing the standard functions. Fun functions should not call eval, or transpile Fun code into JavaScript code.


Fun is Turing-complete, as brainfuck with no I/O can be translated into it as follows. A is used for the cell pointer and the numbered global variables are used for the cell values.

Brainfuck Fun
+ WRITE(A(),ADD(READ(A()),D1())),
< A(SUBTRACT(A(),D1())),
> A(ADD(A(),D1())),
] ))
Footer ))

JavaScript interpreter

A Fun interpreter in JavaScript