goscript 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/.aider-prompt +11 -0
  2. package/LICENSE +21 -0
  3. package/README.md +427 -0
  4. package/builtin/builtin.ts +507 -0
  5. package/cmd/goscript/cmd_compile.go +59 -0
  6. package/cmd/goscript/main.go +23 -0
  7. package/compiler/compile.go +183 -0
  8. package/compiler/compile_comment.go +41 -0
  9. package/compiler/compile_decls.go +72 -0
  10. package/compiler/compile_expr.go +831 -0
  11. package/compiler/compile_field.go +89 -0
  12. package/compiler/compile_spec.go +256 -0
  13. package/compiler/compile_stmt.go +1509 -0
  14. package/compiler/compiler.go +81 -0
  15. package/compiler/config.go +32 -0
  16. package/compiler/context.go +9 -0
  17. package/compiler/file_compiler.go +80 -0
  18. package/compiler/output_path.go +31 -0
  19. package/compiler/pkg_compiler.go +73 -0
  20. package/compiler/writer.go +90 -0
  21. package/compliance/COMPLIANCE.md +133 -0
  22. package/compliance/compliance.go +313 -0
  23. package/compliance/compliance_test.go +57 -0
  24. package/compliance/tests/array_literal/array_literal.go +15 -0
  25. package/compliance/tests/array_literal/array_literal.gs.ts +19 -0
  26. package/compliance/tests/array_literal/expected.log +3 -0
  27. package/compliance/tests/async_basic/async_basic.go +26 -0
  28. package/compliance/tests/async_basic/async_basic.gs.ts +30 -0
  29. package/compliance/tests/async_basic/expected.log +1 -0
  30. package/compliance/tests/basic_arithmetic/basic_arithmetic.go +15 -0
  31. package/compliance/tests/basic_arithmetic/basic_arithmetic.gs.ts +19 -0
  32. package/compliance/tests/basic_arithmetic/expected.log +5 -0
  33. package/compliance/tests/boolean_logic/boolean_logic.go +13 -0
  34. package/compliance/tests/boolean_logic/boolean_logic.gs.ts +17 -0
  35. package/compliance/tests/boolean_logic/expected.log +3 -0
  36. package/compliance/tests/channel_basic/channel_basic.go +12 -0
  37. package/compliance/tests/channel_basic/channel_basic.gs.ts +18 -0
  38. package/compliance/tests/channel_basic/expected.log +1 -0
  39. package/compliance/tests/composite_literal_assignment/composite_literal_assignment.go +20 -0
  40. package/compliance/tests/composite_literal_assignment/composite_literal_assignment.gs.ts +27 -0
  41. package/compliance/tests/composite_literal_assignment/expected.log +2 -0
  42. package/compliance/tests/constants/constants.go +18 -0
  43. package/compliance/tests/constants/constants.gs.ts +22 -0
  44. package/compliance/tests/constants/expected.log +3 -0
  45. package/compliance/tests/copy_independence/copy_independence.go +29 -0
  46. package/compliance/tests/copy_independence/copy_independence.gs.ts +36 -0
  47. package/compliance/tests/copy_independence/expected.log +4 -0
  48. package/compliance/tests/float64/expected.log +6 -0
  49. package/compliance/tests/float64/float64.go +28 -0
  50. package/compliance/tests/float64/float64.gs.ts +32 -0
  51. package/compliance/tests/for_loop_basic/expected.log +5 -0
  52. package/compliance/tests/for_loop_basic/for_loop_basic.go +9 -0
  53. package/compliance/tests/for_loop_basic/for_loop_basic.gs.ts +13 -0
  54. package/compliance/tests/for_loop_condition_only/expected.log +5 -0
  55. package/compliance/tests/for_loop_condition_only/main.go +9 -0
  56. package/compliance/tests/for_loop_condition_only/main.gs.ts +13 -0
  57. package/compliance/tests/for_range/expected.log +9 -0
  58. package/compliance/tests/for_range/for_range.go +26 -0
  59. package/compliance/tests/for_range/for_range.gs.ts +45 -0
  60. package/compliance/tests/for_range_index_use/expected.log +6 -0
  61. package/compliance/tests/for_range_index_use/for_range_index_use.go +11 -0
  62. package/compliance/tests/for_range_index_use/for_range_index_use.gs.ts +18 -0
  63. package/compliance/tests/func_literal/expected.log +1 -0
  64. package/compliance/tests/func_literal/func_literal.go +10 -0
  65. package/compliance/tests/func_literal/func_literal.gs.ts +15 -0
  66. package/compliance/tests/function_call_result_assignment/expected.log +2 -0
  67. package/compliance/tests/function_call_result_assignment/function_call_result_assignment.go +24 -0
  68. package/compliance/tests/function_call_result_assignment/function_call_result_assignment.gs.ts +31 -0
  69. package/compliance/tests/if_statement/expected.log +1 -0
  70. package/compliance/tests/if_statement/if_statement.go +11 -0
  71. package/compliance/tests/if_statement/if_statement.gs.ts +15 -0
  72. package/compliance/tests/interface_to_interface_type_assertion/expected.log +1 -0
  73. package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.go +30 -0
  74. package/compliance/tests/interface_to_interface_type_assertion/interface_to_interface_type_assertion.gs.ts +41 -0
  75. package/compliance/tests/interface_type_assertion/expected.log +1 -0
  76. package/compliance/tests/interface_type_assertion/interface_type_assertion.go +26 -0
  77. package/compliance/tests/interface_type_assertion/interface_type_assertion.gs.ts +36 -0
  78. package/compliance/tests/map_support/expected.log +13 -0
  79. package/compliance/tests/map_support/map_support.go +89 -0
  80. package/compliance/tests/map_support/map_support.gs.ts +102 -0
  81. package/compliance/tests/method_call_on_pointer_receiver/expected.log +1 -0
  82. package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.go +19 -0
  83. package/compliance/tests/method_call_on_pointer_receiver/method_call_on_pointer_receiver.gs.ts +27 -0
  84. package/compliance/tests/method_call_on_pointer_via_value/expected.log +1 -0
  85. package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.go +29 -0
  86. package/compliance/tests/method_call_on_pointer_via_value/method_call_on_pointer_via_value.gs.ts +38 -0
  87. package/compliance/tests/method_call_on_value_receiver/expected.log +1 -0
  88. package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.go +16 -0
  89. package/compliance/tests/method_call_on_value_receiver/method_call_on_value_receiver.gs.ts +24 -0
  90. package/compliance/tests/method_call_on_value_via_pointer/expected.log +2 -0
  91. package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.go +30 -0
  92. package/compliance/tests/method_call_on_value_via_pointer/method_call_on_value_via_pointer.gs.ts +38 -0
  93. package/compliance/tests/multiple_return_values/expected.log +6 -0
  94. package/compliance/tests/multiple_return_values/multiple_return_values.go +19 -0
  95. package/compliance/tests/multiple_return_values/multiple_return_values.gs.ts +23 -0
  96. package/compliance/tests/pointer_assignment_no_copy/expected.log +2 -0
  97. package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.go +28 -0
  98. package/compliance/tests/pointer_assignment_no_copy/pointer_assignment_no_copy.gs.ts +35 -0
  99. package/compliance/tests/pointer_composite_literal_assignment/expected.log +3 -0
  100. package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.go +23 -0
  101. package/compliance/tests/pointer_composite_literal_assignment/pointer_composite_literal_assignment.gs.ts +30 -0
  102. package/compliance/tests/pointer_deref_multiassign/expected.log +0 -0
  103. package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.go +17 -0
  104. package/compliance/tests/pointer_deref_multiassign/pointer_deref_multiassign.gs.ts +27 -0
  105. package/compliance/tests/pointer_initialization/expected.log +1 -0
  106. package/compliance/tests/pointer_initialization/pointer_initialization.go +16 -0
  107. package/compliance/tests/pointer_initialization/pointer_initialization.gs.ts +22 -0
  108. package/compliance/tests/select_receive_on_closed_channel_no_default/expected.log +1 -0
  109. package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.go +15 -0
  110. package/compliance/tests/select_receive_on_closed_channel_no_default/select_receive_on_closed_channel_no_default.gs.ts +31 -0
  111. package/compliance/tests/select_send_on_full_buffered_channel_with_default/expected.log +1 -0
  112. package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.go +13 -0
  113. package/compliance/tests/select_send_on_full_buffered_channel_with_default/select_send_on_full_buffered_channel_with_default.gs.ts +35 -0
  114. package/compliance/tests/select_statement/expected.log +9 -0
  115. package/compliance/tests/select_statement/select_statement.go +109 -0
  116. package/compliance/tests/select_statement/select_statement.gs.ts +239 -0
  117. package/compliance/tests/simple/expected.log +1 -0
  118. package/compliance/tests/simple/simple.go +5 -0
  119. package/compliance/tests/simple/simple.gs.ts +9 -0
  120. package/compliance/tests/simple_deref_assignment/expected.log +2 -0
  121. package/compliance/tests/simple_deref_assignment/simple_deref_assignment.go +19 -0
  122. package/compliance/tests/simple_deref_assignment/simple_deref_assignment.gs.ts +26 -0
  123. package/compliance/tests/slices/expected.log +7 -0
  124. package/compliance/tests/slices/slices.go +22 -0
  125. package/compliance/tests/slices/slices.gs.ts +26 -0
  126. package/compliance/tests/string_rune_conversion/expected.log +3 -0
  127. package/compliance/tests/string_rune_conversion/string_rune_conversion.go +16 -0
  128. package/compliance/tests/string_rune_conversion/string_rune_conversion.gs.ts +22 -0
  129. package/compliance/tests/struct_field_access/expected.log +2 -0
  130. package/compliance/tests/struct_field_access/struct_field_access.go +13 -0
  131. package/compliance/tests/struct_field_access/struct_field_access.gs.ts +20 -0
  132. package/compliance/tests/struct_value_init_clone/expected.log +5 -0
  133. package/compliance/tests/struct_value_init_clone/struct_value_init_clone.go +28 -0
  134. package/compliance/tests/struct_value_init_clone/struct_value_init_clone.gs.ts +35 -0
  135. package/compliance/tests/switch_statement/expected.log +14 -0
  136. package/compliance/tests/switch_statement/switch_statement.go +59 -0
  137. package/compliance/tests/switch_statement/switch_statement.gs.ts +85 -0
  138. package/compliance/tests/value_type_copy_behavior/expected.log +3 -0
  139. package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.go +25 -0
  140. package/compliance/tests/value_type_copy_behavior/value_type_copy_behavior.gs.ts +34 -0
  141. package/design/DESIGN.md +599 -0
  142. package/example/simple/build.bash +10 -0
  143. package/example/simple/go.mod +23 -0
  144. package/example/simple/go.sum +39 -0
  145. package/example/simple/main.go +138 -0
  146. package/example/simple/main.gs.ts +133 -0
  147. package/example/simple/main.ts +3 -0
  148. package/example/simple/main_test.go +59 -0
  149. package/example/simple/main_tools.go +5 -0
  150. package/example/simple/package.json +7 -0
  151. package/example/simple/run.bash +6 -0
  152. package/example/simple/tsconfig.json +28 -0
  153. package/example/simple/yarn.lock +8 -0
  154. package/go.mod +22 -0
  155. package/go.sum +39 -0
  156. package/output/output.go +10 -0
  157. package/package.json +14 -0
  158. package/tsconfig.json +10 -0
  159. package/types/tokens.go +65 -0
  160. package/types/types.go +46 -0
