Commit 490ae753 authored by Matt's avatar Matt

Added a test for trying to apply a non-function

(closes #20)
parent 36ae7186
......@@ -456,7 +456,7 @@ Accoringly, an Expr is one of:
(define (func-apply-expr->string faexpr)
(string-append "<"
(expr->string (func-apply-expr-func faexpr))
"@"
" @ "
(expr->string (func-apply-expr-arg faexpr))
">"))
......@@ -464,7 +464,7 @@ Accoringly, an Expr is one of:
; Evaluate a function application
; TODO: decent error message when the left side of an apply is not a function
(define (eval-func-apply-expr faexpr)
(let* {[f (eval (func-apply-expr-func faexpr))]
(let* {[f (func-or-err faexpr)]
[arg (eval (func-apply-expr-arg faexpr))]
[e-prime (substitute (func-expr-id f)
arg
......@@ -472,6 +472,19 @@ Accoringly, an Expr is one of:
}
(eval e-prime)))
; func-or-err : func-apply-expr -> func-expr
; Evaluates the function portion of a function application. The result should
; be a function; return the function or throw an error
(define (func-or-err faexpr)
(let {[f (eval (func-apply-expr-func faexpr))]
}
(if (func-expr? f)
f
(error 'eval-func-apply-expr
"Cannot evaluate function application ~v, because the first expression did not evaluate to a function (was ~v)"
(func-apply-expr->string faexpr)
(expr->string f)))))
; sub-func-apply-expr : id, value, func-apply-expr -> func-apply-expr
; Create a new function application by substituting instances of id
; with val in the given application expression's two subexpressions
......
......@@ -7,8 +7,10 @@
(require "Qn.rkt")
(require "scanner.rkt")
(require "student-extras.rkt") ; for regexp
(require rackunit)
;;;; Some Q1 Tests
;>>>Q1
......@@ -150,6 +152,7 @@ in [[ say z
;;;; Some Q4 Examples
; (see bulk tests)
;>>>Q4
#| 5.i-iv
......@@ -191,20 +194,19 @@ iv. make-adder:
;; (This requires advanced-student, since the exception-throwing-code
;; needs to be wrapped in a "thunk" -- a lambda of 0 arguments.)
;;
(check-exn #rx"parse!: Unrecognized expr"
(check-exn (regexp (regexp-quote "parse!: Unrecognized expr"))
(λ() (string->expr "[[]]")))
;; Check that eval-ing an identifer throws an error
(check-exn #rx"eval-id: Cannot evaluate Identifier \"x\"!"
;>>>Q3
(check-exn (regexp (regexp-quote "eval-id: Cannot evaluate Identifier \"x\"!"))
(λ () (eval (string->expr "x"))))
;;
;; You don't need to do this -- I'm just including it to show
;; what things a unit-testing library can/should check for.
;;
;; Btw, I should call `(regexp (regexp-quote ...the-err-message...))`
;; or at least quote the "." in my #rx above.
;; Althougy regexp-quote isn't in the student languages,
;; you can `(require "student-extras.rkt")` to get it.
;; Check that eval'ing a func-apply-expr with a not-function throws an error
;; ...hopelessly long error message
;>>>Q4
(check-exn (regexp (regexp-quote "eval-func-apply-expr: Cannot evaluate function application \"<[[2]] @ (1 add 1)>\", because the first expression did not evaluate to a function (was \"2\")"))
(λ () (eval (string->expr "<[[2]] @ (1 add 1)>"))))
(check-equal? (id? 'id) #f)
(check-equal? (id? 23) #f)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment