11import { Class , ClassImpl , ObjectClass } from '../types/classes'
22import { ConstructorDeclaration , MethodDeclaration } from '../ast/types/classes'
3- import { createClass } from '../typeFactories/classFactory'
3+ import { createClassFieldsAndMethods } from '../typeFactories/classFactory'
44import { createMethodSignature } from '../typeFactories/methodFactory'
55import { CyclicInheritanceError } from '../errors'
66import { MethodSignature } from '../types/methods'
77import { Node } from '../ast/types'
88import { Frame } from './environment'
99import { newResult , OK_RESULT , Result } from '.'
1010
11- export const addClassesToFrame = ( node : Node , frame : Frame ) : Result => {
11+ export const addClasses = ( node : Node , frame : Frame ) : Result => {
1212 switch ( node . kind ) {
1313 case 'CompilationUnit' : {
1414 const errors : Error [ ] = [ ]
1515 for ( const classDeclaration of node . topLevelClassOrInterfaceDeclarations ) {
16- const result = addClassesToFrame ( classDeclaration , frame )
16+ const result = addClasses ( classDeclaration , frame )
17+ if ( result . errors . length > 0 ) errors . push ( ...result . errors )
18+ }
19+ if ( errors . length > 0 ) return newResult ( null , errors )
20+ return OK_RESULT
21+ }
22+ case 'NormalClassDeclaration' : {
23+ const classType = new ClassImpl ( node . typeIdentifier )
24+ frame . setType ( node . typeIdentifier , classType )
25+ return newResult ( classType )
26+ }
27+ default :
28+ return OK_RESULT
29+ }
30+ }
31+
32+ export const addClassMethods = ( node : Node , frame : Frame ) : Result => {
33+ switch ( node . kind ) {
34+ case 'CompilationUnit' : {
35+ const errors : Error [ ] = [ ]
36+ for ( const classDeclaration of node . topLevelClassOrInterfaceDeclarations ) {
37+ const result = addClassMethods ( classDeclaration , frame )
1738 if ( result . errors . length > 0 ) errors . push ( ...result . errors )
1839 }
1940 if ( errors . length > 0 ) return newResult ( null , errors )
@@ -27,12 +48,12 @@ export const addClassesToFrame = (node: Node, frame: Frame): Result => {
2748 }
2849 case 'NormalClassDeclaration' : {
2950 const createMethod = ( node : ConstructorDeclaration | MethodDeclaration ) => {
30- const result = addClassesToFrame ( node , frame )
51+ const result = addClassMethods ( node , frame )
3152 if ( result . errors . length > 0 ) return result . errors [ 0 ]
3253 return result . currentType as MethodSignature
3354 }
3455
35- const classType = createClass ( node , frame , createMethod , createMethod )
56+ const classType = createClassFieldsAndMethods ( node , frame , createMethod , createMethod )
3657 if ( classType instanceof Error ) return newResult ( null , [ classType ] )
3758 return newResult ( classType )
3859 }
@@ -41,12 +62,12 @@ export const addClassesToFrame = (node: Node, frame: Frame): Result => {
4162 }
4263}
4364
44- export const resolveClassRelationships = ( node : Node , frame : Frame ) : Result => {
65+ export const addClassParents = ( node : Node , frame : Frame ) : Result => {
4566 switch ( node . kind ) {
4667 case 'CompilationUnit' : {
4768 const errors : Error [ ] = [ ]
4869 for ( const classDeclaration of node . topLevelClassOrInterfaceDeclarations ) {
49- const result = resolveClassRelationships ( classDeclaration , frame )
70+ const result = addClassParents ( classDeclaration , frame )
5071 if ( result . errors . length > 0 ) errors . push ( ...result . errors )
5172 }
5273 if ( errors . length > 0 ) return newResult ( null , errors )
0 commit comments