Commit 2527bba9 authored by Robert Griesemer's avatar Robert Griesemer

casify pretty

R=r
OCL=22899
CL=22899
parent 605d0746
...@@ -54,23 +54,23 @@ export func KindStr(kind int) string { ...@@ -54,23 +54,23 @@ export func KindStr(kind int) string {
export type Object struct { export type Object struct {
id int; // unique id Id int; // unique id
pos int; // source position (< 0 if unknown position) Pos int; // source position (< 0 if unknown position)
kind int; // object kind Kind int; // object kind
ident string; Ident string;
typ *Type; // nil for packages Typ *Type; // nil for packages
pnolev int; // >= 0: package no., <= 0: function nesting level, 0: global level Pnolev int; // >= 0: package no., <= 0: function nesting level, 0: global level
// attached values // attached values
block *array.Array; end int; // stats for function literals; end of block pos Block *array.Array; End int; // stats for function literals; end of block pos
} }
func (obj *Object) IsExported() bool { func (obj *Object) IsExported() bool {
switch obj.kind { switch obj.Kind {
case NONE /* FUNC for now */, CONST, TYPE, VAR, FUNC: case NONE /* FUNC for now */, CONST, TYPE, VAR, FUNC:
ch, size := utf8.DecodeRuneInString(obj.ident, 0); ch, size := utf8.DecodeRuneInString(obj.Ident, 0);
return unicode.IsUpper(ch); return unicode.IsUpper(ch);
} }
return false; return false;
...@@ -78,18 +78,18 @@ func (obj *Object) IsExported() bool { ...@@ -78,18 +78,18 @@ func (obj *Object) IsExported() bool {
export var Universe_void_typ *Type // initialized by Universe to Universe.void_typ export var Universe_void_typ *Type // initialized by Universe to Universe.void_typ
var ObjectId int; var objectId int;
export func NewObject(pos, kind int, ident string) *Object { export func NewObject(pos, kind int, ident string) *Object {
obj := new(Object); obj := new(Object);
obj.id = ObjectId; obj.Id = objectId;
ObjectId++; objectId++;
obj.pos = pos; obj.Pos = pos;
obj.kind = kind; obj.Kind = kind;
obj.ident = ident; obj.Ident = ident;
obj.typ = Universe_void_typ; obj.Typ = Universe_void_typ;
obj.pnolev = 0; obj.Pnolev = 0;
return obj; return obj;
} }
...@@ -133,23 +133,23 @@ func (scope *Scope) Lookup(ident string) *Object { ...@@ -133,23 +133,23 @@ func (scope *Scope) Lookup(ident string) *Object {
} }
func (scope *Scope) Add(obj* Object) { func (scope *Scope) add(obj* Object) {
scope.entries[obj.ident] = obj; scope.entries[obj.Ident] = obj;
} }
func (scope *Scope) Insert(obj *Object) { func (scope *Scope) Insert(obj *Object) {
if scope.LookupLocal(obj.ident) != nil { if scope.LookupLocal(obj.Ident) != nil {
panic("obj already inserted"); panic("obj already inserted");
} }
scope.Add(obj); scope.add(obj);
} }
func (scope *Scope) InsertImport(obj *Object) *Object { func (scope *Scope) InsertImport(obj *Object) *Object {
p := scope.LookupLocal(obj.ident); p := scope.LookupLocal(obj.Ident);
if p == nil { if p == nil {
scope.Add(obj); scope.add(obj);
p = obj; p = obj;
} }
return p; return p;
...@@ -169,8 +169,8 @@ func (scope *Scope) Print() { ...@@ -169,8 +169,8 @@ func (scope *Scope) Print() {
// All nodes have a source position and and token. // All nodes have a source position and and token.
export type Node struct { export type Node struct {
pos int; // source position (< 0 => unknown position) Pos int; // source position (< 0 => unknown position)
tok int; // identifying token Tok int; // identifying token
} }
...@@ -179,8 +179,8 @@ export type Node struct { ...@@ -179,8 +179,8 @@ export type Node struct {
export type Expr struct { export type Expr struct {
Node; Node;
x, y *Expr; // binary (x, y) and unary (y) expressions X, Y *Expr; // binary (X, Y) and unary (Y) expressions
obj *Object; Obj *Object;
} }
...@@ -189,7 +189,7 @@ func (x *Expr) Len() int { ...@@ -189,7 +189,7 @@ func (x *Expr) Len() int {
return 0; return 0;
} }
n := 1; n := 1;
for ; x.tok == Scanner.COMMA; x = x.y { for ; x.Tok == Scanner.COMMA; x = x.Y {
n++; n++;
} }
return n; return n;
...@@ -197,11 +197,11 @@ func (x *Expr) Len() int { ...@@ -197,11 +197,11 @@ func (x *Expr) Len() int {
export func NewExpr(pos, tok int, x, y *Expr) *Expr { export func NewExpr(pos, tok int, x, y *Expr) *Expr {
if x != nil && x.tok == Scanner.TYPE || y != nil && y.tok == Scanner.TYPE { if x != nil && x.Tok == Scanner.TYPE || y != nil && y.Tok == Scanner.TYPE {
panic("no type expression allowed"); panic("no type expression allowed");
} }
e := new(Expr); 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; return e;
} }
...@@ -209,7 +209,7 @@ export func NewExpr(pos, tok int, x, y *Expr) *Expr { ...@@ -209,7 +209,7 @@ export func NewExpr(pos, tok int, x, y *Expr) *Expr {
// TODO probably don't need the tok parameter eventually // TODO probably don't need the tok parameter eventually
export func NewLit(tok int, obj *Object) *Expr { export func NewLit(tok int, obj *Object) *Expr {
e := new(Expr); e := new(Expr);
e.pos, e.tok, e.obj = obj.pos, tok, obj; e.Pos, e.Tok, e.Obj = obj.Pos, tok, obj;
return e; return e;
} }
...@@ -296,47 +296,47 @@ export const /* channel mode */ ( ...@@ -296,47 +296,47 @@ export const /* channel mode */ (
export type Type struct { export type Type struct {
id int; // unique id Id int; // unique id
ref int; // for exporting only: >= 0 means already exported Ref int; // for exporting only: >= 0 means already exported
form int; // type form Form int; // type form
size int; // size in bytes Size int; // size in bytes
obj *Object; // primary type object or NULL Obj *Object; // primary type object or NULL
scope *Scope; // forwards, structs, interfaces, functions Scope *Scope; // forwards, structs, interfaces, functions
// syntactic components // syntactic components
pos int; // source position (< 0 if unknown position) Pos int; // source position (< 0 if unknown position)
expr *Expr; // type name, array length Expr *Expr; // type name, array length
mode int; // channel mode Mode int; // channel mode
key *Type; // receiver type or map key Key *Type; // receiver type or map key
elt *Type; // array, map, channel or pointer element type, function result type Elt *Type; // array, map, channel or pointer element type, function result type
list *array.Array; end int; // struct fields, interface methods, function parameters List *array.Array; End int; // struct fields, interface methods, function parameters
scope *Scope; // struct fields, methods Scope *Scope; // struct fields, methods
} }
var TypeId int; var typeId int;
export func NewType(pos, form int) *Type { export func NewType(pos, form int) *Type {
typ := new(Type); typ := new(Type);
typ.id = TypeId; typ.Id = typeId;
TypeId++; typeId++;
typ.ref = -1; // not yet exported typ.Ref = -1; // not yet exported
typ.pos = pos; typ.Pos = pos;
typ.form = form; typ.Form = form;
return typ; return typ;
} }
func (t *Type) nfields() int { func (t *Type) Nfields() int {
if t.list == nil { if t.List == nil {
return 0; return 0;
} }
nx, nt := 0, 0; nx, nt := 0, 0;
for i, n := 0, t.list.Len(); i < n; i++ { for i, n := 0, t.List.Len(); i < n; i++ {
if t.list.At(i).(*Expr).tok == Scanner.TYPE { if t.List.At(i).(*Expr).Tok == Scanner.TYPE {
nt++; nt++;
} else { } else {
nx++; nx++;
...@@ -349,10 +349,10 @@ func (t *Type) nfields() int { ...@@ -349,10 +349,10 @@ func (t *Type) nfields() int {
} }
// requires complete Type.pos access // requires complete Type.Pos access
export func NewTypeExpr(typ *Type) *Expr { export func NewTypeExpr(typ *Type) *Expr {
obj := NewObject(typ.pos, TYPE, ""); obj := NewObject(typ.Pos, TYPE, "");
obj.typ = typ; obj.Typ = typ;
return NewLit(Scanner.TYPE, obj); return NewLit(Scanner.TYPE, obj);
} }
...@@ -365,16 +365,16 @@ export var BadType = NewType(0, Scanner.ILLEGAL); ...@@ -365,16 +365,16 @@ export var BadType = NewType(0, Scanner.ILLEGAL);
export type Stat struct { export type Stat struct {
Node; Node;
init, post *Stat; Init, Post *Stat;
expr *Expr; Expr *Expr;
block *array.Array; end int; // block end position Block *array.Array; End int; // block end position
decl *Decl; Decl *Decl;
} }
export func NewStat(pos, tok int) *Stat { export func NewStat(pos, tok int) *Stat {
s := new(Stat); s := new(Stat);
s.pos, s.tok = pos, tok; s.Pos, s.Tok = pos, tok;
return s; return s;
} }
...@@ -387,19 +387,19 @@ export var BadStat = NewStat(0, Scanner.ILLEGAL); ...@@ -387,19 +387,19 @@ export var BadStat = NewStat(0, Scanner.ILLEGAL);
export type Decl struct { export type Decl struct {
Node; Node;
exported bool; Exported bool;
ident *Expr; // nil for ()-style declarations Ident *Expr; // nil for ()-style declarations
typ *Type; Typ *Type;
val *Expr; Val *Expr;
// list of *Decl for ()-style declarations // list of *Decl for ()-style declarations
// list of *Stat for func declarations (or nil for forward decl) // list of *Stat for func declarations (or nil for forward decl)
list *array.Array; end int; List *array.Array; End int;
} }
export func NewDecl(pos, tok int, exported bool) *Decl { export func NewDecl(pos, tok int, exported bool) *Decl {
d := new(Decl); d := new(Decl);
d.pos, d.tok, d.exported = pos, tok, exported; d.Pos, d.Tok, d.Exported = pos, tok, exported;
return d; return d;
} }
...@@ -411,28 +411,28 @@ export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false); ...@@ -411,28 +411,28 @@ export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false);
// Program // Program
export type Comment struct { export type Comment struct {
pos int; Pos int;
text string; Text string;
} }
export func NewComment(pos int, text string) *Comment { export func NewComment(pos int, text string) *Comment {
c := new(Comment); c := new(Comment);
c.pos, c.text = pos, text; c.Pos, c.Text = pos, text;
return c; return c;
} }
export type Program struct { export type Program struct {
pos int; // tok is Scanner.PACKAGE Pos int; // tok is Scanner.PACKAGE
ident *Expr; Ident *Expr;
decls *array.Array; Decls *array.Array;
comments *array.Array; Comments *array.Array;
} }
export func NewProgram(pos int) *Program { export func NewProgram(pos int) *Program {
p := new(Program); p := new(Program);
p.pos = pos; p.Pos = pos;
return p; return p;
} }
...@@ -34,7 +34,7 @@ export type Flags struct { ...@@ -34,7 +34,7 @@ export type Flags struct {
} }
type ErrorHandler struct { type errorHandler struct {
filename string; filename string;
src string; src string;
nerrors int; nerrors int;
...@@ -44,7 +44,7 @@ type ErrorHandler struct { ...@@ -44,7 +44,7 @@ type ErrorHandler struct {
} }
func (h *ErrorHandler) Init(filename, src string, columns bool) { func (h *errorHandler) Init(filename, src string, columns bool) {
h.filename = filename; h.filename = filename;
h.src = src; h.src = src;
h.nerrors = 0; h.nerrors = 0;
...@@ -55,7 +55,7 @@ func (h *ErrorHandler) Init(filename, src string, columns bool) { ...@@ -55,7 +55,7 @@ func (h *ErrorHandler) Init(filename, src string, columns bool) {
// Compute (line, column) information for a given source position. // Compute (line, column) information for a given source position.
func (h *ErrorHandler) LineCol(pos int) (line, col int) { func (h *errorHandler) LineCol(pos int) (line, col int) {
line = 1; line = 1;
lpos := 0; lpos := 0;
...@@ -75,7 +75,7 @@ func (h *ErrorHandler) LineCol(pos int) (line, col int) { ...@@ -75,7 +75,7 @@ func (h *ErrorHandler) LineCol(pos int) (line, col int) {
} }
func (h *ErrorHandler) ErrorMsg(pos int, msg string) { func (h *errorHandler) ErrorMsg(pos int, msg string) {
print(h.filename, ":"); print(h.filename, ":");
if pos >= 0 { if pos >= 0 {
// print position // print position
...@@ -97,7 +97,7 @@ func (h *ErrorHandler) ErrorMsg(pos int, msg string) { ...@@ -97,7 +97,7 @@ func (h *ErrorHandler) ErrorMsg(pos int, msg string) {
} }
func (h *ErrorHandler) Error(pos int, msg string) { func (h *errorHandler) Error(pos int, msg string) {
// only report errors that are sufficiently far away from the previous error // only report errors that are sufficiently far away from the previous error
// in the hope to avoid most follow-up errors // in the hope to avoid most follow-up errors
const errdist = 20; const errdist = 20;
...@@ -112,7 +112,7 @@ func (h *ErrorHandler) Error(pos int, msg string) { ...@@ -112,7 +112,7 @@ func (h *ErrorHandler) Error(pos int, msg string) {
} }
func (h *ErrorHandler) Warning(pos int, msg string) { func (h *errorHandler) Warning(pos int, msg string) {
panic("UNIMPLEMENTED"); panic("UNIMPLEMENTED");
} }
...@@ -124,7 +124,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) { ...@@ -124,7 +124,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
return nil, 1; return nil, 1;
} }
var err ErrorHandler; var err errorHandler;
err.Init(src_file, src, flags.columns); err.Init(src_file, src, flags.columns);
var scanner Scanner.Scanner; var scanner Scanner.Scanner;
...@@ -148,7 +148,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) { ...@@ -148,7 +148,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
} }
func FileExists(name string) bool { func fileExists(name string) bool {
fd, err := OS.Open(name, OS.O_RDONLY, 0); fd, err := OS.Open(name, OS.O_RDONLY, 0);
if err == nil { if err == nil {
fd.Close(); fd.Close();
...@@ -158,7 +158,7 @@ func FileExists(name string) bool { ...@@ -158,7 +158,7 @@ func FileExists(name string) bool {
} }
func AddDeps(globalset map [string] bool, wset *array.Array, src_file string, flags *Flags) { func addDeps(globalset map [string] bool, wset *array.Array, src_file string, flags *Flags) {
dummy, found := globalset[src_file]; dummy, found := globalset[src_file];
if !found { if !found {
globalset[src_file] = true; globalset[src_file] = true;
...@@ -168,27 +168,27 @@ func AddDeps(globalset map [string] bool, wset *array.Array, src_file string, fl ...@@ -168,27 +168,27 @@ func AddDeps(globalset map [string] bool, wset *array.Array, src_file string, fl
return; return;
} }
nimports := prog.decls.Len(); nimports := prog.Decls.Len();
if nimports > 0 { if nimports > 0 {
print(src_file, ".6:\t"); print(src_file, ".6:\t");
localset := make(map [string] bool); localset := make(map [string] bool);
for i := 0; i < nimports; i++ { for i := 0; i < nimports; i++ {
decl := prog.decls.At(i).(*AST.Decl); decl := prog.Decls.At(i).(*AST.Decl);
assert(decl.tok == Scanner.IMPORT && decl.val.tok == Scanner.STRING); assert(decl.Tok == Scanner.IMPORT && decl.Val.Tok == Scanner.STRING);
src := decl.val.obj.ident; src := decl.Val.Obj.Ident;
src = src[1 : len(src) - 1]; // strip "'s src = src[1 : len(src) - 1]; // strip "'s
// ignore files when they are seen a 2nd time // ignore files when they are seen a 2nd time
dummy, found := localset[src]; dummy, found := localset[src];
if !found { if !found {
localset[src] = true; localset[src] = true;
if FileExists(src + ".go") { if fileExists(src + ".go") {
wset.Push(src); wset.Push(src);
print(" ", src, ".6"); print(" ", src, ".6");
} else if } else if
FileExists(Platform.GOROOT + "/pkg/" + src + ".6") || fileExists(Platform.GOROOT + "/pkg/" + src + ".6") ||
FileExists(Platform.GOROOT + "/pkg/" + src + ".a") { fileExists(Platform.GOROOT + "/pkg/" + src + ".a") {
} else { } else {
// TODO should collect these and print later // TODO should collect these and print later
...@@ -207,6 +207,6 @@ export func ComputeDeps(src_file string, flags *Flags) { ...@@ -207,6 +207,6 @@ export func ComputeDeps(src_file string, flags *Flags) {
wset := array.New(0); wset := array.New(0);
wset.Push(src_file); wset.Push(src_file);
for wset.Len() > 0 { for wset.Len() > 0 {
AddDeps(globalset, wset, wset.Pop().(string), flags); addDeps(globalset, wset, wset.Pop().(string), flags);
} }
} }
This diff is collapsed.
...@@ -17,7 +17,7 @@ export var ...@@ -17,7 +17,7 @@ export var
USER string; USER string;
func GetEnv(key string) string { func getEnv(key string) string {
n := len(key); n := len(key);
for i := 0; i < sys.envc(); i++ { for i := 0; i < sys.envc(); i++ {
v := sys.envv(i); v := sys.envv(i);
...@@ -30,10 +30,10 @@ func GetEnv(key string) string { ...@@ -30,10 +30,10 @@ func GetEnv(key string) string {
func init() { func init() {
GOARCH = GetEnv("GOARCH"); GOARCH = getEnv("GOARCH");
GOOS = GetEnv("GOOS"); GOOS = getEnv("GOOS");
GOROOT = GetEnv("GOROOT"); GOROOT = getEnv("GOROOT");
USER = GetEnv("USER"); USER = getEnv("USER");
} }
...@@ -42,13 +42,13 @@ func init() { ...@@ -42,13 +42,13 @@ func init() {
export const ( export const (
MAGIC_obj_file = "@gri-go.7@v0"; // make it clear that it cannot be a source file MAGIC_obj_file = "@gri-go.7@v0"; // make it clear that it cannot be a source file
src_file_ext = ".go"; Src_file_ext = ".go";
obj_file_ext = ".7"; Obj_file_ext = ".7";
) )
export func ReadObjectFile(filename string) (data string, ok bool) { export func ReadObjectFile(filename string) (data string, ok bool) {
data, ok = sys.readfile(filename + obj_file_ext); data, ok = sys.readfile(filename + Obj_file_ext);
magic := MAGIC_obj_file; // TODO remove once len(constant) works magic := MAGIC_obj_file; // TODO remove once len(constant) works
if ok && len(data) >= len(magic) && data[0 : len(magic)] == magic { if ok && len(data) >= len(magic) && data[0 : len(magic)] == magic {
return data, ok; return data, ok;
...@@ -58,13 +58,13 @@ export func ReadObjectFile(filename string) (data string, ok bool) { ...@@ -58,13 +58,13 @@ export func ReadObjectFile(filename string) (data string, ok bool) {
export func ReadSourceFile(name string) (data string, ok bool) { export func ReadSourceFile(name string) (data string, ok bool) {
name = Utils.TrimExt(name, src_file_ext) + src_file_ext; name = Utils.TrimExt(name, Src_file_ext) + Src_file_ext;
data, ok = sys.readfile(name); data, ok = sys.readfile(name);
return data, ok; return data, ok;
} }
export func WriteObjectFile(name string, data string) bool { export func WriteObjectFile(name string, data string) bool {
name = Utils.TrimExt(Utils.BaseName(name), src_file_ext) + obj_file_ext; name = Utils.TrimExt(Utils.BaseName(name), Src_file_ext) + Obj_file_ext;
return sys.writefile(name, data); return sys.writefile(name, data);
} }
...@@ -29,7 +29,7 @@ func init() { ...@@ -29,7 +29,7 @@ func init() {
} }
func Usage() { func usage() {
print("usage: pretty { flags } { files }\n"); print("usage: pretty { flags } { files }\n");
Flag.PrintDefaults(); Flag.PrintDefaults();
sys.exit(0); sys.exit(0);
...@@ -40,7 +40,7 @@ func main() { ...@@ -40,7 +40,7 @@ func main() {
Flag.Parse(); Flag.Parse();
if Flag.NFlag() == 0 && Flag.NArg() == 0 { if Flag.NFlag() == 0 && Flag.NArg() == 0 {
Usage(); usage();
} }
// process files // process files
......
This diff is collapsed.
This diff is collapsed.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
package main package main
type Proto struct { export type Proto struct {
a int "a tag"; a int "a tag";
b, c, d *Proto "bcd" "tag"; b, c, d *Proto "bcd" "tag";
*Proto "proto tag" *Proto "proto tag"
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
) )
const /* enum1 */ ( export const /* enum1 */ (
EnumTag0 = iota; EnumTag0 = iota;
EnumTag1; EnumTag1;
EnumTag2; EnumTag2;
...@@ -32,10 +32,10 @@ const /* enum2 */ ( ...@@ -32,10 +32,10 @@ const /* enum2 */ (
) )
type S struct {} export type S struct {}
type T struct { export type T struct {
x, y int; x, y int;
s string; s string;
next_t *T next_t *T
...@@ -43,7 +43,7 @@ type T struct { ...@@ -43,7 +43,7 @@ type T struct {
var ( var (
A = 5; aa = 5;
u, v, w int = 0, 0, 0; u, v, w int = 0, 0, 0;
foo = "foo"; foo = "foo";
fixed_array0 = [10]int{}; fixed_array0 = [10]int{};
...@@ -54,7 +54,7 @@ var ( ...@@ -54,7 +54,7 @@ var (
var ( var (
// Unicode identifiers // Unicode identifiers
ä, ö, ü, Á, Ø, Å, ƒ, ß int; ä, ö, ü, ƒ, ß int;
) )
...@@ -105,24 +105,24 @@ func f3(a *[]int, m map[string] int) { ...@@ -105,24 +105,24 @@ func f3(a *[]int, m map[string] int) {
} }
println("A3"); println("A3");
for i : x := range a { for i, x := range a {
println(i, x); println(i, x);
} }
println("M1"); println("M1");
for i range m { for i := range m {
println(i); println(i);
} }
println("M2"); println("M2");
for i, x range m { for i, x := range m {
println(i, x); println(i, x);
} }
println("M3"); println("M3");
var i string; var i string;
var x int; var x int;
for i : x = range m { for i, x = range m {
println(i, x); println(i, x);
} }
} }
......
...@@ -10,6 +10,7 @@ TMP3=test_tmp3.go ...@@ -10,6 +10,7 @@ TMP3=test_tmp3.go
COUNT=0 COUNT=0
count() { count() {
#echo $1
let COUNT=$COUNT+1 let COUNT=$COUNT+1
let M=$COUNT%10 let M=$COUNT%10
if [ $M == 0 ]; then if [ $M == 0 ]; then
...@@ -25,7 +26,7 @@ apply1() { ...@@ -25,7 +26,7 @@ apply1() {
# files with errors (skip them) # files with errors (skip them)
method1.go | selftest1.go | func3.go | bug014.go | bug029.go | bug032.go | bug050.go | \ 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 ) ;; bug068.go | bug088.go | bug083.go | bug106.go | bug125.go | bug126.go ) ;;
* ) $1 $2; count ;; * ) $1 $2; count $F;;
esac esac
} }
...@@ -130,7 +131,7 @@ if [ $? != 0 ]; then ...@@ -130,7 +131,7 @@ if [ $? != 0 ]; then
echo "Error (selftest1): pretty -t selftest1.go" echo "Error (selftest1): pretty -t selftest1.go"
exit 1 exit 1
fi fi
count count selftest1.go
# run over all .go files # run over all .go files
......
...@@ -11,13 +11,13 @@ import ( ...@@ -11,13 +11,13 @@ import (
) )
type State struct { type state struct {
// setup // setup
err Scanner.ErrorHandler; err Scanner.ErrorHandler;
} }
func (s *State) Init(err Scanner.ErrorHandler) { func (s *state) Init(err Scanner.ErrorHandler) {
s.err = err; s.err = err;
} }
...@@ -42,27 +42,27 @@ func assert(pred bool) { ...@@ -42,27 +42,27 @@ func assert(pred bool) {
} }
func (s *State) Error(pos int, msg string) { func (s *state) Error(pos int, msg string) {
s.err.Error(pos, msg); s.err.Error(pos, msg);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
func (s *State) CheckType() { func (s *state) CheckType() {
} }
func (s *State) CheckDeclaration(d *AST.Decl) { func (s *state) CheckDeclaration(d *AST.Decl) {
if d.tok != Scanner.FUNC && d.list != nil { if d.Tok != Scanner.FUNC && d.List != nil {
// group of parenthesized declarations // group of parenthesized declarations
for i := 0; i < d.list.Len(); i++ { for i := 0; i < d.List.Len(); i++ {
s.CheckDeclaration(d.list.At(i).(*AST.Decl)) s.CheckDeclaration(d.List.At(i).(*AST.Decl))
} }
} else { } else {
// single declaration // single declaration
switch d.tok { switch d.Tok {
case Scanner.IMPORT: case Scanner.IMPORT:
case Scanner.EXPORT: case Scanner.EXPORT:
case Scanner.CONST: case Scanner.CONST:
...@@ -76,9 +76,9 @@ func (s *State) CheckDeclaration(d *AST.Decl) { ...@@ -76,9 +76,9 @@ func (s *State) CheckDeclaration(d *AST.Decl) {
} }
func (s *State) CheckProgram(p *AST.Program) { func (s *state) CheckProgram(p *AST.Program) {
for i := 0; i < p.decls.Len(); i++ { for i := 0; i < p.Decls.Len(); i++ {
s.CheckDeclaration(p.decls.At(i).(*AST.Decl)); s.CheckDeclaration(p.Decls.At(i).(*AST.Decl));
} }
} }
...@@ -86,7 +86,7 @@ func (s *State) CheckProgram(p *AST.Program) { ...@@ -86,7 +86,7 @@ func (s *State) CheckProgram(p *AST.Program) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
export func CheckProgram(err Scanner.ErrorHandler, p *AST.Program) { export func CheckProgram(err Scanner.ErrorHandler, p *AST.Program) {
var s State; var s state;
s.Init(err); s.Init(err);
s.CheckProgram(p); s.CheckProgram(p);
} }
...@@ -11,113 +11,113 @@ import ( ...@@ -11,113 +11,113 @@ import (
export var ( export var (
scope *AST.Scope; Scope *AST.Scope;
types array.Array; Types array.Array;
// internal types // internal types
void_typ, Void_typ,
bad_typ, Bad_typ,
nil_typ, Nil_typ,
// basic types // basic types
bool_typ, Bool_typ,
uint8_typ, Uint8_typ,
uint16_typ, Uint16_typ,
uint32_typ, Uint32_typ,
uint64_typ, Uint64_typ,
int8_typ, Int8_typ,
int16_typ, Int16_typ,
int32_typ, Int32_typ,
int64_typ, Int64_typ,
float32_typ, Float32_typ,
float64_typ, Float64_typ,
float80_typ, Float80_typ,
string_typ, String_typ,
integer_typ, Integer_typ,
// convenience types // convenience types
byte_typ, Byte_typ,
uint_typ, Uint_typ,
int_typ, Int_typ,
float_typ, Float_typ,
uintptr_typ *AST.Type; Uintptr_typ *AST.Type;
true_obj, True_obj,
false_obj, False_obj,
iota_obj, Iota_obj,
nil_obj *AST.Object; Nil_obj *AST.Object;
) )
func DeclObj(kind int, ident string, typ *AST.Type) *AST.Object { func declObj(kind int, ident string, typ *AST.Type) *AST.Object {
obj := AST.NewObject(-1 /* no source pos */, kind, ident); obj := AST.NewObject(-1 /* no source pos */, kind, ident);
obj.typ = typ; obj.Typ = typ;
if kind == AST.TYPE && typ.obj == nil { if kind == AST.TYPE && typ.Obj == nil {
typ.obj = obj; // set primary type object typ.Obj = obj; // set primary type object
} }
scope.Insert(obj); Scope.Insert(obj);
return obj return obj
} }
func DeclType(form int, ident string, size int) *AST.Type { func declType(form int, ident string, size int) *AST.Type {
typ := AST.NewType(-1 /* no source pos */, form); typ := AST.NewType(-1 /* no source pos */, form);
typ.size = size; typ.Size = size;
return DeclObj(AST.TYPE, ident, typ).typ; return declObj(AST.TYPE, ident, typ).Typ;
} }
func Register(typ *AST.Type) *AST.Type { func register(typ *AST.Type) *AST.Type {
typ.ref = types.Len(); typ.Ref = Types.Len();
types.Push(typ); Types.Push(typ);
return typ; return typ;
} }
func init() { func init() {
scope = AST.NewScope(nil); // universe has no parent Scope = AST.NewScope(nil); // universe has no parent
types.Init(32); Types.Init(32);
// Interal types // Interal types
void_typ = AST.NewType(-1 /* no source pos */, AST.VOID); Void_typ = AST.NewType(-1 /* no source pos */, AST.VOID);
AST.Universe_void_typ = void_typ; AST.Universe_void_typ = Void_typ;
bad_typ = AST.NewType(-1 /* no source pos */, AST.BADTYPE); Bad_typ = AST.NewType(-1 /* no source pos */, AST.BADTYPE);
nil_typ = AST.NewType(-1 /* no source pos */, AST.NIL); Nil_typ = AST.NewType(-1 /* no source pos */, AST.NIL);
// Basic types // Basic types
bool_typ = Register(DeclType(AST.BOOL, "bool", 1)); Bool_typ = register(declType(AST.BOOL, "bool", 1));
uint8_typ = Register(DeclType(AST.UINT, "uint8", 1)); Uint8_typ = register(declType(AST.UINT, "uint8", 1));
uint16_typ = Register(DeclType(AST.UINT, "uint16", 2)); Uint16_typ = register(declType(AST.UINT, "uint16", 2));
uint32_typ = Register(DeclType(AST.UINT, "uint32", 4)); Uint32_typ = register(declType(AST.UINT, "uint32", 4));
uint64_typ = Register(DeclType(AST.UINT, "uint64", 8)); Uint64_typ = register(declType(AST.UINT, "uint64", 8));
int8_typ = Register(DeclType(AST.INT, "int8", 1)); Int8_typ = register(declType(AST.INT, "int8", 1));
int16_typ = Register(DeclType(AST.INT, "int16", 2)); Int16_typ = register(declType(AST.INT, "int16", 2));
int32_typ = Register(DeclType(AST.INT, "int32", 4)); Int32_typ = register(declType(AST.INT, "int32", 4));
int64_typ = Register(DeclType(AST.INT, "int64", 8)); Int64_typ = register(declType(AST.INT, "int64", 8));
float32_typ = Register(DeclType(AST.FLOAT, "float32", 4)); Float32_typ = register(declType(AST.FLOAT, "float32", 4));
float64_typ = Register(DeclType(AST.FLOAT, "float64", 8)); Float64_typ = register(declType(AST.FLOAT, "float64", 8));
float80_typ = Register(DeclType(AST.FLOAT, "float80", 10)); Float80_typ = register(declType(AST.FLOAT, "float80", 10));
string_typ = Register(DeclType(AST.STRING, "string", 8)); String_typ = register(declType(AST.STRING, "string", 8));
integer_typ = Register(DeclType(AST.INTEGER, "integer", 8)); Integer_typ = register(declType(AST.INTEGER, "integer", 8));
// All but 'byte' should be platform-dependent, eventually. // All but 'byte' should be platform-dependent, eventually.
byte_typ = Register(DeclType(AST.UINT, "byte", 1)); Byte_typ = register(declType(AST.UINT, "byte", 1));
uint_typ = Register(DeclType(AST.UINT, "uint", 4)); Uint_typ = register(declType(AST.UINT, "uint", 4));
int_typ = Register(DeclType(AST.INT, "int", 4)); Int_typ = register(declType(AST.INT, "int", 4));
float_typ = Register(DeclType(AST.FLOAT, "float", 4)); Float_typ = register(declType(AST.FLOAT, "float", 4));
uintptr_typ = Register(DeclType(AST.UINT, "uintptr", 8)); Uintptr_typ = register(declType(AST.UINT, "uintptr", 8));
// Predeclared constants // Predeclared constants
true_obj = DeclObj(AST.CONST, "true", bool_typ); True_obj = declObj(AST.CONST, "true", Bool_typ);
false_obj = DeclObj(AST.CONST, "false", bool_typ); False_obj = declObj(AST.CONST, "false", Bool_typ);
iota_obj = DeclObj(AST.CONST, "iota", int_typ); Iota_obj = declObj(AST.CONST, "iota", Int_typ);
nil_obj = DeclObj(AST.CONST, "nil", nil_typ); Nil_obj = declObj(AST.CONST, "nil", Nil_typ);
// Builtin functions // Builtin functions
DeclObj(AST.BUILTIN, "len", void_typ); declObj(AST.BUILTIN, "len", Void_typ);
DeclObj(AST.BUILTIN, "new", void_typ); declObj(AST.BUILTIN, "new", Void_typ);
DeclObj(AST.BUILTIN, "panic", void_typ); declObj(AST.BUILTIN, "panic", Void_typ);
DeclObj(AST.BUILTIN, "print", void_typ); declObj(AST.BUILTIN, "print", Void_typ);
// scope.Print(); // scope.Print();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment