Parsing Scheme (+ (* 2 3) 1) * 1

Similar documents
Scheme in Scheme: The Metacircular Evaluator Eval and Apply

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Turtles All The Way Down

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Discussion 12 The MCE (solutions)

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Functional Programming. Pure Functional Programming

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Racket: Modules, Contracts, Languages

CS 320: Concepts of Programming Languages

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Lexical vs. Dynamic Scope

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Scope, Functions, and Storage Management

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Teaching London Computing

Assembly Language Manual for the STACK Computer

Konzepte von Programmiersprachen

Project 2: Scheme Interpreter

Verified compilation of a first-order Lisp language

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Implementing Recursion

CSC 2400: Computer Systems. Using the Stack for Function Calls

Mid-Term 2 Grades

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

Comp 311: Sample Midterm Examination

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CSE 341: Programming Languages

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

DRAWING ENVIRONMENT DIAGRAMS

An Explicit Continuation Evaluator for Scheme

CS 360 Programming Languages Interpreters

Lecture 15 CIS 341: COMPILERS

Programming Languages: Application and Interpretation

Lecture 8: Simple Calculator Application

Scope and Parameter Passing 1 / 19

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Lecture 12: Conditional Expressions and Local Binding

Mechanized Operational Semantics

An Elegant Weapon for a More Civilized Age

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

Higher-Order Functions

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

(scheme-1) has lambda but NOT define

Recap: Functions as first-class values

CDA 3103 Computer Organization Homework #7 Solution Set

Topic 7: Activation Records

Compiling Techniques

CSci 450, Org. of Programming Languages. Chapter 12 Expression Language Compilation

A Type System for Object Initialization In the Java TM Bytecode Language

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

UNIT 8C Computer Organization: The Machine s Language. von Neumann Architecture

Chapter 2: Instructions How we talk to the computer

Scope & Symbol Table. l Most modern programming languages have some notion of scope.

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS1 Recitation. Week 2

UNIT 3

CS61A Notes Week 13: Interpreters

What are compilers? A Compiler

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Address Modes effective address

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

An Explicit-Continuation Metacircular Evaluator

Project 3 Due October 21, 2015, 11:59:59pm

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

CSE 5317 Midterm Examination 4 March Solutions

cs173: Programming Languages Midterm Exam

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Smalltalk Implementation

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Variables and Bindings

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Binary Code Analysis: Concepts and Perspectives

A Brief Survey of Abstract Machines for Functional Programming Languages

Static Program Analysis

02 B The Java Virtual Machine

Implementing Functions at the Machine Level

Code Generation. Lecture 30

Question Points Score

Vectors in Scheme. Data Structures in Scheme

Chapter 3: Operating-System Structures

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

Transcription:

Parsing Scheme + (+ (* 2 3) 1) * 1 2 3

Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply +

Compiling Scheme make-return START make-test make-close make-assign make- pair? yes yes yes yes yes no no no if? lambda? define? quote? no no symbol? yes make-refer 0 1 2 make-frame no make-apply make- make-

Interpreting Bytecode frame frame halt 3 pc = halt acc = 7 2 refer 1 apply * refer apply +

bytecode frame 3 frame halt registers pc accumulator s environment frames val val... val 2 stack refer apply * refer apply + 1 pc arg env pc arg env...... pc arg env environment frame stack

Frame Bytecode Set program counter to second continuation Push registers onto frame stack Set program counter to first continuation

Constant Bytecode Set accumulator to value of operand Set program counter to continuation

Argument Bytecode Push accumulator onto stack Set program counter to continuation

Refer Bytecode Lookup value of operand in environment and store it in accumulator Set program counter to continuation

Apply Bytecode (primitive) Apply primitive to vals on stack Pop frame stack

Apply Bytecode (closure) Make application frame using closure's list (vars) and stack (vals) Push application frame onto environment Set program counter to body of closure

Pop frame stack Return Bytecode

Test Bytecode If accumulator is not #f then set program counter to first continuation else set program counter to second continuation

Assign Bytecode Make definition using operand and accumulator Add definition to top frame of environment Set program counter to continuation

Close Bytecode Make closure using operands and environment Set program counter to continuation

halt Halt Bytecode

Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 1

Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + 2

Compiling Scheme make-return START pair? yes make-test make-close make-assign make- yes yes yes yes no no no if? lambda? define? quote? no no symbol? yes make-refer 0 1 2 make-frame no make-apply make- make- 3

Interpreting Bytecode frame frame halt 3 pc = halt acc = 7 2 refer 1 apply * refer apply + 4

bytecode frame 3 frame halt registers pc accumulator s environment frames val val... val 2 stack refer apply * refer apply + 1 pc arg env pc arg env...... environment pc arg env frame stack 5

Frame Bytecode Set program counter to second continuation Push registers onto frame stack Set program counter to first continuation 6

Constant Bytecode Set accumulator to value of operand Set program counter to continuation 7

Argument Bytecode Push accumulator onto stack Set program counter to continuation 8

Refer Bytecode Lookup value of operand in environment and store it in accumulator Set program counter to continuation 9

Apply Bytecode (primitive) Apply primitive to vals on stack Pop frame stack 10

Apply Bytecode (closure) Make application frame using closure's list (vars) and stack (vals) Push application frame onto environment Set program counter to body of closure 11

Pop frame stack Return Bytecode 12

Test Bytecode If accumulator is not #f then set program counter to first continuation else set program counter to second continuation 13

Assign Bytecode Make definition using operand and accumulator Add definition to top frame of environment Set program counter to continuation 14

Close Bytecode Make closure using operands and environment Set program counter to continuation 15

Halt Bytecode halt 16