DOC PREVIEW
Berkeley COMPSCI 164 - Growing the language

This preview shows page 1-2-3-4-5-6-7-48-49-50-51-52-53-54-97-98-99-100-101-102-103 out of 103 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 103 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 Lecture 3 Growing the language Scopes, binding, train wrecks, and syntactic sugar. Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to Programming Languages and Compilers, Spring 2012 UC BerkeleyAdministrativia Wed 1pm discussion section has moved. See piazza.com. - By the way, you are expected to read all piazza announcements. In HW1, most wrote their first web mash-up. Congratulations! Lessons: - modern programs use multiple languages: HTML, CSS, JS, regex - learning curve: languages and tools not so easy to learn - in CS164, we’ll learn skills to improve the situation PA1 assigned today. – Teams of two. – Your repos on bitbucket.org. Submissions from bitbucket, too. – We will require that you exchange files via bitbucket. 2Today Grow a language. Case studies on two languages. The unit calculator: allow the user to - add own units - reuse expressions Lambda interpreter: add control structures - if, while, for, comprehensions - using syntactic desugaring and lambdas 3Part 1: Growing the calculator language 4In L2, we implemented google constructs Example: 34 knots in mph # speed of S.F. ferry boat --> 39.126 mph Example: # volume * (energy / volume) / power = time half a dozen pints * (110 Calories per 12 fl oz) / 25 W in days --> 1.704 days Now we will change the language to be extensible 5How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) code 44LOC 3. Add non-SI units code 56LOC 4. Explicit unit conversion code 78LOC this step also includes a simple parser: code 120LOC 5. Allowing users to add custom non-SI units 6Growing language w/out interpreter changes We want to design the language to be extensible – Without changes to the base language – And thus without changes to the interpreter For calc, we want the user to add new units – Assume the language knows about meters (feet, …) – Users may wan to add, say, Angstrom and light year How do we make the language extensible? 7Our ideas minute = 60 s yard = 36 inch 8Bind a value to an identifier minute = 60 s hour = 60 minute day = 24 hour month = 30.5 day // maybe not define month? year = 365 day km = 1000 m inch = 0.0254 m yard = 36 inch acre = 4840 yard^2 hectare = (100 m)^2 2 acres in hectare → 0.809371284 hectare 9Implementing user units Assume units extends existing measures. We want the user to add ft when m or yard is known 10How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) code 44LOC 3. Add non-SI units code 56LOC 4. Explicit unit conversion code 78LOC this step also includes a simple parser: code 120LOC 5. Allowing users to add custom non-SI units 6. Allowing users to add custom measures 11How do we add new measures? No problem for Joule, as long you have kg, m, s: J = kg m^2 / s^2 But other units must be defined from first principles: Electric current: – Ampere Currency: – USD, EUR, YEN, with BigMac as the SI unit Coolness: – DanGarcias, with Fonzie as the SI unit 12Our ideas Attempt 1: when we evaluate a = 10 b and b is not known, add it as a new SI unit. This may lead to spuriously SI units introduced due to typos. Attempt 2: ask the user to explicitly declare the new SI unit: SI Ampere 13Our solution Add into language a construct introducing an SI unit SI A // Ampere mA = 0.0001 A SI BigMac USD = BigMac / 3.57 // BigMac = $3.57 GBP = BigMac / 2.29 // BigMac = £2.29 With “SI <id>”, language needs no built-in SI units SI m km = 1000 m inch = 0.0254 m yard = 36 inch 14Implementing SI id 15How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) code 44LOC 3. Add non-SI units code 56LOC 4. Explicit unit conversion code 78LOC this step also includes a simple parser: code 120LOC 5. Allowing users to add custom non-SI units 6. Allowing users to add custom measures code 7. Reuse of values 16Motivating example Compute # of PowerBars burnt on a 0.5 hour-long run SI m, kg, s lb = 0.454 kg; N = kg m / s^2 J = N m; cal = 4.184 J powerbar = 250 cal 0.5hr * 170lb * (0.00379 m^2/s^3) in powerbar --> 0.50291 powerbar Want to retype the formula after each morning run? 0.5 hr * 170 lb * (0.00379 m^2/s^3) 17Reuse of values To avoid typing 170 lb * (0.00379 m^2/s^3) … we’ll use same solution as for introducing units: Just name the value with an identifier. c = 170 lb * (0.00379 m^2/s^3) 28 min * c # … next morning 1.1 hour * c Should time given be in min or hours? Either. Check this out! Calculator converts automatically! 18How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) code 44LOC 3. Add non-SI units code 56LOC 4. Explicit unit conversion code 78LOC this step also includes a simple parser: code 120LOC 5. Allowing users to add custom non-SI units 6. Allowing users to add custom measures code 7. Reuse of values (no new code needed) 8. Reuse of expressions (bind names to expressions) 19Another motivating example You want to print the current time left to deadline now = 2011 year + 0 month + 18 day + 15 hour + 40 minute --- pretend that now is always set to current time of day Let’s try to compute time to deadline deadline = 2011 year + 1 month + 3 day // 2/3/2012 timeLeft = deadline - now timeLeft in day --> time left Wait for current time to advance. Print time left now. What does the following print? timeLeft in day --> updated time left How to achieve this behavior? 20timeLeft is bound to an expression 21Naming values vs. naming expressions “Naming an expression” means that we evaluate it lazily when we need its value 22How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) code 44LOC 3. Add non-SI units code 56LOC 4. Explicit unit conversion code 78LOC this step also includes a simple parser: code 120LOC 5. Allowing users to add custom non-SI units 6. Allowing users to add custom measures code 7. Reuse of values (no new code needed) 8. Reuse of expressions code (not fully lazy) 23Summary: Calculator is an extensible language Very little built-in knowledge – Introduce base units with ‘SI name’ – Arithmetic performs general unit types and conversion No need to define all units in terms of SI units cal = 4.184 J Reuse of values by naming the values. myConstant =


View Full Document

Berkeley COMPSCI 164 - Growing the language

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Growing the language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Growing the language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Growing the language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?