This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlibrary.go
281 lines (243 loc) · 6.43 KB
/
library.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2011 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.
package main
import (
"fmt"
"go/ast"
"regexp"
"strings"
)
// JavaScript library name.
const LIB_RESERVED_NAME = "g"
// Reserved Words in JavaScript.
//
// https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words
// http://golang.org/ref/spec#Keywords
var reserved = map[string]struct{}{
/* These ones are reserved too in Go.
"break": void,
"case": void,
"const": void,
"continue": void,
"default": void,
"delete": void,
"else": void,
"false": void,
"for": void,
"if": void,
"import": void,
"interface": void,
"package": void,
"return": void,
"switch": void,
"true": void,
"var": void,*/
"catch": void,
"debugger": void,
"do": void,
"finally": void,
"function": void,
"in": void,
"instanceof": void,
"new": void,
"this": void,
"throw": void,
"try": void,
"typeof": void,
"void": void,
"while": void,
"with": void,
"class": void,
"enum": void,
"export": void,
"extends": void,
"implements": void,
"let": void,
"null": void,
"private": void,
"protected": void,
"public": void,
"static": void,
"super": void,
"yield": void,
}
var validImport = []string{"fmt", "math", "rand"}
// Constants to translate.
var Constant = map[string]string{
"math.E": "Math.E",
"math.Ln2": "Math.LN2",
"math.Log2E": "Math.LOG2E",
"math.Ln10": "Math.LN10",
"math.Log10E": "Math.LOG10E",
"math.Pi": "Math.PI",
"math.Sqrt2": "Math.SQRT2",
}
// Functions that can be translated since JavaScript has an equivalent one.
var Function = map[string]string{
"print": "console.error", // since print/println is used in Go to debug
"println": "console.error",
"fmt.Print": "console.log",
"fmt.Println": "console.log",
"fmt.Printf": "console.log",
"fmt.Sprint": "",
"fmt.Sprintf": "",
"math.Abs": "Math.abs",
"math.Acos": "Math.acos",
"math.Asin": "Math.asin",
"math.Atan": "Math.atan",
"math.Atan2": "Math.atan2",
"math.Ceil": "Math.ceil",
"math.Cos": "Math.cos",
"math.Exp": "Math.exp",
"math.Floor": "Math.floor",
"math.Log": "Math.log",
"math.Max": "Math.max",
"math.Min": "Math.min",
"math.Pow": "Math.pow",
"math.Sin": "Math.sin",
"math.Sqrt": "Math.sqrt",
"math.Tan": "Math.tan",
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/round
//"math.": "Math.round",
"rand.Float32": "Math.random",
"rand.Float64": "Math.random",
}
var Char = map[int]string{'\n': "\\n", '\t': "\\t"}
// Imports
//
// http://golang.org/doc/go_spec.html#Import_declarations
// https://developer.mozilla.org/en/JavaScript/Reference/Statements/import
// getImport translates an import sentence.
func (tr *translation) getImport(spec []ast.Spec) {
// godoc go/ast ImportSpec
// Doc *CommentGroup // associated documentation; or nil
// Name *Ident // local package name (including "."); or nil
// Path *BasicLit // import path
// Comment *CommentGroup // line comments; or nil
// EndPos token.Pos // end of spec (overrides Path.Pos if nonzero)
for _, v := range spec {
iSpec := v.(*ast.ImportSpec)
path := strings.Replace(iSpec.Path.Value, "\"", "", -1)
// Core library
if !strings.Contains(path, ".") {
found := false
for _, v := range validImport {
if v == path {
found = true
break
}
}
if !found {
tr.addError("%s: import from core library", path)
continue
}
}
//import objectName.*;
//fmt.Println(iSpec.Name, pathDir)
}
}
// GetArgs returns the arguments of a Go function, formatted for JS.
func (tr *translation) GetArgs(funcName string, args []ast.Expr) string {
var jsArgs string
switch funcName {
case "print", "fmt.Print", "fmt.Sprint":
jsArgs = tr.joinArgsPrint(args, false)
case "println", "fmt.Println":
jsArgs = tr.joinArgsPrint(args, true)
case "fmt.Printf", "fmt.Sprintf":
jsArgs = tr.joinArgsPrintf(args)
default:
for i, v := range args {
if i != 0 {
jsArgs += "," + SP
}
jsArgs += tr.getExpression(v).String()
}
}
return jsArgs
}
// == Utility
//
// validIdent checks if the name is a reserved word in JavaScript, returning a
// safe name adding "_" at the end of the name.
// It checks also if the name is the JavaScript library.
func validIdent(name interface{}) string {
name_ := fmt.Sprintf("%s", name)
if Bootstrap {
return name_
}
if _, ok := reserved[name_]; ok {
return name_ + "_"
}
if name_ == LIB_RESERVED_NAME {
return name_ + "_"
}
return name_
}
// joinArgsPrint returns arguments of Print, Println.
func (tr *translation) joinArgsPrint(args []ast.Expr, addLine bool) string {
var jsArgs string
lenArgs := len(args) - 1
// Appends a character.
add := func(s, char string) string {
if strings.HasSuffix(s, "\"") {
s = s[:len(s)-1] + char + "\""
} else {
s += SP + "+" + SP + "\"" + char + "\""
}
return s
}
for i, v := range args {
expr := tr.getExpression(v).String()
if i != 0 {
jsArgs += SP + "+" + SP + expr
} else {
jsArgs = expr
}
if addLine {
if i == lenArgs {
jsArgs = add(jsArgs, Char['\n'])
} else {
jsArgs = add(jsArgs, " ")
}
}
}
return jsArgs
}
// Matches verbs for "fmt.Printf"
// http://golang.org/pkg/fmt/
var (
reVerb = regexp.MustCompile(`%[+\-# 0]?[bcdefgopqstvxEGTUX]`)
reVerbWidth = regexp.MustCompile(`%[0-9]+[.]?[0-9]*[bcdefgoqxEGUXsqxX]`)
)
// joinArgsPrintf returns arguments of Printf.
func (tr *translation) joinArgsPrintf(args []ast.Expr) string {
result := ""
// == Format
format := tr.getExpression(args[0]).String()
format = strings.Replace(format, "%%", "%", -1) // literal percent sign
format = reVerb.ReplaceAllString(format, VERB)
if reVerbWidth.MatchString(format) {
format = reVerbWidth.ReplaceAllString(format, VERB)
}
// ==
values := strings.Split(format, VERB)
for i, v := range args[1:] {
if i != 0 {
result += fmt.Sprintf("%s+%s\"", SP, SP)
}
if values[i] != `"` {
result += fmt.Sprintf("%s+%s", values[i]+`"`+SP, SP)
}
result += tr.getExpression(v).String()
}
// Last value
last := values[len(values)-1]
if last != `"` {
result += fmt.Sprintf("%s+%s", SP, SP+`"`+last)
}
return result
}