source: code/trunk/vendor/modernc.org/ccgo/v3/lib/design-notes.adoc@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 2.8 KB
Line 
1= Design Notes
2
3== Problems:
4
5Translating C to Go is harder than it looks.
6
7Jan says: It's impossible in the general case to turn C char* into Go
8[]byte. It's possible to do it probably often for concrete C code
9cases - based also on author's C coding style. The first problem this
10runs into is that Go does not guarantee that the backing array will
11keep its address stable due to Go movable stacks. C expects the
12opposite, a pointer never magically modifies itself, so some code will
13fail.
14
15INSERT CODE EXAMPLES ILLUSTRATING THE PROBLEM HERE
16
17== How the parser works
18
19There are no comment nodes in the C AST. Instead every cc.Token has a
20Sep field: https://godoc.org/modernc.org/cc/v3#Token
21
22It captures, when configured to do so, all white space preceding the
23token, combined, including comments, if any. So we have all white
24space/comments information for every token in the AST. A final white
25space/comment, preceding EOF, is available as field TrailingSeperator
26in the AST: https://godoc.org/modernc.org/cc/v3#AST.
27
28To get the lexically first white space/comment for any node, use
29tokenSeparator():
30https://gitlab.com/cznic/ccgo/-/blob/6551e2544a758fdc265c8fac71fb2587fb3e1042/v3/go.go#L1476
31
32The same with a default value is comment():
33https://gitlab.com/cznic/ccgo/-/blob/6551e2544a758fdc265c8fac71fb2587fb3e1042/v3/go.go#L1467
34
35== Looking forward
36
37Eric says: In my visualization of how the translator would work, the
38output of a ccgo translation of a module at any given time is a file
39of pseudo-Go code in which some sections may be enclosed by a Unicode
40bracketing character (presently using the guillemot quotes U+ab and
41U+bb) meaning "this is not Go yet" that intentionally makes the Go
42compiler barf. This expresses a color on the AST nodes.
43
44So, for example, if I'm translating hello.c with a ruleset that does not
45include print -> fmt.Printf, this:
46
47---------------------------------------------------------
48#include <stdio>
49
50/* an example comment */
51
52int main(int argc, char *argv[])
53{
54 printf("Hello, World")
55}
56---------------------------------------------------------
57
58becomes this without any explicit rules at all:
59
60---------------------------------------------------------
61«#include <stdio>»
62
63/* an example comment */
64
65func main
66{
67 «printf(»"Hello, World"!\n"«)»
68}
69---------------------------------------------------------
70
71Then, when the rule print -> fmt.Printf is added, it becomes
72
73---------------------------------------------------------
74import (
75 "fmt"
76)
77
78/* an example comment */
79
80func main
81{
82 fmt.Printf("Hello, World"!\n")
83}
84---------------------------------------------------------
85
86because with that rule the AST node corresponding to the printf
87call can be translated and colored "Go". This implies an import
88of fmt. We observe that there are no longer C-colored spans
89and drop the #includes.
90
91// end
Note: See TracBrowser for help on using the repository browser.