Scotty

From Esolang
Jump to: navigation, search

History

Scotty is an educational esoteric language created in the Spring of 2016 for a course on programming languages. It is an imperative language with a syntax that is incredibly easy to tokenize, making it relatively easy to implement language features without needing to perform any lexical analysis of the input.

Description

Programs in this language can be split into lines. The final line of the program is a prefix expression, where the result of the evaluation of that expression is the output of the program. The language definition includes variables and functions, which are defined on the first n - 1 lines of the program. Each function can take exactly one parameter, and that parameter is passed by value. Functions cannot access variables outside of the local scope of the function.

Each line of the program can be split on spaces, and each command is uniquely identified by the first token of the expression.

Grammar

<program>       ::= <program_lines>
<program_lines> ::= <prefix_exp> | <fn_def> <program_lines> | <var_assign> <program_lines>
<var_assign>    ::= <identifier> = <prefix_exp>
<fn_def>        ::= fun <identifier> <identifier> = <prefix_exp>
<prefix_exp>    ::= + <prefix_exp> <prefix_exp>   |
                    - <prefix_exp> <prefix_exp>   |
                    * <prefix_exp> <prefix_exp>   |
                    / <prefix_exp> <prefix_exp>   |
                    ( <identifier> <prefix_exp> ) |
                    <identifier>                  |
                    <number>
<identifier>    ::= <letter><id_tail> | <letter>
<id_tail>       ::= <letter><id_tail> | <digit><id_tail> | <letter> | <digit>
<number>        ::= -<digits> | <digits>
<digits>        ::= <digit><digits> | .<digits> | <digit>
<digit>         ::= (0 .. 9)
<letter>        ::= ('A'..'Z') ++ ('a'..'z')

Example Programs

Simple Numeric Evaluation

+ 1 1

With Variable Assignment

x = + 1 1
+ x 1

With Function Definition

fun square x = * x x
( square 3 )

Functions within Functions

fun square x = * x x
fun cube x = * ( square x ) x
( cube 3 )

Functions and Variables

fun square x = * x x
fun cube x = * ( square x ) x
something = 3
( cube something )

Interpreter

Scotty interpreter written in Haskell

The above interpreter does absolutely NO checking of the input program. It assumes that the code provided is a valid Scotty program. Obvious future work is to add syntax checking to the program.