CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Similar documents
PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Fall 2017, Lyn Turbak

PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Spring 2018, Lyn Turbak

Metaprogramming in SML: PostFix and S-expressions

Metaprogramming in SML: PostFix and S-expressions

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

Interpre'ng and Compiling Intex

Interpre'ng and Compiling Intex

Interpre'ng and Compiling Intex SOLUTIONS

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring SOLUTIONS

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring

Design Concepts in Programming Languages Scheme Supplement

CS301 Compiler Design and Implementation Handout # 18 Prof. Lyn Turbak February 19, 2003 Wellesley College

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

A brief tour of history

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

LISP: LISt Processing

Overview. Factorial Revisited. Iteration via Tail Recursion in Racket. An itera*ve approach to factorial. What is itera*on?

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Hofl, a Higher-order Functional Language. 1 An Overview of Hofl. 2 Abstractions and Function Applications

Denotational Semantics

Design Concepts in Programming Languages

Bindex: Naming, Free Variables, and Environments SOLUTIONS

Operational Semantics

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Recursive List Func.ons in Racket

Vectors in Scheme. Data Structures in Scheme

Overview. Iteration via Tail Recursion in Racket SOLUTIONS. Factorial Revisited. An itera*ve approach to factorial

Overview. Iteration via Tail Recursion in Racket. Factorial Revisited. An itera*ve approach to factorial. What is itera*on?

Bindex: Naming, Free Variables, and Environments

Scheme as implemented by Racket

Scheme Quick Reference

CSE 341 Section Handout #6 Cheat Sheet

Programming Languages: Application and Interpretation

Func+on applica+ons (calls, invoca+ons)

Functions & First Class Function Values

Valex: Primitive Operators and Desugaring

Shell programming. Introduction to Operating Systems

Case Study: Undefined Variables

Scheme Quick Reference

CS 314 Principles of Programming Languages. Lecture 16

Hofl: First-class Functions and Scoping

Problem Set 5 Due:??

Programming Languages: Application and Interpretation

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

Languages and Compiler Design II IR Code Generation I

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

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Racket: Modules, Contracts, Languages

Module 8: Local and functional abstraction

User-defined Functions. Conditional Expressions in Scheme

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Essentials of Programming Languages Language

Essentials of Programming Languages Language

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

CS 314 Principles of Programming Languages

Symbolic Computation

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Control Structures. CIS 118 Intro to LINUX

FL: A Functional Language

CSC 533: Programming Languages. Spring 2015

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Functional Programming. Pure Functional Programming

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Lecture 8: Simple Calculator Application

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Introduction to Scheme

How to Design Programs

CS 206 Introduction to Computer Science II

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Functional Programming. Pure Functional Languages

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

CS 211 Programming Practicum Spring 2018

An Explicit-Continuation Metacircular Evaluator

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Box-and-arrow Diagrams

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

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

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

CS 211 Programming Practicum Fall 2018

Scheme: Strings Scheme: I/O

CSc 520 Principles of Programming Languages

CS61A Midterm 2 Review (v1.1)

FOFL and FOBS: First-Order Functions

Structure and Interpretation of Computer Programs

From Syntactic Sugar to the Syntactic Meth Lab:

Homework 3 COSE212, Fall 2018

Documentation for LISP in BASIC

Transcription:

A PostFix Interpreter in Racket CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

PostFix PostFix is a stack- based mini- language that will be our first foray into the study of metalanguages = programs that manipulate programs. For the syntax and seman>cs of PostFix, see these notes: hap://cs.wellesley.edu/~cs251/notes/dcpl- introduc>on.pdf Here s an example PostFix program (postfix 2 2 nget 0 gt (mul) (swap 1 nget mul add) sel exec) 11-2

PostFix Syntax number of expected arguments commands (postfix 2 2 nget 0 gt (mul) (swap 1 nget mul add) sel exec) executable sequence command executable sequence command A PostFix command C is one of: An integer One of pop, swap, nget, sel, exec, add, mul, sub, div, rem, ; arithops lt, eq, gt ; relops An executable sequence of the form (C1 Cn) 11-3

PostFix Syntax Abstrac>ons in Racket (define (postfix-program? sexp) (and (list? sexp) (>= (length sexp) 2) (eq? (first sexp) 'postfix) (integer? (second sexp)) (postfix-command-sequence? (rest (rest sexp))))) (define (postfix-command-sequence? sexp) (and (list? sexp) (forall? postfix-command? sexp))) (define (postfix-command? sexp) (or (integer? sexp) (memq sexp '(pop swap nget sel exec add mul sub div rem ; arithops lt eq gt)) ; relops (postfix-command-sequence? sexp))) (define (postfix-numargs pgm) (second pgm)) (define (postfix-commands pgm) (rest (rest pgm))) 11-4

Tes>ng membership with member > (member 'c '(a b c d e)) '(c d e) > (member 'x '(a b c d e)) #f 11-5

PostFix command seman>cs Stack Before Command Stack After ( ) integer N (N ) (v1 ) pop ( ) (v1 v2 ) swap (v2 v1 ) (v1 v2 ) sub (other arithops similar) (v1 v2 ) lt (other relops similar) (N )where N is v2 v1 (N )where N is 1 if v2 < v1 and N is 0 otherwise (i v1 vk) nget (vi v1 vk) if 1 i k (velse vthen vtest ) sel (vthen ) if vtest 0 (velse ) if vtest = 0 ( ) (C1 Cn) ((C1 Cn) ) 11-6

postfix-exec-command Skeleton ;; Initially simplify things by ignoring errors (define (postfix-exec-command cmd stk) (cond ((integer? cmd)??) ((eq? cmd 'pop)??) ((eq? cmd 'swap)??) ((eq? cmd 'sub)??) ; other arithops similar ((eq? cmd 'lt)??) ; other relops similar ((eq? cmd 'sel)??) ((postfix-command-sequence? cmd)??) ((eq? cmd 'exec)??) (else (error "unrecognized command" cmd)))) 11-7

