Interpre'ng and Compiling Intex

Similar documents
Interpre'ng and Compiling Intex SOLUTIONS

Interpre'ng and Compiling Intex

Metaprogramming in SML: PostFix and S-expressions

Metaprogramming in SML: PostFix and S-expressions

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

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

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

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

Bindex: Naming, Free Variables, and Environments

Bindex: Naming, Free Variables, and Environments SOLUTIONS

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

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

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

Introduc)on To Standard ML

Problem Set 5 Due:??

Hofl: First-class Functions and Scoping

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

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

FOFL and FOBS: First-Order Functions

Ways to implement a language

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

Valex: Primitive Operators and Desugaring

CSE 341 Section 5. Winter 2018

Metaprogramming: InterpretaBon

LISP: LISt Processing

CS 360 Programming Languages Interpreters

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

CS115 - Module 10 - General Trees

Functions & First Class Function Values

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CSE341, Spring 2013, Final Examination June 13, 2013

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

A LISP Interpreter in ML

CS251 Programming Languages Handout # 36 Prof. Lyn Turbak April 4, 2007 Wellesley College Revised April 14, Scoping in Hofl

Homework 3 COSE212, Fall 2018

CSc 520. Principles of Programming Languages 11: Haskell Basics

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

Sum-of-Product (SOP) Datatypes in SML

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS153: Compilers Lecture 15: Local Optimization

A quick introduction to SML

Principles of Programming Languages

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Ruby: Introduction, Basics

Problem Set CVO 103, Spring 2018

CS 6353 Compiler Construction Project Assignments

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

CSE341 Spring 2017, Final Examination June 8, 2017

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Programming Languages: Application and Interpretation

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Languages and Compiler Design II IR Code Generation I

Functional Programming. Pure Functional Programming

Datatype declarations

Exercises on ML. Programming Languages. Chanseok Oh

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

Principles of Programming Languages

Lecture 3: Recursion; Structural Induction

15 150: Principles of Functional Programming. Exceptions

Programming Languages and Techniques (CIS120)

Lecture 2: SML Basics

CS 6353 Compiler Construction Project Assignments

Chapter 2 SML, a Functional Programming Language

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

CSE3322 Programming Languages and Implementation

Begin at the beginning

CMSC 330: Organization of Programming Languages

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

List Processing in SML

ASTs, Objective CAML, and Ocamlyacc

CSCI 2041: Functions, Mutation, and Arrays

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Programming Languages and Compilers (CS 421)

CSci 4223 Lecture 18 April 3, 2013 Topics: OOP vs. FP

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

CSE341 Autumn 2017, Final Examination December 12, 2017

List Processing in SML

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

ML 4 A Lexer for OCaml s Type System

Ruby: Introduction, Basics

List Processing in Ocaml

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Control in Sequential Languages

Code Genera*on for Control Flow Constructs

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Functional programming Primer I

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

Fall Lecture 3 September 4. Stephen Brookes

Lecture 2: Big-Step Semantics

Mid-Term 2 Grades

More On Syntax Directed Translation

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

Transcription:

