White [9] to create a series of compilers implement object-oriented features

Characterized in that the object-oriented package.

The objects can be  data  and  operations on the data  packed together to form an indivisible whole, as much as possible to hide the details of the interior, retaining only some of the interface with external contact occurs.


Object-oriented semantic feature

Us to understand object-oriented semantics package from the following three characteristic angles.

  • From the perspective of the type

       We allow programmers to create their own type, to improve the scalability of the program.

  • From the perspective of scope
  1. The visibility of the class. As a type, he should no longer within the scope of the entire program is visible.
  2. Scope members of the object. Properties of an object can be accessed throughout the interior of the object, no matter which position statement.
  • From the perspective of survival
  1. Lifetime member variables of an object, usually with the survival of the object is the same. When creating an object, do initialize all member variables, when destroying objects, along with the destruction of all the member variables together.
  2. The type of binding members (such as JAVA static members) is scoped to all object instances, shared by all instances. So his survival was there before any of an object instance is created, before the destruction of the last object will not disappear.

Design and analysis of the rules of grammar class

The following defines the syntax rules of a class.

classDeclaration
    : CLASS IDENTIFIER
      (EXTENDS typeType)?
      (IMPLEMENTS typeList)?
      classBody
    ;

classBody
    : '{' classBodyDeclaration* '}'
    ;

classBodyDeclaration
    : ';'
    | memberDeclaration
    ;

memberDeclaration
    : functionDeclaration
    | fieldDeclaration
    ;

functionDeclaration
    : typeTypeOrVoid IDENTIFIER formalParameters ('[' ']')*
      (THROWS qualifiedNameList)?
      functionBody
    ;

Grammar rule explanations:

  • IDENTIFIER the CLASS : The class declaration begins with the keyword class.
  • (EXTENDS typeType)? (IMPLEMENTS typelist)? : Determine whether inherited or achieved.
  • classBody  : to keep up with the body of a class.
  • Body of the class members to be declared in the class.
  • Now the role is a function declaration method of the class.

That specific parsing it?

After doing lexical analysis and syntax analysis, semantic analysis interpreter will stage scanning AST, identify all custom types to refer to these types to declare variables elsewhere. Because type declarations may be anywhere in the code, it is best to use a separate traversing to identify and record type.

Then, when we declare a variable, you can refer to this type of. Another semantic analysis work is done variable type of digestion . For example, when we declare "Bird bird = Bird (); " , the need to know where the definition of Bird objects in order to properly access its members.

In doing semantic analysis, should the type definition is stored in a data structure in which:

public class Class extends Scope implements Type{
    ...
}

public abstract class Scope extends Symbol{
    // 该Scope中的成员,包括变量、方法、类等。
    protected List<Symbol> symbols = new LinkedList<Symbol>(
}

public interface Type {
    public String getName();    //类型名称

    public Scope getEnclosingScope();
}

In this design, we see Class is a Scope (Scope), Scope which will be able to save all kinds of original members, can now be directly reused, properties and methods used to save the class , the class diagram drawn below may :( reference: [white] to create a compiler series 8 scope and survival: to achieve block scope and functions )

In doing lexical analysis, we will resolve a lot of identifiers that appear in different grammatical rules, including the emergence of a variable declaration, expressions, as well as the class name, method name, and so on. In the semantic analysis phase, we want these identifiers individually identified, this is a variable that refers to a local variable; that is a method name and so on.

Names of variables, classes and functions, we are called symbols. An important work of compilation process is the establishment of the symbol table, it helps us to further compile or execute the program, and will use the symbol table to save a few classes above information. In the symbol table, we store its name, type, scope and other information. For the classes and functions, we have the appropriate place to store class variables, methods, parameters, return value and other information.

How the object is instantiated?

First by  the constructor  to create the object.

In grammar, we did not use new keyword to create an object represented, but omitted a new, direct calls to a class with the same name of the function, which is our unique design, sample code as follows:

Mammal mammal = Mammal("dog"); //playscript特别的构造方法,不需要new关键字
Bird bird = Bird();            //采用缺省构造方法

But in semantic checks when the current scope is definitely not find such a function, because the initialization method of a class is defined inside the class , as long as we check, Mammal and Bird is not a class name on it a. Further, Mammal class does have a constructor Mammal (), and the Bird class constructor is not a fact, explicitly defined, but that does not mean that a member variable can not be initialized. We learn from the Java initialization mechanism is to provide a default initialization method, the default initialization method, the work will be executed when the object is initialized member declaration . Objects made after default initialization, go call the constructor explicitly defined, so as to improve the whole object instantiation process.

How to manage data objects in memory?

Object structure of struct in C and C ++ language, can be stored in a  stack  in. Object stored in the stack is declared and instantiated directly, rather than using the new keyword to create.

If you create a new keyword, is in fact a piece of application heap memory, and assigned to a pointer variable , as shown below:

When the object stored in the heap, a plurality of variables can reference the same object, such as in FIG variables a and b can reference the same object object1. Class member variables can also reference other objects, such as a class member in object1 quoted object2 object. Beyond the lifetime of the object can create a lifetime of its stack frame.

If the object is stored in the stack where it's survival of the scope is the same , it can automatically create and destroy , so no additional memory management. The disadvantage is that the object is no way to long-term presence and share .

In the heap objects created inside though can be shared, but increased the burden of memory management .

Access to the object properties and methods

Properties and methods of our dot operator to access the object:

mammal.speak();                          //访问对象方法
println("mammal.name = " + mammal.name); //访问对象的属性

Reference properties and methods is also an expression syntax is defined as follows:

expression
    : ...
    | expression bop='.'
      ( IDENTIFIER       //对象属性
      | functionCall     //对象方法
      )
     ...
     ;

In addition, members can also set the target visibility. In other words, some members inside the only objects to use, and some can be accessed from the outside . This how to achieve it?

It's just a semantic issue, semantic checking is done at compile time stage, it does not allow private members are external access, report compilation errors on it.


Reference: "Geek Time - compiler theory of beauty"

Published 62 original articles · won praise 34 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41960890/article/details/105300542