Writing Interpreters Racket eval. CSE341 Section 7. ASTs, Interpreters, MUPL. Sunjay Cauligi. February 21 st, Sunjay Cauligi CSE341 Section 7

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

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

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

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

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

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

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

CS 360 Programming Languages Interpreters

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

CSE341 Spring 2016, Final Examination June 6, 2016

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

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Case Study: Undefined Variables

Macros. Pat Hanrahan. CS448h Fall 2015

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Syntax Errors; Static Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSE 12 Abstract Syntax Trees

CSE341 Spring 2017, Final Examination June 8, 2017

Concepts of programming languages

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE Programming Languages Final exam - Autumn 2012

CS1 Lecture 3 Jan. 18, 2019

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

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

CS1 Lecture 3 Jan. 22, 2018

QUIZ. What is wrong with this code that uses default arguments?

CS1 Recitation. Week 1

Project 5 - The Meta-Circular Evaluator

CIS 194: Homework 5. Due Monday, 18 February. Expressions. (2 + 3) 4 would be represented by the value

Intro. Scheme Basics. scm> 5 5. scm>

From Syntactic Sugar to the Syntactic Meth Lab:

CSE341 Spring 2017, Final Examination June 8, 2017

Project 5 - The Meta-Circular Evaluator

CSE341 Autumn 2017, Final Examination December 12, 2017

6.945 Adventures in Advanced Symbolic Programming

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CS 320: Concepts of Programming Languages

Modules, Structs, Hashes, and Operational Semantics

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

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

Kripke-Style Contextual Modal Type Theory

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Principles of Programming Languages

Variables and numeric types

The role of semantic analysis in a compiler

Overview of Semantic Analysis. Lecture 9

Functional Programming. Pure Functional Languages

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Lecture 7: The Untyped Lambda Calculus

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Rethinking Scala Macros

Types and Static Type Checking (Introducing Micro-Haskell)

Lambda Calculus LC-1

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CSE 3302 Programming Languages Lecture 2: Syntax

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

6.001: Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Languages

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Abstract Syntax Trees & Top-Down Parsing

6.001 Notes: Section 8.1

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

Warm-up and Memoization

Welcome to CS 115 (Winter 2018)

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

Semantic Analysis. Lecture 9. February 7, 2018

CS 360: Programming Languages Lecture 12: More Haskell

Syntax and Grammars 1 / 21

Parsing II Top-down parsing. Comp 412

Values and Variables 1 / 30

Welcome to CS 115 (Winter 2019)

Racket Pattern Matching

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Undergraduate Compilers in a Day

CS 314 Principles of Programming Languages

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Semantic Analysis. Compiler Architecture

Scheme-Style Macros: Patterns and Lexical Scope

Extending Pattern Directed Inference Systems. EECS 344 Winter 2008

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

SCHEME INTERPRETER GUIDE 4

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

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Programming Languages

Types, Expressions, and States

Compilers and computer architecture: Semantic analysis

Python Day 3 11/28/16

Transcription:

Writing Interpreters Racket eval CSE341 Section 7 ASTs, Interpreters, MUPL Sunjay Cauligi February 21 st, 2013

