halt and quit

halt captures the rest of the pending expression and stops evaluating,
but the interpreter loop keeps running.

quit ends the interpreter session and quits the Thun program.
This commit is contained in:
Simon Forman 2022-09-07 20:42:48 -07:00
parent 08f977324f
commit 2c0a0aafcc
1 changed files with 28 additions and 1 deletions

View File

@ -499,8 +499,12 @@ def repl(stack=(), dictionary=None):
break
try:
stack, _, dictionary = run(text, stack, dictionary)
except SystemExit as e:
raise SystemExit from e
except:
print_exc()
except SystemExit as e:
raise SystemExit from e
except:
print_exc()
print()
@ -543,9 +547,13 @@ def interp(stack=(), dictionary=None):
print(e)
except NotAListError as e:
print(e)
except SystemExit as e:
raise SystemExit from e
except:
print_exc()
print(stack_to_string(stack))
except SystemExit as e:
raise SystemExit from e
except:
print_exc()
return stack
@ -723,6 +731,22 @@ def loop(stack, expr, dictionary):
return stack, expr, dictionary
@inscribe
def halt(stack, expr, dictionary):
'''
Put the pending expression onto the stack and halt.
'''
return (expr, stack), (), dictionary
@inscribe
def quit(stack, expr, dictionary):
'''
Stop the interpreter.
'''
raise SystemExit
'''
@ -1218,4 +1242,7 @@ if __name__ == '__main__':
J = interp if '-q' in sys.argv else repl
dictionary = initialize()
Def.load_definitions(DEFS.splitlines(), dictionary)
stack = J(dictionary=dictionary)
try:
stack = J(dictionary=dictionary)
except SystemExit:
pass