SlideShare a Scribd company logo
Compiler Case Study -
Design Patterns in C#
Ganesh Samarthyam
ganesh@codeops.tech
Design Patterns - Introduction
“Applying design principles is the key to creating
high-quality software!”
Architectural principles:
Axis, symmetry, rhythm, datum, hierarchy, transformation
How to Apply Principles in Practice?
Design principles
Code
How to bridge
the gap?
What are Smells?
“Smells'are'certain'structures'
in'the'code'that'suggest'
(some4mes'they'scream'for)'
the'possibility'of'refactoring.”''
What is Refactoring?
Refactoring (noun): a change
made to the internal structure of
software to make it easier to
understand and cheaper to
modify without changing its
observable behavior
Refactor (verb): to restructure
software by applying a series
of refactorings without
changing its observable
behavior
What are Design Patterns?
recurrent solutions
to common
design problems
Pattern Name
Problem
Solution
Consequences
Principles to Patterns
Expr expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Encapsulate object creation
Builder pattern
Why Care About Patterns?
❖ Patterns capture expert
knowledge in the form of
proven reusable solutions
❖ Better to reuse proven
solutions than to “re-invent”
the wheel
❖ When used correctly, patterns
positively influence software
quality
❖ Creates maintainable,
extensible, and reusable code
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Design Patterns - Catalog
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Creational
Deals with controlled
object creation
Factory method, for
example
Structural
Deals with
composition of classes
or objects
Composite, for example
Behavioral
Deals with interaction
between objects/
classes and
distribution of
responsibility
Strategy, for example
3 Principles Behind Patterns
Design'principles'
behind'pa0erns'
Program'to'an'
interface,'not'to'an'
implementa7on''
Favor'object'
composi7on''
over'inheritance'
Encapsulate'what'
varies'
Compiler Case Study
Case Study Overview
❖ Smelly code segments (specially if-else based on types
and switch statements) used first
❖ They are “refactored” using design patterns
❖ The case study shows code-snippets; compilable, self-
contained source code files shared with you
What You Need to Know
❖ Understand essential object oriented constructs such as runtime
polymorphism (virtual functions), and principles (such as
encapsulation)
❖ Know essential C# language features - classes, inheritance, etc
❖ Data structures and algorithms (especially binary trees, tree
traversals, and stacks)
❖ NO background in compilers needed
❖ Extremely simplified compiler code example; purposefully
done to make best use of this case study
Simple Expression Tree Example
+
* 30
10 20
Expression: ((10 * 20) + 30)
Post-order Traversal: Simulating Code Gen
10 20 * 30 +
post-order
traversal
result
+
* 30
10 20
Front-end
Intermediate code (CIL, Java bytecodes, …)
Interpreter (Virtual
Machine)
Using Stack for Evaluation
Initial
empty
10
push 10
10
push 20
20
200
perform
10 * 20
push
200
200
push 30
30
230
perform
200 +
30
push
230
End
result
= 230
10 20 * 30 +
Mnemonic Names in Java and .NET
10 20 * 30 +
push 10
push 20
multiply
push 30
add
.NET CIL Instructions Java Bytecode Instructions
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
(ldc => load constant ;
i4 => integer of 32 bits;
s => signed)
bipush 10
bipush 20
imul
bipush 30
iadd
(bipush stands for “byte integer push”)
Hands-on Exercise
Use the ildasm tool to disassemble the Expr.cs code
// Expr.cs
using System;
class Test {
    public static void Main() {
        int a = 10, b = 20, c = 30;
        int r = ((a * b) + c);
        Console.WriteLine(r);
    }
}
((10	*	20)	+	30)Source Code
C#
Compiler
.NET CLR
(Common
Language
Runtime)
ldc.i4.s 0x0a
ldc.i4.s 0x14
mul
ldc.i4.s 0x1e
add
+
* 30
10 20
Facade Pattern
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a
higher-level interface that makes the subsystem easier to use.
Facade Pattern in Compilers
“The Compiler class acts as a facade: It offers clients a single,
simple interface to the compiler subsystem.”
Facade Pattern in Compilers
public virtual void Compile(StreamReader input, BytecodeStream output) {
Scanner scanner = new Scanner(input);
var builder = new ProgramNodeBuilder();
Parser parser = new Parser();
parser.Parse(scanner, builder);
RISCCodeGenerator generator = new RISCCodeGenerator(output);
ProgramNode parseTree = builder.GetRootNode();
parseTree.Traverse(generator);
}
Procedural (conditional) Code
How to get rid of
conditional statements?
public void GenCode()
{
if (_left == null && _right == null)
{
Console.WriteLine("ldc.i4.s " + _value);
}
else
{
// its an intermediate node
_left.GenCode();
_right.GenCode();
switch (_value)
{
case "+":
Console.WriteLine("add");
break;
case "-":
Console.WriteLine("sub");
break;
case "*":
Console.WriteLine("mul");
break;
case "/":
Console.WriteLine("div");
break;
default:
Console.WriteLine("Not implemented yet!");
break;
}
}
}
Hands-on Exercise
Refactor the “../Composite/Before/Compiler.cs” to
get rid of conditional statements and use runtime
polymorphism instead
Hands-on Exercise
internal abstract class Expr
{
public abstract void GenCode();
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class BinaryExpr : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public BinaryExpr(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override void GenCode()
{
_left.GenCode();
_right.GenCode();
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Composite Pattern
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Composite Pattern: Discussion
❖ There are many situations where
a group of components form
larger components
❖ Simplistic approach: Make
container component contain
primitive ones
❖ Problem: Code has to treat
container and primitive
components differently
Compose objects into tree structures to represent part-whole hierarchies. Composite
lets client treat individual objects and compositions of objects uniformly.
❖ Perform recursive
composition of
components
❖ Clients don’t have to
treat container and
primitive components
differently
BTW, Did You Observe?
// ((10 * 20) + 30)
var expr = new Expr(
new Expr(
new Expr(null, "10", null), "*", new Expr(null, "20", null)),
"+",
new Expr(null, "30", null));
expr.GenCode();
// ((10 * 20) + 10)
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
expr.GenCode();
Hands-on Exercise
function interpret(node: binary expression tree)
| if node is a leaf then
return (node.value)
| if node is binary operator op then
return interpret(node.left) op interpret(node.right)
Rewrite the code to “evaluate” the expression instead
of generating the code - here is the “pseudo-code”
Solution
internal class Constant : Expr
{
private readonly int _val;
public Constant(int arg)
{
_val = arg;
}
public override int Interpret()
{
return _val;
}
}
internal class Plus : Expr
{
private readonly Expr _left;
private readonly Expr _right;
public Plus(Expr arg1, Expr arg2)
{
_left = arg1;
_right = arg2;
}
public override int Interpret()
{
return _left.Interpret() + _right.Interpret();
}
}
Register-Based Instructions (x86)
movl $10, -8(%rbp)
movl $20, -12(%rbp)
movl $30, -16(%rbp)
movl -8(%rbp), %eax
imull -12(%rbp), %eax
addl -16(%rbp), %eax
popq %rbp
retq
int main() {
int a = 10;
int b = 20;
int c = 30;
return ((a * b) + c);
}
cc -S expr.c
Register-Based Instructions (Android)
((a * b) + c);
javac -source 1.7 -target 1.7 expr.java
dx --dex --output=expr.dex expr.class
dexdump -d expr.dex
const/16 v1, #int 10 // #a
const/16 v2, #int 20 // #14
const/16 v3, #int 30 // #1e
mul-int v0, v1, v2
add-int/2addr v0, v3
return v0
Hands-on Exercise
Generate register-based instructions for the given
expression ((10 * 20) + 30)
You can generate for an imaginary machine (i.e., a
virtual machine like Dalvik/Android) or a real-
machine (like x86-64)
Solution
internal class Register
{
private readonly int _index;
private Register(int index)
{
_index = index;
}
public static Register GetRegister(int index)
{
return new Register(index);
}
// FreeRegister to be implemented
// not implemented for now for the sake of simplicity
public int GetIndex()
{
return _index;
}
}
Solution
abstract class Expr {
public abstract Register GenCode();
}
class Constant : Expr {
int val;
public Constant(int arg) {
val = arg;
}
public override Register GenCode() {
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val);
return targetRegister;
}
}
class Plus : Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public override Register GenCode() {
Register firstRegister = left.GenCode();
Register secondRegister = right.GenCode();
Register targetRegister = RegisterAllocator.GetNextRegister();
Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(),
secondRegister.GetIndex());
return targetRegister;
}
}
Solution
((10 + 20) + 30);
Assuming infinite registers from v0,
with target register as the first argument,
here is the mnemonic code generated for our
imaginary (i.e., virtual) machine
const/16 v0, #int 10
const/16 v1, #int 20
add-int v2, v0, v1
const/16 v3, #int 30
add-int v4, v2, v3
How to Improve the Tree Creation?
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
Expr expr = new Plus(
new Multiply(
new Constant(10),
new Constant(20)),
new Constant(30));
Hands-on Exercise: Solution
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Factory Method Pattern: Structure
Factory Method Pattern: Discussion
❖ A class cannot anticipate the
class of objects it must create
❖ A class wants its subclasses to
specify the objects it creates
Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory method lets a class defer instantiation to subclasses.
❖ Delegate the responsibility
to one of the several helper
subclasses
❖ Also, localize the
knowledge of which
subclass is the delegate
Scenario
internal class Constant : Expr
{
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
return new Constant(arg);
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
For expressions like ((10 * 20)
+ 10), why should we create
different Constant objects?
Can we reuse them?
Hands-on Exercise
Improve this code to reuse immutable objects (for the
ones with the same values) instead of creating
duplicate objects.
public static Constant Make(int arg)
{
return new Constant(arg);
}
Hands-on Exercise: Solution
internal class Constant : Expr
{
private static readonly
Dictionary<int, Constant> Pool = new Dictionary<int, Constant>();
private readonly int _val;
private Constant(int val)
{
_val = val;
}
public static Constant Make(int arg)
{
if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg);
return Pool[arg];
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
Flyweight Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Flyweight Pattern: Discussion
❖ When an application uses a
large number of small objects,
it can be expensive
❖ How to share objects at
granular level without
prohibitive cost?
Use sharing to support large numbers of fine-grained objects efficiently
❖ When it is possible to share
objects (i.e., objects don't
depend on identity)
❖ When object’s value
remain the same
irrespective of the contexts
- they can be shared
❖ Share the commonly used
objects in a pool
Scenario
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
This code still sucks - if the
expression becomes more
complex, can you really read
it? Can we simplify this?
Hands-on Exercise
Improve this code to use “builder pattern”.
// ((10 * 20) + 10)
var expr = Addition.Make(
Multiplication.Make(
Constant.Make(10),
Constant.Make(20)),
Constant.Make(10));
expr.GenCode();
var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
Hands-on Exercise: Solution
internal class ExprBuilder
{
private Expr _expr;
public ExprBuilder Const(int arg)
{
_expr = Constant.Make(arg);
return this;
}
public ExprBuilder Plus(int arg)
{
_expr = new Addition(_expr, Constant.Make(arg));
return this;
}
public ExprBuilder Mult(int arg)
{
_expr = new Multiplication(_expr, Constant.Make(arg));
return this;
}
public Expr Build()
{
return _expr;
}
}
Builder Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Builder Pattern: Discussion
❖ Creating or assembling a
complex object can be tedious
Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
❖ Make the algorithm for
creating a complex object
independent of parts that
make up the object and
how they are assembled
❖ The construction process
allows different
representations for the
object that is constructed
Scenario
Provide the ability to traverse
the aggregate structure
without exposing
implementation details (also
use iteration instead of
recursion)
internal class Constant : Expr
{
private readonly int _val;
public Constant(int val)
{
_val = val;
}
public override void GenCode()
{
Console.WriteLine("ldc.i4.s " + _val);
}
}
internal class Plus : BinaryExpr
{
public Plus(Expr arg1, Expr arg2) : base(arg1, arg2)
{
}
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
}
Hands-on Exercise
Traverse the expression tree using “external” iteration
public override void GenCode()
{
base.GenCode();
Console.WriteLine("add");
}
Hands-on Exercise: Solution
internal class ExprIterator
{
public static IEnumerable<Expr> Traverse(Expr node)
{
if (node != null)
{
foreach (var left in Traverse(node.GetLeft())) yield return left;
foreach (var right in Traverse(node.GetRight())) yield return right;
yield return node;
}
}
}
foreach (var node in ExprIterator.Traverse(expr))
node.GenCode();
Iterator Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Iterator Pattern: Discussion
❖ How to provide a way to
traverse an aggregate structure
in different ways, but without
exposing its implementation
details?
Provide a way to access the elements of an aggregate object sequentially without
exposing its underlying representation.
❖ Take the responsibility for
access and traversal out of
the list object and put it
into an iterator object
❖ Let the iterator keep track
of the element visited,
traversal order, etc
Scenario
How to separate:
a) code generation logic
from node types?
b) how to support different
target types?
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public void genCode() {
left.genCode();
right.genCode();
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
}
}
Hands-on Exercise
Refactor this code to remove the if-else condition check
(explicit type-checking code) and use runtime
polymorphism instead
if(t == Target.JVM) {
System.out.println("iadd");
}
else { // DOTNET
System.out.println("add");
}
Hands-on Exercise
internal abstract class Target
{
public abstract void GenCode(Constant constant);
public abstract void GenCode(Plus plus);
public abstract void GenCode(Mult mult);
}
internal class JvmTarget : Target
{
public override void GenCode(Constant constant)
{
Console.WriteLine("bipush " + constant.GetValue());
}
public override void GenCode(Plus plus)
{
Console.WriteLine("iadd");
}
public override void GenCode(Mult mult)
{
Console.WriteLine("imul");
}
}
Strategy Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Strategy Pattern: Discussion
❖ Useful when there is a set of
related algorithms and a client
object needs to be able to
dynamically pick and choose
an algorithm that suits its
current need
Defines a family of algorithms, encapsulates each one, and makes
them interchangeable. Strategy lets the algorithm vary
independently from clients that use it
❖ The implementation of each of the
algorithms is kept in a separate
class referred to as a strategy.
❖ An object that uses a Strategy
object is referred to as a context
object.
❖ Changing the behavior of a
Context object is a matter of
changing its Strategy object to the
one that implements the required
algorithm
Solution Using Visitor pattern
class Plus extends Expr {
private Expr left, right;
public Plus(Expr arg1, Expr arg2) {
left = arg1;
right = arg2;
}
public Expr getLeft() {
return left;
}
public Expr getRight() {
return right;
}
public void accept(Visitor v) {
v.visit(this);
}
}
class DOTNETVisitor extends Visitor {
public void visit(Constant arg) {
System.out.println("ldarg " + arg.getVal());
}
public void visit(Plus plus) {
genCode(plus.getLeft());
genCode(plus.getRight());
System.out.println("add");
}
public void visit(Sub sub) {
genCode(sub.getLeft());
genCode(sub.getRight());
System.out.println("sub");
}
public void genCode(Expr expr) {
expr.accept(this);
}
}
Visitor Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Call Sequence
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Visitor Pattern: Discussion
❖ Many distinct and unrelated
operations need to be
performed on objects in an
object structure, and you want
to avoid “polluting” their
classes with these operations
Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the
elements on which it operations
❖ Create two class
hierarchies:
❖ One for the elements
being operated on
❖ One for the visitors that
define operations on the
elements
Procedural (conditional) Code
How to get rid of
conditional statements?
internal class Interpreter
{
private static readonly Stack<int> ExecutionStack = new Stack<int>();
private static int Interpret(byte[] byteCodes)
{
var pc = 0;
while (pc < byteCodes.Length)
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
return ExecutionStack.Pop();
}
// rest of the code elided ...
}
Hands-on Exercise
Refactor this code to remove the switch statements and
use runtime polymorphism instead
switch (byteCodes[pc++])
{
case (byte) BYTECODE.ADD:
ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop());
break;
case (byte) BYTECODE.LDCI4S:
ExecutionStack.Push(byteCodes[pc++]);
break;
}
Command Pattern: Solution
internal class Interpreter
{
private readonly Stack<int> _evalStack = new Stack<int>();
public int Interpret(ByteCode[] byteCodes)
{
foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack);
return _evalStack.Pop();
}
}
internal abstract class ByteCode
{
public abstract void Exec(Stack<int> evalStack);
}
internal class ADD : ByteCode
{
public override void Exec(Stack<int> evalStack)
{
var lval = evalStack.Pop();
var rval = evalStack.Pop();
evalStack.Push(rval + lval);
}
}
Command Pattern: Structure
Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
Command Pattern: Discussion
❖ Want to issue requests or
execute commands without
knowing anything about the
operation being requested or
the receiver of the request
Encapsulate a request as an object, thereby letting you parameterize clients with
different requests, queue or log requests, and support undoable operations.
❖ Parameterize objects by an
action to perform, with a
common method such as
Execute
❖ Can be useful to specify,
queue, and execute
requests at different times;
also to support undo or
logging
Patterns Discussed in this Case-Study
✓ Facade
✓ Composite
✓ Factory method
✓ Flyweight
✓ Builder
✓ Iterator pattern
✓ Strategy
✓ Visitor
✓ Command
BTW, What Architecture Style is it?
Lexical Analysis
Parsing
Semantic Analysis
Program file
Intermediate Code
Generation
Optimization
Native Code
Generation
Native assembly/machine code
Token stream
AST (Abstract Syntax Tree)
Attributed tree/
augmented syntax tree
Intermediate code
Optimized
intermediate code
Its “pipe-and-filter” Style!
sediment
pre-
carbon
ultra-filter
post-
carbon
Filtered
water
Real-world “pipes and filters”
Pattern Oriented Software Architecture (POSA)
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh

