-
Notifications
You must be signed in to change notification settings - Fork 97
Chapter 1
Berry is an ultra-lightweight dynamic type embedded scripting language. The language mainly supports procedural programming, as well as object-oriented programming and functional program- ming. An important design goal of Berry is to be able to run on embedded devices with very small memory, so the language is very streamlined. Nevertheless, Berry is still a feature-rich scripting language.
Readers can go to the project’s GitHub page https://github.com/skiars/berry to get the source code of the Berry interpreter. Readers need to compile the Berry interpreter by themselves. The specific compilation method can be found in the README.md document in the root directory of the source code, which can also be viewed on the GitHub page of the project.
First, you must install software such as GCC, git, and make. If you do not use version control, you can download the source code directly on GitHub without installing git. Readers can use search engines to retrieve information about these software. Readers using Linux and macOS systems should also install the GNU Readline library1. Use git command 2 Clone the interpreter source code from the remote warehouse to the local:
git clone https://github.com/skiars/berry.git
Enter the berry directory and use the make command to compile the interpreter:
cd berry
make
Now you should be able to find the executable file of the interpreter in the berry directory (in Windows systems, the file name of the interpreter program is “berry.exe”, while in Linux and macOS systems the file name is “berry”), you can run the executable file directly 3 To start the interpreter.
In Linux or macOS, you can use the command sudo make install to install the interpreter, and then you can start the interpreter with the berry command in the terminal.
REPL (Read Eval Print Loop) is generally translated as an interactive interpreter, and this article has also become the interactive mode of the interpreter. This mode consists of four elements: Read, read the source code input by the user from the input device; evaluate, compile and execute the source code input by the user; print, output the result of the evaluation process; cycle, cycle the above operations.
Start the interpreter directly (enter berry in the terminal or command window without parame- ters, or double-click berry.exe in Windows) to enter the REPL mode, and you will see the following interface:
Berry 0.0.4 (build in Feb 1 2019, 13:14:04)
[GCC 8.1.0] on Windows (default)
>
The first two lines of the interface display the version, compilation time, compiler and operating system of the Berry interpreter. The symbol “>” in the third line is called the prompt, and the cursor is displayed behind the prompt. When using the REPL mode, after inputting the source code, pressing the “Enter” key will immediately execute the code and output the result. Press the Ctrl+Ckey combination to exit the REPL. In the case of using the Readline library, use the Ctrl+D key combination to exit the REPL when the input is empty. Since there is no need to edit script files, REPL mode can be used for quick development or idea verification.
Hello World Program
Take the classic “Hello World” program as an example. Enter print('Hello World') in the REPL and execute it. The results are as follows:
Berry 0.0.4 (build in Feb 1 2019, 13:14:04) [GCC 8.1.0] on Windows (default)
> print('Hello World')
Hello World
>
The interpreter output the text “Hello World”. This line of code realizes the output of the string 'Hello World' by calling the print function. In REPL, if the return value of the expression is not nil, the return value will be displayed. For example, entering the expression 1 + 2 will display the calculation result 3:
>1+2
3
Therefore, the simplest “Hello World” program under REPL is to directly enter the string 'Hello World' and run:
=>'Hello World'
Hello World
More usage of REPL
You can also use the interactive mode of the Berry interpreter as a scientific calculator. However, some mathematical functions cannot be used directly. Instead, use the import math
statement to import the mathematical library, and then use the functions in the mathematical library. “math.” as a prefix, for example sin function should be written as math.sin
:
> import math
> math.pi
3.14159
> math.sin(math.pi / 2)
1
> math.sqrt(2)
1.41421
Berry’s script file is a file that stores Berry code, and the script file can be executed by an interpreter. Usually, the script file is a text file with the extension ”.be”. The command to execute the script using the interpreter is:
berry script_file
script_file
is the file name of the script file. Using this command will run the interpreter to execute the Berry code in the script_file
script file, and the interpreter will exit after execution. If you want the interpreter to enter the REPL mode after executing the script file, you can add
the -i parameter to the command to call the interpreter:
berry -i script_file
This command will first execute the code in the script_file
file and then enter the REPL mode.
Before introducing Berry’s syntax, let’s take a look at a simple code (you can run this code in REPL mode):
1 def func(x) # a function example
2 return x + 1.5
3 end
4 print('func(10) =', func(10))
This code defines a function func and calls it later. Before understanding how this code works, we first introduce the syntax elements of the Berry language.
In the above code, the specific classification of grammatical elements is: def
, return
and end
are keywords of Berry language; and “# a function example” in the first line is called a comment; print
, func
and x
are some identifiers, they are usually used to represent a variable; 1.5
and 10
these numbers are called numerical literals, they are equivalent to the numbers used in daily life; 'func(10) =' It is a string literal, they are used in places where you need to represent text; + is an addition operator, here the addition operator can be used to add the variable x and the value 1.5.
The above classification is actually done from the perspective of a lexical analyzer. Lexical analysis is the first step in Berry source code analysis. In order to write the correct source code, we start with the most basic lexical introduction.
Comments are some text that does not generate any code. They are used to make comments in the source code and be read by people, while the compiler will not interpret their content. Berry supports single-line comments and cross-line block comments. Single-line comments start with the character “#’ until the end of the newline character. The quick note starts with the text “#-” and ends with the text “-#”. The following is an example of using annotations:
# This is a line comment
#- This is a
block comment
-#
The literal value is a fixed value written directly in the source code during programming. Berry’s literals are integers, real numbers, booleans, strings, and nil. For example, the value 34 is an integer denomination.
Numerical Literal Value
Numerical literals include Integer (integer) literals and Real number (real) literals.
40 # Integer literal
0x80 # Hexadecimal literal (integer)
3.14 # Real literal
1.1e-6 # Real literal
Numeric literals are written similarly to everyday writing. Berry supports hexadecimal integer denominations. Hexadecimal literals start with the prefix 0x or 0X, followed by a hexadecimal number.
Boolean literal value
Boolean values (boolean) are used to represent true and false in the logic state. You can use the keywords true
and false
to represent Boolean literals.
String literal
A string is a piece of text, and its literal writing is to use a pair of ' or " to surround the string text:
'this is a string'
"this is a string"
String literals provide some escape sequences to represent characters that cannot be input di- rectly. The escape sequence starts with the character '', and then follows a specific sequence of characters to achieve escape. The escape sequences specified by Berry are
Escape character TODO
\a
\f
\r
\v
\'
\?
significance
Ring the bell
Form feed \n
Carriage return
Vertical tab
apostrophe \" question mark \0
Table 1.1: Escape character sequence
Escape character
significance
Backspace
Horizontal tab Backslash Double quotes Null character