Technology

PlsExplain: A shrimp dynamic programming language where every price is defined

A shrimp dynamically typed programming language with first-class comments, where every price is defined by a comment.

Important Repo

Running the Interpreter

Dependencies

The interpreter is depending on python3 and lark. Lark would possibly presumably well also moreover be installed with pip install lark.

REPL

Invoke the interpreter without account for line arguments to open the interactive mode

>>

To exit press Ctrl-D.

Running a Scripts

To attain a script written in PlsExplain, pass it because the essential account for line argument.

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

First-Class Comments

Comments are first-class values in PlsExplain. This implies that they are expressions, would possibly presumably well also moreover be saved in variables, passed as characteristic arguments and be returned by capabilities.

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

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

Explaining comments don’t can enjoy to be comment-literals:

>> 42 “that is not any longer a comment”
Backtrace (latest name final):

line 1:
42 “that is not any longer a comment”
^~~~~~~~~~~~~~~~~~~~~~~~~~
type error: type JlString can no longer be aged to level to values

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

>>> 42 "that is not any longer a comment"
Backtrace (latest name final):

line 1:
42 "that is not any longer a comment"
^~~~~~~~~~~~~~~~~~~~~~~~~~
type error: type JlString can no longer be aged to level to values

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

Whereas you happen to look this error, it in overall technique that you may well presumably well enjoy forgotten to separate two expressions with an semicolon.

Auto-generated Comments

If a price is not any longer defined by an explicit comment, the interpreter automagically generates a valuable comment.

The level to operator

The comment explaining a price would possibly presumably well also moreover be retrieved with the ?operator.

Manipulating Comments

The auto-generated comment can in most cases be a puny bit verbose:

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

This program prints:

24.0 /*the product of the product of the product of the product 
of the #1.0 and the variation of the variation of 
the variation of the quantity 4.0 and the #1.0 and the 
#1.0 and the #1.0 and the variation of the 
dissimilarity of the quantity 4.0 and the #1.0 and the quantity
1.0 and the variation of the quantity 4.0 and the #1.0
and the quantity 4.0*/

Since comments are first-class values we can manipulate them on the hover. This enables us to generate even more valuable comments.

let fact = fn(n) {
     if (n == 0) {
        1
     } else {
        /lets generate a valuable comment for the return price */;
        /(Comments would possibly presumably well also moreover be concatenated with +) */
        let comment = /the factorial of */ + n?;
        /level to the return price with the generated comment */
        (fact(n - 1) n) comment;
     }
};
let h = 4 /*the sequence of hours i've slept*/;
print(fact(h));

The output of this program is more concise:

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

Meta-Comments and Meta-Meta-Comments and …

Since comments are first-class values, comments are also defined by comments. Clearly comments explaining a comment are also defined by comments which in flip are defined by comments etc.

Datatypes
Sort Description
Comment a in point of fact valuable type
String unicode strings
Number floating level numbers
Bool Corrector False
Unit easiest the price ()
Checklist Lists of values

Lists

Currently there would possibly be not always any particular syntax for lists, as a substitute the builtin capabilities checklist,append,keep and obtaincan enjoy to be aged to catch and manipulate Lists.

>> print(l) [1, 2, 3, world] /*a checklist of the #1 and the quantity 2 and the quantity 3 and the string "world"*/ >>> print(obtain(l, 2)) 3 /*the quantity 3*/ >>> keep(l, 0, "hallo") >>> print(l) [hallo, 2, 3, world] /*a checklist of the string "hallo" and the quantity 2 and the quantity 3 and the string "world"*/

Syntax

The Syntax is Expression basically based utterly.

Literals

Sort Examples
Comment /that is a comment */
String "hiya" "withn plug " chars"
Number 1, -1.0, 42.5
Bool Correct, False
Unit ()

Grouping

(a + b) c

Blocks

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

More than one expressions would possibly presumably well also moreover be grouped with curly braces. Expressions are separated with semicolons. The semicolon after the final Expression is optionally available in the market. Blocks keep in thoughts to the fee of their final expression.

Choices

Definition

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

Choices would possibly presumably well also moreover be defined with the keyword fnfollowed by a parenthesized checklist of parameters and the characteristic body. The braces are optionally available in the market, when the body is a single expression. All capabilities are anonymous and first-class.

Calling a characteristic

print("hiya")

Nothing particular here.

Variables

Declaration

let x = 42

Variables are declared with the keyword let and ought to be initialized. Variables are lexicaly scoped. Declarations keep in thoughts to the assigned price.

Project

x = 100

Assignments keep in thoughts to the assigned price.

Unary Expressions

-a !b

Nothing keen.

Binary Expressions

a + b

Operators ordered by reducing precedence:

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

The finest keen operator is the level to operator (?). See First Class Comments for more details.
The logic operators &and |are no longer short circuiting.

Regulate Traipse with the circulate Expressions

If

if (condition) { "sparkling" } else { "untrue" }

Parenthesis spherical the condition are major. Braces spherical single expressions and the else department are optionally available in the market. If expressions keep in thoughts to the fee of the taken department.

Whereas

while (condition) { do_something() }

Parenthesis spherical the condition are major. The Braces cant be neglected, if the body is a single expression. Whereas loops keep in thoughts to the fee of the loop body throughout the final iteration.

Builtin Choices

Feature Description
print(args...) Print any sequence of values. When called with a single argument, the arguments comment is printed as neatly
input() Learn single line and return it as a string.
str(price) Convert price to string.
cmnt(price) Convert price to comment.
num(price) Convert string to quantity. Returns ()if the conversion fails.
checklist(args...) Make checklist containing the arguments.
append(checklist, price) Append price to checklist
keep(checklist, index, price) Save price into checklist
obtain(checklist, index) Catch price out of checklist

Related Articles

Back to top button