Skip to content

Commit 13073ef

Browse files
lisp/machine: improve stack discipline comments
Make them physically resemble a stack for easier understanding. Shorter. Less prose. Helps understand the machine more easily. This also some of the descriptions which were inaccurate.
1 parent 94ba8c8 commit 13073ef

1 file changed

Lines changed: 52 additions & 32 deletions

File tree

source/lone/lisp/machine.c

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,11 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
317317
return true;
318318
case LONE_LISP_MACHINE_STEP_EVALUATED_OPERATOR:
319319
/* Evaluated operator is in machine->value.
320-
* Stack holds the unevaluated operands,
321-
* the environment to evaluate them in
322-
* and the machine's next step after
323-
* the application is finished. */
320+
* Stack:
321+
* unevaluated-operands-list
322+
* environment
323+
* next-step
324+
*/
324325
machine->applicable = machine->value;
325326
machine->unevaluated = lone_lisp_machine_pop_value(lone, machine);
326327
machine->environment = lone_lisp_machine_pop_value(lone, machine);
@@ -358,10 +359,11 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
358359
case LONE_LISP_MACHINE_STEP_OPERAND_EVALUATION:
359360
/* Results are accumulated in machine->list.
360361
* Remaining operands are in machine->unevaluated.
361-
* Stack holds the evaluated operand list head
362-
* as well as the applicable value the results
363-
* will be applied to and the machine's next step
364-
* after the application is finished. */
362+
* Stack:
363+
* evaluated-operands-list-head
364+
* applicable
365+
* next-step
366+
*/
365367
lone_lisp_machine_push_value(lone, machine, machine->list);
366368
machine->expression = lone_lisp_list_first(machine->unevaluated);
367369
if (lone_lisp_list_has_rest(machine->unevaluated)) {
@@ -370,17 +372,20 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
370372
lone_lisp_machine_push_step(lone, machine, LONE_LISP_MACHINE_STEP_OPERAND_ACCUMULATION);
371373
} else {
372374
/* Evlis tail recursion
373-
* Nothing is pushed onto the stack */
375+
* no new data is pushed onto the stack */
374376
lone_lisp_machine_push_step(lone, machine, LONE_LISP_MACHINE_STEP_LAST_OPERAND_ACCUMULATION);
375377
}
376378
goto expression_evaluation;
377379
case LONE_LISP_MACHINE_STEP_OPERAND_ACCUMULATION:
378380
/* Evaluated operand is in machine->value.
379-
* Stack holds the environment to evaluate operands in,
380-
* the list of remaining unevaluated operands,
381-
* the list of all evaluated operands so far,
382-
* the applicable the results will be applied to
383-
* and the machine's next step after the application. */
381+
* Stack:
382+
* environment
383+
* unevaluated-operands-list
384+
* evaluated-operands-list
385+
* evaluated-operands-list-head
386+
* applicable
387+
* next-step
388+
*/
384389
machine->environment = lone_lisp_machine_pop_value(lone, machine);
385390
machine->unevaluated = lone_lisp_list_rest(lone_lisp_machine_pop_value(lone, machine));
386391
machine->list = lone_lisp_machine_pop_value(lone, machine);
@@ -391,9 +396,12 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
391396
return true;
392397
case LONE_LISP_MACHINE_STEP_LAST_OPERAND_ACCUMULATION:
393398
/* Evaluated operand is in machine->value.
394-
* Stack holds the list of evaluated operands,
395-
* the applicable the results will be applied to
396-
* and the machine's next step after the application. */
399+
* Stack:
400+
* evaluated-operands-list
401+
* evaluated-operands-list-head
402+
* applicable
403+
* next-step
404+
*/
397405
machine->list = lone_lisp_machine_pop_value(lone, machine);
398406
head = lone_lisp_machine_pop_value(lone, machine);
399407
machine->applicable = lone_lisp_machine_pop_value(lone, machine);
@@ -403,7 +411,9 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
403411
case LONE_LISP_MACHINE_STEP_APPLICATION:
404412
/* Operator is in machine->applicable.
405413
* Operands, evaluated or not, are in machine->list.
406-
* Stack holds the machine's next step after the application. */
414+
* Stack:
415+
* next-step
416+
*/
407417
switch (lone_lisp_heap_value_of(machine->applicable)->type) {
408418
case LONE_LISP_TYPE_FUNCTION:
409419
machine->environment = bind_arguments(
@@ -458,21 +468,25 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
458468
return true;
459469
case LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION:
460470
/* Sequence is in machine->unevaluated.
461-
* Stack holds the machine's next step after the application. */
471+
* Stack:
472+
* next-step
473+
*/
462474
machine->expression = lone_lisp_list_first(machine->unevaluated);
463475
if (lone_lisp_list_has_rest(machine->unevaluated)) {
464476
lone_lisp_machine_push_value(lone, machine, machine->environment);
465477
lone_lisp_machine_push_value(lone, machine, machine->unevaluated);
466478
lone_lisp_machine_push_step(lone, machine, LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION_NEXT);
467479
} else {
468480
/* tail recursion optimization
469-
* nothing is saved on the stack */
481+
* no new data is saved on the stack */
470482
lone_lisp_machine_restore_step(lone, machine);
471483
}
472484
goto expression_evaluation;
473485
case LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION_FROM_PRIMITIVE:
474486
/* Sequence is in machine->unevaluated.
475-
* Stack holds the machine's next step after the application. */
487+
* Stack:
488+
* next-step
489+
*/
476490
machine->expression = lone_lisp_list_first(machine->unevaluated);
477491
if (lone_lisp_list_has_rest(machine->unevaluated)) {
478492
lone_lisp_machine_push_value(lone, machine, machine->environment);
@@ -486,28 +500,34 @@ bool lone_lisp_machine_cycle(struct lone_lisp *lone)
486500
goto expression_evaluation;
487501
case LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION_NEXT:
488502
/* Result of expression is in machine->value.
489-
* Stack holds the unevaluated expressions,
490-
* the environment they're being evaluated in
491-
* and the machine's next step after application. */
503+
* Stack:
504+
* unevaluated-expressions-list
505+
* environment
506+
* next-step
507+
*/
492508
machine->unevaluated = lone_lisp_list_rest(lone_lisp_machine_pop_value(lone, machine));
493509
machine->environment = lone_lisp_machine_pop_value(lone, machine);
494510
machine->step = LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION;
495511
return true;
496512
case LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION_FROM_PRIMITIVE_NEXT:
497513
/* Result of expression is in machine->value.
498-
* Stack holds the unevaluated expressions,
499-
* the environment they're being evaluated in
500-
* and the machine's next step after application. */
514+
* Stack:
515+
* unevaluated-expressions-list
516+
* environment
517+
* next-step
518+
*/
501519
machine->unevaluated = lone_lisp_list_rest(lone_lisp_machine_pop_value(lone, machine));
502520
machine->environment = lone_lisp_machine_pop_value(lone, machine);
503521
machine->step = LONE_LISP_MACHINE_STEP_SEQUENCE_EVALUATION_FROM_PRIMITIVE;
504522
return true;
505523
case LONE_LISP_MACHINE_STEP_RESUME_PRIMITIVE:
506-
/* Stack holds the environment,
507-
* the primitive to resume,
508-
* the primitive's current step value,
509-
* whatever data the primitive saved
510-
* and the machine's next step after application. */
524+
/* Stack:
525+
* environment
526+
* primitive
527+
* primitive-step
528+
* primitive-data...
529+
* next-step
530+
*/
511531
machine->environment = lone_lisp_machine_pop_value(lone, machine);
512532
machine->applicable = lone_lisp_machine_pop_value(lone, machine);
513533
lone_lisp_machine_restore_primitive_step(lone, machine);

0 commit comments

Comments
 (0)