An Interpreter for Zoom Language - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

An Interpreter for Zoom Language

Description:

The interpreter should be able to execute all java statements and ... Parser/Lexer takes program source and creates AST Nodes. int x = 1 2 * y creates tree: ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 34
Provided by: andrew380
Category:

less

Transcript and Presenter's Notes

Title: An Interpreter for Zoom Language


1
An Interpreter for Zoom Language
  • Final Presentation SE690/696
  • By Andrew Deren
  • Advisor Xiaoping Jia
  • June 4, 2004

2
Agenda
  • Project Goals
  • Zoom Overview
  • Interpreter Overview
  • Architecture
  • Zoom extensions
  • Demo

3
Project Goals
  • To develop interpreter for implementation of Zoom
    Language.
  • The interpreter should be able to execute all
    java statements and expressions and zoom
    extensions to java
  • Should support all native types for zoom
    including all collection types

4
Zoom Overview
  • ZOOM is a set of tools and notation
    specifications used for development of large
    scale Object-Oriented systems.
  • ZOOM stands for Z-based Object Oriented Modeling
  • The ZOOM Project is supervised by Dr. Jia

5
Parts of Zoom
  • There are 3 parts of ZOOM
  • ZOOM-D design notation
  • Formal notation to specify design models object
    design models and user interface design.
  • ZOOM-S specification notation
  • Formal notation to describe the use cases with
    formally specified preconditions and
    postconditions.
  • ZOOM-I implementation language
  • Can be any object-oriented language Java, C,
    etc
  • Currently work is done on extending Java.
  • Language will be extended with zoom specific
    features, but parts of the software system can be
    developed in target language.

6
What is Interpreter?
  • An interpreter is a program that accepts any
    program (the source program) expressed in a
    particular language (the source language), and
    runs that source program immediately.
  • The interpreter does not translate the source
    program into object code prior to execution.

7
Why Write Interpreter?
  • Easier to test programs, no need to recompile.
    Can test fragments of code.
  • Changes to design of ZOOM language are easier to
    test in interpreter.
  • Some features of ZOOM might be hard to translate
    to java code directly.
  • Interpreters are much easier to implement than
    compilers.

8
Programming Language Lifecycle Model
Can be implemented as Interpreter to test
language features
9
Interpreter Architecture
  • 1. Program text is fetched to ZOOM Parser which
    builds AST nodes.
  • 2. AST nodes are transformed into language
    elements (Statements, Expressions, etc)
  • 3. Type checker checks type validity of the
    program.
  • 4. Interpreter walks AST evaluating statements
    and expressions.

10
Interpreter Architecture
  • Operates on AST elements (expressions,
    statements, etc)
  • Interpreter runtime system holds all data in
    objects derived from Val
  • Each Val type knows how to interact with other
    Val types and supports all operators that can be
    performed by that type

int x 3 // creates ValInt int y 4 //
created ValInt int z x y // calls
x.operator_plus(y)
11
AST
  • AST abstract syntax tree
  • Represents program structure as a tree structure
  • Parser/Lexer takes program source and creates AST
    Nodes
  • int x 1 2 y creates tree

12
Interpreter Architecture
  • There is a total of 28 build in types. Each type
    supports implements its methods and operators
    Array, Collection types, numeric types, enum
    types, meta type, etc.
  • Its a fairly large project (over 30,000 lines of
    java code)
  • There are several option to control the
    interpretation of code (egc semantics, collection
    creation size, etc)

13
Architecture
  • Does not use virtual machine for interpretation
  • Operates directly on AST (Abstract Syntax Tree)
    nodes instead of bytecode
  • Not designed for speed, but for extensibility and
    flexibility

14
Class Diagram
15
Class Diagram
16
4 modes of execution
  • Command line
  • Can execute jzoom files containing execute or
    eval statements
  • Zoom console
  • - expression and execute statement evaluation
    and loading from file
  • GUI interface
  • - interactive zoom program evaluation
  • Programming API
  • - used by other zoom tools to execute zoom
    programs or fragments of programs

17
Execute Eval Statements
  • Main execution block for interpreter is execute
    statement
  • execute eval
  • import declarations
  • class declarations
  • function declarations
  • enum declarations
  • typedef declarations
  • statements

18
Execute Statement Example
  • execute
  • import AddressDb
  • class Address String name String city
  • void printAddress(Address a)
  • println(Name a.name)
  • println(City a.city)
  • ListAddress addresses AddressDb.Load(data.da
    t)
  • addresses new Address()
  • foreach (Address a addresses)
  • printAddress(a)

