Skip to content

Commit

Permalink
fixing "check" and adding dedicated comments and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidedc committed Mar 10, 2017
1 parent 9c96a91 commit 148b195
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions run-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test_low_level = ->
test_assignments()
test_caching()
test_strings()
test_check()

selftest = ->
test_low_level()
Expand Down
22 changes: 18 additions & 4 deletions sources/eval.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,30 @@ Eval_cons = ->
Eval_binding = ->
push(get_binding(cadr(p1)))

# checks a predicate, i.e. check(A = B)
### check =====================================================================
Tags
----
scripting, JS, internal, treenode, general concept
Parameters
----------
p
General description
-------------------
Checks the predicate p, e.g. check(a = b)
Note how "check" can turn what normally would be an assignment into a test,
so in the case above "a" is not assigned anything.
###

# check definition
Eval_check = ->
push(cadr(p1))
Eval_predicate()
p1 = pop()
if (iszero(p1))
stop("check(arg): arg is zero")
push(symbol(NIL)) # no result is printed
push(p1)

Eval_det = ->
push(cadr(p1))
Expand Down
51 changes: 51 additions & 0 deletions tests/check.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
test_check = ->
if DEBUG then console.log "test_check ----------------------------"

run_test [

# note how check can turn an assignment
# into a test, so in this case a is not
# assigned anything

"check(a=b)",
"0",

"a",
"a",

"check(a=a)",
"1",

"check(a==a)",
"1",

"check(a===a)",
"check(a=== ? a)\nStop: syntax error",

"check(a+1=a)",
"0",

"check(a+1==a)",
"0",

"check(a+1===a)",
"check(a+1=== ? a)\nStop: syntax error",

"check(1)",
"1",

"check(0)",
"0",

"check(and(1,0))",
"0",

"check(and(1,1))",
"1",

# if not a predicate, just return
# what passed
"check(pi)",
"pi",

]

0 comments on commit 148b195

Please sign in to comment.