Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Closures & Environments CS153: Compilers Greg Morrisett.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
Metacircular Evaluation SICP Chapter 4 Mark Boady.
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.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Lexical vs. Dynamic Scope …where do I point to?. Intro… Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The Role of the Study of Programming Languages in the Education of a Programmer Dan Friedman Indiana University.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
EOPL3: Section 3.3 PROC and App B: SLLGEN
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp... (method-app-exp (obj-exp method-name rands)
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
3.2 The Front End > (a-program ; add1(+(3,x)) (primapp-exp (incr-prim) ((primap-exp (add-prim) ((lit-exp 3) (var-exp x)))))) Want to avoid having to specify.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Corky Cartwright January 18, 2017
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
Case Study: Undefined Variables
5.4.1: Implementation Method Invocation
Is everyone signed up on piazza?
Semantics of PLs via Interpreters: Getting Started
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
Programming Language Principles
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Extending our interpreted language with Scheme’s set!
Chapter 1 Review: BNF Grammar for lists:
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Inductively Defined Data Concrete and Abstract syntax
Presentation transcript:

Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda (x) (* x x) 6) 36

Front End Compiler program textsyntax tree answer % javac square6.java % java square6 36 % g++ -o square6 square6.cpp %./square6 36 object code VM/OS

Defining language will be Scheme (of course). Defined language will be simple arithmetic expressions: 3 x +(3,x) add1(+(3,x)) 3.1 A Simple Interpreter

Syntax: ::= ::= ( { }* (,) ) ::= + | - | * | add1 | sub1 A Simple Interpreter a-program (exp) lit-exp (datum) var-exp (id) primapp-exp (prim rands)

A Simple Interpreter (define-datatype program program? (a-program (exp expression?)) (define-datatype expression expression? (lit-exp (datum number?)) (var-exp (id symbol?)) (primapp-exp (prim primitive?) (rands (list-of expression?)))

A Simple Interpreter (define-datatype primitive primitive? (add-prim) (subtract-prim) (mult-prim) (incr-prim) (decr-prim)) > (a-program ; add1(+(3,x)) (primapp-exp (incr-prim) ((primapp-exp (add-prim) ((lit-exp 3) (var-exp 'x))))))

Interpreter Code (define eval-program (lambda (pgm) (cases program pgm (a-program (body) (eval-expression body (init-env)))))) (define init-env (lambda () (extend-env '(i v x) '(1 5 10) (empty-env))))

Interpreter Code (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum) (var-exp (id) (apply-env env id)) (primapp-exp (prim rands) (let ((args (eval-rands rands env))) (apply-primitive prim args))))))

Interpreter Code (define eval-rands (lambda (rands env) (map (lambda (x) (eval-rand x env))rands))) (define eval-rand (lambda (rand env) (eval-expression rand env)))

Interpreter Code (define apply-primitive (lambda (prim args) (cases primitive prim (add-prim () (+ (car args) (cadr args))) (subtract-prim () (- (car args) (cadr args))) (mult-prim () (* (car args) (cadr args))) (incr-prim () (+ (car args) 1)) (decr-prim () (- (car args) 1)))))