Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
2527bba9
Commit
2527bba9
authored
Jan 16, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify pretty
R=r OCL=22899 CL=22899
parent
605d0746
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
203 additions
and
202 deletions
+203
-202
ast.go
usr/gri/pretty/ast.go
+76
-76
compilation.go
usr/gri/pretty/compilation.go
+17
-17
parser.go
usr/gri/pretty/parser.go
+0
-0
platform.go
usr/gri/pretty/platform.go
+10
-10
pretty.go
usr/gri/pretty/pretty.go
+2
-2
printer.go
usr/gri/pretty/printer.go
+0
-0
scanner.go
usr/gri/pretty/scanner.go
+0
-0
selftest0.go
usr/gri/pretty/selftest0.go
+1
-1
selftest2.go
usr/gri/pretty/selftest2.go
+9
-9
test.sh
usr/gri/pretty/test.sh
+3
-2
typechecker.go
usr/gri/pretty/typechecker.go
+13
-13
universe.go
usr/gri/pretty/universe.go
+72
-72
No files found.
usr/gri/pretty/ast.go
View file @
2527bba9
...
...
@@ -54,23 +54,23 @@ export func KindStr(kind int) string {
export
type
Object
struct
{
i
d
int
;
// unique id
I
d
int
;
// unique id
p
os
int
;
// source position (< 0 if unknown position)
k
ind
int
;
// object kind
i
dent
string
;
t
yp
*
Type
;
// nil for packages
p
nolev
int
;
// >= 0: package no., <= 0: function nesting level, 0: global level
P
os
int
;
// source position (< 0 if unknown position)
K
ind
int
;
// object kind
I
dent
string
;
T
yp
*
Type
;
// nil for packages
P
nolev
int
;
// >= 0: package no., <= 0: function nesting level, 0: global level
// attached values
block
*
array
.
Array
;
e
nd
int
;
// stats for function literals; end of block pos
Block
*
array
.
Array
;
E
nd
int
;
// stats for function literals; end of block pos
}
func
(
obj
*
Object
)
IsExported
()
bool
{
switch
obj
.
k
ind
{
switch
obj
.
K
ind
{
case
NONE
/* FUNC for now */
,
CONST
,
TYPE
,
VAR
,
FUNC
:
ch
,
size
:=
utf8
.
DecodeRuneInString
(
obj
.
i
dent
,
0
);
ch
,
size
:=
utf8
.
DecodeRuneInString
(
obj
.
I
dent
,
0
);
return
unicode
.
IsUpper
(
ch
);
}
return
false
;
...
...
@@ -78,18 +78,18 @@ func (obj *Object) IsExported() bool {
export
var
Universe_void_typ
*
Type
// initialized by Universe to Universe.void_typ
var
O
bjectId
int
;
var
o
bjectId
int
;
export
func
NewObject
(
pos
,
kind
int
,
ident
string
)
*
Object
{
obj
:=
new
(
Object
);
obj
.
id
=
O
bjectId
;
O
bjectId
++
;
obj
.
Id
=
o
bjectId
;
o
bjectId
++
;
obj
.
p
os
=
pos
;
obj
.
k
ind
=
kind
;
obj
.
i
dent
=
ident
;
obj
.
t
yp
=
Universe_void_typ
;
obj
.
p
nolev
=
0
;
obj
.
P
os
=
pos
;
obj
.
K
ind
=
kind
;
obj
.
I
dent
=
ident
;
obj
.
T
yp
=
Universe_void_typ
;
obj
.
P
nolev
=
0
;
return
obj
;
}
...
...
@@ -133,23 +133,23 @@ func (scope *Scope) Lookup(ident string) *Object {
}
func
(
scope
*
Scope
)
A
dd
(
obj
*
Object
)
{
scope
.
entries
[
obj
.
i
dent
]
=
obj
;
func
(
scope
*
Scope
)
a
dd
(
obj
*
Object
)
{
scope
.
entries
[
obj
.
I
dent
]
=
obj
;
}
func
(
scope
*
Scope
)
Insert
(
obj
*
Object
)
{
if
scope
.
LookupLocal
(
obj
.
i
dent
)
!=
nil
{
if
scope
.
LookupLocal
(
obj
.
I
dent
)
!=
nil
{
panic
(
"obj already inserted"
);
}
scope
.
A
dd
(
obj
);
scope
.
a
dd
(
obj
);
}
func
(
scope
*
Scope
)
InsertImport
(
obj
*
Object
)
*
Object
{
p
:=
scope
.
LookupLocal
(
obj
.
i
dent
);
p
:=
scope
.
LookupLocal
(
obj
.
I
dent
);
if
p
==
nil
{
scope
.
A
dd
(
obj
);
scope
.
a
dd
(
obj
);
p
=
obj
;
}
return
p
;
...
...
@@ -169,8 +169,8 @@ func (scope *Scope) Print() {
// All nodes have a source position and and token.
export
type
Node
struct
{
p
os
int
;
// source position (< 0 => unknown position)
t
ok
int
;
// identifying token
P
os
int
;
// source position (< 0 => unknown position)
T
ok
int
;
// identifying token
}
...
...
@@ -179,8 +179,8 @@ export type Node struct {
export
type
Expr
struct
{
Node
;
x
,
y
*
Expr
;
// binary (x, y) and unary (y
) expressions
o
bj
*
Object
;
X
,
Y
*
Expr
;
// binary (X, Y) and unary (Y
) expressions
O
bj
*
Object
;
}
...
...
@@ -189,7 +189,7 @@ func (x *Expr) Len() int {
return
0
;
}
n
:=
1
;
for
;
x
.
tok
==
Scanner
.
COMMA
;
x
=
x
.
y
{
for
;
x
.
Tok
==
Scanner
.
COMMA
;
x
=
x
.
Y
{
n
++
;
}
return
n
;
...
...
@@ -197,11 +197,11 @@ func (x *Expr) Len() int {
export
func
NewExpr
(
pos
,
tok
int
,
x
,
y
*
Expr
)
*
Expr
{
if
x
!=
nil
&&
x
.
tok
==
Scanner
.
TYPE
||
y
!=
nil
&&
y
.
t
ok
==
Scanner
.
TYPE
{
if
x
!=
nil
&&
x
.
Tok
==
Scanner
.
TYPE
||
y
!=
nil
&&
y
.
T
ok
==
Scanner
.
TYPE
{
panic
(
"no type expression allowed"
);
}
e
:=
new
(
Expr
);
e
.
pos
,
e
.
tok
,
e
.
x
,
e
.
y
=
pos
,
tok
,
x
,
y
;
e
.
Pos
,
e
.
Tok
,
e
.
X
,
e
.
Y
=
pos
,
tok
,
x
,
y
;
return
e
;
}
...
...
@@ -209,7 +209,7 @@ export func NewExpr(pos, tok int, x, y *Expr) *Expr {
// TODO probably don't need the tok parameter eventually
export
func
NewLit
(
tok
int
,
obj
*
Object
)
*
Expr
{
e
:=
new
(
Expr
);
e
.
pos
,
e
.
tok
,
e
.
obj
=
obj
.
p
os
,
tok
,
obj
;
e
.
Pos
,
e
.
Tok
,
e
.
Obj
=
obj
.
P
os
,
tok
,
obj
;
return
e
;
}
...
...
@@ -296,47 +296,47 @@ export const /* channel mode */ (
export
type
Type
struct
{
i
d
int
;
// unique id
I
d
int
;
// unique id
r
ef
int
;
// for exporting only: >= 0 means already exported
f
orm
int
;
// type form
s
ize
int
;
// size in bytes
o
bj
*
Object
;
// primary type object or NULL
s
cope
*
Scope
;
// forwards, structs, interfaces, functions
R
ef
int
;
// for exporting only: >= 0 means already exported
F
orm
int
;
// type form
S
ize
int
;
// size in bytes
O
bj
*
Object
;
// primary type object or NULL
S
cope
*
Scope
;
// forwards, structs, interfaces, functions
// syntactic components
p
os
int
;
// source position (< 0 if unknown position)
e
xpr
*
Expr
;
// type name, array length
m
ode
int
;
// channel mode
k
ey
*
Type
;
// receiver type or map key
e
lt
*
Type
;
// array, map, channel or pointer element type, function result type
list
*
array
.
Array
;
e
nd
int
;
// struct fields, interface methods, function parameters
s
cope
*
Scope
;
// struct fields, methods
P
os
int
;
// source position (< 0 if unknown position)
E
xpr
*
Expr
;
// type name, array length
M
ode
int
;
// channel mode
K
ey
*
Type
;
// receiver type or map key
E
lt
*
Type
;
// array, map, channel or pointer element type, function result type
List
*
array
.
Array
;
E
nd
int
;
// struct fields, interface methods, function parameters
S
cope
*
Scope
;
// struct fields, methods
}
var
T
ypeId
int
;
var
t
ypeId
int
;
export
func
NewType
(
pos
,
form
int
)
*
Type
{
typ
:=
new
(
Type
);
typ
.
id
=
T
ypeId
;
T
ypeId
++
;
typ
.
Id
=
t
ypeId
;
t
ypeId
++
;
typ
.
r
ef
=
-
1
;
// not yet exported
typ
.
p
os
=
pos
;
typ
.
f
orm
=
form
;
typ
.
R
ef
=
-
1
;
// not yet exported
typ
.
P
os
=
pos
;
typ
.
F
orm
=
form
;
return
typ
;
}
func
(
t
*
Type
)
n
fields
()
int
{
if
t
.
l
ist
==
nil
{
func
(
t
*
Type
)
N
fields
()
int
{
if
t
.
L
ist
==
nil
{
return
0
;
}
nx
,
nt
:=
0
,
0
;
for
i
,
n
:=
0
,
t
.
l
ist
.
Len
();
i
<
n
;
i
++
{
if
t
.
list
.
At
(
i
)
.
(
*
Expr
)
.
t
ok
==
Scanner
.
TYPE
{
for
i
,
n
:=
0
,
t
.
L
ist
.
Len
();
i
<
n
;
i
++
{
if
t
.
List
.
At
(
i
)
.
(
*
Expr
)
.
T
ok
==
Scanner
.
TYPE
{
nt
++
;
}
else
{
nx
++
;
...
...
@@ -349,10 +349,10 @@ func (t *Type) nfields() int {
}
// requires complete Type.
p
os access
// requires complete Type.
P
os access
export
func
NewTypeExpr
(
typ
*
Type
)
*
Expr
{
obj
:=
NewObject
(
typ
.
p
os
,
TYPE
,
""
);
obj
.
t
yp
=
typ
;
obj
:=
NewObject
(
typ
.
P
os
,
TYPE
,
""
);
obj
.
T
yp
=
typ
;
return
NewLit
(
Scanner
.
TYPE
,
obj
);
}
...
...
@@ -365,16 +365,16 @@ export var BadType = NewType(0, Scanner.ILLEGAL);
export
type
Stat
struct
{
Node
;
init
,
p
ost
*
Stat
;
e
xpr
*
Expr
;
block
*
array
.
Array
;
e
nd
int
;
// block end position
d
ecl
*
Decl
;
Init
,
P
ost
*
Stat
;
E
xpr
*
Expr
;
Block
*
array
.
Array
;
E
nd
int
;
// block end position
D
ecl
*
Decl
;
}
export
func
NewStat
(
pos
,
tok
int
)
*
Stat
{
s
:=
new
(
Stat
);
s
.
pos
,
s
.
t
ok
=
pos
,
tok
;
s
.
Pos
,
s
.
T
ok
=
pos
,
tok
;
return
s
;
}
...
...
@@ -387,19 +387,19 @@ export var BadStat = NewStat(0, Scanner.ILLEGAL);
export
type
Decl
struct
{
Node
;
e
xported
bool
;
i
dent
*
Expr
;
// nil for ()-style declarations
t
yp
*
Type
;
v
al
*
Expr
;
E
xported
bool
;
I
dent
*
Expr
;
// nil for ()-style declarations
T
yp
*
Type
;
V
al
*
Expr
;
// list of *Decl for ()-style declarations
// list of *Stat for func declarations (or nil for forward decl)
list
*
array
.
Array
;
e
nd
int
;
List
*
array
.
Array
;
E
nd
int
;
}
export
func
NewDecl
(
pos
,
tok
int
,
exported
bool
)
*
Decl
{
d
:=
new
(
Decl
);
d
.
pos
,
d
.
tok
,
d
.
e
xported
=
pos
,
tok
,
exported
;
d
.
Pos
,
d
.
Tok
,
d
.
E
xported
=
pos
,
tok
,
exported
;
return
d
;
}
...
...
@@ -411,28 +411,28 @@ export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false);
// Program
export
type
Comment
struct
{
p
os
int
;
t
ext
string
;
P
os
int
;
T
ext
string
;
}
export
func
NewComment
(
pos
int
,
text
string
)
*
Comment
{
c
:=
new
(
Comment
);
c
.
pos
,
c
.
t
ext
=
pos
,
text
;
c
.
Pos
,
c
.
T
ext
=
pos
,
text
;
return
c
;
}
export
type
Program
struct
{
p
os
int
;
// tok is Scanner.PACKAGE
i
dent
*
Expr
;
d
ecls
*
array
.
Array
;
c
omments
*
array
.
Array
;
P
os
int
;
// tok is Scanner.PACKAGE
I
dent
*
Expr
;
D
ecls
*
array
.
Array
;
C
omments
*
array
.
Array
;
}
export
func
NewProgram
(
pos
int
)
*
Program
{
p
:=
new
(
Program
);
p
.
p
os
=
pos
;
p
.
P
os
=
pos
;
return
p
;
}
usr/gri/pretty/compilation.go
View file @
2527bba9
...
...
@@ -34,7 +34,7 @@ export type Flags struct {
}
type
E
rrorHandler
struct
{
type
e
rrorHandler
struct
{
filename
string
;
src
string
;
nerrors
int
;
...
...
@@ -44,7 +44,7 @@ type ErrorHandler struct {
}
func
(
h
*
E
rrorHandler
)
Init
(
filename
,
src
string
,
columns
bool
)
{
func
(
h
*
e
rrorHandler
)
Init
(
filename
,
src
string
,
columns
bool
)
{
h
.
filename
=
filename
;
h
.
src
=
src
;
h
.
nerrors
=
0
;
...
...
@@ -55,7 +55,7 @@ func (h *ErrorHandler) Init(filename, src string, columns bool) {
// Compute (line, column) information for a given source position.
func
(
h
*
E
rrorHandler
)
LineCol
(
pos
int
)
(
line
,
col
int
)
{
func
(
h
*
e
rrorHandler
)
LineCol
(
pos
int
)
(
line
,
col
int
)
{
line
=
1
;
lpos
:=
0
;
...
...
@@ -75,7 +75,7 @@ func (h *ErrorHandler) LineCol(pos int) (line, col int) {
}
func
(
h
*
E
rrorHandler
)
ErrorMsg
(
pos
int
,
msg
string
)
{
func
(
h
*
e
rrorHandler
)
ErrorMsg
(
pos
int
,
msg
string
)
{
print
(
h
.
filename
,
":"
);
if
pos
>=
0
{
// print position
...
...
@@ -97,7 +97,7 @@ func (h *ErrorHandler) ErrorMsg(pos int, msg string) {
}
func
(
h
*
E
rrorHandler
)
Error
(
pos
int
,
msg
string
)
{
func
(
h
*
e
rrorHandler
)
Error
(
pos
int
,
msg
string
)
{
// only report errors that are sufficiently far away from the previous error
// in the hope to avoid most follow-up errors
const
errdist
=
20
;
...
...
@@ -112,7 +112,7 @@ func (h *ErrorHandler) Error(pos int, msg string) {
}
func
(
h
*
E
rrorHandler
)
Warning
(
pos
int
,
msg
string
)
{
func
(
h
*
e
rrorHandler
)
Warning
(
pos
int
,
msg
string
)
{
panic
(
"UNIMPLEMENTED"
);
}
...
...
@@ -124,7 +124,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
return
nil
,
1
;
}
var
err
E
rrorHandler
;
var
err
e
rrorHandler
;
err
.
Init
(
src_file
,
src
,
flags
.
columns
);
var
scanner
Scanner
.
Scanner
;
...
...
@@ -148,7 +148,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
}
func
F
ileExists
(
name
string
)
bool
{
func
f
ileExists
(
name
string
)
bool
{
fd
,
err
:=
OS
.
Open
(
name
,
OS
.
O_RDONLY
,
0
);
if
err
==
nil
{
fd
.
Close
();
...
...
@@ -158,7 +158,7 @@ func FileExists(name string) bool {
}
func
A
ddDeps
(
globalset
map
[
string
]
bool
,
wset
*
array
.
Array
,
src_file
string
,
flags
*
Flags
)
{
func
a
ddDeps
(
globalset
map
[
string
]
bool
,
wset
*
array
.
Array
,
src_file
string
,
flags
*
Flags
)
{
dummy
,
found
:=
globalset
[
src_file
];
if
!
found
{
globalset
[
src_file
]
=
true
;
...
...
@@ -168,27 +168,27 @@ func AddDeps(globalset map [string] bool, wset *array.Array, src_file string, fl
return
;
}
nimports
:=
prog
.
d
ecls
.
Len
();
nimports
:=
prog
.
D
ecls
.
Len
();
if
nimports
>
0
{
print
(
src_file
,
".6:
\t
"
);
localset
:=
make
(
map
[
string
]
bool
);
for
i
:=
0
;
i
<
nimports
;
i
++
{
decl
:=
prog
.
d
ecls
.
At
(
i
)
.
(
*
AST
.
Decl
);
assert
(
decl
.
tok
==
Scanner
.
IMPORT
&&
decl
.
val
.
t
ok
==
Scanner
.
STRING
);
src
:=
decl
.
val
.
obj
.
i
dent
;
decl
:=
prog
.
D
ecls
.
At
(
i
)
.
(
*
AST
.
Decl
);
assert
(
decl
.
Tok
==
Scanner
.
IMPORT
&&
decl
.
Val
.
T
ok
==
Scanner
.
STRING
);
src
:=
decl
.
Val
.
Obj
.
I
dent
;
src
=
src
[
1
:
len
(
src
)
-
1
];
// strip "'s
// ignore files when they are seen a 2nd time
dummy
,
found
:=
localset
[
src
];
if
!
found
{
localset
[
src
]
=
true
;
if
F
ileExists
(
src
+
".go"
)
{
if
f
ileExists
(
src
+
".go"
)
{
wset
.
Push
(
src
);
print
(
" "
,
src
,
".6"
);
}
else
if
F
ileExists
(
Platform
.
GOROOT
+
"/pkg/"
+
src
+
".6"
)
||
F
ileExists
(
Platform
.
GOROOT
+
"/pkg/"
+
src
+
".a"
)
{
f
ileExists
(
Platform
.
GOROOT
+
"/pkg/"
+
src
+
".6"
)
||
f
ileExists
(
Platform
.
GOROOT
+
"/pkg/"
+
src
+
".a"
)
{
}
else
{
// TODO should collect these and print later
...
...
@@ -207,6 +207,6 @@ export func ComputeDeps(src_file string, flags *Flags) {
wset
:=
array
.
New
(
0
);
wset
.
Push
(
src_file
);
for
wset
.
Len
()
>
0
{
A
ddDeps
(
globalset
,
wset
,
wset
.
Pop
()
.
(
string
),
flags
);
a
ddDeps
(
globalset
,
wset
,
wset
.
Pop
()
.
(
string
),
flags
);
}
}
usr/gri/pretty/parser.go
View file @
2527bba9
This diff is collapsed.
Click to expand it.
usr/gri/pretty/platform.go
View file @
2527bba9
...
...
@@ -17,7 +17,7 @@ export var
USER
string
;
func
G
etEnv
(
key
string
)
string
{
func
g
etEnv
(
key
string
)
string
{
n
:=
len
(
key
);
for
i
:=
0
;
i
<
sys
.
envc
();
i
++
{
v
:=
sys
.
envv
(
i
);
...
...
@@ -30,10 +30,10 @@ func GetEnv(key string) string {
func
init
()
{
GOARCH
=
G
etEnv
(
"GOARCH"
);
GOOS
=
G
etEnv
(
"GOOS"
);
GOROOT
=
G
etEnv
(
"GOROOT"
);
USER
=
G
etEnv
(
"USER"
);
GOARCH
=
g
etEnv
(
"GOARCH"
);
GOOS
=
g
etEnv
(
"GOOS"
);
GOROOT
=
g
etEnv
(
"GOROOT"
);
USER
=
g
etEnv
(
"USER"
);
}
...
...
@@ -42,13 +42,13 @@ func init() {
export
const
(
MAGIC_obj_file
=
"@gri-go.7@v0"
;
// make it clear that it cannot be a source file
s
rc_file_ext
=
".go"
;
o
bj_file_ext
=
".7"
;
S
rc_file_ext
=
".go"
;
O
bj_file_ext
=
".7"
;
)
export
func
ReadObjectFile
(
filename
string
)
(
data
string
,
ok
bool
)
{
data
,
ok
=
sys
.
readfile
(
filename
+
o
bj_file_ext
);
data
,
ok
=
sys
.
readfile
(
filename
+
O
bj_file_ext
);
magic
:=
MAGIC_obj_file
;
// TODO remove once len(constant) works
if
ok
&&
len
(
data
)
>=
len
(
magic
)
&&
data
[
0
:
len
(
magic
)]
==
magic
{
return
data
,
ok
;
...
...
@@ -58,13 +58,13 @@ export func ReadObjectFile(filename string) (data string, ok bool) {
export
func
ReadSourceFile
(
name
string
)
(
data
string
,
ok
bool
)
{
name
=
Utils
.
TrimExt
(
name
,
src_file_ext
)
+
s
rc_file_ext
;
name
=
Utils
.
TrimExt
(
name
,
Src_file_ext
)
+
S
rc_file_ext
;
data
,
ok
=
sys
.
readfile
(
name
);
return
data
,
ok
;
}
export
func
WriteObjectFile
(
name
string
,
data
string
)
bool
{
name
=
Utils
.
TrimExt
(
Utils
.
BaseName
(
name
),
src_file_ext
)
+
o
bj_file_ext
;
name
=
Utils
.
TrimExt
(
Utils
.
BaseName
(
name
),
Src_file_ext
)
+
O
bj_file_ext
;
return
sys
.
writefile
(
name
,
data
);
}
usr/gri/pretty/pretty.go
View file @
2527bba9
...
...
@@ -29,7 +29,7 @@ func init() {
}
func
U
sage
()
{
func
u
sage
()
{
print
(
"usage: pretty { flags } { files }
\n
"
);
Flag
.
PrintDefaults
();
sys
.
exit
(
0
);
...
...
@@ -40,7 +40,7 @@ func main() {
Flag
.
Parse
();
if
Flag
.
NFlag
()
==
0
&&
Flag
.
NArg
()
==
0
{
U
sage
();
u
sage
();
}
// process files
...
...
usr/gri/pretty/printer.go
View file @
2527bba9
This diff is collapsed.
Click to expand it.
usr/gri/pretty/scanner.go
View file @
2527bba9
This diff is collapsed.
Click to expand it.
usr/gri/pretty/selftest0.go
View file @
2527bba9
...
...
@@ -4,7 +4,7 @@
package
main
type
Proto
struct
{
export
type
Proto
struct
{
a
int
"a tag"
;
b
,
c
,
d
*
Proto
"bcd"
"tag"
;
*
Proto
"proto tag"
...
...
usr/gri/pretty/selftest2.go
View file @
2527bba9
...
...
@@ -11,7 +11,7 @@ import (
)
const
/* enum1 */
(
export
const
/* enum1 */
(
EnumTag0
=
iota
;
EnumTag1
;
EnumTag2
;
...
...
@@ -32,10 +32,10 @@ const /* enum2 */ (
)
type
S
struct
{}
export
type
S
struct
{}
type
T
struct
{
export
type
T
struct
{
x
,
y
int
;
s
string
;
next_t
*
T
...
...
@@ -43,7 +43,7 @@ type T struct {
var
(
A
=
5
;
aa
=
5
;
u
,
v
,
w
int
=
0
,
0
,
0
;
foo
=
"foo"
;
fixed_array0
=
[
10
]
int
{};
...
...
@@ -54,7 +54,7 @@ var (
var
(
// Unicode identifiers
ä
,
ö
,
ü
,
Á
,
Ø
,
Å
,
ƒ
,
ß
int
;
ä
,
ö
,
ü
,
ƒ
,
ß
int
;
)
...
...
@@ -105,24 +105,24 @@ func f3(a *[]int, m map[string] int) {
}
println
(
"A3"
);
for
i
:
x
:=
range
a
{
for
i
,
x
:=
range
a
{
println
(
i
,
x
);
}
println
(
"M1"
);
for
i
range
m
{
for
i
:=
range
m
{
println
(
i
);
}
println
(
"M2"
);
for
i
,
x
range
m
{
for
i
,
x
:=
range
m
{
println
(
i
,
x
);
}
println
(
"M3"
);
var
i
string
;
var
x
int
;
for
i
:
x
=
range
m
{
for
i
,
x
=
range
m
{
println
(
i
,
x
);
}
}
...
...
usr/gri/pretty/test.sh
View file @
2527bba9
...
...
@@ -10,6 +10,7 @@ TMP3=test_tmp3.go
COUNT
=
0
count
()
{
#echo $1
let
COUNT
=
$COUNT
+1
let
M
=
$COUNT
%10
if
[
$M
==
0
]
;
then
...
...
@@ -25,7 +26,7 @@ apply1() {
# files with errors (skip them)
method1.go
|
selftest1.go
|
func3.go
|
bug014.go
|
bug029.go
|
bug032.go
|
bug050.go
|
\
bug068.go
|
bug088.go
|
bug083.go
|
bug106.go
|
bug125.go
|
bug126.go
)
;;
*
)
$1
$2
;
count
;;
*
)
$1
$2
;
count
$F
;;
esac
}
...
...
@@ -130,7 +131,7 @@ if [ $? != 0 ]; then
echo
"Error (selftest1): pretty -t selftest1.go"
exit
1
fi
count
count
selftest1.go
# run over all .go files
...
...
usr/gri/pretty/typechecker.go
View file @
2527bba9
...
...
@@ -11,13 +11,13 @@ import (
)
type
S
tate
struct
{
type
s
tate
struct
{
// setup
err
Scanner
.
ErrorHandler
;
}
func
(
s
*
S
tate
)
Init
(
err
Scanner
.
ErrorHandler
)
{
func
(
s
*
s
tate
)
Init
(
err
Scanner
.
ErrorHandler
)
{
s
.
err
=
err
;
}
...
...
@@ -42,27 +42,27 @@ func assert(pred bool) {
}
func
(
s
*
S
tate
)
Error
(
pos
int
,
msg
string
)
{
func
(
s
*
s
tate
)
Error
(
pos
int
,
msg
string
)
{
s
.
err
.
Error
(
pos
,
msg
);
}
// ----------------------------------------------------------------------------
func
(
s
*
S
tate
)
CheckType
()
{
func
(
s
*
s
tate
)
CheckType
()
{
}
func
(
s
*
S
tate
)
CheckDeclaration
(
d
*
AST
.
Decl
)
{
if
d
.
tok
!=
Scanner
.
FUNC
&&
d
.
l
ist
!=
nil
{
func
(
s
*
s
tate
)
CheckDeclaration
(
d
*
AST
.
Decl
)
{
if
d
.
Tok
!=
Scanner
.
FUNC
&&
d
.
L
ist
!=
nil
{
// group of parenthesized declarations
for
i
:=
0
;
i
<
d
.
l
ist
.
Len
();
i
++
{
s
.
CheckDeclaration
(
d
.
l
ist
.
At
(
i
)
.
(
*
AST
.
Decl
))
for
i
:=
0
;
i
<
d
.
L
ist
.
Len
();
i
++
{
s
.
CheckDeclaration
(
d
.
L
ist
.
At
(
i
)
.
(
*
AST
.
Decl
))
}
}
else
{
// single declaration
switch
d
.
t
ok
{
switch
d
.
T
ok
{
case
Scanner
.
IMPORT
:
case
Scanner
.
EXPORT
:
case
Scanner
.
CONST
:
...
...
@@ -76,9 +76,9 @@ func (s *State) CheckDeclaration(d *AST.Decl) {
}
func
(
s
*
S
tate
)
CheckProgram
(
p
*
AST
.
Program
)
{
for
i
:=
0
;
i
<
p
.
d
ecls
.
Len
();
i
++
{
s
.
CheckDeclaration
(
p
.
d
ecls
.
At
(
i
)
.
(
*
AST
.
Decl
));
func
(
s
*
s
tate
)
CheckProgram
(
p
*
AST
.
Program
)
{
for
i
:=
0
;
i
<
p
.
D
ecls
.
Len
();
i
++
{
s
.
CheckDeclaration
(
p
.
D
ecls
.
At
(
i
)
.
(
*
AST
.
Decl
));
}
}
...
...
@@ -86,7 +86,7 @@ func (s *State) CheckProgram(p *AST.Program) {
// ----------------------------------------------------------------------------
export
func
CheckProgram
(
err
Scanner
.
ErrorHandler
,
p
*
AST
.
Program
)
{
var
s
S
tate
;
var
s
s
tate
;
s
.
Init
(
err
);
s
.
CheckProgram
(
p
);
}
usr/gri/pretty/universe.go
View file @
2527bba9
...
...
@@ -11,113 +11,113 @@ import (
export
var
(
s
cope
*
AST
.
Scope
;
t
ypes
array
.
Array
;
S
cope
*
AST
.
Scope
;
T
ypes
array
.
Array
;
// internal types
v
oid_typ
,
b
ad_typ
,
n
il_typ
,
V
oid_typ
,
B
ad_typ
,
N
il_typ
,
// basic types
b
ool_typ
,
u
int8_typ
,
u
int16_typ
,
u
int32_typ
,
u
int64_typ
,
i
nt8_typ
,
i
nt16_typ
,
i
nt32_typ
,
i
nt64_typ
,
f
loat32_typ
,
f
loat64_typ
,
f
loat80_typ
,
s
tring_typ
,
i
nteger_typ
,
B
ool_typ
,
U
int8_typ
,
U
int16_typ
,
U
int32_typ
,
U
int64_typ
,
I
nt8_typ
,
I
nt16_typ
,
I
nt32_typ
,
I
nt64_typ
,
F
loat32_typ
,
F
loat64_typ
,
F
loat80_typ
,
S
tring_typ
,
I
nteger_typ
,
// convenience types
b
yte_typ
,
u
int_typ
,
i
nt_typ
,
f
loat_typ
,
u
intptr_typ
*
AST
.
Type
;
B
yte_typ
,
U
int_typ
,
I
nt_typ
,
F
loat_typ
,
U
intptr_typ
*
AST
.
Type
;
t
rue_obj
,
f
alse_obj
,
i
ota_obj
,
n
il_obj
*
AST
.
Object
;
T
rue_obj
,
F
alse_obj
,
I
ota_obj
,
N
il_obj
*
AST
.
Object
;
)
func
D
eclObj
(
kind
int
,
ident
string
,
typ
*
AST
.
Type
)
*
AST
.
Object
{
func
d
eclObj
(
kind
int
,
ident
string
,
typ
*
AST
.
Type
)
*
AST
.
Object
{
obj
:=
AST
.
NewObject
(
-
1
/* no source pos */
,
kind
,
ident
);
obj
.
t
yp
=
typ
;
if
kind
==
AST
.
TYPE
&&
typ
.
o
bj
==
nil
{
typ
.
o
bj
=
obj
;
// set primary type object
obj
.
T
yp
=
typ
;
if
kind
==
AST
.
TYPE
&&
typ
.
O
bj
==
nil
{
typ
.
O
bj
=
obj
;
// set primary type object
}
s
cope
.
Insert
(
obj
);
S
cope
.
Insert
(
obj
);
return
obj
}
func
D
eclType
(
form
int
,
ident
string
,
size
int
)
*
AST
.
Type
{
func
d
eclType
(
form
int
,
ident
string
,
size
int
)
*
AST
.
Type
{
typ
:=
AST
.
NewType
(
-
1
/* no source pos */
,
form
);
typ
.
s
ize
=
size
;
return
DeclObj
(
AST
.
TYPE
,
ident
,
typ
)
.
t
yp
;
typ
.
S
ize
=
size
;
return
declObj
(
AST
.
TYPE
,
ident
,
typ
)
.
T
yp
;
}
func
R
egister
(
typ
*
AST
.
Type
)
*
AST
.
Type
{
typ
.
ref
=
t
ypes
.
Len
();
t
ypes
.
Push
(
typ
);
func
r
egister
(
typ
*
AST
.
Type
)
*
AST
.
Type
{
typ
.
Ref
=
T
ypes
.
Len
();
T
ypes
.
Push
(
typ
);
return
typ
;
}
func
init
()
{
s
cope
=
AST
.
NewScope
(
nil
);
// universe has no parent
t
ypes
.
Init
(
32
);
S
cope
=
AST
.
NewScope
(
nil
);
// universe has no parent
T
ypes
.
Init
(
32
);
// Interal types
v
oid_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
VOID
);
AST
.
Universe_void_typ
=
v
oid_typ
;
b
ad_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
BADTYPE
);
n
il_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
NIL
);
V
oid_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
VOID
);
AST
.
Universe_void_typ
=
V
oid_typ
;
B
ad_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
BADTYPE
);
N
il_typ
=
AST
.
NewType
(
-
1
/* no source pos */
,
AST
.
NIL
);
// Basic types
bool_typ
=
Register
(
D
eclType
(
AST
.
BOOL
,
"bool"
,
1
));
uint8_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uint8"
,
1
));
uint16_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uint16"
,
2
));
uint32_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uint32"
,
4
));
uint64_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uint64"
,
8
));
int8_typ
=
Register
(
D
eclType
(
AST
.
INT
,
"int8"
,
1
));
int16_typ
=
Register
(
D
eclType
(
AST
.
INT
,
"int16"
,
2
));
int32_typ
=
Register
(
D
eclType
(
AST
.
INT
,
"int32"
,
4
));
int64_typ
=
Register
(
D
eclType
(
AST
.
INT
,
"int64"
,
8
));
float32_typ
=
Register
(
D
eclType
(
AST
.
FLOAT
,
"float32"
,
4
));
float64_typ
=
Register
(
D
eclType
(
AST
.
FLOAT
,
"float64"
,
8
));
float80_typ
=
Register
(
D
eclType
(
AST
.
FLOAT
,
"float80"
,
10
));
string_typ
=
Register
(
D
eclType
(
AST
.
STRING
,
"string"
,
8
));
integer_typ
=
Register
(
D
eclType
(
AST
.
INTEGER
,
"integer"
,
8
));
Bool_typ
=
register
(
d
eclType
(
AST
.
BOOL
,
"bool"
,
1
));
Uint8_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uint8"
,
1
));
Uint16_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uint16"
,
2
));
Uint32_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uint32"
,
4
));
Uint64_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uint64"
,
8
));
Int8_typ
=
register
(
d
eclType
(
AST
.
INT
,
"int8"
,
1
));
Int16_typ
=
register
(
d
eclType
(
AST
.
INT
,
"int16"
,
2
));
Int32_typ
=
register
(
d
eclType
(
AST
.
INT
,
"int32"
,
4
));
Int64_typ
=
register
(
d
eclType
(
AST
.
INT
,
"int64"
,
8
));
Float32_typ
=
register
(
d
eclType
(
AST
.
FLOAT
,
"float32"
,
4
));
Float64_typ
=
register
(
d
eclType
(
AST
.
FLOAT
,
"float64"
,
8
));
Float80_typ
=
register
(
d
eclType
(
AST
.
FLOAT
,
"float80"
,
10
));
String_typ
=
register
(
d
eclType
(
AST
.
STRING
,
"string"
,
8
));
Integer_typ
=
register
(
d
eclType
(
AST
.
INTEGER
,
"integer"
,
8
));
// All but 'byte' should be platform-dependent, eventually.
byte_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"byte"
,
1
));
uint_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uint"
,
4
));
int_typ
=
Register
(
D
eclType
(
AST
.
INT
,
"int"
,
4
));
float_typ
=
Register
(
D
eclType
(
AST
.
FLOAT
,
"float"
,
4
));
uintptr_typ
=
Register
(
D
eclType
(
AST
.
UINT
,
"uintptr"
,
8
));
Byte_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"byte"
,
1
));
Uint_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uint"
,
4
));
Int_typ
=
register
(
d
eclType
(
AST
.
INT
,
"int"
,
4
));
Float_typ
=
register
(
d
eclType
(
AST
.
FLOAT
,
"float"
,
4
));
Uintptr_typ
=
register
(
d
eclType
(
AST
.
UINT
,
"uintptr"
,
8
));
// Predeclared constants
true_obj
=
DeclObj
(
AST
.
CONST
,
"true"
,
b
ool_typ
);
false_obj
=
DeclObj
(
AST
.
CONST
,
"false"
,
b
ool_typ
);
iota_obj
=
DeclObj
(
AST
.
CONST
,
"iota"
,
i
nt_typ
);
nil_obj
=
DeclObj
(
AST
.
CONST
,
"nil"
,
n
il_typ
);
True_obj
=
declObj
(
AST
.
CONST
,
"true"
,
B
ool_typ
);
False_obj
=
declObj
(
AST
.
CONST
,
"false"
,
B
ool_typ
);
Iota_obj
=
declObj
(
AST
.
CONST
,
"iota"
,
I
nt_typ
);
Nil_obj
=
declObj
(
AST
.
CONST
,
"nil"
,
N
il_typ
);
// Builtin functions
DeclObj
(
AST
.
BUILTIN
,
"len"
,
v
oid_typ
);
DeclObj
(
AST
.
BUILTIN
,
"new"
,
v
oid_typ
);
DeclObj
(
AST
.
BUILTIN
,
"panic"
,
v
oid_typ
);
DeclObj
(
AST
.
BUILTIN
,
"print"
,
v
oid_typ
);
declObj
(
AST
.
BUILTIN
,
"len"
,
V
oid_typ
);
declObj
(
AST
.
BUILTIN
,
"new"
,
V
oid_typ
);
declObj
(
AST
.
BUILTIN
,
"panic"
,
V
oid_typ
);
declObj
(
AST
.
BUILTIN
,
"print"
,
V
oid_typ
);
// scope.Print();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment