Skip to content

Commit 195f220

Browse files
committed
add more flags, add script run tests on brown grid
1 parent 00a0446 commit 195f220

File tree

5 files changed

+425
-36
lines changed

5 files changed

+425
-36
lines changed

‎base/python-desugar-flags.rkt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@
4646
(define-flag dsg-function-arguments)
4747
(define-flag dsg-function-starargs)
4848

49+
(define-flag dsg-callable-runtime-checking)
4950

51+
; if dsg-decorator is false, no class decorator will be desugared.
52+
(define-flag dsg-decorator)
53+
; if dsg-metaclass is false,
54+
(define-flag dsg-metaclass)
5055

56+
(define-flag dsg-multiple-inheritance)
5157

5258
; flags for pass: this is interesting--how important the pass statement in Python?
5359
; no idea how to write the code

‎base/python-desugar.rkt‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@
373373
;handle the implicit construction case
374374
(if (eq? dsg-raise true)
375375
(rec-desugar (LexApp expr (list) (list) (none) (none)))
376-
; Why does CApp work here? it is supposed to be (simple-apply-method ...)
377376
(CLet '$call (LocalId) (rec-desugar expr)
378377
(simple-apply-method (py-getfield (CId '$call (LocalId)) '__call__) (list) )))
379378
(rec-desugar expr)))]
@@ -612,19 +611,44 @@
612611

613612
[LexClass (scp name bases body keywords stararg kwarg decorators)
614613
(cond
615-
[(empty? decorators)
614+
[(or (empty? decorators) (eq? dsg-decorator false))
616615
; no decorators, desugar class
617616
(let* ([scope (type-case LocalOrGlobal scp
618617
[Locally-scoped () (LocalId)]
619618
[Globally-scoped () (GlobalId)]
620619
[else (error 'expr"should be no more instance scope!")])]
620+
; bases-list only reserves the base class
621+
; meta-class is stored at keywords
621622
[bases-list (if (empty? bases)
622623
(list (CId '%object (GlobalId)))
623624
(map desugar bases))]
624625
[base-id (new-id)]
625626
; (CNone) is because we may not have a tuple class object yet, type-uniqbases fixes it.
626-
[bases-tuple (CTuple (CNone) (cons (CId base-id (LocalId)) (rest bases-list)))]
627-
[new-class (make-class scope name bases-tuple (desugar body))]
627+
[bases-tuple
628+
(if (eq? dsg-multiple-inheritance true)
629+
(CTuple (CNone) (cons (CId base-id (LocalId)) (rest bases-list)))
630+
(CTuple (CNone) (list (CId '%object (GlobalId)))))]
631+
[new-class
632+
(if (eq? dsg-multiple-inheritance true)
633+
(make-class scope name bases-tuple (desugar body))
634+
(CSeq ; simplify the make-class
635+
(CAssign (CId name scope)
636+
(CBuiltinPrim 'type-new
637+
(list (CObject (CNone)
638+
(some (MetaStr (symbol->string name)))))))
639+
(CSeq
640+
(set-field (CId name scope) '__bases__ bases-tuple)
641+
(CSeq
642+
(set-field (CId name scope) '__mro__
643+
(CBuiltinPrim 'type-buildmro
644+
(list
645+
(CTuple (CNone) (list (CId name scope)))
646+
(CBuiltinPrim 'obj-getattr (list (CId name scope)
647+
(make-pre-str "__bases__"))))))
648+
(CSeq
649+
(desugar body)
650+
(CId name scope)))))
651+
)]
628652
[call-metaclass (CApp (CId '%call_metaclass (GlobalId))
629653
(list (make-builtin-str (symbol->string name))
630654
(CBuiltinPrim 'type-uniqbases (list bases-tuple))
@@ -633,12 +657,42 @@
633657
(CTuple (CId '%tuple (GlobalId)) (map rec-desugar (option->list stararg)))
634658
(CTuple (CId '%tuple (GlobalId)) (map rec-desugar (option->list kwarg))))
635659
(none))])
636-
(CLet base-id (LocalId) (first bases-list)
637-
(if (and (empty? keywords) (none? kwarg))
638-
(CIf (CBuiltinPrim 'type-metaclass (list (first bases-list)))
639-
call-metaclass
640-
new-class)
641-
call-metaclass)))]
660+
(cond [(and (eq? dsg-metaclass true)
661+
(eq? dsg-multiple-inheritance true))
662+
(CLet base-id (LocalId) (first bases-list)
663+
(if (and (empty? keywords) (none? kwarg))
664+
(CIf (CBuiltinPrim 'type-metaclass (list (first bases-list)))
665+
call-metaclass
666+
new-class)
667+
call-metaclass))]
668+
669+
[(and (eq? dsg-metaclass false)
670+
(eq? dsg-multiple-inheritance false))
671+
(CSeq
672+
(CAssign (CId name scope)
673+
(CBuiltinPrim 'type-new
674+
(list (CObject (CNone) (some (MetaStr (symbol->string name)))))))
675+
(CSeq
676+
(set-field (CId name scope) '__bases__
677+
(CTuple (CNone) (list (CId '%object (GlobalId)))))
678+
(CSeq
679+
(set-field (CId name scope) '__mro__
680+
(CBuiltinPrim 'type-buildmro
681+
(list
682+
(CTuple (CNone)
683+
(list (CId name scope)))
684+
(CBuiltinPrim 'obj-getattr (list (CId name scope)
685+
(make-pre-str "__bases__"))))))
686+
(CSeq body (CId name scope)))))]
687+
[(eq? dsg-metaclass false) ; TODO: this can be simplified too.
688+
(CLet base-id (LocalId) (first bases-list)
689+
new-class)]
690+
691+
[(eq? dsg-multiple-inheritance false)
692+
(if (and (empty? keywords) (none? kwarg))
693+
new-class
694+
call-metaclass)]
695+
))]
642696
[else
643697
; first apply decorators to the class
644698
(rec-desugar

0 commit comments

Comments
(0)