Skip to content

Commit 6021582

Browse files
committed
Add formatting for scoping
1 parent 45a0a2e commit 6021582

1 file changed

Lines changed: 96 additions & 76 deletions

File tree

state-manipulation.scm

Lines changed: 96 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,22 @@
332332
((is-top-scope-class-divider? state) '())
333333
(else (cons (get-top-scope state) (extract-new-class-instance-state-sub (get-tail-scope state)))))))
334334

335+
; Updates a class instance with a new instance state
335336
(define update-class-instance
336337
(lambda (instancename new-instance-state state)
337-
(G-push-state->state
338-
instancename
339-
(list (car (get-value-from-pair (G-value-lookup->value_state instancename state empty-cfuncs))) new-instance-state)
340-
state)))
338+
(G-push-state->state instancename
339+
(list (get-instance-value-front (get-value-from-pair (G-value-lookup->value_state instancename state empty-cfuncs)))
340+
new-instance-state)
341+
state)))
341342

343+
(define get-instance-value-front car)
344+
345+
; Pops a state to the class level
342346
(define G-pop-to-class-level->state
343347
(lambda (state)
344-
(list (car (reverse state)))))
348+
(list (get-reversedstate-head (reverse state)))))
349+
350+
(define get-reversedstate-head car)
345351

346352

347353
; Pushes a stack divider to a state
@@ -350,6 +356,7 @@
350356
(cons `((.cf) (,classname)) state)))
351357

352358

359+
; Gets a valid state for a 'this' keyword call
353360
(define get-valid-this-call-state
354361
(lambda (state)
355362
(cond
@@ -383,28 +390,23 @@
383390
(cond
384391
; If the if condition is true, evaluate the statements inside of it.
385392
((get-value-from-pair eval-ifcond)
386-
387-
; The state for evaluating the if statement's statements is the state after evaluating the if statement's condition (side effects challenge)
388-
(evaluate-statement-list->state
389-
(list (get-if-then arglist))
390-
(get-state-from-pair eval-ifcond)
391-
cfuncsinstance))
392-
393+
(evaluate-statement-list->state (list (get-if-then arglist))
394+
(get-state-from-pair eval-ifcond)
395+
cfuncsinstance))
393396
((has-else? arglist)
394-
(evaluate-statement-list->state
395-
(list (get-if-else arglist))
396-
(get-state-from-pair eval-ifcond)
397-
cfuncsinstance))
398-
399-
; If the if condition is false, return '() for the return value, and also return the updated state after evaluating the condition (side effects challenge)
397+
(evaluate-statement-list->state (list (get-if-else arglist))
398+
(get-state-from-pair eval-ifcond)
399+
cfuncsinstance))
400400
(else (get-state-from-pair eval-ifcond))))))
401401

402+
; Checks if an arglist has an if-else branch
402403
(define get-if-else
403404
(lambda (arglist)
404405
(cond
405406
((not (has-else? arglist)) (error "no else statement"))
406407
(else (get-args-after-if-else arglist)))))
407408

409+
; Checks if an arglist has an else branch
408410
(define has-else?
409411
(lambda (arglist)
410412
(pair? (get-else-from-if-else arglist))))
@@ -424,34 +426,35 @@
424426
(evaluate-inner-try-statement arglist
425427
state
426428
(lambda (s)
427-
(throw (G-remove-scope-from-state->state(evaluate-statement-list->state(get-finally-from-try arglist)
428-
(G-add-empty-scope-to-state->state s)
429-
cfuncsinstance))))
429+
(throw (G-remove-scope-from-state->state (evaluate-statement-list->state (get-finally-from-try arglist)
430+
(G-add-empty-scope-to-state->state s)
431+
cfuncsinstance))))
430432
cfuncsinstance)))))
431433

434+
; Evaluate an inner try statement
432435
(define evaluate-inner-try-statement
433436
(lambda (arglist state finally cfuncsinstance)
434437
(finally
435-
(G-remove-scope-from-state->state
436-
(evaluate-statement-list->state (get-statements-from-try arglist)
437-
(G-add-empty-scope-to-state->state state)
438-
(cfuncs-update-catch cfuncsinstance
439-
(lambda (s e)
440-
(finally
441-
(G-remove-scope-from-state->state
442-
(evaluate-statement-list->state
443-
(get-statements-from-catch (get-catch-from-try arglist))
444-
(G-push-state->state (get-exception-from-catch (get-catch-from-try arglist))
445-
e
446-
(G-add-empty-scope-to-state->state (G-remove-scope-from-state->state s)))
447-
cfuncsinstance))))))))))
448-
438+
(G-remove-scope-from-state->state (evaluate-statement-list->state (get-statements-from-try arglist)
439+
(G-add-empty-scope-to-state->state state)
440+
(cfuncs-update-catch cfuncsinstance
441+
(lambda (s e)
442+
(finally
443+
(G-remove-scope-from-state->state (evaluate-statement-list->state
444+
(get-statements-from-catch (get-catch-from-try arglist))
445+
(G-push-state->state (get-exception-from-catch (get-catch-from-try arglist))
446+
e
447+
(G-add-empty-scope-to-state->state (G-remove-scope-from-state->state s)))
448+
cfuncsinstance))))))))))
449+
450+
; Gets the catch statement from try
449451
(define get-catch-from-try
450452
(lambda (arglist)
451453
(cond
452454
((null? (get-catch-wrapper arglist)) '())
453455
(else (get-inner-catch-statement (get-contents-of-catch arglist))))))
454456

457+
; Gets the finally statement from try
455458
(define get-finally-from-try
456459
(lambda (arglist)
457460
(cond
@@ -474,28 +477,25 @@
474477
(lambda (break)
475478
(evaluate-recursive-while arglist state (cfuncs-update-break cfuncsinstance break))))))
476479

480+
481+
; Evaluate a while statement
477482
(define evaluate-recursive-while
478483
(lambda (arglist state cfuncsinstance)
479484
(call/cc
480485
(lambda (endcontinue)
481486
(let* ([eval-while-cond (G-eval-atomic-statement->value_state (get-while-cond arglist) state cfuncsinstance)])
482487
(cond
483-
; If the while condition is true, evaluate the statements inside of it.
484488
((get-value-from-pair eval-while-cond)
485-
(evaluate-recursive-while
486-
arglist
487-
; The state for evaluating the while statement's statements is the state after evaluating the while statement's condition (side effects challenge)
488-
(evaluate-statement-list->state
489-
(list (get-while-statement arglist))
490-
(get-state-from-pair eval-while-cond)
491-
492-
; s is a passed in state
493-
(cfuncs-update-continue
494-
cfuncsinstance
495-
(lambda (s) (endcontinue (evaluate-recursive-while arglist s cfuncsinstance)))))
496-
497-
cfuncsinstance))
498-
489+
(evaluate-recursive-while arglist
490+
(evaluate-statement-list->state (list (get-while-statement arglist))
491+
(get-state-from-pair eval-while-cond)
492+
(cfuncs-update-continue cfuncsinstance
493+
(lambda (s)
494+
(endcontinue (evaluate-recursive-while arglist
495+
s
496+
cfuncsinstance)))))
497+
498+
cfuncsinstance))
499499
; If the while condition is false, return '() for the return value, and also return the updated state after evaluating the condition (side effects challenge)
500500
(else (get-state-from-pair eval-while-cond))))))))
501501

@@ -531,18 +531,24 @@
531531
(lambda (value state)
532532
(let* ([cn (cadr value)])
533533
(cond
534-
(else (list (list 'classname cn) (G-eval-class-closure->state cn state)))))))
534+
(else (list (list 'classname cn)
535+
(G-eval-class-closure->state cn state)))))))
535536

536537

537538
; Pushes the declaration statement to the state
538539
(define declare-var->state
539540
(lambda (name state)
540-
(initialize-var->state name '() state)))
541+
(initialize-var->state name empty-value-for-declare state)))
542+
543+
(define empty-value-for-declare '())
541544

542545
; Pushes and initializes the variable to the state
543546
(define initialize-var->state
544547
(lambda (name value state)
545-
(cons (append-head-scope-to-scope (list (list name) (list value)) (get-top-scope state)) (get-tail-scope state))))
548+
(cons (append-head-scope-to-scope (list (list name)
549+
(list value))
550+
(get-top-scope state))
551+
(get-tail-scope state))))
546552

547553
; Determines if an argument is a declare (and not an assign).
548554
(define only-declare?
@@ -584,6 +590,7 @@
584590
(lambda (arglist)
585591
(eq? (get-value-from-pair (get-value-from-pair arglist)) 'classname)))
586592

593+
; checks if an arglist is a dot-expression
587594
(define dot-expr?
588595
(lambda (arglist)
589596
(cond
@@ -597,20 +604,25 @@
597604
(arglist-tail arglist)))
598605

599606

607+
; evaluates a dotted expression
600608
(define evaluate-dotted-expr->value_state
601609
(lambda (arglist state cfuncsinstance)
602610
(cond
603611
((eq? (dotted-class-instance arglist) 'this)
604-
(list (get-value-from-pair
605-
(G-value-lookup->value_state (dotted-class-call arglist) (get-valid-this-call-state state) cfuncsinstance))
612+
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
613+
(get-valid-this-call-state state)
614+
cfuncsinstance))
606615
state))
607616
((eq? (dotted-class-instance arglist) 'super)
608-
(list (get-value-from-pair
609-
(G-value-lookup->value_state (dotted-class-call arglist) (get-tail-scope (get-valid-this-call-state state)) cfuncsinstance))
617+
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
618+
(get-tail-scope (get-valid-this-call-state state))
619+
cfuncsinstance))
610620
state))
611-
(else (list (get-value-from-pair
612-
(G-value-lookup->value_state (dotted-class-call arglist) (G-get-instance-state (dotted-class-instance arglist) state) cfuncsinstance))
613-
state)))))
621+
(else
622+
(list (get-value-from-pair (G-value-lookup->value_state (dotted-class-call arglist)
623+
(G-get-instance-state (dotted-class-instance arglist) state)
624+
cfuncsinstance))
625+
state)))))
614626

615627
; FUNCALL Section
616628

@@ -682,31 +694,35 @@
682694
(get-state-from-pair evaluate-assign)) cfuncsinstance))
683695
(else (error "variable undeclared args:" arglist "state" state))))))
684696

697+
; Evaluates a dotted assignment expression
685698
(define evaluate-dotted-assign->value_state
686699
(lambda (dot-expression assign-value state cfuncsinstance)
687700
(cond
688701
((eq? (dotted-class-instance dot-expression) 'super)
689-
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value) (get-tail-scope (get-valid-this-call-state state)) cfuncsinstance)]
702+
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value)
703+
(get-tail-scope (get-valid-this-call-state state))
704+
cfuncsinstance)]
690705
; tail-scope is called to remove .cf pointer
691706
[evaled-state (get-state-from-pair evaled-assign)]
692707
[evaled-value (get-value-from-pair evaled-assign)])
693-
694-
(list evaled-value (G-merge-states->state state evaled-state))))
708+
(list evaled-value (G-merge-states->state state evaled-state))))
709+
695710
((eq? (dotted-class-instance dot-expression) 'this)
696-
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value) (get-valid-this-call-state state) cfuncsinstance)]
711+
(let* ([evaled-assign (G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value)
712+
(get-valid-this-call-state state)
713+
cfuncsinstance)]
697714
; tail-scope is called to remove .cf pointer
698715
[evaled-state (get-state-from-pair evaled-assign)]
699716
[evaled-value (get-value-from-pair evaled-assign)])
700-
701-
(list evaled-value (G-merge-states->state state evaled-state))))
717+
(list evaled-value (G-merge-states->state state evaled-state))))
718+
702719
(else
703720
(update-class-instance (dotted-class-instance dot-expression)
704-
(extract-new-class-instance-state (get-state-from-pair
705-
(G-eval-assign->value_state `(= ,(dotted-class-call dot-expression) ,assign-value) (construct-dotted-state dot-expression state) cfuncsinstance)))
706-
state)))))
707-
708-
709-
721+
(extract-new-class-instance-state (get-state-from-pair G-eval-assign->value_state
722+
`(= ,(dotted-class-call dot-expression) ,assign-value)
723+
(construct-dotted-state dot-expression state)
724+
cfuncsinstance)))
725+
state))))
710726

711727
; Determines whether or not an assignment argument is reached
712728
(define G-assign?
@@ -739,12 +755,14 @@
739755
((eq? (length arglist) 2)
740756
(eval-expr-uni->value_state (get-op-from-expr arglist)
741757
(get-arg1-from-expr arglist)
742-
state cfuncsinstance))
758+
state
759+
cfuncsinstance))
743760
((eq? (length arglist) 3)
744761
(eval-expr-multi->value_state (get-op-from-expr arglist)
745762
(get-arg1-from-expr arglist)
746763
(get-arg2-from-expr arglist)
747-
state cfuncsinstance))
764+
state
765+
cfuncsinstance))
748766
(else (error "invalid number of arguments")))))
749767

750768

@@ -821,7 +839,8 @@
821839
; We return a (value, state), hence the cons for the value and the state
822840
; The value is derived from applying the operator on arg1 and arg2
823841
; To handle side effects, the state passed into arg2 is the state after evaluating arg1
824-
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'boolean) (eq? (G-type-lookup arg2 state cfuncsinstance) 'boolean))
842+
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'boolean)
843+
(eq? (G-type-lookup arg2 state cfuncsinstance) 'boolean))
825844
(cons ((boolean-operator-to-function-multi op)
826845
(get-value-from-pair lookup-arg1)
827846
(get-value-from-pair lookup-arg2))
@@ -835,7 +854,8 @@
835854
(define eval-math-expr-multi->value_state
836855
(lambda (op arg1 arg2 state cfuncsinstance)
837856
(cond
838-
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'integer) (eq? (G-type-lookup arg2 state cfuncsinstance) 'integer))
857+
((and (eq? (G-type-lookup arg1 state cfuncsinstance) 'integer)
858+
(eq? (G-type-lookup arg2 state cfuncsinstance) 'integer))
839859
(eval-math-expr-int-multi->value_state op arg1 arg2 state cfuncsinstance))
840860
(else (error "invalid types for math expression")))))
841861

0 commit comments

Comments
 (0)