More Related Content

What's hot

Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
String in java
String in javaString in java
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
ankitgarg_er
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
Kumar Gaurav
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Head First Java Chapter 1
Head First Java Chapter 1Head First Java Chapter 1
Head First Java Chapter 1
Tom Henricksen
 
DATABASE MANAGEMENT SYSTEM LAB.pdf
DATABASE MANAGEMENT SYSTEM LAB.pdfDATABASE MANAGEMENT SYSTEM LAB.pdf
DATABASE MANAGEMENT SYSTEM LAB.pdf
Prof. Dr. K. Adisesha
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
Sunil OS
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
Bharat Kalia
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
AbhayDhupar
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Sunil OS
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
Reflection power pointpresentation ppt
Reflection power pointpresentation pptReflection power pointpresentation ppt
Reflection power pointpresentation ppt
Rohit Vipin Mathews
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 

What's hot (20)

Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
String in java
String in javaString in java
String in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Head First Java Chapter 1
Head First Java Chapter 1Head First Java Chapter 1
Head First Java Chapter 1
 
DATABASE MANAGEMENT SYSTEM LAB.pdf
DATABASE MANAGEMENT SYSTEM LAB.pdfDATABASE MANAGEMENT SYSTEM LAB.pdf
DATABASE MANAGEMENT SYSTEM LAB.pdf
 