A New Mini-Language: Intex Interpre'ng and Compiling Intex CS5 Programming Languages Spring 08, Lyn Turbak Department of Computer Science Wellesley College Intex programs are simple arithme'c expressions on integers that can refer to integer arguments. Intex is the first in a sequence of mini-languages that can be extended to culminate in something that is similar to Racket. At each step along the way, we can add features that allow us to study different programming language dimensions. Intex: integer expressions, posi'onal program arguments B: Intex + named arguments & local naming Valex: B + condi'onals, mul'ple kinds of s (booleans, strings, lists), dynamic type checking, and syntac'c sugar HOFL (Racket-like language): Valex + first-class func'ons HOFLEC: HOFL + SML-like mutable cells Intex Intex Syntax Trees & Syntac'c Data Types numargs Intex Div Int Add body datatype pgm = Intex of int * exp and exp = Int of int of int of binop * exp * exp and binop = Add Sub Mul Div Rem (* Sample AST as SOP tree *) val avg = Intex(, (Add,, ), Int )) (* Sample AST as s-expression *) (intex (/ (+ ($ ) ($ )) ) ; can even write: (intex (/ (+ $ $) ) How do we write this Intex program as SML SOP tree? As s-expression? numargs Intex Div Int Mul body Sub Int 3 Int 5 9 Intex 3 Intex 4

Intex Implementa'on #: Intex Interpreter in SML Given an avg-in-intex program, how can we execute it? avg machine (I) q avg-in-intex program q Intex interpreter machine (I) ² Intex-in-SML-interpreter program ² SML interpreter machine in wx VM (ignore details) Intex Interpreter Without Error Checking: Skeleton (* Intex.pgm -> int list -> int *) fun run (Intex(numargs, exp)) args =??? (* Intex.exp -> int list -> int *) and eval (Int i) args =??? eval ( ) args =??? eval ((binop, exp, exp)) args =??? (* Intex.binop -> int * int -> int *) and binoptofun Add = op+ binoptofun Mul = op* binoptofun Sub = op- binoptofun Div = (fn(x,y) => x div y) binoptofun Rem = (fn(x,y) => x mod y) Intex 5 Intex 6 Intex Interpreter With Error Checking Try it out exception EvalError of string (* Intex.pgm -> int list -> int *) fun run (Intex(numargs, exp)) args = if numargs <> length args then raise EvalError "Mismatch between expected and actual number of args" else eval exp args - run (Intex(, (Mul,, ))) [5]; val it = 5 : int - run (Intex(,, ))) [5]; val it = : int (* Intex.exp -> int list -> int *) and eval (Int i) args = i eval ( ) args = if ( <= 0) orelse ( > length args) then raise EvalError " out of bounds" else List.nth(args, -) eval ((binop, exp, exp)) args = let val i = eval exp args val i = eval exp args in (case (binop, i) of (Div, 0) => raise EvalError "Division by 0" (Rem,0) => raise EvalError "Remainder by 0" _ => (binoptofun binop)(i, i)) end Intex 7 - run (Intex(,, ))) [0]; uncaught exception EvalError - run avg [5,5]; val it = : int - map (run fc) [[~40], [0], [3], [98], []]; val it = [~40,~8,0,36,0] : int list Intex 8

Handling Errors (* Intex.pgm -> int list -> string *) fun testrun pgm args = Int.toString (run pgm args) (* Convert to string so same type as error messages below *) handle EvalError msg => "EvalError: " ^ msg other => "Unknown exception: " ^ (exnmessage other) - testrun (Intex(,, ))) [5]; val it = "" : string - testrun (Intex(,, ))) [0]; val it = "EvalError: Division by 0" : string - map (testrun fc) [[~40], [0], [3], [98], []]; val it = ["~40","~8","0","36","0"] : string list Intex 9 Intex programs as S-expression strings Intex(, (Mul,, ) Intex(, (Add,, ), Int )) "(intex (* ($ ) ($ ))" "(intex (/ (+ ($ ) ($ )) ))" Intex(, (Mul, (Sub,, Int 3), Int 5), Int 9)) "(intex (/ (* (- ($ ) 3) 5) 9))" Intex Running Intex programs as S-expression strings (* string -> string -> string *) fun testrun' pgmsexpstring argssexpstring = testrun (stringtopgm pgmsexpstring) (sexpstringtointlist argssexpstring) handle SexpError (msg, sexp) => ("SexpError: " ^ msg ^ " " ^ (Sexp.sexpToString sexp)) Sexp.IllFormedSexp msg => ("SexpError: Ill-formed sexp " ^ msg) other => "Unknown exception: " ^ (exnmessage other) - testrun' "(intex (/ (+ ($ ) ($ )) ))" "(5 5)"; val it = "" : string - map (testrun' "(intex (/ (* (- ($ ) 3) 5) 9))") = ["(-40)", "(0)", "(3)", "(98)", "()"]; val it = ["~40","~8","0","36","0"] : string list - map (testrun' "(intex (/ ($ ) ($ )))")= = ["(-7)", "(0)", "(4)"]; val it = [","EvalError: Division by 0",""] : string list Intex A Read-Eval-Print Loop (REPL) in Intex - repl(); intex> (+ ) 3 intex> (#args 6 7) intex> (+ ($ ) ($ )) 3 intex> ( * $ $) 4 intex> (#run (intex (/ (+ ($ ) ($ )) )) 5 5) intex> (#run "avg.itx" 5 5) intex> (#run avg.itx 5 5) intex> (#quit) Moriturus te saluto! Intex

What do we know about this program? val test = Intex(, (Sub, (Mul,, 3), )) Dynamic vs. Sta'c Checking: Indices Dynamic check (at runjme) : eval ( ) args = if ( <= 0) orelse ( > length args) then raise EvalError " out of bounds" else List.nth(args, -) StaJc check (at compile Jme or checking Jme, before runjme) : Idea: We know numargs from program, so can use this to check all argument references without running the program. Such checks are done by examining thee program syntax tree. O[en there is a choice between a bo)om-up and top-down approach to processing the tree. Intex 3 You will do both approaches for checking in PS9. Intex 4 Sta'c Index Checking: Top Down (PS9). In top-down phase, pass numargs to every subexpression in program.. Check numargs against every. a. Return true for indices that pass test and subexps without arg indices b. Return false if any fails test. true true and and true Sta'c Index Checking: Bo_om Up (PS9). Calculate (min,max) for every subexpression in tree in bo_om-up fashion. Compare (min,max) at root of program body expression to (, numargs), returning true if a subrange and false otherwise. Check if in inclusive range (, numargs) (,) (,) (, - ) (,) (,) Intex 5 Intex 6

Intex Implementa'on #: Intex-to-Posaix-compiler in SML Given an avg-in-intex program, how can we execute it? Hand-Compiling Intex to PostFix Manually translate the following Intex programs to equivalent PostFix programs: (intex 0 (* (- 7 4) (/ 8 ))) (intex 4 (* (- $ $) (/ $3 $4))) avg machine (I) q avg-in-postfix program ² avg-in-intex program ² Intex-to-PostFix-compiler machine o Intex-to-PostFix-compiler-in-SML program o SML interpreter machine in wx VM (ignore details) q PostFix interpreter machine (I) ² PostFix-in-SML-interpreter program ² SML interpreter machine in wx VM (ignore details) Intex 7 ReflecJon: How did you figure out how to translate Intex indices into PostFix Nget indices? Intex 8 Can we automate this process? Yes! We can define an intextopostfix func'on with type Intex.pgm -> PostFix.pgm and then use it like this: intextopostfix Skeleton (* val intextopostfix: Intex.pgm -> PostFix.pgm *) fun intextopostfix (Intex.Intex(numargs, exp)) = PostFix.PostFix(numargs, exptocmds exp 0???) (*??? is the initial depth argument *) fun translatestring intexpgmstring = PostFix.pgmToString (intextopostfix (Intex.stringToPgm intexpgmstring)) - translatestring "(intex (* ($ ) ($ )))"; val it = "(postfix nget nget mul)" : string - translatestring "(intex (/ (+ ($ ) ($ )) ))"; val it = "(postfix nget 3 nget add div)" : string - translatestring "(intex 4 (* (- ($ ) ($ )) (/ ($ 3) ($ 4))))"; val it = "(postfix 4 nget 3 nget sub 4 nget 6 nget div mul)" : string Intex 9 (* val exptocmds: Intex.exp -> PostFix.cmd list *) (* depth arg statically tracks how many s are on stack above the program arguments *) and exptocmds (Intex.Int i) depth =??? exptocmds (Intex. ) depth =??? [PostFix.Int ( + depth), PostFix.Nget] (* specified argument is on stack at + depth *) exptocmds (Intex.(binop,exp,exp)) depth =??? (exptocmds exp depth) (* st rand is at same depth as whole binapp *) @ (exptocmds exp (depth + )) (* for nd rand, add to depth to account for st rand *) @ [PostFix.Arithop (binoptoarithop binop)] and binoptoarithop Intex.Add = PostFix.Add binoptoarithop Intex.Sub = PostFix.Sub binoptoarithop Intex.Mul = PostFix.Mul binoptoarithop Intex.Div = PostFix.Div binoptoarithop Intex.Rem = PostFix.Rem Intex 0