LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style | |
3 // license that can be found in the LICENSE file. | |
4 | |
5 package eval | |
6 | |
7 import ( | |
8 "fmt" | |
9 "go/scanner" | |
10 "go/token" | |
11 ) | |
12 | |
13 | |
14 // A compiler captures information used throughout an entire | |
15 // compilation. Currently it includes only the error handler. | |
16 // | |
17 // TODO(austin) This might actually represent package level, in which | |
18 // case it should be package compiler. | |
19 type compiler struct { | |
20 fset *token.FileSet | |
21 errors scanner.ErrorHandler | |
22 numErrors int | |
23 silentErrors int | |
24 } | |
25 | |
26 func (a *compiler) diagAt(pos token.Pos, format string, args ...interface{}) { | |
27 a.errors.Error(a.fset.Position(pos), fmt.Sprintf(format, args...)) | |
28 a.numErrors++ | |
29 } | |
30 | |
31 func (a *compiler) numError() int { return a.numErrors + a.silentErrors } | |
32 | |
33 // The universal scope | |
34 func newUniverse() *Scope { | |
35 sc := &Scope{nil, 0} | |
36 sc.block = &block{ | |
37 offset: 0, | |
38 scope: sc, | |
39 global: true, | |
40 defs: make(map[string]Def), | |
41 } | |
42 return sc | |
43 } | |
44 | |
45 var universe *Scope = newUniverse() | |
46 | |
47 | |
48 // TODO(austin) These can all go in stmt.go now | |
49 type label struct { | |
50 name string | |
51 desc string | |
52 // The PC goto statements should jump to, or nil if this label | |
53 // cannot be goto'd (such as an anonymous for loop label). | |
54 gotoPC *uint | |
55 // The PC break statements should jump to, or nil if a break | |
56 // statement is invalid. | |
57 breakPC *uint | |
58 // The PC continue statements should jump to, or nil if a | |
59 // continue statement is invalid. | |
60 continuePC *uint | |
61 // The position where this label was resolved. If it has not | |
62 // been resolved yet, an invalid position. | |
63 resolved token.Pos | |
64 // The position where this label was first jumped to. | |
65 used token.Pos | |
66 } | |
67 | |
68 // A funcCompiler captures information used throughout the compilation | |
69 // of a single function body. | |
70 type funcCompiler struct { | |
71 *compiler | |
72 fnType *FuncType | |
73 // Whether the out variables are named. This affects what | |
74 // kinds of return statements are legal. | |
75 outVarsNamed bool | |
76 *codeBuf | |
77 flow *flowBuf | |
78 labels map[string]*label | |
79 } | |
80 | |
81 // A blockCompiler captures information used throughout the compilation | |
82 // of a single block within a function. | |
83 type blockCompiler struct { | |
84 *funcCompiler | |
85 block *block | |
86 // The label of this block, used for finding break and | |
87 // continue labels. | |
88 label *label | |
89 // The blockCompiler for the block enclosing this one, or nil | |
90 // for a function-level block. | |
91 parent *blockCompiler | |
92 } | |
LEFT | RIGHT |