Operators in java
Operators in javaOperators in java
Operators in java
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Reflection power pointpresentation ppt
Reflection power pointpresentation pptReflection power pointpresentation ppt
Reflection power pointpresentation ppt
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 

Similar to Design Patterns - Compiler Case Study - Hands-on Examples

Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#
CodeOps Technologies LLP
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study
CodeOps Technologies LLP
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
CodeOps Technologies LLP
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
rik0
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
PyCon Italia
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
Hugo Gävert
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
Giovanni Bassi
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
.NET Conf UY
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
Rodolfo Finochietti
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
tdc-globalcode
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
ssuserd64918
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
OOO "Program Verification Systems"
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
CodeOps Technologies LLP
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
Vikas Sharma
 

Similar to Design Patterns - Compiler Case Study - Hands-on Examples (20)

Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#
 
Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study Applied Design Patterns - A Compiler Case Study
Applied Design Patterns - A Compiler Case Study
 
Effective Object Oriented Design in Cpp
Effective Object Oriented Design in CppEffective Object Oriented Design in Cpp
Effective Object Oriented Design in Cpp
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 

More from Ganesh Samarthyam

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
Ganesh Samarthyam
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
Ganesh Samarthyam
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
Ganesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
Ganesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
Ganesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
Ganesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
Ganesh Samarthyam
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
Ganesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
Ganesh Samarthyam
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
Ganesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
Ganesh Samarthyam
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
Ganesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
Ganesh Samarthyam
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 

