# A Basic Interpreter --- CS 135 // 2021-04-13 ## Administrivia - Pet pictures. Send them to me. I neeeed them. - MathCS talk on Friday + "A Tutorial on Designing Person-Centered Accessible Technologies" + Professor Meredith Moore + 2:00 to 3:00pm - Noyce scholarship ## Assignment 2 - An individual assignment - Due Tuesday, 4/20 before class # Questions ## ...about anything? # Interpreter for Arithmetic ## AST for Arithmetic - Recall that last time we defined a simple datatype to represent the abstract syntax of arithmetic ```haskell -- Abstract syntax tree for expressions data Expr = Plus Expr Expr | Mult Expr Expr | Num Int deriving Show ``` - By focusing on the AST, we can turn our attention to the *semantics* of a language instead of its syntax ## Interpreter for Arithmetic - We also wrote a simple interpreter: ```haskell interp :: Expr -> Int interp exp = case exp of Num n -> n Plus x y -> interp x + interp y Mult x y -> interp x * interp y ``` - Note that we have now defined the "meaning" of what `Plus` and `Mult` actually represent Note: Discuss the different interpretations we could assign to + here. (64-bit, concatenation, etc.) ## Adding Minus - Suppose we'd like to add subtraction to our language of arithmetic - It is tempting to add an extra AST node `Minus` similar to `Plus` and `Mult` - Is this necessary though? + Can subtraction be implemented with only `Plus` and `Mult`? + Yes! `x-y = x + y*(-1)` Note: Actually implement this in the parser and discuss why this is a useful thing to do ## Desugaring - Implementing `Minus` in this way is often called **desugaring** - We were also able to avoid modifying the core language and interpreter + Every time the core language changes, it can cause downstream effects + Interpreter, type checker, and other tools would need to be updated - Whenever possible, we will add features to our language via desugaring # Conditionals ## Adding Conditionals - Now suppose we'd like to add conditionals to our language ```lisp (if cnd thn els) ``` - Can we implement `if` with desugaring? + No! The whole point is to only evaluate one of the branches rather than both. - What values can `cnd` be? - What values can `thn` and `els` be? ## Truth/Falsy - Using the truthy/falsy approach is tempting, but it can get confusing because of inconsistencies ---
| Value | JS | Perl | PHP | Python | Ruby | |-----------|--------|--------|--------|--------|--------| | `0` | falsy | falsy | falsy | falsy | truthy | | `""` | falsy | falsy | falsy | falsy | truthy | | `NaN` | falsy | truthy | truthy | truthy | truthy | | null/None | falsy | falsy | falsy | falsy | falsy | | `"0"` | truthy | falsy | falsy | truthy | truthy | | `-1` | truthy | truthy | truthy | truthy | truthy | | `[]` | truthy | truthy | falsy | falsy | truthy | | empty obj | truthy | falsy | falsy | falsy | truthy |
## Adding a Boolean Type - Instead of the truthy/falsy approach, let's add a Boolean type + We can use `#t` for true and `#f` for false + The `cnd` of `(if cnd thn els)` will need to be a Boolean then - Need to update our `Expr` type to support Booleans and the conditional expression - Also need to update our interpreter! + What type should `interp` return? ## The `Value` Type - Since our language now supports multiple primitive types, let's add a `Value` type: ```haskell data Value = NumV Int | BoolV Bool ``` ## Adding Boolean Operators - Let's add operations for conjunction (logical and) and disjunction (logical or) - Can we do this with desugaring? + `(and x y)` is the same as `(if x y #f)` + `(or x y)` is the same as `(if x #t y)` - What about negation? + `(not x)` is the same as `(if x #f #t)`