SlideShare a Scribd company logo
1 of 46
Download to read offline
Interpreter Case Study -
Design Patterns
Ganesh Samarthyam
ganesh@codeops.tech
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 will be 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 Java language features - classes, inheritance, etc
❖ Data structures and algorithms (especially binary trees, tree
traversals, and stacks)
❖ NO background in compilers/interpreters needed
❖ Extremely simplified interpreter code examples;
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”)
((a	*	b)	+	c)Source Code
Java
Compiler
JVM
(Java
Virtual
Machine)
9: iload_1
10: iload_2
11: imul
12: iload_3
13: iadd
+
* c
a b
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
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
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
Patterns Discussed in this Case-Study
✓ Composite
✓ Factory method
✓ Flyweight
✓ Builder
✓ 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

Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)Mohd Effandi
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphismramya marichamy
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingAbdullah Jan
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in Crgnikate
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and loopingxcoolanurag
 
Python functions
Python functionsPython functions
Python functionsAliyamanasa
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - PointersWingston
 

What's hot (20)

Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Advance python
Advance pythonAdvance python
Advance python
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
Python functions
Python functionsPython functions
Python functions
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 

Similar to Interpreter Case Study - Design Patterns

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
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
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
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)PROIDEA
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already KnowKevlin Henney
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)jeffz
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedPascal-Louis Perez
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design PatternsStefano Fago
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConfJaroslaw Palka
 
Coding convention
Coding conventionCoding convention
Coding conventionKhoa Nguyen
 

Similar to Interpreter Case Study - Design Patterns (20)

C# programming
C# programming C# programming
C# programming
 
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#
 
Testing for share
Testing for share Testing for share
Testing for share
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
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
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Functional Programming You Already Know
Functional Programming You Already KnowFunctional Programming You Already Know
Functional Programming You Already Know
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConf
 
Coding convention
Coding conventionCoding convention
Coding convention
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 

More from CodeOps Technologies LLP

AWS Serverless Event-driven Architecture - in lastminute.com meetup
AWS Serverless Event-driven Architecture - in lastminute.com meetupAWS Serverless Event-driven Architecture - in lastminute.com meetup
AWS Serverless Event-driven Architecture - in lastminute.com meetupCodeOps Technologies LLP
 
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONSBUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONSCodeOps Technologies LLP
 
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICESAPPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICESCodeOps Technologies LLP
 
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPSBUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPSCodeOps Technologies LLP
 
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNERCREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNERCodeOps Technologies LLP
 
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...CodeOps Technologies LLP
 
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESSWRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESSCodeOps Technologies LLP
 
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
Training And Serving ML Model Using Kubeflow by Jayesh SharmaTraining And Serving ML Model Using Kubeflow by Jayesh Sharma
Training And Serving ML Model Using Kubeflow by Jayesh SharmaCodeOps Technologies LLP
 
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
Deploy Microservices To Kubernetes Without Secrets by Reenu SalujaDeploy Microservices To Kubernetes Without Secrets by Reenu Saluja
Deploy Microservices To Kubernetes Without Secrets by Reenu SalujaCodeOps Technologies LLP
 
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...CodeOps Technologies LLP
 
YAML Tips For Kubernetes by Neependra Khare
YAML Tips For Kubernetes by Neependra KhareYAML Tips For Kubernetes by Neependra Khare
YAML Tips For Kubernetes by Neependra KhareCodeOps Technologies LLP
 
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...CodeOps Technologies LLP
 
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
Monitor Azure Kubernetes Cluster With Prometheus by Mamta JhaMonitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
Monitor Azure Kubernetes Cluster With Prometheus by Mamta JhaCodeOps Technologies LLP
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsCodeOps Technologies LLP
 
Distributed Tracing: New DevOps Foundation
Distributed Tracing: New DevOps FoundationDistributed Tracing: New DevOps Foundation
Distributed Tracing: New DevOps FoundationCodeOps Technologies LLP
 
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire  "Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire CodeOps Technologies LLP
 