@@ -0,0 +1,138 @@
1
+ package main
2
+
3
+ // MyStruct demonstrates a simple struct with public and private fields.
4
+ // It will be converted into a TypeScript class by goscript.
5
+ type MyStruct struct {
6
+ // MyInt is a public integer field, initialized to zero.
7
+ MyInt int
8
+ // MyString is a public string field, initialized to empty string.
9
+ MyString string
10
+ // myBool is a private boolean field, initialized to false.
11
+ myBool bool
12
+ }
13
+
14
+ // GetMyString returns the MyString field.
15
+ func (m *MyStruct) GetMyString() string {
16
+ return m.MyString
17
+ }
18
+
19
+ // GetMyBool returns the myBool field.
20
+ func (m *MyStruct) GetMyBool() bool {
21
+ return m.myBool
22
+ }
23
+
24
+ // NewMyStruct creates a new MyStruct instance.
25
+ func NewMyStruct(s string) MyStruct {
26
+ return MyStruct{MyString: s}
27
+ }
28
+
29
+ func vals() (int, int) {
30
+ return 1, 2
31
+ }
32
+
33
+ func main() {
34
+ println("Hello from GoScript example!")
35
+
36
+ // Basic arithmetic
37
+ a, b := 10, 3
38
+ println("Addition:", a+b, "Subtraction:", a-b, "Multiplication:", a*b, "Division:", a/b, "Modulo:", a%b)
39
+
40
+ // Boolean logic and comparisons
41
+ println("Logic &&:", true && false, "||:", true || false, "!:!", !true)
42
+ println("Comparisons:", a == b, a != b, a < b, a > b, a <= b, a >= b)
43
+
44
+ // string(rune) conversion
45
+ var r rune = 'X'
46
+ s := string(r)
47
+ println("string('X'):", s)
48
+
49
+ var r2 rune = 121 // 'y'
50
+ s2 := string(r2)
51
+ println("string(121):", s2)
52
+
53
+ var r3 rune = 0x221A // '√'
54
+ s3 := string(r3)
55
+ println("string(0x221A):", s3)
56
+
57
+ // Arrays
58
+ arr := [3]int{1, 2, 3}
59
+ println("Array elements:", arr[0], arr[1], arr[2])
60
+
61
+ // Slices and range loop
62
+ slice := []int{4, 5, 6}
63
+ println("Slice elements:", slice[0], slice[1], slice[2])
64
+ sum := 0
65
+ for idx, val := range slice {
66
+ sum += val
67
+ println("Range idx:", idx, "val:", val)
68
+ }
69
+ println("Range sum:", sum)
70
+
71
+ // Basic for loop
72
+ prod := 1
73
+ for i := 1; i <= 3; i++ {
74
+ prod *= i
75
+ }
76
+ println("Product via for:", prod)
77
+
78
+ // Struct, pointers, copy independence
79
+ instance := NewMyStruct("go-script")
80
+ println("instance.MyString:", instance.GetMyString())
81
+ instance.MyInt = 42
82
+ copyInst := instance
83
+ copyInst.MyInt = 7
84
+ println("instance.MyInt:", instance.MyInt, "copyInst.MyInt:", copyInst.MyInt)
85
+
86
+ // Pointer initialization and dereference assignment
87
+ ptr := new(MyStruct)
88
+ ptr.MyInt = 9
89
+ println("ptr.MyInt:", ptr.MyInt)
90
+ deref := *ptr
91
+ deref.MyInt = 8
92
+ println("After deref assign, ptr.MyInt:", ptr.MyInt, "deref.MyInt:", deref.MyInt)
93
+
94
+ // Method calls on pointer receiver
95
+ ptr.myBool = true
96
+ println("ptr.GetMyBool():", ptr.GetMyBool())
97
+
98
+ // Composite literal assignment
99
+ comp := MyStruct{MyInt: 100, MyString: "composite", myBool: false}
100
+ println("comp fields:", comp.MyInt, comp.GetMyString(), comp.GetMyBool())
101
+
102
+ // Multiple return values and blank identifier
103
+ x, _ := vals()
104
+ _, y := vals()
105
+ println("vals x:", x, "y:", y)
106
+
107
+ // If/else
108
+ if a > b {
109
+ println("If branch: a>b")
110
+ } else {
111
+ println("Else branch: a<=b")
112
+ }
113
+
114
+ // Switch statement
115
+ switch a {
116
+ case 10:
117
+ println("Switch case 10")
118
+ default:
119
+ println("Switch default")
120
+ }
121
+ // Goroutines and Channels
122
+ println("\nGoroutines and Channels:")
123
+ ch := make(chan string)
124
+ go func() {
125
+ println("Goroutine: Sending message")
126
+ ch <- "Hello from goroutine!"
127
+ }()
128
+ msg := <-ch
129
+ println("Main goroutine: Received message:", msg)
130
+
131
+ // Function Literals
132
+ println("\nFunction Literals:")
133
+ add := func(x, y int) int {
134
+ return x + y
135
+ }
136
+ sum = add(5, 7)
137
+ println("Function literal result:", sum)
138
+ }
@@ -0,0 +1,133 @@
1
+ import * as goscript from "@go/builtin";
2
+
3
+ class MyStruct {
4
+ // MyInt is a public integer field, initialized to zero.
5
+ public MyInt: number = 0;
6
+ // MyString is a public string field, initialized to empty string.
7
+ public MyString: string = "";
8
+ // myBool is a private boolean field, initialized to false.
9
+ private myBool: boolean = false;
10
+
11
+ // GetMyString returns the MyString field.
12
+ public GetMyString(): string {
13
+ const m = this
14
+ return m.MyString
15
+ }
16
+
17
+ // GetMyBool returns the myBool field.
18
+ public GetMyBool(): boolean {
19
+ const m = this
20
+ return m.myBool
21
+ }
22
+
23
+ constructor(init?: Partial<MyStruct>) { if (init) Object.assign(this, init as any); }
24
+ public clone(): MyStruct { return Object.assign(Object.create(MyStruct.prototype) as MyStruct, this); }
25
+ }
26
+
27
+ // NewMyStruct creates a new MyStruct instance.
28
+ export function NewMyStruct(s: string): MyStruct {
29
+ return new MyStruct({ MyString: s })
30
+ }
31
+
32
+ function vals(): [number, number] {
33
+ return [1, 2]
34
+ }
35
+
36
+ export function main(): void {
37
+ console.log("Hello from GoScript example!")
38
+
39
+ // Basic arithmetic
40
+ let a = 10
41
+ let b = 3
42
+ console.log("Addition:", a + b, "Subtraction:", a - b, "Multiplication:", a * b, "Division:", a / b, "Modulo:", a % b)
43
+
44
+ // Boolean logic and comparisons
45
+ console.log("Logic &&:", true && false, "||:", true || false, "!:!", !true)
46
+ console.log("Comparisons:", a == b, a != b, a < b, a > b, a <= b, a >= b)
47
+
48
+ // string(rune) conversion
49
+ let r: number = 'X';
50
+ let s = String.fromCharCode(r)
51
+ console.log("string('X'):", s)
52
+
53
+ // 'y'
54
+ let r2: number = 121;
55
+ let s2 = String.fromCharCode(r2)
56
+ console.log("string(121):", s2)
57
+
58
+ // '√'
59
+ let r3: number = 0x221A;
60
+ let s3 = String.fromCharCode(r3)
61
+ console.log("string(0x221A):", s3)
62
+
63
+ // Arrays
64
+ let arr = [1, 2, 3]
65
+ console.log("Array elements:", arr[0], arr[1], arr[2])
66
+
67
+ // Slices and range loop
68
+ let slice = [4, 5, 6]
69
+ console.log("Slice elements:", slice[0], slice[1], slice[2])
70
+ let sum = 0
71
+ for (let idx = 0; idx < slice.length; idx++) {
72
+ const val = slice[idx]
73
+ {
74
+ sum += val
75
+ console.log("Range idx:", idx, "val:", val)
76
+ }
77
+ }
78
+ console.log("Range sum:", sum)
79
+
80
+ // Basic for loop
81
+ let prod = 1
82
+ for (let i = 1; i <= 3; i++) {
83
+ prod *= i
84
+ }
85
+ console.log("Product via for:", prod)
86
+
87
+ // Struct, pointers, copy independence
88
+ let instance = NewMyStruct("go-script").clone()
89
+ console.log("instance.MyString:", instance.GetMyString())
90
+ instance.MyInt = 42
91
+ let copyInst = instance.clone()
92
+ copyInst.MyInt = 7
93
+ console.log("instance.MyInt:", instance.MyInt, "copyInst.MyInt:", copyInst.MyInt)
94
+
95
+ // Pointer initialization and dereference assignment
96
+ let ptr = new(MyStruct)
97
+ ptr.MyInt = 9
98
+ console.log("ptr.MyInt:", ptr.MyInt)
99
+ let deref = ptr.clone()
100
+ deref.MyInt = 8
101
+ console.log("After deref assign, ptr.MyInt:", ptr.MyInt, "deref.MyInt:", deref.MyInt)
102
+
103
+ // Method calls on pointer receiver
104
+ ptr.myBool = true
105
+ console.log("ptr.GetMyBool():", ptr.GetMyBool())
106
+
107
+ // Composite literal assignment
108
+ let comp = new MyStruct({ MyInt: 100, MyString: "composite", myBool: false }).clone()
109
+ console.log("comp fields:", comp.MyInt, comp.GetMyString(), comp.GetMyBool())
110
+
111
+ // Multiple return values and blank identifier
112
+ let [x, ] = vals()
113
+ let [, y] = vals()
114
+ console.log("vals x:", x, "y:", y)
115
+
116
+ // If/else
117
+ if (a > b) {
118
+ console.log("If branch: a>b")
119
+ } else {
120
+ console.log("Else branch: a<=b")
121
+ }
122
+
123
+ // Switch statement
124
+ switch (a) {
125
+ case 10:
126
+ console.log("Switch case 10")
127
+ break
128
+ default:
129
+ console.log("Switch default")
130
+ break
131
+ }
132
+ }
133
+
@@ -0,0 +1,3 @@
1
+ import { main } from "@go/example/main.gs.js"
2
+
3
+ main()
@@ -0,0 +1,59 @@
1
+ package main_test
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "os/exec"
7
+ "path/filepath"
8
+ "testing"
9
+
10
+ "github.com/paralin/goscript/compiler"
11
+ "github.com/sirupsen/logrus"
12
+ )
13
+
14
+ func TestBuildRunExampleSimple(t *testing.T) {
15
+ // Set up paths
16
+ projectDir, err := filepath.Abs(".")
17
+ if err != nil {
18
+ t.Fatalf("failed to determine project directory: %v", err)
19
+ }
20
+ outputDir := filepath.Join(projectDir, "output")
21
+
22
+ // Initialize the compiler
23
+ logger := logrus.New()
24
+ logger.SetLevel(logrus.DebugLevel)
25
+ le := logrus.NewEntry(logger)
26
+
27
+ conf := &compiler.Config{
28
+ OutputPathRoot: outputDir,
29
+ }
30
+ if err := conf.Validate(); err != nil {
31
+ t.Fatalf("invalid compiler config: %v", err)
32
+ }
33
+
34
+ comp, err := compiler.NewCompiler(conf, le, nil)
35
+ if err != nil {
36
+ t.Fatalf("failed to create compiler: %v", err)
37
+ }
38
+
39
+ // Compile the package
40
+ if err := comp.CompilePackages(context.Background(), "."); err != nil {
41
+ t.Fatalf("compilation failed: %v", err)
42
+ }
43
+
44
+ // Log the compiled file
45
+ outFile, err := os.ReadFile(filepath.Join(outputDir, "@go/example/main.gs.ts"))
46
+ if err != nil {
47
+ t.Fatalf("failed to read output file: %v", err)
48
+ }
49
+ t.Log(string(outFile))
50
+
51
+ // Run the compiled TypeScript file
52
+ cmd := exec.Command("tsx", "--tsconfig", "./tsconfig.json", "./main.ts")
53
+ cmd.Dir = projectDir
54
+ cmd.Stdout = os.Stdout
55
+ cmd.Stderr = os.Stderr
56
+ if err := cmd.Run(); err != nil {
57
+ t.Fatalf("run failed: %v", err)
58
+ }
59
+ }
@@ -0,0 +1,5 @@
1
+ //go:build tools
2
+
3
+ package main
4
+
5
+ import _ "github.com/paralin/goscript/cmd/goscript"
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@example/simple",
3
+ "private": true,
4
+ "devDependencies": {
5
+ "typescript": "^5.4.2"
6
+ }
7
+ }
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+ set -eo pipefail
3
+
4
+ bash build.bash
5
+
6
+ tsx --tsconfig ./tsconfig.json ./main.ts
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "esnext",
4
+ "target": "esnext",
5
+ "moduleResolution": "node",
6
+ "jsx": "react",
7
+ "baseUrl": "./",
8
+ "paths": {
9
+ "@go/*": ["output/@go/*"],
10
+ "@go/builtin": ["../../builtin/builtin.ts"]
11
+ },
12
+ "allowSyntheticDefaultImports": true,
13
+ "declaration": true,
14
+ "esModuleInterop": true,
15
+ "forceConsistentCasingInFileNames": true,
16
+ "importsNotUsedAsValues": "remove",
17
+ "noEmit": true,
18
+ "resolveJsonModule": true,
19
+ "skipLibCheck": true,
20
+ "strict": true,
21
+ "lib": ["webworker", "dom", "ES6"]
22
+ },
23
+ "exclude": ["node_modules", "vendor", "dist"],
24
+ "ts-node": {
25
+ "esm": true,
26
+ "experimentalSpecifierResolution": true
27
+ }
28
+ }
@@ -0,0 +1,8 @@
1
+ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
+ # yarn lockfile v1
3
+
4
+
5
+ typescript@^5.4.2:
6
+ version "5.8.3"
7
+ resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.8.3.tgz#92f8a3e5e3cf497356f4178c34cd65a7f5e8440e"
8
+ integrity sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==
package/go.mod ADDED
@@ -0,0 +1,22 @@
1
+ module github.com/paralin/goscript
2
+
3
+ go 1.24
4
+
5
+ toolchain go1.24.2
6
+
7
+ require (
8
+ github.com/aperturerobotics/cli v1.0.0
9
+ github.com/pkg/errors v0.9.1
10
+ github.com/sanity-io/litter v1.5.8
11
+ github.com/sirupsen/logrus v1.9.3
12
+ golang.org/x/tools v0.32.0
13
+ )
14
+
15
+ require (
16
+ github.com/aperturerobotics/common v0.21.2 // indirect
17
+ github.com/stretchr/testify v1.10.0 // indirect
18
+ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
19
+ golang.org/x/mod v0.24.0 // indirect
20
+ golang.org/x/sync v0.13.0 // indirect
21
+ golang.org/x/sys v0.32.0 // indirect
22
+ )
package/go.sum ADDED
@@ -0,0 +1,39 @@
1
+ github.com/aperturerobotics/cli v1.0.0 h1:s3xT2h7eBih4/4yZKTn/HQ6P+qpk6ygWZl2416xAI1M=
2
+ github.com/aperturerobotics/cli v1.0.0/go.mod h1:wtlINjMcKuwyV1x4ftReuA6hHZcPB8kPMXHyQqGFCSc=
3
+ github.com/aperturerobotics/common v0.21.2 h1:fqnPL5Oovpd8nDaNBYGiD1UpZhcH/JfpsS8gt5iBDyA=
4
+ github.com/aperturerobotics/common v0.21.2/go.mod h1:FrecdNcsYvVS8RcWCR8FUkKFh+XmouFOYKHpBdMqqBA=
5
+ github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6
+ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7
+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8
+ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9
+ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
10
+ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11
+ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
12
+ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
13
+ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14
+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15
+ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16
+ github.com/sanity-io/litter v1.5.8 h1:uM/2lKrWdGbRXDrIq08Lh9XtVYoeGtcQxk9rtQ7+rYg=
17
+ github.com/sanity-io/litter v1.5.8/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
18
+ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
19
+ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
20
+ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
21
+ github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
22
+ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23
+ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
24
+ github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
25
+ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
26
+ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
27
+ golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
28
+ golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
29
+ golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
30
+ golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
31
+ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32
+ golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
33
+ golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
34
+ golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
35
+ golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
36
+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37
+ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38
+ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
39
+ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
@@ -0,0 +1,10 @@
1
+ package output
2
+
3
+ import (
4
+ "path/filepath"
5
+ )
6
+
7
+ // ComputeModulePath computes the root of the output typescript module.
8
+ func ComputeModulePath(outputRoot, goPkg string) string {
9
+ return filepath.Join(outputRoot, goPkg)
10
+ }
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "goscript",
3
+ "version": "0.0.2",
4
+ "description": "Go to TypeScript compiler",
5
+ "scripts": {
6
+ "example": "cd ./example/simple && bash run.bash",
7
+ "test": "go test -v ./..."
8
+ },
9
+ "devDependencies": {
10
+ "prettier": "^3.5.3",
11
+ "tsx": "^4.0.0",
12
+ "typescript": "^5.0.0"
13
+ }
14
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "target": "ES2022",
5
+ "lib": ["ES2020", "dom"],
6
+ "paths": {
7
+ "@go/builtin": ["./builtin/builtin.ts"]
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,65 @@
1
+ package types
2
+
3
+ import (
4
+ "go/token"
5
+ )
6
+
7
+ var tokenMap = map[token.Token]string{
8
+ token.ADD: "+",
9
+ token.SUB: "-",
10
+ token.MUL: "*",
11
+ token.QUO: "/",
12
+ token.REM: "%",
13
+ token.OR: "|",
14
+ token.XOR: "^",
15
+ token.SHL: "<<",
16
+ token.SHR: ">>",
17
+
18
+ token.ADD_ASSIGN: "+=",
19
+ token.SUB_ASSIGN: "-=",
20
+ token.MUL_ASSIGN: "*=",
21
+ token.QUO_ASSIGN: "/=",
22
+ token.REM_ASSIGN: "%=",
23
+
24
+ token.AND_ASSIGN: "&=",
25
+ token.OR_ASSIGN: "|=",
26
+ token.XOR_ASSIGN: "^=", // TODO: check if this works
27
+ token.SHL_ASSIGN: "<<=",
28
+ token.SHR_ASSIGN: ">>=",
29
+ token.AND_NOT_ASSIGN: "&^=",
30
+
31
+ token.LAND: "&&",
32
+ token.LOR: "||",
33
+ // token.ARROW: ""
34
+ token.INC: "++",
35
+ token.DEC: "--",
36
+ token.EQL: "==",
37
+ token.LSS: "<",
38
+ token.GTR: ">",
39
+ token.ASSIGN: "=",
40
+ token.NOT: "!",
41
+
42
+ token.NEQ: "!=",
43
+ token.LEQ: "<=",
44
+ token.GEQ: ">=",
45
+ token.DEFINE: "=", // :=
46
+ token.ELLIPSIS: "...", // TODO
47
+
48
+ token.LPAREN: "(",
49
+ token.LBRACK: "[",
50
+ token.LBRACE: "{",
51
+ token.COMMA: ",",
52
+ token.PERIOD: ".",
53
+
54
+ token.RPAREN: ")",
55
+ token.RBRACK: "]",
56
+ token.RBRACE: "}",
57
+ token.SEMICOLON: ";",
58
+ token.COLON: ":",
59
+ }
60
+
61
+ // TokenToTs looks up the typescript version of a token.
62
+ func TokenToTs(tok token.Token) (string, bool) {
63
+ t, ok := tokenMap[tok]
64
+ return t, ok
65
+ }
package/types/types.go ADDED
@@ -0,0 +1,46 @@
1
+ package types
2
+
3
+ // goToTypescriptPrimitives maps Go primitive types to their TypeScript equivalents.
4
+ //
5
+ // Assumptions:
6
+ // - Target environment is similar to GOOS=js GOARCH=wasm, where `int` and `uint` are 32 bits.
7
+ // - 32-bit Go integers fit safely within the JS/TypeScript `number` type.
8
+ // - 64-bit integers (`int64`, `uint64`) require TypeScript `bigint` (ES2020+).
9
+ // - Only primitive types are handled here. Composite types (pointers, slices, maps, structs, etc.)
10
+ // are not handled by this mapping.
11
+ var goToTypescriptPrimitives = map[string]string{
12
+ // Boolean
13
+ "bool": "boolean",
14
+
15
+ // Strings
16
+ "string": "string",
17
+
18
+ // Signed Integers
19
+ "int": "number",
20
+ "int8": "number",
21
+ "int16": "number",
22
+ "int32": "number",
23
+ "rune": "number", // alias for int32
24
+ "int64": "bigint", // Requires TypeScript target >= ES2020
25
+
26
+ // Unsigned Integers
27
+ "uint": "number",
28
+ "uint8": "number", // byte is an alias for uint8
29
+ "byte": "number",
30
+ "uint16": "number",
31
+ "uint32": "number",
32
+ "uint64": "bigint", // Requires TypeScript target >= ES2020
33
+
34
+ // Floating Point Numbers
35
+ "float32": "number",
36
+ "float64": "number",
37
+ }
38
+
39
+ // GoBuiltinToTypescript returns the TypeScript equivalent of a Go primitive type name.
40
+ // Returns the TypeScript type and true if found, or an empty string and false otherwise.
41
+ //
42
+ // Only primitive types listed in goToTypescriptPrimitives are handled.
43
+ func GoBuiltinToTypescript(typeName string) (string, bool) {
44
+ val, ok := goToTypescriptPrimitives[typeName]
45
+ return val, ok
46
+ }