Legal vs. Nonlegal ASTs Consider the Following (add 3 4) (add (const 3) (const 4)) (add (const 3) (bool #t))

Legal vs. Nonlegal ASTs Consider the Following (add 3 4) (add (const 3) (const 4)) (add (const 3) (bool #t)) Syntax vs. semantics

Legal vs. Nonlegal ASTs Consider the Following (add 3 4) (add (const 3) (const 4)) (add (const 3) (bool #t)) Syntax vs. semantics No need to check for syntax

Legal vs. Nonlegal ASTs Consider the Following (add 3 4) (add (const 3) (const 4)) (add (const 3) (bool #t)) Syntax vs. semantics No need to check for syntax Must check semantics

Checking Semantics Nice Case add const 3 const 4

Checking Semantics Nice Case add const 3 const 4

Checking Semantics Nice Case add const 3 const 4

Checking Semantics Nice Case const 7

Checking Semantics Nice Case const 7 Not Nice Case add const 3 bool #t

Checking Semantics Nice Case const 7 Not Nice Case add const 3 bool #t

Checking Semantics Nice Case const 7 Not Nice Case add const 3 bool #t

Checking Semantics Nice Case const 7 Not Nice Case Error: add applied to non-number!

Valid Assumptions Allowed to Assume Input AST is valid Each node in AST has right types Remember that nodes such as add and multiply take ASTs, not numbers! Illegal input ASTs may crash the interpreter this is OK

Valid Assumptions Allowed to Assume Input AST is valid Each node in AST has right types Remember that nodes such as add and multiply take ASTs, not numbers! Illegal input ASTs may crash the interpreter this is OK Need to Check Return types from subexpressions E.g. (add (const 3) (bool #t)) is a legal AST, but has a wrong value being passed to add

Reviewing Macros What is a Macro? Extends language syntax (allows new constructs) Written in terms of existing syntax

Reviewing Macros What is a Macro? Extends language syntax (allows new constructs) Written in terms of existing syntax Expanded before language is actually interpreted/compiled

MUPL Macros A Clever Trick Interpreting MUPL using Racket MUPL is represented as Racket structs

MUPL Macros A Clever Trick Interpreting MUPL using Racket MUPL is represented as Racket structs In Racket, these are just more data types

MUPL Macros A Clever Trick Interpreting MUPL using Racket MUPL is represented as Racket structs In Racket, these are just more data types Why not write a Racket function that returns MUPL ASTs?

MUPL Macros A Clever Trick Interpreting MUPL using Racket MUPL is represented as Racket structs In Racket, these are just more data types Why not write a Racket function that returns MUPL ASTs? Note on Hygiene Implementing macros in this manner doesn t give very good macro hygiene

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Racket s quote function Quoting a Set of Tokens Syntactically, Racket statements can be thought of as lists of tokens (+ 3 4) is a plus sign, a 3, and a 4

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Racket s quote function Quoting a Set of Tokens Syntactically, Racket statements can be thought of as lists of tokens (+ 3 4) is a plus sign, a 3, and a 4 quote-ing a parenthesized expression produces a list of tokens

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Racket s quote function Quoting a Set of Tokens Syntactically, Racket statements can be thought of as lists of tokens Examples (+ 3 4) is a plus sign, a 3, and a 4 quote-ing a parenthesized expression produces a list of tokens (+ 3 4) => 7 (quote (+ 3 4)) => '(+ 3 4) (quote (+ 3 #t)) => '(+ 3 #t) (+ 3 #t) => Error

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Self Interpretation Notes on eval Many languages provide an eval function or something similar Performs interpretation/compilation at runtime

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Self Interpretation Notes on eval Many languages provide an eval function or something similar Performs interpretation/compilation at runtime Needs full language implementation during runtime

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Self Interpretation Notes on eval Many languages provide an eval function or something similar Performs interpretation/compilation at runtime Needs full language implementation during runtime Use of eval It s useful, but there s usually a better way Makes analysis, debugging difficult

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Eval in Racket Racket s eval function Racket s eval operates on lists of tokens Like those generated from quote

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Eval in Racket Racket s eval function Racket s eval operates on lists of tokens Like those generated from quote Examples (define quoted (quote (+ 3 4))) (eval quoted) => 7 (define bad-quoted (quote (+ 3 #t))) (eval bad-quoted) => Error

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Quasiquoting Quasiquoting Inserts evaluated tokens into a quote Convenient for generating dynamic token lists

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Quasiquoting Quasiquoting Examples Inserts evaluated tokens into a quote Convenient for generating dynamic token lists (quasiquote (+ 3 (unquote (+ 2 2)))) => '(+ 3 4) (quasiquote (+ 3 (unquote (quote (I love CSE 338))))) => '(+ 3 (I love CSE 338))

Writing Interpreters Racket eval Quoting in Racket Eval in Racket Quasiquoting Quasiquoting Examples Inserts evaluated tokens into a quote Convenient for generating dynamic token lists (quasiquote (+ 3 (unquote (+ 2 2)))) => '(+ 3 4) (quasiquote (+ 3 (unquote (quote (I love CSE 338))))) => '(+ 3 (I love CSE 338)) (quasiquote (+ (unquote (eval (quote (- 5 2)))) (unquote (eval (quasiquote (+ (unquote (/ 4 2)) 2)))))) => '(+ 3 4)

Writing Interpreters Racket eval Cute Little Typographical Shortcuts '(a b c) <=> (quote (a b c)) `(a b,(+ 2 2) d) <=> (quasiquote (a b (unquote (+ 2 2)) d)) (λ (x) (+ x 1)) <=> (lambda (x) (+ x 1))