This article begins with a script engine that describes how to implement a high-level language. Because the project quantity is bigger, therefore will separate several articles to speak. Learn to make a script or start with something simple. The command script introduced in the previous article paves the ground for implementing the principles of advanced languages. First, the architecture of high-level and low-level language scripts is consistent. Secondly, in order to have a larger space for optimization, we will convert the high-level language into low-level language, and with a low-level script engine to implement the script engine of the high-level language. Of course, it is customary, in this case, we call the low-level language "instruction".
At this stage, the language we implement is not an inert, weakly typed language that supports only basic types, arrays, and function pointers. As an extension, implicit type conversions and function overloads will also be included in the topics of these articles. Well, let's introduce grammar.
To dispense with the trouble of parsing the C-language function pointer statement, I borrowed Pascal's syntax here. We will construct a language that is very similar to Pascal.
File structure:
The high-level language scripts we will implement are support for multiple files. The scripting engine always needs an external function. In order to allow the host program to provide a declaration of external functions, we made a multiple-file scripting engine. There can be something like the C-language #include. Pascal has a strange annotation rule: Use curly braces to annotate.
The structure is as follows:
unit 单元名;
uses 单元名1,单元名2,……;
type
新类型名称=类型声明;
……
var
变量名组:类型;
……
interface
公开的函数声明;
implementation
公开和非公开的函数实现(非公开函数不需要声明)
end.
For the language itself, type and uses should ideally belong to interface and implementation. But let's do it for the sake of convenience. Otherwise, neither can reveal more principles, and give themselves trouble.
Type declaration:
Type declarations have generic types, array types, and function pointers.
Common types are Boolean, Integer, Real, char, and string.
The declaration method of an array type is the array of type.
The function pointer's declaration method is consistent with the function declaration, except that the function pointer does not appear in the function name. For example, we need a function pointer that inputs two integers to output an integer, and we write:
type MyPointer=function(a,b:integer):integer;