19
Collection Classes
  • Supports all syntax for collection classes from
    zoom standard library
  • Set, List, OrderedSet, Relation, Bag, Map
  • Build in operators and construction of Set and
    List
  • Can use literal collection prefix to create
    collections (M - map, B - bag, O ordered set, R
    - relation)
  • All collection classes support
  • c get size of collection
  • ci get i-th element
  • x in C or x !in C check membership
  • All collection classes support extensive list of
    methods and since collection classes are
    immutable all operations return new instance
  • Built in functions for list classes (List,
    Sequence, OrderedSet)
  • first, last, head, tail, reverse

20
Collection Classes
  • Example uses of collections
  • Mapint, String map 1 -gt One, 2 -gt
    TwoM
  • List list int x x gt 5 x lt 10 _at_ pow(x, 2)
  • int listsize list
  • boolean hasit 36 in list
  • Set set 1 .. 10
  • int second set1
  • Tuple a (1, One, Jeden, Odin)
  • int first a.first
  • Can use foreach statement
  • foreach (Type t collection)
  • do something with t
  • Can iterate on collections using (implemented in
    interpreter by Sam Chen)
  • select, reject, collectNested, collect, sortedBy,
    sum, isUnique, any
  • Ex
  • s.select(i _at_ i gt 2)
  • s.sortedBy(c _at_ c.ord)

21
Collection Classes Hierarchy
22
Extended Types Operations
  • Can use enum and typedef
  • Built in functions head, tail, front, last,
    toChar
  • Special interaction functions print, println,
    prompt, alert, confirm
  • Built in all Math functions and constants (do not
    have to use Math.method())
  • All collection classes have overloaded operators
    for collection manipulation (look at zoom
    reference manual for all supported operators)
  • Can use stringindex or foreach(char c string)
    on string object

23
Native Types
  • Supports all java native types and operations on
    those types (byte, short, int, long, float,
    double, char, string, null)
  • Can instantiate and call methods on java library
    types. Have to specify full package name. Ex
  • java.util.Hashtable ht new java.util.Hashtable(
    )
  • Java interaction done through reflection and all
    types are automatically converted from java
    representation to interpreter representation.

24
Enumerations
  • Enumerations work differently from java 1.5
  • enum Color white, red, yellow, green, blue,
    brown, black
  • Color.name() "Color"
  • Color.first() Color.white
  • Color.last() Color.black
  • Color.count() 7
  • Color0 Color.white
  • Color2 Color.yellow
  • Color2.name "yellow"
  • Color.white.ord 0
  • Color.yellow.name yellow
  • Supports all operations on enums (look at zoom
    reference for all allowable operations)

25
Extended Guarded Statement
  • Extended guarded statement can be used to test
    pre and post conditions and roll-back changes.
  • Currently Animator tool uses it extensively.

26
Conditional Expressions
  • boolean result if (x 3)
  • int z x 2
  • println(z)
  • else
  • println("Else")
  • --------------------------------------------------
    -------------
  • String num if (z 1) then "One"
  • elsif (z 2) then "Two"
  • elsif (z gt 2 z lt 10) then "Between 2 and 10"
  • else "Other" endif
  • --------------------------------------------------
    -------------
  • int numDays switch month
  • case Months.January, Months.March, Months.May,
    Months.July,
  • Months.August, Months.October,
    Months.December 31
  • case Months.February 28
  • default 30

27
Interpreter API
  • Other zoom tools can use interpreter using
    interpreter API
  • Can execute fragments of code (statements,
    expressions or methods)
  • Can persist interpreter state between executions
  • Currently animator tool uses interpreter API
  • Easy to extend and add built in types, functions,
    constants or other functionality.

28
Interpreter GUI interface
29
Interpreter Console Command
  • Integrated into zoom command console interface
  • Supported operations
  • Will evaluate all statements (will wrap inside of
    execute statement)
  • Can execute single expressions
  • Ex
  • SetString s One, Tow
  • s Three, Four Five
  • s // prints One, Two, Three, Four,
    Five
  • s // prints 5

30
AST Viewer
  • AST viewer shows graphical representation of the
    program
  • Can be used from GUI or console mode

31
Future Work
  • There are some missing features from the
    interpreter.
  • Step-by-step execution
  • Exception handling
  • Multi-threading

32
Demo
33
References
  • David A. Watt Deryck F. Brown. Programming
    Language Processors in Java. Prentice Hall, 2000.
  • Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman.
    Compilers Principles, Techniques and Tools.
    Addison-Wesley,1988.
  • Ravi Sethi. Programming Languages, Concepts
    Constructs. Addison-Wesley, 1996.
  • Randy M. Kaplan. Constructing Language Processors
    for Little Languages. John Wiley Sons, Inc.,
    1994.
  • O.G. Kakde. Algorithms for Compiler Design.
    Charles Rivera Media, 2003.
  • Keith D. Cooper Linda Torczon. Engineering a
    Compiler. Morgan Kaufmann, 2004.
Write a Comment
User Comments (0)
About PowerShow.com