September 29

Tasks Accomplished

Implementing Design Principles for DMD Rafactoring

The goal was to apply design principles in the context of a compiler by separating the logic for semantic analysis from the structure of the Abstract Syntax Tree (AST) itself.

There are various types of design patterns but I focused on the visitor's pattern which is more robust and scalable
I started off by understanding the concept of Design pattern in software development, basically I would say design patterns are reusable solutions to common problems in software design.

Facts about Design patterns

  • Design patterns can’t just be copied and pasted into a program, the way you can with off-the-shelf functions or libraries.
  • Design pattern is not a specific piece of code, but a general concept for solving a particular problem. It's more like a process to solve a problem that suits the program or piece of code written.
  • Design patterns are not Algorithms, although they are similar but totally different.
    pattern provides a higher-level description of a solution, a particular design pattern can be applied differently to two coding problems. while an algorithm always defines a clear set of actions that can achieve some goal.

I also learnt that to use a design pattern effectively I should consider the following components in mind

  1. Intent of the Pattern, describes what the pattern is meant to solve and how to solve it.
    Just like the visitor pattern being used in the dmd codebase, the aim for it is to create new operations without changing the classes of the objects on which it operates.
  2. Motivation, describes how useful the pattern is and what it tends to solve.
  3. Structure of Classes, shows the correlation between the key classes and components involved in the pattern.
  4. Code example, describes the implementation of the pattern in any language in this case D language.

Using visitor's pattern

The visitor's pattern is one among several types of behavioral design pattern.
I can say that the Visitor pattern is a behavioral design pattern that lets you separate algorithms from the objects on which they operate.
What does this mean?
The Visitor pattern suggests that you create and place the new behavior into a separate class called visitor without adding it into existing classes. i.e the original object that had to perform the behavior is now passed to one of the visitor’s methods as an argument, providing the method access to all necessary data contained within the object.
This is exactly what I did in the dsymbolsem.d file

Learning and implementing Uniform Function Call Syntax(UFCS)

I learnt about how useful this feature is, it basically allows functions to be called using method-call syntax on the object, even when the function isn't a member of the object's type.
E.g A normal function call int result = foo(5);
a UFCS call int result = 5.foo();
UFCS allows for a more fluent and extensible function call.

Getting acquainted with the D language syntax

Still learning the D lang and improving my skills on it with the help of this book

Initial Refactoring of DMD AST

I've successfully decoupled the semantic functions from the AST file (attrib.d) to dsymbolsem.d
Build was successful. Pull request submitted https://github.com/dlang/dmd/pull/16880

Challenges encountered earlier

  • Although having little knowledge of how extensible UFCS is I got trapped in a simple error which I didn't realize
src/dmd/dsymbolsem.d(7557): Error: no property `newScope` for `super` of type `dmd.visitor.Visitor`
src/dmd/dsymbolsem.d(7557):    	the following error occured while looking for a UFCS match
src/dmd/dsymbolsem.d(7557): Error: function `newScope` is not callable using argument types `(Visitor, Scope*)`
src/dmd/dsymbolsem.d(7557):    	cannot pass argument `super` of type `dmd.visitor.Visitor` to parameter `Dsymbol d`

With the help of my mentor and a reviewer( Wilsonator) I understood that since it's a visitor, newScope method does not exist there for visitors. So UFCS was used for this dpd.newScope(sc) instead of this

override void visit(DeprecatedDeclaration dpd)
	{
    	**auto scx = super.newScope(sc);**
  • Encountered other errors which I fixed.

Summary

This week laid a strong foundation for the separation of semantic routines from AST structure using the Visitor Pattern.

Resources