PlsExplain: A small dynamic programming language the keep every price is explained

PlsExplain: A small dynamic programming language the keep every price is explained

A small dynamically typed programming language with first-class feedback, the keep every price is explained by a commentary.

Well-known Repo

Working the Interpreter

Dependencies

The interpreter is reckoning on python3 and lark. Lark might maybe furthermore be installed with pip set up lark.

REPL

Invoke the interpreter with out inform line arguments to initiate the interactive mode

>>

To exit press Ctrl-D.

Working a Scripts

To attain a script written in PlsExplain, pass it as the critical inform line argument.

$ ./pls_explain.py examples/hello_world.pe
Hi there World /*the string "Hi there World"*/

First-Class Comments

Comments are first-class values in PlsExplain. Which capacity they’re expressions, might maybe furthermore be saved in variables, passed as feature arguments and be returned by suggestions.

>> let x = 40 + 2 /the number fourtytwo */;
>>> print(x)
42 /the number fourtytwo */
“>

>>> let x = 40 + 2 /the number fourtytwo */;
>>> print(x)
42 /the number fourtytwo */

Explaining feedback fabricate no longer must be commentary-literals:

>> 42 “this is no longer a commentary”
Backtrace (most up-to-date call final):

line 1:
42 “this is no longer a commentary”
^~~~~~~~~~~~~~~~~~~~~~~~~~
form error: form JlString can no longer be stale to conceal values

>>> let s = “no longer a commentary either”
“>

>>> 42 "this is no longer a commentary"
Backtrace (most up-to-date call final):

line 1:
42 "this is no longer a commentary"
^~~~~~~~~~~~~~~~~~~~~~~~~~
form error: form JlString can no longer be stale to conceal values

>>> let s = "no longer a commentary either"

Whilst you to find this error, it in most cases capacity that you occupy forgotten to separate two expressions with an semicolon.

Auto-generated Comments

If a price is no longer explained by an train commentary, the interpreter automagically generates a beneficial commentary.

The conceal operator

The commentary explaining a price might maybe furthermore be retrieved with the ?operator.

Manipulating Comments

The auto-generated commentary can infrequently be a little bit bit verbose:

let truth = fn(n) {
     if (n == 0) {
        1
     } else {
        truth(n - 1) n;
     }
};
print(truth(4));

This program prints:

24.0 /*the fabricated from the fabricated from the fabricated from the product 
of the number 1.0 and the adaptation of the adaptation of 
the adaptation of the number 4.0 and the number 1.0 and the 
number 1.0 and the number 1.0 and the adaptation of the 
distinction of the number 4.0 and the number 1.0 and the number
1.0 and the adaptation of the number 4.0 and the number 1.0
and the number 4.0*/

Since feedback are first-class values we can manipulate them on the hobble. This permits us to generate some distance more beneficial feedback.

let truth = fn(n) {
     if (n == 0) {
        1
     } else {
        /lets generate a beneficial commentary for the return price */;
        /(Comments might maybe furthermore be concatenated with +) */
        let commentary = /the factorial of */ + n?;
        /conceal the return price with the generated commentary */
        (truth(n - 1) n) commentary;
     }
};
let h = 4 /*the assortment of hours i've slept*/;
print(truth(h));

The output of this program is more concise:

24.0 /the factorial of the assortment of hours i've slept*/

Meta-Comments and Meta-Meta-Comments and …

Since feedback are first-class values, feedback are furthermore explained by feedback. Obviously feedback explaining a commentary are furthermore explained by feedback which in turn are explained by feedback and so on.

Datatypes
Form Description
Disclose a really noteworthy form
String unicode strings
Amount floating level numbers
Bool Trustyor Spurious
Unit easiest the price ()
Listing Lists of values

Lists

Within the intervening time there will not be this kind of thing as a special syntax for lists, as a replace the builtin suggestions list,append,keep and uncovermust be stale to make and manipulate Lists.

>> print(l) [1, 2, 3, world] /*a list of the number 1 and the number 2 and the number 3 and the string "world"*/ >>> print(uncover(l, 2)) 3 /*the number 3*/ >>> keep(l, 0, "hallo") >>> print(l) [hallo, 2, 3, world] /*a list of the string "hallo" and the number 2 and the number 3 and the string "world"*/

Syntax

The Syntax is Expression based totally mostly.

Literals

Form Examples
Disclose /this is a commentary */
String "hello" "withn slump " chars"
Amount 1, -1.0, 42.5
Bool Trusty, Spurious
Unit ()

Grouping

(a + b) c

Blocks

{ print("hello"); print("world") }

More than one expressions might maybe furthermore be grouped with curly braces. Expressions are separated with semicolons. The semicolon after the final Expression is non-mandatory. Blocks exhaust into consideration to the price of their final expression.

Capabilities

Definition

let f = fn (arg) {
    print(arg);
};

Capabilities might maybe furthermore be outlined with the keyword fnfollowed by a parenthesized list of parameters and the feature body. The braces are non-mandatory, when the body is a single expression. All suggestions are nameless and first-class.

Calling a feature

print("hello")

Nothing special right here.

Variables

Declaration

let x = 42

Variables are declared with the keyword let and desires to be initialized. Variables are lexicaly scoped. Declarations exhaust into consideration to the assigned price.

Assignment

x = 100

Assignments exhaust into consideration to the assigned price.

Unary Expressions

-a !b

Nothing queer.

Binary Expressions

a + b

Operators ordered by reducing precedence:

Operators Examples
? x?
*, -, % x y, x % 5
+, - x + y
==, <, > x < y
& x & y
` `

Among the finest queer operator is the conceal operator (?). Ogle First Class Comments for more facts.
The good judgment operators &and |are no longer quick circuiting.

Alter Circulation Expressions

If

if (situation) { "honest" } else { "fraudulent" }

Parenthesis spherical the situation are wanted. Braces spherical single expressions and the else branch are non-mandatory. If expressions exhaust into consideration to the price of the taken branch.

Whereas

whereas (situation) { do_something() }

Parenthesis spherical the situation are wanted. The Braces cant be unnoticed, if the body is a single expression. Whereas loops exhaust into consideration to the price of the loop body for the length of the final iteration.

Builtin Capabilities

Just Description
print(args...) Print any assortment of values. When known as with a single argument, the arguments commentary is printed as successfully
input() Be taught single line and return it as a string.
str(price) Convert price to string.
cmnt(price) Convert price to commentary.
num(price) Convert string to number. Returns ()if the conversion fails.
list(args...) Rating list containing the arguments.
append(list, price) Append price to list
keep(list, index, price) Place price into list
uncover(list, index) Rating price out of list

Be taught More

Leave a Reply

Your email address will not be published. Required fields are marked *