Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Prof. Fateman CS 164 Lecture 261 Functional Tiger, Calling variations Lecture 26.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
ISBN Chapter 10 Implementing Subprograms.
Prof. Fateman CS164 Lecture 241 Other Control Flow ideas: Throw, Catch, Continuations and Call/CC Lecture 24.
Prof. Fateman CS 164 Lecture 141 Language definition by interpreter Lecture 14.
ISBN Chapter 10 Implementing Subprograms.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Run time vs. Compile time
Semantics of Calls and Returns
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Prof. Fateman CS 164 Lecture 161 Properly Tail Recursive Interpreter. Some timings of compilation vs interpretation Lecture 16.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Runtime Environments Compiler Construction Chapter 7.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Operational Semantics of Scheme
Run-Time Environments Chapter 7
User-Written Functions
Functions.
Implementing Subprograms
The Environment Model*
More Object Oriented Programming
Mini Language Interpreter Programming Languages (CS 550)
Implementing Subprograms
The Metacircular Evaluator
CSE401 Introduction to Compiler Construction
Modern Programming Languages Lecture 20 Fakhar Lodhi
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Introduction to Intermediate Code, virtual machine implementation
Abstraction and Repetition
Overview of Semantic Analysis
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Corresponds with Chapter 5
Presentation transcript:

Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15

Prof. Fateman CS 164 Lecture 152 So far, two ways of looking at MJ for executing code MJ interpreter (see simple-interp.lisp) MJ translator into Common Lisp (see simple-translate.lisp) –Common Lisp can be translated into machine language

Prof. Fateman CS 164 Lecture 153 MJ interpreter Environment includes all Class data Method calls require setting up a new stack frame –All formal parameters matched to actual parameters –Stack environment then passed to interpreter recursively –Statements in method body evaluated –Return from method call pops frame from stack –To test --Try running (say) Factorial, tracing mj-call or mj-dispatch or mj-new-frame or insert output statements (format t "~%xxx=~s" xxx)

Prof. Fateman CS 164 Lecture 154 MJ translator Running AST through the translator produces a new (file) of Lisp code. Environment still has Class methods. “New” programs produce instances. Method calls changed into Lisp function calls. Lisp takes care of all the rest, in particular –All formal parameters matched to actual parameters –Stack environment then passed to interpreter recursively –Statements in method body executed by Lisp –Return from method returns Lisp stack to state previous to method call –Just trace names, e.g. mjl-ClassName-MethodName

Prof. Fateman CS 164 Lecture 155 MJ translator Consider Factorial.java. The translator produces about 24 lines of lisp.

Prof. Fateman CS 164 Lecture 156 Factorial in MJ class Factorial{ public static void main(String[] a){ System.out.println(new Fac().ComputeFac(10)); }} class Fac { public int ComputeFac(int num){ int num_aux ; if (num < 1) num_aux = 1 ; else num_aux = num * (this.ComputeFac(num-1)) ; return num_aux ; }

Prof. Fateman CS 164 Lecture 157 Compiled Factorial ;; Fac::ComputeFac (defun mjl-Fac-ComputeFac (this L1) ; L1 is num (let ((L2 0)) ; L2 is num_aux (if (< L1 1) (setf L2 1) (setf L2 (* L1 (let* ((obj this) (vtable (elt obj 0))) ;recursive call. Explain obj (funcall (gethash 'ComputeFac vtable) obj (- L1 1)))))) L2)) ;; Vtable for Fac (setf mjl-Fac (make-hash-table)) (setf (gethash 'ComputeFac mjl-Fac) #'mjl-Fac-ComputeFac) ;; Constructor for Fac (defun mjl-Fac+ () (let ((obj (make-array '(1)))) (setf (elt obj 0) mjl-Fac) obj))

Prof. Fateman CS 164 Lecture 158 Compiled Factorial ;; Main routine (format *mj-stdout* "~A~%" ;print the result (let* ((obj (mjl-Fac+)) (vtable (elt obj 0))) ;create a Fac object (funcall (gethash 'ComputeFac vtable) obj 10)))

Prof. Fateman CS 164 Lecture 159 Looking up vars at runtime Lisp translation makes the separation clear. There are local variables handled by Lisp, and other variables or methods are looked up in the object. If we are doing our own interpreting, we have two places to look –Local parameters to a method –Vars relative to this. (the object whose method is being called) Variables in this class Variables in the parent class(es)

Prof. Fateman CS 164 Lecture 1510 Adding bindings to an environment.. Scheme (defun extend-env1 (p1 a1 e) ;; add one binding to the environment (cons (cons p1 a1) e)) (defun extend-env (p a e) ;; add a list of bindings to the environment. We have two ;; variable/value lists. (extend-env '(a b c) '(1 2 3) '((d. 4))) ;; -> ((a. 1) (b. 2) (c. 3) (d. 4)) (if (null p) e (extend-env1 (car p)(car a)(extend-env (cdr p)(cdr a) e))))

Prof. Fateman CS 164 Lecture 1511 Adding bindings to a MJ env (defun mj-new-frame(method this actuals e) ;; pick out the formal args from method. ;; we will map them to the actuals. ;; make a new env, same as the old layout but with a new frame. ;; the frame is an array, and will contain formal actual mapping. ;; Make a new environment.. Copy all the old bindings into it and put in ;; a few more. It looks like… (cons (layout method) new-frame))

Prof. Fateman CS 164 Lecture 1512 We need less info for typechecking The layout (classes, methods) The variables: we do not really need space for values (though it won’t hurt much); we need the types and the line/column locations of declarations for error messages. We don’t need to actually locate (with array offsets etc) the location of values, but if we compute them, we can use this for compiling, later.