Skip to content

Commit 4fa441e

Browse files
Merge pull request #65 from chibash/transpiler
supports enum types
2 parents 4d26693 + ed1014a commit 4fa441e

File tree

7 files changed

+349
-99
lines changed

7 files changed

+349
-99
lines changed

server/src/transpiler/code-generator/c-runtime.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ErrorLog } from '../utils'
55
import { 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'
1010
import { InstanceType, ClassTable } from '../classes'
1111
import { 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
402411
export 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

416425
export 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) {
430439
export const prefixOfarrayTypeNameInC = 'array_type'
431440

432441
export 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

server/src/transpiler/code-generator/code-generator.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as AST from '@babel/types'
44
import { runBabelParser, ErrorLog, CodeWriter } from '../utils'
55
import { Integer, BooleanT, Void, Any, ObjectType, FunctionType,
66
StaticType, ByteArrayClass, isPrimitiveType, encodeType, sameType, typeToString, ArrayType, objectType,
7-
StringT, UnionType, VectorClass, StringType } from '../types'
7+
StringT, UnionType, VectorClass, StringType,
8+
EnumType} from '../types'
89
import * as visitor from '../visitor'
910
import { getCoercionFlag, getStaticType } from '../names'
1011
import TypeChecker, { typecheck, codeTagFunction } from '../type-checker'
@@ -101,6 +102,7 @@ export class CodeGenerator extends visitor.NodeVisitor<VariableEnv> {
101102
if (type instanceof InstanceType)
102103
this.signatures += cr.externNew(type)
103104
}
105+
else if (type instanceof EnumType) {}
104106
else
105107
throw this.errorLog.push('fatal: bad external type', node)
106108
}
@@ -515,6 +517,8 @@ export class CodeGenerator extends visitor.NodeVisitor<VariableEnv> {
515517
classProperty(node: AST.ClassProperty, env: VariableEnv): void {}
516518
classMethod(node: AST.ClassMethod, env: VariableEnv): void {}
517519

520+
tsEnumDeclaration(node: AST.TSEnumDeclaration, env: VariableEnv): void {}
521+
518522
variableDeclaration(node: AST.VariableDeclaration, env: VariableEnv): void {
519523
if (this.isConstFunctionDeclaration(node, env))
520524
return
@@ -1506,6 +1510,10 @@ export class CodeGenerator extends visitor.NodeVisitor<VariableEnv> {
15061510
this.visit(node.object, env)
15071511
this.result.write(`, ${propertyCode})`)
15081512
}
1513+
else if (objType instanceof EnumType) {
1514+
const value = objType.getMember(propertyName)
1515+
this.result.write(`${value}`)
1516+
}
15091517
else
15101518
throw this.errorLog.push('fatal: unknown array property', node)
15111519
}

0 commit comments

Comments
 (0)