Skip to content

Commit

Permalink
Fix "if" again when no else position.
Browse files Browse the repository at this point in the history
For JS, return null rather than undefined. For python, return null
rather than error-ing out.
  • Loading branch information
kanaka committed Aug 15, 2024
1 parent 1d2e6d0 commit c64353f
Show file tree
Hide file tree
Showing 22 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ <h2>A Delightfully Diminutive Lisp</h2>
<div class="console" id="console"></div>
</div>
<div class="eleven columns" style="font-family: monospace">
miniMAL: &nbsp;<a href="js/web/miniMAL-min.js">min</a> (&nbsp;829 bytes),
<a href="js/stepB_web.js">orig</a> (4081 bytes, 119 lines)<br>
miniMAL: &nbsp;<a href="js/web/miniMAL-min.js">min</a> (&nbsp;835 bytes),
<a href="js/stepB_web.js">orig</a> (4089 bytes, 119 lines)<br>
Core Lib: <a href="js/web/miniMAL-core.js">min</a> (1086 bytes),
<a href="js/core.json">orig</a> (4161 bytes, 120 lines)
</div>
Expand Down
2 changes: 1 addition & 1 deletion js/miniMAL-node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/step2_eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: (a => {throw ast + " not found"})() // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step3_env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: (a => {throw ast + " not found"})() // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step4_if_fn_do.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: (a => {throw ast + " not found"})() // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step5_tco.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: (a => {throw ast + " not found"})() // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step6_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: (a => {throw ast + " not found"})() // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step7_interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: E.throw(ast + " not found") // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step8_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: E.throw(ast + " not found") // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/step9_try.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function EVAL(env, ast, f, el) {
? env[ast] // lookup symbol
: E.throw(ast + " not found") // undefined symbol
} else {
return ast
return ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/stepA_miniMAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function EVAL(env, ast, f, el) {
? Object.keys(ast).reduce(
(a,k) => (a[k] = EVAL(env, ast[k]), a), {}) // eval object values
: ast // return ast unchanged
: ast
: ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/stepB_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function EVAL(env, ast, f, el) {
? Object.keys(ast).reduce(
(a,k) => (a[k] = EVAL(env, ast[k]), a), {}) // eval object values
: ast // return ast unchanged
: ast
: ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/stepB_web.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function EVAL(env, ast, f, el) {
? Object.keys(ast).reduce(
(a,k) => (a[k] = EVAL(env, ast[k]), a), {}) // eval object values
: ast // return ast unchanged
: ast
: ast ?? null
}
} else {
// apply
Expand Down
2 changes: 1 addition & 1 deletion js/web/miniMAL-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion python/step4_if_fn_do.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def EVAL(ast, env):
elif "do" == ast[0]:
return [EVAL(a, env) for a in ast[1:]][-1]
elif "if" == ast[0]:
return EVAL(ast[2] if EVAL(ast[1], env) else ast[3], env)
return EVAL(ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0], env)
elif "fn" == ast[0]:
return lambda *a: EVAL(ast[2], Env(env, ast[1], a))
else:
Expand Down
2 changes: 1 addition & 1 deletion python/step5_tco.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
2 changes: 1 addition & 1 deletion python/step6_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
2 changes: 1 addition & 1 deletion python/step7_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
2 changes: 1 addition & 1 deletion python/step8_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
2 changes: 1 addition & 1 deletion python/step9_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
2 changes: 1 addition & 1 deletion python/stepA_miniMAL.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def EVAL(ast, env):
[EVAL(a, env) for a in ast[1:-1]]
ast = ast[-1] # TCO
elif "if" == ast[0]:
ast = ast[2] if EVAL(ast[1], env) else ast[3] # TCO
ast = ast[2] if EVAL(ast[1], env) else (*ast[3:4], None)[0] # TCO
elif "fn" == ast[0]:
fn = lambda *a: EVAL(ast[2], Env(env, ast[1], [*a]))
fn.A = [ast[2], env, ast[1]]
Expand Down
4 changes: 2 additions & 2 deletions tests/step4_if_fn_do.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
;=>false
["if", null, 8, 7]
;=>7
["if", null, 8]
;=>undefined
["if", false, 8]
;=>null


;; Testing basic conditionals
Expand Down

0 comments on commit c64353f

Please sign in to comment.