Skip to content
s-hadinger edited this page Aug 9, 2021 · 30 revisions

Chapter 1 - Basic Information

1.1 Introduction

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.

1.2 Start using

1.2.1 Get Interpreter

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.

1.2.2 REPL environment

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:

Clone this wiki locally