postfix-exec-command Fleshed Out ;; Initially simplify things by ignoring errors (define (postfix-exec-command cmd stk) (cond ((integer? cmd) (cons cmd stk)) ((eq? cmd 'pop) (rest stk)) ((eq? cmd 'swap) (cons (second stk) (cons (first stk) (rest (rest stk))))) ((eq? cmd 'sub) (cons (- (second stk) (first stk)) (rest (rest stk)))) ; other arithops similar ((eq? cmd 'lt) (cons (if (< (second stk) (first stk)) 1 0) (rest (rest stk)))) ; other relops similar ((eq? cmd 'nget) (cons (list-ref stk (first stk)) (rest stk))) ((eq? cmd 'sel) (cons (if (= (third stk) 0) (first stk) (second stk)) (rest (rest (rest stk))))) ((postfix-command-sequence? cmd) (cons cmd stk)) ((eq? cmd 'exec)??) ;; See slide 11-10 (else (error "unrecognized command" cmd)))) 11-8

postfix-exec-commands ;; Execute command list on initial stack ;; and return final stack (define (postfix-exec-commands cmds init-stk) (foldl postfix-exec-command init-stk cmds)) > (postfix-exec-commands '(pop swap sub) '(4 7 5 8)) '(2 8) 11-9

Handling exec in postfix-exec-command (define (postfix-exec-command cmd stk) (cond (postfix-exec-commands (first stk) (rest stk))) (else (error "unrecognized command" cmd)))) 11-10

Side Effects and Sequencing: printf and begin > (begin (printf "~a + ~a is ~a\n" 1 2 (+ 1 2)) (printf "~a * ~a is ~a\n" 3 4 (* 3 4))) 1 + 2 is 3 3 * 4 is 12 (define (print-and-return val) (begin (printf "~a\n" val) val)) > (* (print-and-return 3) (print-and-return (+ (print-and-return 4) (print-and-return 5)))) 3 ; printed 4 ; printed 5 ; printed 9 ; printed 27 ; returned 11-11

(begin e) desugars to e begin is just sugar! (begin e1 e2 ) desugars to (let ((id1 e1)) ; id1 is fresh (begin e2 )) 11-12

postfix-exec-commands with tracing ;; Execute command list on initial stack ;; and return final stack ;; Print each command and stack resulting from executing it (define (postfix-exec-commands cmds init-stk) (foldl (λ (cmd stk) (let ((new-stk (postfix-exec-command cmd stk))) (begin (printf "after executing ~a, stack is ~a\n" cmd new-stk) new-stk))) init-stk cmds)) > (postfix-exec-commands '(pop swap sub) '(4 7 5 8)) after executing pop, stack is (7 5 8) after executing swap, stack is (5 7 8) after executing sub, stack is (2 8) '(2 8) 11-13

postfix-run ;; Run a postfix program on initial stack from args ;; Simplify things by not checking for errors. (define (postfix-run pgm args) (let ((final-stk (postfix-exec-commands (postfix-commands pgm) args))) (first final-stk))) > (postfix-run '(postfix 2 7 4 pop swap sub) '(5 8)) after executing 7, stack is (7 5 8) after executing 4, stack is (4 7 5 8) after executing pop, stack is (7 5 8) after executing swap, stack is (5 7 8) after executing sub, stack is (2 8) 2 11-14

BeAer handling of arithops (define (postfix-exec-command cmd stk) (cond ((postfix-arithop? cmd) (cons ((postfix-arithop->racket-binop cmd) (second stk) (first stk)) (rest (rest stk)))) )) (define postfix-arithops (list (list 'add +) (list 'mul *) (list 'sub -) (list 'div quotient) (list 'rem remainder))) (define (postfix-arithop? cmd) (assoc cmd postfix-arithops)) (define (postfix-arithop->racket-binop arithop) (second (assoc postfix-arithops))) 11-15

postfix-run with errors ;; Run a postfix program on initial stack from args ;; This version checks for errors (define (postfix-run pgm args) (cond ((not (postfix-program? pgm)) (error "Invalid PostFix program" pgm)) ((not (postfix-arguments? args)) (error "Invalid PostFix arguments" pgm)) ((not (= (postfix-numargs pgm) (length args))) (error "expected number of arguments does not match actual number of arguments" (list (postfix-numargs pgm) (length args)))) (else (let ((final-stack (postfix-exec-commands (postfix-commands pgm) args))) (cond ((null? final-stack) (error "Stack empty at end of program")) ((not (integer? (first final-stack))) (error "Top of final stack not an integer")) (else (first final-stack))))))) 11-16

postfix-exec-command with errors (define (postfix-exec-command cmd stk) (cond ((integer? cmd) (cons cmd stk)) ((eq? cmd 'pop) (if (< (length stk) 1) (error "postfix pop requires stack with at least one value" (list cmd stk)) (rest stk))) ((eq? cmd 'swap) (if (< (length stk) 2) (error "postfix swap requires stack with at least two values" (list cmd stk)) (cons (second stk) (cons (first stk) (rest (rest stk)))))) ((postfix-arithop? cmd) (cond ((< (stack-size stk) 2) (error "postfix arithop requires two arguments" (list cmd stk))) ((or (not (integer? (first stk))) (not (integer? (second stk)))) (error "postfix arithop requires two integers" (list cmd stk))) (else (cons ((postfix-arithop->racket-binop cmd) (second stk) (first stk)) (rest (rest stk))))) ;; Other cases omitted (else (error "Unknown PostFix command" cmd)))) 11-17