Skip to content

Commit

Permalink
Merge pull request #11 from yann-gael/fixlambda
Browse files Browse the repository at this point in the history
Added metaclasses
  • Loading branch information
NicolasAnquetil authored May 17, 2024
2 parents ea39c2f + 54bcd64 commit a1101e4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
18 changes: 12 additions & 6 deletions src/MoosePy/MSEPythonImporterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def greet(name):
MSEPythonImporterTest >> testClassIsInModel [

importer accept: pyDoc.
self assert: importer model numberOfClasses equals: 3.
self assert: importer model numberOfClasses equals: 4.
self assert: importer model name equals: 'testPythonModelName'
]

{ #category : #tests }
MSEPythonImporterTest >> testClasses [

importer accept: pyDoc.
self assert: importer classes size equals: 3.
self assert: importer classes size equals: 4.
self assert: (importer classNamed: 'Ball') isNotNil.
self assert: (importer classNamed: 'Player') isNotNil
]
Expand All @@ -155,11 +155,11 @@ MSEPythonImporterTest >> testClasses [
MSEPythonImporterTest >> testClassesImportedTwiceAreOnlyImportedOnce [

importer accept: pyDoc.
self assert: importer classes size equals: 3.
self assert: importer classes size equals: 4.

importer accept: pyDoc.

self assert: importer classes size equals: 3
self assert: importer classes size equals: 4
]

{ #category : #'tests - extensions' }
Expand Down Expand Up @@ -849,7 +849,7 @@ class A(S):
{ #category : #'tests - metaclasses' }
MSEPythonImporterTest >> testSSingleClassingleMetaclass [

| classA classMeta |
| classA classMeta classAMeta |
importer accept: (self parseCode: '
class Meta(type):
pass
Expand All @@ -860,13 +860,19 @@ class A(metaclass=Meta):

self
assert: (importer model allWithType: FamixPythonClass) size
equals: 3.
equals: 4.

classA := importer classNamed: 'A'.
self assert: classA isNotNil.
self assert: (classA superInheritances first target name) equals: 'object'.

classMeta := importer classNamed: 'Meta'.
self assert: classMeta isNotNil.
self assert: (classMeta superInheritances first target name) equals: 'type'.

classAMeta := classA metaclass.
self assert: classAMeta isNotNil.
self assert: classAMeta name equals: 'Meta'
]

{ #category : #tests }
Expand Down
71 changes: 48 additions & 23 deletions src/MoosePy/MSEPythonToFamixImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,56 @@ MSEPythonToFamixImporterVisitor >> createAnnotationTypeNamed: aString [
]

{ #category : #'private-entity-creation' }
MSEPythonToFamixImporterVisitor >> createClass: arg1 [

| tmp1 tmp2 tmp3 tmp5 |
tmp2 := arg1 cname value.
tmp1 := model newClass.
tmp1 name: arg1 cname value.
arg1 superClasses do: [ :arg2 |
| tmp6 |
tmp6 := model newInheritance.
tmp6
superclass: (self classNamed: arg2 name);
subclass: tmp1 ].
tmp3 := arg1 superClasses reject: [ :arg3 |
arg3 class = PyMetaclassNode ].
tmp3 ifEmpty: [
| tmp7 |
tmp5 := model newInheritance.
tmp7 := model newClass.
tmp7
MSEPythonToFamixImporterVisitor >> createClass: smaCCClass [

| famixClass superClasses metaClasses |
famixClass := model newClass.
famixClass name: smaCCClass cname value.

"Reject all metaclass declarations, keep only superclasses (if any)"
"TODO Handle PyTrailersNode"
superClasses := smaCCClass superClasses reject: [ :s |
s class = PyMetaclassNode ].
superClasses do: [ :s |
| superName superObject inheritance |
superName := s name.
superObject := self classNamed: superName.
superObject ifNil: [
superObject := model newClass
name: superName;
stub: true ].
inheritance := model newInheritance.
inheritance
superclass: superObject;
subclass: famixClass ].

"Case for object, only if needed!"
superClasses ifEmpty: [
| inheritance object |
inheritance := model newInheritance.
object := model newClass.
object
name: 'object';
isStub: true.
tmp5
superclass: tmp7 yourself;
subclass: tmp1 ].
^ self setSourceAnchor: tmp1 from: arg1
inheritance
superclass: object yourself;
subclass: famixClass ].

"Reject all superclass declarations, keep only metaclasses (if any, there should be at most one)."
metaClasses := smaCCClass superClasses reject: [ :s |
s class = PySymbolNode or: [
s class = PyTrailersNode ] ].
metaClasses do: [ :m |
| metaClassName metaClassObject |
metaClassName := m superclass name.
metaClassObject := self classNamed: metaClassName.
metaClassObject ifNil: [
metaClassObject := model newClass
name: metaClassName;
stub: true ].
famixClass metaclass: metaClassObject ].

^ self setSourceAnchor: famixClass from: smaCCClass
]

{ #category : #'private-entity-creation' }
Expand Down

0 comments on commit a1101e4

Please sign in to comment.