@@ -5,7 +5,7 @@ import { ErrorLog } from '../utils'
55import { Integer , Float , BooleanT , StringT , Void , Null , Any ,
66 ObjectType , objectType , FunctionType ,
77 StaticType , isPrimitiveType , typeToString , ArrayType , sameType , encodeType , isSubtype ,
8- ByteArrayClass , UnionType ,
8+ ByteArrayClass , UnionType , EnumType ,
99 VectorClass } from '../types'
1010import { InstanceType , ClassTable } from '../classes'
1111import { VariableEnv } from './variables'
@@ -46,6 +46,8 @@ function typeToCType2(type: StaticType): string {
4646 return funcTypeInC
4747 else if ( type instanceof ObjectType || type instanceof UnionType )
4848 return anyTypeInC
49+ else if ( type instanceof EnumType )
50+ return 'int32_t'
4951 else
5052 switch ( type ) {
5153 case Integer :
@@ -80,7 +82,7 @@ export function typeConversion(from: StaticType | undefined, to: StaticType | un
8082 return '('
8183
8284 if ( sameType ( from , to ) )
83- if ( from === Integer )
85+ if ( from === Integer || from instanceof EnumType )
8486 return '(int32_t)('
8587 else if ( from === Float )
8688 return '(float)('
@@ -100,21 +102,21 @@ export function typeConversion(from: StaticType | undefined, to: StaticType | un
100102 case Integer :
101103 if ( from === Float )
102104 return '(int32_t)('
103- else if ( from === BooleanT )
105+ else if ( from === BooleanT || from instanceof EnumType )
104106 return '('
105107 else if ( from === Any || from instanceof UnionType )
106108 return 'safe_value_to_int('
107109 else
108110 break
109111 case Float :
110- if ( from === Integer || from === BooleanT )
112+ if ( from === Integer || from === BooleanT || from instanceof EnumType )
111113 return '(float)('
112114 else if ( from === Any || from instanceof UnionType )
113115 return 'safe_value_to_float('
114116 else
115117 break
116118 case BooleanT :
117- if ( from === Integer )
119+ if ( from === Integer || from instanceof EnumType )
118120 return '('
119121 else if ( from === Float )
120122 return '(int32_t)('
@@ -136,17 +138,24 @@ export function typeConversion(from: StaticType | undefined, to: StaticType | un
136138 case BooleanT :
137139 return 'bool_to_value('
138140 default :
139- return '('
141+ if ( from instanceof EnumType )
142+ return 'int_to_value('
143+ else
144+ return '('
140145 }
141- default : // "to" is either String, Object, Array, or UnionType
142- if ( from === Any || from instanceof ObjectType || from instanceof UnionType ) {
146+ default : // "to" is either String, Object, Array, EnumType, or UnionType
147+ if ( to instanceof EnumType ) {
148+ if ( from === Any || from instanceof UnionType )
149+ return 'safe_value_to_int('
150+ }
151+ else if ( from === Any || from instanceof ObjectType || from instanceof UnionType ) {
143152 if ( to === StringT )
144153 return 'safe_value_to_string(false, '
145154 else if ( to instanceof ObjectType ) {
146155 if ( to === objectType )
147156 return 'safe_value_to_object(false, '
148157 else if ( to instanceof ArrayType ) {
149- if ( to . elementType === Integer )
158+ if ( to . elementType === Integer || to . elementType instanceof EnumType )
150159 return 'safe_value_to_intarray(false, '
151160 else if ( to . elementType === Float )
152161 return 'safe_value_to_floatarray(false, '
@@ -187,7 +196,7 @@ function typeConversionToUnion(from: StaticType, to: UnionType, env: VariableEnv
187196 if ( objType === objectType )
188197 return 'safe_value_to_object(true, '
189198 else if ( objType instanceof ArrayType ) {
190- if ( objType . elementType === Integer )
199+ if ( objType . elementType === Integer || objType . elementType instanceof EnumType )
191200 return 'safe_value_to_intarray(true, '
192201 else if ( objType . elementType === Float )
193202 return 'safe_value_to_floatarray(true, '
@@ -375,7 +384,7 @@ export function arrayElementGetter(t: StaticType | undefined, arrayType: StaticT
375384 return '(gc_vector_get('
376385 else if ( arrayType === Any )
377386 return '(gc_safe_array_get('
378- else if ( t === Integer )
387+ else if ( t === Integer || t instanceof EnumType )
379388 return '(*gc_intarray_get('
380389 else if ( t === Float )
381390 return '(*gc_floatarray_get('
@@ -401,7 +410,7 @@ export const accumulateInUnknownArray = 'gc_safe_array_acc'
401410// makes an array object from elements
402411export function arrayFromElements ( arrayType : ArrayType , env : VariableEnv ) {
403412 const t = arrayType . elementType
404- if ( t === Integer )
413+ if ( t === Integer || t instanceof EnumType )
405414 return 'gc_make_intarray('
406415 else if ( t === Float )
407416 return 'gc_make_floatarray('
@@ -415,7 +424,7 @@ export function arrayFromElements(arrayType: ArrayType, env: VariableEnv) {
415424
416425export function arrayFromSize ( arrayType : ArrayType , env : VariableEnv ) {
417426 const t = arrayType . elementType
418- if ( t === Integer )
427+ if ( t === Integer || t instanceof EnumType )
419428 return 'gc_new_intarray('
420429 else if ( t === Float )
421430 return 'gc_new_floatarray('
@@ -430,7 +439,7 @@ export function arrayFromSize(arrayType: ArrayType, env: VariableEnv) {
430439export const prefixOfarrayTypeNameInC = 'array_type'
431440
432441export function actualElementType ( t : StaticType ) {
433- if ( t === Integer )
442+ if ( t === Integer || t instanceof EnumType )
434443 return Integer
435444 else if ( t === Float )
436445 return Float
0 commit comments