Difference between revisions of "Itty Bitty Programming Language Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 106: Line 106:
 
       (raise-argument-error 'exp "expression?" exp)))
 
       (raise-argument-error 'exp "expression?" exp)))
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Evaluate the expression <exp> in the environment <code>env</code>.
 +
 +
* IdentifierExp evaluates to the value found by looking its name up in the environment.
 +
* NilExp evaluates to the empty list (this is: the value [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._null%29%29 null])
 +
* IntExp evaluates to its value.
 +
* AddExp first evaluates its sub-expressions: left_exp and right_exp, ensures that they both pass [https://docs.racket-lang.org/reference/number-types.html#%28def._%28%28quote._~23~25kernel%29._integer~3f%29%29 integer?], and evaluates to the [https://docs.racket-lang.org/reference/generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._%2B%29%29 sum] of the left and right values.
 +
* ConsExp first evaluates its sub-expressions: car_exp and cdr_exp, ensures that they both pass [[#value?|value?]], and evaluates to a [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons] cell of the car and cdr values.
 +
* CarOfConsExp first evaluates its sub-expression: cons_exp, ensures that it passes [[#value?|value?]], and evaluates to a [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons] cell of the car and cdr values.
 +
 +
<!--
 +
* Functions are lexically scoped: A function evaluates to a closure holding the function and the current environment.
 +
* An <code>ifgreater</code> evaluates its first two subexpressions to values v<sub>1</sub> and v<sub>2</sub> respectively. If both values are integers, it evaluates its third subexpression if v<sub>1</sub> is a strictly greater integer than v<sub>2</sub> else it evaluates its fourth subexpression.
 +
* An <code>mlet</code> expression evaluates its first expression to a value v. Then it evaluates the second expression to a value, in an environment extended to map the name in the mlet expression to v.
 +
* A call evaluates its first and second subexpressions to values. If the first is not a closure, it is an error. Else, it evaluates the closure’s function’s body in the closure’s environment extended to map the function’s name to the closure (unless the name field is #f) and the function’s argument-name (i.e., the parameter name) to the result of the second subexpression.
 +
* A pair expression evaluates its two subexpressions and produces a (new) pair holding the results.
 +
* A fst expression evaluates its subexpression. If the result for the subexpression is a pair, then the result for the fst expression is the e1 field in the pair.
 +
* A snd expression evaluates its subexpression. If the result for the subexpression is a pair, then the result for the snd expression is the e2 field in the pair.
 +
* An <code>isaunit</code> expression evaluates its subexpression. If the result is an aunit expression, then the result for the isaunit expression is the MUPL value <code>(int 1)</code>, else the result is the MUPL value <code>(int 0)</code>.
 +
-->
  
 
==Macros==
 
==Macros==

Revision as of 01:53, 5 April 2023

Credit

This assignment is based on MUPL by Dan Grossman and his team at UW.

Code To Use

Code To Investigate

Ast

Expression Structs

(struct IdentifierExp (name)                                           #:transparent)
(struct IntExp        (value)                                          #:transparent)
(struct AddExp        (left_exp right_exp)                             #:transparent)
(struct IfGreaterExp  (left_exp right_exp then_body_exp else_body_exp) #:transparent)
(struct NilExp        ()                                               #:transparent)
(struct IsNilExp      (exp)                                            #:transparent)
(struct ConsExp       (car_exp cdr_exp)                                #:transparent)
(struct CarOfConsExp  (cons_exp)                                       #:transparent)
(struct CdrOfConsExp  (cons_exp)                                       #:transparent)
(struct LetExp        (binding_name binding_exp body_exp)              #:transparent)
(struct FunctionExp   (name_option parameter_name body_exp)            #:transparent)
(struct CallExp       (function_exp argument_exp)                      #:transparent)

expression?

(define (expression? exp)
  (or (IdentifierExp? exp)
      (IntExp? exp)
      (AddExp? exp)
      (IfGreaterExp? exp)
      (NilExp? exp)
      (IsNilExp? exp)
      (ConsExp? exp)
      (CarOfConsExp? exp)
      (CdrOfConsExp? exp)
      (LetExp? exp)
      (FunctionExp? exp)
      (CallExp? exp)))

Code To Implement

Warmup

file: src/main/racket/ibpl/warmup.rkt Racket-logo.svg
functions: racket-integers->ib-IntExps
ib-IntExps->racket-integers

racket-integers->ib-IntExps

ib-IntExps->racket-integers

Interpreter

file: src/main/racket/ibpl/interpreter.rkt Racket-logo.svg
functions: value?
expand-environment
evaluate
(struct closure (env function) #:transparent)

value?

IBPL expressions evaluate to Racket values. value? determines if the paramater v is a valid value.

Valid value types are cons cells, integers, closures, and the null empty list.

For a cons cell to be a valid value, its car and cdr must be valid values.

ensure-value?

(define (ensure-value? v)
  (if (value? v)
      v
      (raise-argument-error 'v "value?" v)))

lookup-value-in-environment

(struct entry (name value) #:transparent)

(define (lookup-value-in-environment env identifier-name)
  (cond [(null? env) (error "unbound identifier during evaluation" identifier-name)]
        [(equal? (entry-name (car env)) identifier-name) (entry-value (car env))]
        [#t (lookup-value-in-environment (cdr env) identifier-name)]))

expand-environment

(define (expand-environment name value env)
        (error 'not-yet-implemented))

Requirement: raise-argument-error for invalid parameters.

name must be a string?
value must be a value?
env must be a list?

evaluate

(define (evaluate exp env)
  (if (expression? exp)
      (if (list? env)
          (ensure-value? 
                 (error 'not-yet-implemented))
          (raise-argument-error 'env "list?" env))
      (raise-argument-error 'exp "expression?" exp)))

Evaluate the expression <exp> in the environment env.

  • IdentifierExp evaluates to the value found by looking its name up in the environment.
  • NilExp evaluates to the empty list (this is: the value null)
  • IntExp evaluates to its value.
  • AddExp first evaluates its sub-expressions: left_exp and right_exp, ensures that they both pass integer?, and evaluates to the sum of the left and right values.
  • ConsExp first evaluates its sub-expressions: car_exp and cdr_exp, ensures that they both pass value?, and evaluates to a cons cell of the car and cdr values.
  • CarOfConsExp first evaluates its sub-expression: cons_exp, ensures that it passes value?, and evaluates to a cons cell of the car and cdr values.


Macros

file: src/main/racket/ibpl/warmup.rkt Racket-logo.svg
functions: IbIfNil
IbLet*
IbIfEq

Treat each Racket function as an Itty Bitty macro. Return an abstract syntax tree of expressions which can later be evaluated.

Alert: do not invoke evaluate. Each Racket function's return value should have all of the necessary logic encoded in its AST of expressions.

IbIfNil

IbLet*

(struct binding (name exp) #:transparent)

IbIfEq

Functions

Define Racket values which are abstract syntax trees of expressions that can be later evaluated.

Alert: do not invoke evaluate. Each Racket value you define should have all of the necessary logic encoded in its AST of expressions.

ib-double

; racket version for reference
(define (racket-double n) (+ n n))

ib-sum-curry

; racket version for reference
(define (racket-sum-curry a) (lambda (b) (+ a b)))

ib-call-with-one

; racket version for reference
(define (racket-call-with-one proc) (proc 1))

ib-map

ib-map-add-n

Testing

file: test_all.rkt Racket-logo.svg Test
source folder: src/test/racket/ibpl

note: ensure that you have removed all printing to receive credit for any assignment.