forked from elliotchance/c2go
/
literals.go
70 lines (55 loc) · 1.58 KB
/
literals.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
// This file contains transpiling functions for literals and constants. Literals
// are single values like 123 or "hello".
package transpiler
import (
"fmt"
"go/token"
goast "go/ast"
"strconv"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/util"
)
func transpileFloatingLiteral(n *ast.FloatingLiteral) *goast.BasicLit {
return &goast.BasicLit{
Kind: token.FLOAT,
Value: fmt.Sprintf("%f", n.Value),
}
}
func transpileStringLiteral(n *ast.StringLiteral) goast.Expr {
return util.NewCallExpr("[]byte",
util.NewStringLit(strconv.Quote(n.Value+"\x00")))
}
func transpileIntegerLiteral(n *ast.IntegerLiteral) *goast.BasicLit {
return &goast.BasicLit{
Kind: token.INT,
Value: n.Value,
}
}
func transpileCharacterLiteral(n *ast.CharacterLiteral) *goast.BasicLit {
return &goast.BasicLit{
Kind: token.CHAR,
Value: fmt.Sprintf("%q", n.Value),
}
}
func transpilePredefinedExpr(n *ast.PredefinedExpr, p *program.Program) (*goast.BasicLit, string, error) {
// A predefined expression is a literal that is not given a value until
// compile time.
//
// TODO: Predefined expressions are not evaluated
// https://github.com/elliotchance/c2go/issues/81
var value string
switch n.Name {
case "__PRETTY_FUNCTION__":
value = "[]byte(\"void print_number(int *)\")"
case "__func__":
value = fmt.Sprintf("[]byte(\"%s\")", p.Function.Name)
default:
// There are many more.
panic(fmt.Sprintf("unknown PredefinedExpr: %s", n.Name))
}
return &goast.BasicLit{
Kind: token.STRING,
Value: value,
}, "const char*", nil
}