More from Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 

Recently uploaded

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 

Recently uploaded (20)

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 

Design Patterns - Compiler Case Study - Hands-on Examples

  • 1. Compiler Case Study - Design Patterns in C# Ganesh Samarthyam ganesh@codeops.tech
  • 2. Design Patterns - Introduction
  • 3. “Applying design principles is the key to creating high-quality software!” Architectural principles: Axis, symmetry, rhythm, datum, hierarchy, transformation
  • 4. How to Apply Principles in Practice? Design principles Code How to bridge the gap?
  • 6. What is Refactoring? Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior
  • 7. What are Design Patterns? recurrent solutions to common design problems Pattern Name Problem Solution Consequences
  • 8. Principles to Patterns Expr expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build(); Encapsulate object creation Builder pattern
  • 9. Why Care About Patterns? ❖ Patterns capture expert knowledge in the form of proven reusable solutions ❖ Better to reuse proven solutions than to “re-invent” the wheel ❖ When used correctly, patterns positively influence software quality ❖ Creates maintainable, extensible, and reusable code
  • 10. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 11. Design Patterns - Catalog Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994 Creational Deals with controlled object creation Factory method, for example Structural Deals with composition of classes or objects Composite, for example Behavioral Deals with interaction between objects/ classes and distribution of responsibility Strategy, for example
  • 12. 3 Principles Behind Patterns Design'principles' behind'pa0erns' Program'to'an' interface,'not'to'an' implementa7on'' Favor'object' composi7on'' over'inheritance' Encapsulate'what' varies'
  • 14. Case Study Overview ❖ Smelly code segments (specially if-else based on types and switch statements) used first ❖ They are “refactored” using design patterns ❖ The case study shows code-snippets; compilable, self- contained source code files shared with you
  • 15. What You Need to Know ❖ Understand essential object oriented constructs such as runtime polymorphism (virtual functions), and principles (such as encapsulation) ❖ Know essential C# language features - classes, inheritance, etc ❖ Data structures and algorithms (especially binary trees, tree traversals, and stacks) ❖ NO background in compilers needed ❖ Extremely simplified compiler code example; purposefully done to make best use of this case study
  • 16. Simple Expression Tree Example + * 30 10 20 Expression: ((10 * 20) + 30)
  • 17. Post-order Traversal: Simulating Code Gen 10 20 * 30 + post-order traversal result + * 30 10 20
  • 18. Front-end Intermediate code (CIL, Java bytecodes, …) Interpreter (Virtual Machine)
  • 19. Using Stack for Evaluation Initial empty 10 push 10 10 push 20 20 200 perform 10 * 20 push 200 200 push 30 30 230 perform 200 + 30 push 230 End result = 230 10 20 * 30 +
  • 20. Mnemonic Names in Java and .NET 10 20 * 30 + push 10 push 20 multiply push 30 add .NET CIL Instructions Java Bytecode Instructions ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add (ldc => load constant ; i4 => integer of 32 bits; s => signed) bipush 10 bipush 20 imul bipush 30 iadd (bipush stands for “byte integer push”)
  • 21. Hands-on Exercise Use the ildasm tool to disassemble the Expr.cs code // Expr.cs using System; class Test {     public static void Main() {         int a = 10, b = 20, c = 30;         int r = ((a * b) + c);         Console.WriteLine(r);     } }
  • 22. ((10 * 20) + 30)Source Code C# Compiler .NET CLR (Common Language Runtime) ldc.i4.s 0x0a ldc.i4.s 0x14 mul ldc.i4.s 0x1e add + * 30 10 20
  • 23. Facade Pattern Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • 24. Facade Pattern in Compilers “The Compiler class acts as a facade: It offers clients a single, simple interface to the compiler subsystem.”
  • 25. Facade Pattern in Compilers public virtual void Compile(StreamReader input, BytecodeStream output) { Scanner scanner = new Scanner(input); var builder = new ProgramNodeBuilder(); Parser parser = new Parser(); parser.Parse(scanner, builder); RISCCodeGenerator generator = new RISCCodeGenerator(output); ProgramNode parseTree = builder.GetRootNode(); parseTree.Traverse(generator); }
  • 26. Procedural (conditional) Code How to get rid of conditional statements? public void GenCode() { if (_left == null && _right == null) { Console.WriteLine("ldc.i4.s " + _value); } else { // its an intermediate node _left.GenCode(); _right.GenCode(); switch (_value) { case "+": Console.WriteLine("add"); break; case "-": Console.WriteLine("sub"); break; case "*": Console.WriteLine("mul"); break; case "/": Console.WriteLine("div"); break; default: Console.WriteLine("Not implemented yet!"); break; } } }
  • 27. Hands-on Exercise Refactor the “../Composite/Before/Compiler.cs” to get rid of conditional statements and use runtime polymorphism instead
  • 28. Hands-on Exercise internal abstract class Expr { public abstract void GenCode(); } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class BinaryExpr : Expr { private readonly Expr _left; private readonly Expr _right; public BinaryExpr(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override void GenCode() { _left.GenCode(); _right.GenCode(); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 29. Composite Pattern Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 30. Composite Pattern: Discussion ❖ There are many situations where a group of components form larger components ❖ Simplistic approach: Make container component contain primitive ones ❖ Problem: Code has to treat container and primitive components differently Compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects and compositions of objects uniformly. ❖ Perform recursive composition of components ❖ Clients don’t have to treat container and primitive components differently
  • 31. BTW, Did You Observe? // ((10 * 20) + 30) var expr = new Expr( new Expr( new Expr(null, "10", null), "*", new Expr(null, "20", null)), "+", new Expr(null, "30", null)); expr.GenCode(); // ((10 * 20) + 10) Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30)); expr.GenCode();
  • 32. Hands-on Exercise function interpret(node: binary expression tree) | if node is a leaf then return (node.value) | if node is binary operator op then return interpret(node.left) op interpret(node.right) Rewrite the code to “evaluate” the expression instead of generating the code - here is the “pseudo-code”
  • 33. Solution internal class Constant : Expr { private readonly int _val; public Constant(int arg) { _val = arg; } public override int Interpret() { return _val; } } internal class Plus : Expr { private readonly Expr _left; private readonly Expr _right; public Plus(Expr arg1, Expr arg2) { _left = arg1; _right = arg2; } public override int Interpret() { return _left.Interpret() + _right.Interpret(); } }
  • 34. Register-Based Instructions (x86) movl $10, -8(%rbp) movl $20, -12(%rbp) movl $30, -16(%rbp) movl -8(%rbp), %eax imull -12(%rbp), %eax addl -16(%rbp), %eax popq %rbp retq int main() { int a = 10; int b = 20; int c = 30; return ((a * b) + c); } cc -S expr.c
  • 35. Register-Based Instructions (Android) ((a * b) + c); javac -source 1.7 -target 1.7 expr.java dx --dex --output=expr.dex expr.class dexdump -d expr.dex const/16 v1, #int 10 // #a const/16 v2, #int 20 // #14 const/16 v3, #int 30 // #1e mul-int v0, v1, v2 add-int/2addr v0, v3 return v0
  • 36. Hands-on Exercise Generate register-based instructions for the given expression ((10 * 20) + 30) You can generate for an imaginary machine (i.e., a virtual machine like Dalvik/Android) or a real- machine (like x86-64)
  • 37. Solution internal class Register { private readonly int _index; private Register(int index) { _index = index; } public static Register GetRegister(int index) { return new Register(index); } // FreeRegister to be implemented // not implemented for now for the sake of simplicity public int GetIndex() { return _index; } }
  • 38. Solution abstract class Expr { public abstract Register GenCode(); } class Constant : Expr { int val; public Constant(int arg) { val = arg; } public override Register GenCode() { Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("const/16 v{0}, #int {1}", targetRegister.GetIndex(), val); return targetRegister; } } class Plus : Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public override Register GenCode() { Register firstRegister = left.GenCode(); Register secondRegister = right.GenCode(); Register targetRegister = RegisterAllocator.GetNextRegister(); Console.WriteLine("add-int v{0}, v{1}, v{2}", targetRegister.GetIndex(), firstRegister.GetIndex(), secondRegister.GetIndex()); return targetRegister; } }
  • 39. Solution ((10 + 20) + 30); Assuming infinite registers from v0, with target register as the first argument, here is the mnemonic code generated for our imaginary (i.e., virtual) machine const/16 v0, #int 10 const/16 v1, #int 20 add-int v2, v0, v1 const/16 v3, #int 30 add-int v4, v2, v3
  • 40. How to Improve the Tree Creation? var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); Expr expr = new Plus( new Multiply( new Constant(10), new Constant(20)), new Constant(30));
  • 41. Hands-on Exercise: Solution internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 43. Factory Method Pattern: Discussion ❖ A class cannot anticipate the class of objects it must create ❖ A class wants its subclasses to specify the objects it creates Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. ❖ Delegate the responsibility to one of the several helper subclasses ❖ Also, localize the knowledge of which subclass is the delegate
  • 44. Scenario internal class Constant : Expr { private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { return new Constant(arg); } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } For expressions like ((10 * 20) + 10), why should we create different Constant objects? Can we reuse them?
  • 45. Hands-on Exercise Improve this code to reuse immutable objects (for the ones with the same values) instead of creating duplicate objects. public static Constant Make(int arg) { return new Constant(arg); }
  • 46. Hands-on Exercise: Solution internal class Constant : Expr { private static readonly Dictionary<int, Constant> Pool = new Dictionary<int, Constant>(); private readonly int _val; private Constant(int val) { _val = val; } public static Constant Make(int arg) { if (!Pool.ContainsKey(arg)) Pool[arg] = new Constant(arg); return Pool[arg]; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } }
  • 47. Flyweight Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 48. Flyweight Pattern: Discussion ❖ When an application uses a large number of small objects, it can be expensive ❖ How to share objects at granular level without prohibitive cost? Use sharing to support large numbers of fine-grained objects efficiently ❖ When it is possible to share objects (i.e., objects don't depend on identity) ❖ When object’s value remain the same irrespective of the contexts - they can be shared ❖ Share the commonly used objects in a pool
  • 49. Scenario // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); This code still sucks - if the expression becomes more complex, can you really read it? Can we simplify this?
  • 50. Hands-on Exercise Improve this code to use “builder pattern”. // ((10 * 20) + 10) var expr = Addition.Make( Multiplication.Make( Constant.Make(10), Constant.Make(20)), Constant.Make(10)); expr.GenCode(); var expr = new ExprBuilder().Const(10).Mult(20).Plus(30).Build();
  • 51. Hands-on Exercise: Solution internal class ExprBuilder { private Expr _expr; public ExprBuilder Const(int arg) { _expr = Constant.Make(arg); return this; } public ExprBuilder Plus(int arg) { _expr = new Addition(_expr, Constant.Make(arg)); return this; } public ExprBuilder Mult(int arg) { _expr = new Multiplication(_expr, Constant.Make(arg)); return this; } public Expr Build() { return _expr; } }
  • 52. Builder Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 53. Builder Pattern: Discussion ❖ Creating or assembling a complex object can be tedious Separate the construction of a complex object from its representation so that the same construction process can create different representations. ❖ Make the algorithm for creating a complex object independent of parts that make up the object and how they are assembled ❖ The construction process allows different representations for the object that is constructed
  • 54. Scenario Provide the ability to traverse the aggregate structure without exposing implementation details (also use iteration instead of recursion) internal class Constant : Expr { private readonly int _val; public Constant(int val) { _val = val; } public override void GenCode() { Console.WriteLine("ldc.i4.s " + _val); } } internal class Plus : BinaryExpr { public Plus(Expr arg1, Expr arg2) : base(arg1, arg2) { } public override void GenCode() { base.GenCode(); Console.WriteLine("add"); } }
  • 55. Hands-on Exercise Traverse the expression tree using “external” iteration public override void GenCode() { base.GenCode(); Console.WriteLine("add"); }
  • 56. Hands-on Exercise: Solution internal class ExprIterator { public static IEnumerable<Expr> Traverse(Expr node) { if (node != null) { foreach (var left in Traverse(node.GetLeft())) yield return left; foreach (var right in Traverse(node.GetRight())) yield return right; yield return node; } } } foreach (var node in ExprIterator.Traverse(expr)) node.GenCode();
  • 57. Iterator Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 58. Iterator Pattern: Discussion ❖ How to provide a way to traverse an aggregate structure in different ways, but without exposing its implementation details? Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. ❖ Take the responsibility for access and traversal out of the list object and put it into an iterator object ❖ Let the iterator keep track of the element visited, traversal order, etc
  • 59. Scenario How to separate: a) code generation logic from node types? b) how to support different target types? class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public void genCode() { left.genCode(); right.genCode(); if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); } } }
  • 60. Hands-on Exercise Refactor this code to remove the if-else condition check (explicit type-checking code) and use runtime polymorphism instead if(t == Target.JVM) { System.out.println("iadd"); } else { // DOTNET System.out.println("add"); }
  • 61. Hands-on Exercise internal abstract class Target { public abstract void GenCode(Constant constant); public abstract void GenCode(Plus plus); public abstract void GenCode(Mult mult); } internal class JvmTarget : Target { public override void GenCode(Constant constant) { Console.WriteLine("bipush " + constant.GetValue()); } public override void GenCode(Plus plus) { Console.WriteLine("iadd"); } public override void GenCode(Mult mult) { Console.WriteLine("imul"); } }
  • 62. Strategy Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 63. Strategy Pattern: Discussion ❖ Useful when there is a set of related algorithms and a client object needs to be able to dynamically pick and choose an algorithm that suits its current need Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it ❖ The implementation of each of the algorithms is kept in a separate class referred to as a strategy. ❖ An object that uses a Strategy object is referred to as a context object. ❖ Changing the behavior of a Context object is a matter of changing its Strategy object to the one that implements the required algorithm
  • 64. Solution Using Visitor pattern class Plus extends Expr { private Expr left, right; public Plus(Expr arg1, Expr arg2) { left = arg1; right = arg2; } public Expr getLeft() { return left; } public Expr getRight() { return right; } public void accept(Visitor v) { v.visit(this); } } class DOTNETVisitor extends Visitor { public void visit(Constant arg) { System.out.println("ldarg " + arg.getVal()); } public void visit(Plus plus) { genCode(plus.getLeft()); genCode(plus.getRight()); System.out.println("add"); } public void visit(Sub sub) { genCode(sub.getLeft()); genCode(sub.getRight()); System.out.println("sub"); } public void genCode(Expr expr) { expr.accept(this); } }
  • 65. Visitor Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 66. Visitor Pattern: Call Sequence Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 67. Visitor Pattern: Discussion ❖ Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operations ❖ Create two class hierarchies: ❖ One for the elements being operated on ❖ One for the visitors that define operations on the elements
  • 68. Procedural (conditional) Code How to get rid of conditional statements? internal class Interpreter { private static readonly Stack<int> ExecutionStack = new Stack<int>(); private static int Interpret(byte[] byteCodes) { var pc = 0; while (pc < byteCodes.Length) switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; } return ExecutionStack.Pop(); } // rest of the code elided ... }
  • 69. Hands-on Exercise Refactor this code to remove the switch statements and use runtime polymorphism instead switch (byteCodes[pc++]) { case (byte) BYTECODE.ADD: ExecutionStack.Push(ExecutionStack.Pop() + ExecutionStack.Pop()); break; case (byte) BYTECODE.LDCI4S: ExecutionStack.Push(byteCodes[pc++]); break; }
  • 70. Command Pattern: Solution internal class Interpreter { private readonly Stack<int> _evalStack = new Stack<int>(); public int Interpret(ByteCode[] byteCodes) { foreach (var byteCode in byteCodes) byteCode.Exec(_evalStack); return _evalStack.Pop(); } } internal abstract class ByteCode { public abstract void Exec(Stack<int> evalStack); } internal class ADD : ByteCode { public override void Exec(Stack<int> evalStack) { var lval = evalStack.Pop(); var rval = evalStack.Pop(); evalStack.Push(rval + lval); } }
  • 71. Command Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 72. Command Pattern: Discussion ❖ Want to issue requests or execute commands without knowing anything about the operation being requested or the receiver of the request Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. ❖ Parameterize objects by an action to perform, with a common method such as Execute ❖ Can be useful to specify, queue, and execute requests at different times; also to support undo or logging
  • 73. Patterns Discussed in this Case-Study ✓ Facade ✓ Composite ✓ Factory method ✓ Flyweight ✓ Builder ✓ Iterator pattern ✓ Strategy ✓ Visitor ✓ Command
  • 74. BTW, What Architecture Style is it? Lexical Analysis Parsing Semantic Analysis Program file Intermediate Code Generation Optimization Native Code Generation Native assembly/machine code Token stream AST (Abstract Syntax Tree) Attributed tree/ augmented syntax tree Intermediate code Optimized intermediate code
  • 76. Pattern Oriented Software Architecture (POSA)