BT Language Syntax Design
Notes 2024-11-08

Chain syntax

When designing the BT language, the initial setting was to support chain syntax, because when using PHP language, function nesting is too troublesome. For example:

clearly implements a simple string processing, but it cannot be simplified, or even The code needs to be read from right to left, which is very confusing, so I decided to develop a script language that can support chain syntax. This was my initial positioning. After a period of hard work, BT language was implemented, supporting chain syntax. Convert the above code into BT language:

The importance of prototype functions

The benefits of chain syntax are very obvious. It is executed from left to right and is simple and easy to understand. More importantly, you never have to be confused about passing parameters!

Why do you say that? For example, the basic data types in the BT language have prototype functions, similar to the mechanisms of Rust and JavaScript. Take str.join() as an example, join is implemented based on the string type, and what is processed is str itself, but it is very confusing in PHP. Let’s illustrate with an example:

The implode function mainly processes arrays. We divide all parameters into two types, one is using The parameters for processing the data itself are called source data, such as $array, and the other is auxiliary parameters, such as this comma for splitting: , Through the implode function, you will find that the source data is placed in the second place, and the auxiliary parameters are placed in the first place. Let's look at another example:

You will find that the source data parameters are placed in the first place, and the auxiliary parameters are placed in the second place. Why? The second parameter of the trim function is not required, but both parameters are of string type. You can judge it when setting the program and put the source data in the second position. If there is only one parameter, then use the first parameter as the source data.

The parameter settings for this kind of function in PHP are very confusing to me, but the chain syntax is different. You never have to be confused about passing parameters, because it is a function based on data prototypes and does not need to pass source data parameters, so you can operate as you like:

Why use fn when declaring functions?

The chain syntax of BT language refers to Rust and JavaScript. In the above example, you can see that the function keyword uses fn. This keyword is used to declare functions.

General programming languages use function more, but BT language continues the writing method of its native language Rust, using fn to declare functions. Some languages use func. To be honest, I personally don’t like it very much. If you want to be concise, just be more concise. I feel that removing the term is ugly, and it is better to use the full name directly, so I finally considered fn.

In the BT language, the parameters of the function can be set to default values. This method refers to JavaScript. It should be noted here that the return value of the function is the last statement taken by default. You can also use the return statement to set the return value. This method refers to the Rust language.

Conditional expression

In the BT language, the conditional expression also refers to Rust, using the if keyword, followed by a conditional expression. Note that the conditional expression does not require parentheses, including various types of loops. The conditional expression does not require parentheses, for example:

The parentheses of the conditional expression is redundant, unless you need to explicitly change the priority, for example:

Therefore, in BT language, it is recommended not to use parentheses, because it is not necessary. Although it is supported, I believe that even if you are used to using parentheses, when you use BT language, you will slowly find that it is very pleasant not to use parentheses.

Here I considered whether the curly braces should also be removed, but after comprehensive consideration, I think curly braces are necessary, because curly braces can avoid unnecessary errors and make the code clearer and more readable. This is also because Python defines the structure of the code block through indentation instead of using curly braces. You must maintain the indentation structure of the code. For me, this loses flexibility and freedom, so after careful consideration, keep the curly braces.

Statement terminator

There is no need to add a terminator semicolon to statements in BT. Newlines and spaces are terminators. Although semicolons are supported, their use is not recommended.

The above form is supported by BT. From my personal aesthetic point of view, I prefer to use spaces or commas to separate single-line statements. This syntax refers to python.

Abnormal syntax

The syntax in the BT language is free. Since expressions have return values, its syntax can support this use:

You read that right, if is written in the array. Also, if you are happy, the Array array can also be separated without commas, for example:

As long as you are happy, the Object object is more flexible:

However, commas are used to separate fields, which may or may not be used. Single quotes may or may not be added to Key, so the best way is:

Isn't this more comfortable to look at than any programming language? ? ? Compilation will actually be faster.