@@ -10,6 +10,7 @@ import {
1010 ObjectLiteral ,
1111 ArrayLiteral ,
1212 AssignmentExpr ,
13+ TypeAnnotation ,
1314} from "../parser/typeAst" ;
1415
1516interface TranspileOptions {
@@ -23,13 +24,60 @@ interface TranspileOptions {
2324export function transpileToJS ( ast : Program , options : TranspileOptions = { } ) : string {
2425 const ctx = new TranspileContext ( options ) ;
2526 const programCode = transpileProgram ( ast , ctx ) ;
26- // Mark function runtime //
27+
28+ // State management runtime //
2729 const runtime =
2830 options . injectRuntime !== false
2931 ? `// JistScript Runtime
3032const mark = typeof window !== 'undefined' && window.console
3133 ? (...args) => console.log('[JistScript]', ...args)
3234 : (...args) => console.log('[JistScript]', ...args);
35+
36+ // State management system
37+ const __jist_state_counter = { value: 0 };
38+ const __jist_state_store = new Map();
39+ const __jist_component_states = new Map();
40+
41+ function useState(initialValue, typeInfo) {
42+ const stateId = __jist_state_counter.value++;
43+
44+ if (!__jist_state_store.has(stateId)) {
45+ __jist_state_store.set(stateId, initialValue);
46+ }
47+
48+ const currentValue = __jist_state_store.get(stateId);
49+
50+ const setState = (newValue) => {
51+ if (typeInfo && !validateType(newValue, typeInfo)) {
52+ console.warn(\`[JistScript] Type mismatch: expected \${typeInfo.type}, got \${typeof newValue}\`);
53+ }
54+ __jist_state_store.set(stateId, newValue);
55+ return newValue;
56+ };
57+
58+ return [currentValue, setState];
59+ }
60+
61+ function validateType(value, typeInfo) {
62+ switch (typeInfo.type) {
63+ case 'String':
64+ return typeof value === 'string';
65+ case 'Number':
66+ return typeof value === 'number';
67+ case 'Boolean':
68+ return typeof value === 'boolean';
69+ case 'Array':
70+ if (!Array.isArray(value)) return false;
71+ if (typeInfo.elementType) {
72+ return value.every(item => validateType(item, { type: typeInfo.elementType }));
73+ }
74+ return true;
75+ case 'Object':
76+ return typeof value === 'object' && value !== null && !Array.isArray(value);
77+ default:
78+ return true;
79+ }
80+ }
3381`
3482 : "" ;
3583
@@ -61,6 +109,11 @@ function transpileStatement(stmt: Statement, ctx: TranspileContext): string {
61109 case "VarDeclaration" : {
62110 const decl = stmt as VarDeclaration ;
63111 const keyword = decl . constant ? "const" : "let" ;
112+ if ( decl . destructuring && decl . value ) {
113+ const elements = decl . destructuring . elements . map ( e => e . symbol ) . join ( ", " ) ;
114+ const value = transpileExpression ( decl . value , ctx ) ;
115+ return `${ indent } ${ keyword } [${ elements } ] = ${ value } ;` ;
116+ }
64117 const value = decl . value ? transpileExpression ( decl . value , ctx ) : "undefined" ;
65118 return `${ indent } ${ keyword } ${ decl . identifier } = ${ value } ;` ;
66119 }
@@ -118,6 +171,11 @@ function transpileExpression(expr: Expression, ctx: TranspileContext): string {
118171 case "CallExpr" : {
119172 const call = expr as CallExpr ;
120173 const caller = transpileExpression ( call . caller , ctx ) ;
174+ if ( caller === "useState" && call . typeAnnotation ) {
175+ const typeInfo = transpileTypeAnnotation ( call . typeAnnotation ) ;
176+ const args = call . args . map ( arg => transpileExpression ( arg , ctx ) ) . join ( ", " ) ;
177+ return `useState(${ args } , ${ typeInfo } )` ;
178+ }
121179 const args = call . args . map ( arg => transpileExpression ( arg , ctx ) ) . join ( ", " ) ;
122180 return `${ caller } (${ args } )` ;
123181 }
@@ -153,3 +211,14 @@ function transpileExpression(expr: Expression, ctx: TranspileContext): string {
153211 return `/* Unknown: ${ expr . kind } */` ;
154212 }
155213}
214+
215+ function transpileTypeAnnotation ( typeAnnotation : TypeAnnotation ) : string {
216+ let typeInfo = `{ type: "${ typeAnnotation . typeName } "` ;
217+ if ( typeAnnotation . genericTypes && typeAnnotation . genericTypes . length > 0 ) {
218+ const elementType = typeAnnotation . genericTypes [ 0 ] . typeName ;
219+ typeInfo += `, elementType: "${ elementType } "` ;
220+ }
221+
222+ typeInfo += " }" ;
223+ return typeInfo ;
224+ }
0 commit comments