More from CodeOps Technologies LLP (20)

AWS Serverless Event-driven Architecture - in lastminute.com meetup
AWS Serverless Event-driven Architecture - in lastminute.com meetupAWS Serverless Event-driven Architecture - in lastminute.com meetup
AWS Serverless Event-driven Architecture - in lastminute.com meetup
 
Understanding azure batch service
Understanding azure batch serviceUnderstanding azure batch service
Understanding azure batch service
 
DEVOPS AND MACHINE LEARNING
DEVOPS AND MACHINE LEARNINGDEVOPS AND MACHINE LEARNING
DEVOPS AND MACHINE LEARNING
 
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONSSERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
 
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONSBUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
 
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICESAPPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
 
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPSBUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
 
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNERCREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
 
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
 
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESSWRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
 
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
Training And Serving ML Model Using Kubeflow by Jayesh SharmaTraining And Serving ML Model Using Kubeflow by Jayesh Sharma
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
 
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
Deploy Microservices To Kubernetes Without Secrets by Reenu SalujaDeploy Microservices To Kubernetes Without Secrets by Reenu Saluja
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
 
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
 
YAML Tips For Kubernetes by Neependra Khare
YAML Tips For Kubernetes by Neependra KhareYAML Tips For Kubernetes by Neependra Khare
YAML Tips For Kubernetes by Neependra Khare
 
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
 
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
Monitor Azure Kubernetes Cluster With Prometheus by Mamta JhaMonitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
 
Jet brains space intro presentation
Jet brains space intro presentationJet brains space intro presentation
Jet brains space intro presentation
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and Streams
 
Distributed Tracing: New DevOps Foundation
Distributed Tracing: New DevOps FoundationDistributed Tracing: New DevOps Foundation
Distributed Tracing: New DevOps Foundation
 
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire  "Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
 

Recently uploaded

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 

Recently uploaded (20)

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 

Interpreter Case Study - Design Patterns

  • 1. Interpreter Case Study - Design Patterns Ganesh Samarthyam ganesh@codeops.tech
  • 2. 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 will be shared with you
  • 3. What You Need to Know ❖ Understand essential object oriented constructs such as runtime polymorphism (virtual functions), and principles (such as encapsulation) ❖ Know essential Java language features - classes, inheritance, etc ❖ Data structures and algorithms (especially binary trees, tree traversals, and stacks) ❖ NO background in compilers/interpreters needed ❖ Extremely simplified interpreter code examples; purposefully done to make best use of this case study
  • 4. Simple Expression Tree Example + * 30 10 20 Expression: ((10 * 20) + 30)
  • 5. Post-order Traversal: Simulating Code Gen 10 20 * 30 + post-order traversal result + * 30 10 20
  • 6. Front-end Intermediate code (CIL, Java bytecodes, …) Interpreter (Virtual Machine)
  • 7. 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 +
  • 8. 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”)
  • 10. 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 ... }
  • 11. 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; }
  • 12. 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); } }
  • 13. Command Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 14. 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
  • 15. 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; } } }
  • 16. 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"); } }
  • 17. Composite Pattern Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 18. 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
  • 19. 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();
  • 20. 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”
  • 21. 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(); } }
  • 22. 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
  • 23. 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
  • 24. 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)
  • 25. 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; } }
  • 26. 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; } }
  • 27. 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
  • 28. 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));
  • 29. 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); } }
  • 31. 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
  • 32. 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?
  • 33. 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); }
  • 34. 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); } }
  • 35. Flyweight Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 36. 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
  • 37. 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?
  • 38. 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();
  • 39. 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; } }
  • 40. Builder Pattern: Structure Source: “Design Patterns: Elements of Reusable Object-Oriented Software”, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley,1994
  • 41. 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
  • 42. Patterns Discussed in this Case-Study ✓ Composite ✓ Factory method ✓ Flyweight ✓ Builder ✓ Command
  • 43. 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
  • 45. Pattern Oriented Software Architecture (POSA)