@@ -26,53 +26,6 @@ natively, you should instead use wasm or gopherjs.
2626
2727It's currently an experimental project, and not ready for production.
2828
29- ## Generated Code
30-
31- Below is a simple example of how code is generated:
32-
33- ``` go
34- package main
35-
36- // MyStruct is converted into a class.
37- type MyStruct struct {
38- // MyInt is a public integer field, initialized to zero.
39- MyInt int
40- // myBool is a private boolean field, intialized to false.
41- myBool bool
42- }
43-
44- func main () {
45- println (" Hello world" )
46- }
47- ```
48-
49- Generated with ` goscript compile . ` :
50-
51- ``` typescript
52- // MyStruct is converted into a class.
53- class MyStruct {
54- // MyInt is a public integer field, initialized to zero.
55- public myInt: number = 0 ;
56- // myBool is a private boolean field, intialized to false.
57- private myBool: boolean = false ;
58- }
59-
60- export function main() {
61- console .log (" Hello world" );
62- }
63- ```
64-
65- Code is compiled with ` GOARCH=js ` and uses a 32-bit environment similar to wasm.
66-
67- All Go import paths are prefixed with ` @go/ ` and can be imported in TypeScript:
68-
69- ``` typescript
70- import { MyFunction , MyStruct } from ' @go/github.com/myorg/mypackage' ;
71-
72- MyFunction ();
73- let myThing = new MyStruct ();
74- myThing .DoSometing ();
75- ```
7629
7730## Usage
7831
@@ -141,7 +94,10 @@ This example initializes the compiler with basic configuration, creates a compil
14194
14295## Roadmap
14396
97+ Check [ the compliance tests] ( ./compliance/COMPLIANCE.md ) for implementation progress.
98+
14499 - [X] Simple programs compile & run
100+ - [ ] Compliance for basic language features complete
145101 - [ ] Function coloring and async logic
146102 - [ ] Work our way up to more complex programs
147103 - [ ] Generate init() function to recursively initialize packages
@@ -151,3 +107,284 @@ This example initializes the compiler with basic configuration, creates a compil
151107 - [ ] performance testing
152108 - [ ] examples of calling Go code from TypeScript
153109
110+
111+ ## Generated Code
112+
113+ Below is a simple example of how code is generated:
114+
115+ ``` go
116+ package main
117+
118+ // MyStruct demonstrates a simple struct with public and private fields.
119+ // It will be converted into a TypeScript class by goscript.
120+ type MyStruct struct {
121+ // MyInt is a public integer field, initialized to zero.
122+ MyInt int
123+ // MyString is a public string field, initialized to empty string.
124+ MyString string
125+ // myBool is a private boolean field, initialized to false.
126+ myBool bool
127+ }
128+
129+ // GetMyString returns the MyString field.
130+ func (m *MyStruct ) GetMyString () string {
131+ return m.MyString
132+ }
133+
134+ // GetMyBool returns the myBool field.
135+ func (m *MyStruct ) GetMyBool () bool {
136+ return m.myBool
137+ }
138+
139+ // NewMyStruct creates a new MyStruct instance.
140+ func NewMyStruct (s string ) MyStruct {
141+ return MyStruct{MyString: s}
142+ }
143+
144+ func vals () (int , int ) {
145+ return 1 , 2
146+ }
147+
148+ func main () {
149+ println (" Hello from GoScript example!" )
150+
151+ // Basic arithmetic
152+ a , b := 10 , 3
153+ println (" Addition:" , a+b, " Subtraction:" , a-b, " Multiplication:" , a*b, " Division:" , a/b, " Modulo:" , a%b)
154+
155+ // Boolean logic and comparisons
156+ println (" Logic &&:" , true && false , " ||:" , true || false , " !:!" , !true )
157+ println (" Comparisons:" , a == b, a != b, a < b, a > b, a <= b, a >= b)
158+
159+ // string(rune) conversion
160+ var r rune = ' X'
161+ s := string (r)
162+ println (" string('X'):" , s)
163+
164+ var r2 rune = 121 // 'y'
165+ s2 := string (r2)
166+ println (" string(121):" , s2)
167+
168+ var r3 rune = 0x221A // '√'
169+ s3 := string (r3)
170+ println (" string(0x221A):" , s3)
171+
172+ // Arrays
173+ arr := [3 ]int {1 , 2 , 3 }
174+ println (" Array elements:" , arr[0 ], arr[1 ], arr[2 ])
175+
176+ // Slices and range loop
177+ slice := []int {4 , 5 , 6 }
178+ println (" Slice elements:" , slice[0 ], slice[1 ], slice[2 ])
179+ sum := 0
180+ for idx , val := range slice {
181+ sum += val
182+ println (" Range idx:" , idx, " val:" , val)
183+ }
184+ println (" Range sum:" , sum)
185+
186+ // Basic for loop
187+ prod := 1
188+ for i := 1 ; i <= 3 ; i++ {
189+ prod *= i
190+ }
191+ println (" Product via for:" , prod)
192+
193+ // Struct, pointers, copy independence
194+ instance := NewMyStruct (" go-script" )
195+ println (" instance.MyString:" , instance.GetMyString ())
196+ instance.MyInt = 42
197+ copyInst := instance
198+ copyInst.MyInt = 7
199+ println (" instance.MyInt:" , instance.MyInt , " copyInst.MyInt:" , copyInst.MyInt )
200+
201+ // Pointer initialization and dereference assignment
202+ ptr := new (MyStruct)
203+ ptr.MyInt = 9
204+ println (" ptr.MyInt:" , ptr.MyInt )
205+ deref := *ptr
206+ deref.MyInt = 8
207+ println (" After deref assign, ptr.MyInt:" , ptr.MyInt , " deref.MyInt:" , deref.MyInt )
208+
209+ // Method calls on pointer receiver
210+ ptr.myBool = true
211+ println (" ptr.GetMyBool():" , ptr.GetMyBool ())
212+
213+ // Composite literal assignment
214+ comp := MyStruct{MyInt: 100 , MyString: " composite" , myBool: false }
215+ println (" comp fields:" , comp.MyInt , comp.GetMyString (), comp.GetMyBool ())
216+
217+ // Multiple return values and blank identifier
218+ x , _ := vals ()
219+ _ , y := vals ()
220+ println (" vals x:" , x, " y:" , y)
221+
222+ // If/else
223+ if a > b {
224+ println (" If branch: a>b" )
225+ } else {
226+ println (" Else branch: a<=b" )
227+ }
228+
229+ // Switch statement
230+ switch a {
231+ case 10 :
232+ println (" Switch case 10" )
233+ default :
234+ println (" Switch default" )
235+ }
236+ }
237+ ```
238+
239+ Generated with ` goscript compile . ` :
240+
241+ ``` typescript
242+ import * as goscript from " @go/builtin" ;
243+
244+ class MyStruct {
245+ // MyInt is a public integer field, initialized to zero.
246+ public MyInt: number = 0 ;
247+ // MyString is a public string field, initialized to empty string.
248+ public MyString: string = " " ;
249+ // myBool is a private boolean field, initialized to false.
250+ private myBool: boolean = false ;
251+
252+ // GetMyString returns the MyString field.
253+ public GetMyString(): string {
254+ const m = this
255+ return m .MyString
256+ }
257+
258+ // GetMyBool returns the myBool field.
259+ public GetMyBool(): boolean {
260+ const m = this
261+ return m .myBool
262+ }
263+
264+ constructor (init ? : Partial <MyStruct >) { if (init ) Object .assign (this , init as any ); }
265+ public clone(): MyStruct { return Object .assign (Object .create (MyStruct .prototype ) as MyStruct , this ); }
266+ }
267+
268+ // NewMyStruct creates a new MyStruct instance.
269+ export function NewMyStruct(s : string ): MyStruct {
270+ return new MyStruct ({ MyString: s })
271+ }
272+
273+ function vals(): [number , number ] {
274+ return [1 , 2 ]
275+ }
276+
277+ export function main(): void {
278+ console .log (" Hello from GoScript example!" )
279+
280+ // Basic arithmetic
281+ let a = 10
282+ let b = 3
283+ console .log (" Addition:" , a + b , " Subtraction:" , a - b , " Multiplication:" , a * b , " Division:" , a / b , " Modulo:" , a % b )
284+
285+ // Boolean logic and comparisons
286+ console .log (" Logic &&:" , true && false , " ||:" , true || false , " !:!" , ! true )
287+ console .log (" Comparisons:" , a == b , a != b , a < b , a > b , a <= b , a >= b )
288+
289+ // string(rune) conversion
290+ let r: number = ' X' ;
291+ let s = String .fromCharCode (r )
292+ console .log (" string('X'):" , s )
293+
294+ // 'y'
295+ let r2: number = 121 ;
296+ let s2 = String .fromCharCode (r2 )
297+ console .log (" string(121):" , s2 )
298+
299+ // '√'
300+ let r3: number = 0x221A ;
301+ let s3 = String .fromCharCode (r3 )
302+ console .log (" string(0x221A):" , s3 )
303+
304+ // Arrays
305+ let arr = [1 , 2 , 3 ]
306+ console .log (" Array elements:" , arr [0 ], arr [1 ], arr [2 ])
307+
308+ // Slices and range loop
309+ let slice = [4 , 5 , 6 ]
310+ console .log (" Slice elements:" , slice [0 ], slice [1 ], slice [2 ])
311+ let sum = 0
312+ for (let idx = 0 ; idx < slice .length ; idx ++ ) {
313+ const val = slice [idx ]
314+ {
315+ sum += val
316+ console .log (" Range idx:" , idx , " val:" , val )
317+ }
318+ }
319+ console .log (" Range sum:" , sum )
320+
321+ // Basic for loop
322+ let prod = 1
323+ for (let i = 1 ; i <= 3 ; i ++ ) {
324+ prod *= i
325+ }
326+ console .log (" Product via for:" , prod )
327+
328+ // Struct, pointers, copy independence
329+ let instance = NewMyStruct (" go-script" ).clone ()
330+ console .log (" instance.MyString:" , instance .GetMyString ())
331+ instance .MyInt = 42
332+ let copyInst = instance .clone ()
333+ copyInst .MyInt = 7
334+ console .log (" instance.MyInt:" , instance .MyInt , " copyInst.MyInt:" , copyInst .MyInt )
335+
336+ // Pointer initialization and dereference assignment
337+ let ptr = new (MyStruct )
338+ ptr .MyInt = 9
339+ console .log (" ptr.MyInt:" , ptr .MyInt )
340+ let deref = ptr .clone ()
341+ deref .MyInt = 8
342+ console .log (" After deref assign, ptr.MyInt:" , ptr .MyInt , " deref.MyInt:" , deref .MyInt )
343+
344+ // Method calls on pointer receiver
345+ ptr .myBool = true
346+ console .log (" ptr.GetMyBool():" , ptr .GetMyBool ())
347+
348+ // Composite literal assignment
349+ let comp = new MyStruct ({ MyInt: 100 , MyString: " composite" , myBool: false }).clone ()
350+ console .log (" comp fields:" , comp .MyInt , comp .GetMyString (), comp .GetMyBool ())
351+
352+ // Multiple return values and blank identifier
353+ let [x, ] = vals ()
354+ let [, y] = vals ()
355+ console .log (" vals x:" , x , " y:" , y )
356+
357+ // If/else
358+ if (a > b ) {
359+ console .log (" If branch: a>b" )
360+ } else {
361+ console .log (" Else branch: a<=b" )
362+ }
363+
364+ // Switch statement
365+ switch (a ) {
366+ case 10 :
367+ console .log (" Switch case 10" )
368+ break
369+ default :
370+ console .log (" Switch default" )
371+ break
372+ }
373+ }
374+ ```
375+
376+ Code is compiled with ` GOARCH=js ` and uses a 32-bit environment similar to wasm.
377+
378+ All Go import paths are prefixed with ` @go/ ` and can be imported in TypeScript:
379+
380+ ``` typescript
381+ import { MyFunction , MyStruct } from ' @go/github.com/myorg/mypackage' ;
382+
383+ MyFunction ();
384+ let myThing = new MyStruct ();
385+ myThing .DoSometing ();
386+ ```
387+
388+ ## License
389+
390+ MIT
0 commit comments