Plt-2002-2 11/19/2015 4.1-1 Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.

Slides:



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

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1.1 Lesson – Evaluating Algebraic Expressions
EOPL3: Section 3.3 PROC and App B: SLLGEN
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
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:
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
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:
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
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.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Implementing Recursion
Env. Model Implementation
5.4.1: Implementation Method Invocation
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)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
CSE S. Tanimoto Lambda Calculus
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Inductively Defined Data Concrete and Abstract syntax
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter

plt /19/ Languages and Values defined (source) language: implemented by the interpreter defining (host) language: in which the interpreter itself is written — here: Scheme expressed values: possible values of expressions in a language denoted values: values bound to variables e.g. in Scheme: lots of expressed values: numbers, pairs, … denoted values: only locations with expressed values

plt /19/ First Language program: expression a-program (exp) expression: number lit-expr (datum) : id var-exp (id) : primitive '(' expression *',' ')' primapp-exp (prim rands) primitive: '+' | '-' | '*' | 'add1' | 'sub1'

plt /19/ AST for First Language (1) (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?)) ) the field names are not really relevant

plt /19/ AST for First Language (2) (define-datatype primitive primitive? (add-prim) (subtract-prim) (mult-prim) (incr-prim) (decr-prim) )

plt /19/ Interpreter (1) (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) ) ) ) evaluation follows the datatype, i.e., the AST. environment simulates Roman numerals, arbitrary, initialization is not totally transparent.

plt /19/ Interpreter (2) (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) ; represents itself datum ) (var-exp (id) ; needs environment (apply-env env id) ) (primapp-exp (prim rands) (let ((args (eval-rands rands env))) (apply-primitive prim args) ) ) ) ) )

plt /19/ Interpreter (3) (define eval-rands (lambda (rands env) (map (lambda (x) (eval-rand x env)) rands ) ) ) (define eval-rand (lambda (rand env) (eval-expression rand env) ) map does not imply an order of evaluation.

plt /19/ Interpreter (4) (define apply-primitive (lambda (prim args) (cases primitive prim (add-prim () (+ (car args) (cadr args)) ) (subtract-prim..) (mult-prim..) (incr-prim () (+ (car args) 1)) (decr-prim..) ) ) ) BUG: grammar does not guarantee enough operands