Commit c45d2a76 authored by Rob Pike's avatar Rob Pike

simplify flag interface. no more BVal etc. you just get a pointer.

fixed everything except the tutorial.

R=rsc
DELTA=404  (94 added, 139 deleted, 171 changed)
OCL=22414
CL=22422
parent 863dafb9
This diff is collapsed.
......@@ -13,8 +13,7 @@ import (
)
// If an IPv6 tunnel is running (see go/stubl), we can try dialing a real IPv6 address.
var ipv6 = false
var ipv6_flag = flag.Bool("ipv6", false, &ipv6, "assume ipv6 tunnel is present")
var ipv6 = flag.Bool("ipv6", false, "assume ipv6 tunnel is present")
// fd is already connected to www.google.com port 80.
// Run an HTTP request to fetch the main page.
......@@ -67,7 +66,7 @@ var googleaddrs = []string {
export func TestDialGoogle(t *testing.T) {
// If no ipv6 tunnel, don't try the last address.
if !ipv6 {
if !*ipv6 {
googleaddrs[len(googleaddrs)-1] = ""
}
......
......@@ -9,10 +9,7 @@ import (
"flag";
)
var chatty bool;
func init() {
flag.Bool("chatty", false, &chatty, "chatty");
}
var chatty = flag.Bool("chatty", false, "chatty")
// Insert tabs after newlines - but not the last one
func Tabify(s string) string {
......@@ -89,7 +86,7 @@ export func Main(tests []Test) {
println("testing: warning: no tests to run");
}
for i := 0; i < len(tests); i++ {
if chatty {
if *chatty {
println("=== RUN ", tests[i].name);
}
t := new(T);
......@@ -100,7 +97,7 @@ export func Main(tests []Test) {
println("--- FAIL:", tests[i].name);
print(t.errors);
ok = false;
} else if chatty {
} else if *chatty {
println("--- PASS:", tests[i].name);
print(t.errors);
}
......
......@@ -14,12 +14,11 @@ import (
"malloc";
)
var chatty bool;
var chatty_flag = flag.Bool("v", false, &chatty, "chatty");
var chatty = flag.Bool("v", false, "chatty");
func main() {
malloc.Free(malloc.Alloc(1));
if chatty {
if *chatty {
fmt.printf("%+v %v\n", *malloc.GetStats(), uint64(0));
}
}
......
......@@ -15,15 +15,14 @@ import (
"unsafe";
)
var chatty bool;
var chatty_flag = flag.Bool("v", false, &chatty, "chatty");
var chatty = flag.Bool("v", false, "chatty");
var footprint uint64;
var allocated uint64;
func bigger() {
if f := malloc.GetStats().sys; footprint < f {
footprint = f;
if chatty {
if *chatty {
println("Footprint", footprint, " for ", allocated);
}
if footprint > 1e9 {
......@@ -58,7 +57,7 @@ func main() {
// prime();
var blocks [1] struct { base *byte; siz uint64; };
for i := 0; i < 1<<12; i++ {
if i%(1<<10) == 0 && chatty {
if i%(1<<10) == 0 && *chatty {
println(i);
}
b := rand.rand() % len(blocks);
......
......@@ -13,14 +13,13 @@ import (
"malloc"
)
var chatty bool;
var chatty_flag = flag.Bool("v", false, &chatty, "chatty");
var chatty = flag.Bool("v", false, "chatty");
var oldsys uint64;
func bigger() {
if st := malloc.GetStats(); oldsys < st.sys {
oldsys = st.sys;
if chatty {
if *chatty {
println(st.sys, " system bytes for ", st.alloc, " Go bytes");
}
if st.sys > 1e9 {
......@@ -34,7 +33,7 @@ func main() {
malloc.GetStats().alloc = 0; // ignore stacks
for i := 0; i < 1<<8; i++ {
for j := 1; j <= 1<<22; j<<=1 {
if i == 0 && chatty {
if i == 0 && *chatty {
println("First alloc:", j);
}
b := malloc.Alloc(uint64(j));
......@@ -45,11 +44,11 @@ func main() {
}
bigger();
}
if i%(1<<10) == 0 && chatty {
if i%(1<<10) == 0 && *chatty {
println(i);
}
if i == 0 {
if chatty {
if *chatty {
println("Primed", i);
}
// malloc.frozen = true;
......
......@@ -15,12 +15,9 @@ import (
"strconv"
)
var chatty bool;
var chatty_flag = flag.Bool("v", false, &chatty, "chatty");
var reverse bool;
var reverse_flag = flag.Bool("r", false, &reverse, "reverse");
var longtest bool;
var longtest_flag = flag.Bool("l", false, &longtest, "long test");
var chatty = flag.Bool("v", false, "chatty");
var reverse = flag.Bool("r", false, "reverse");
var longtest = flag.Bool("l", false, "long test");
var b []*byte;
var stats = malloc.GetStats();
......@@ -42,7 +39,7 @@ func OkAmount(size, n uint64) bool {
}
func AllocAndFree(size, count int) {
if chatty {
if *chatty {
fmt.printf("size=%d count=%d ...\n", size, count);
}
n1 := stats.alloc;
......@@ -57,13 +54,13 @@ func AllocAndFree(size, count int) {
}
}
n2 := stats.alloc;
if chatty {
if *chatty {
fmt.printf("size=%d count=%d stats=%+v\n", size, count, *stats);
}
n3 := stats.alloc;
for j := 0; j < count; j++ {
i := j;
if reverse {
if *reverse {
i = count - 1 - j;
}
alloc := stats.alloc;
......@@ -81,7 +78,7 @@ func AllocAndFree(size, count int) {
}
n4 := stats.alloc;
if chatty {
if *chatty {
fmt.printf("size=%d count=%d stats=%+v\n", size, count, *stats);
}
if n2-n1 != n3-n4 {
......@@ -104,7 +101,7 @@ func main() {
for j := 1; j <= 1<<22; j<<=1 {
n := len(b);
max := uint64(1<<28);
if !longtest {
if !*longtest {
max = 1<<22;
}
if uint64(j)*uint64(n) > max {
......
......@@ -14,16 +14,19 @@ import (
var (
flags Compilation.Flags;
silent = Flag.Bool("s", false, nil, "silent mode: no pretty print output");
verbose = Flag.Bool("v", false, &flags.verbose, "verbose mode: trace parsing");
sixg = Flag.Bool("6g", true, &flags.sixg, "6g compatibility mode");
//TODO fix this code again
//deps = Flag.Bool("d", false, &flags.deps, "print dependency information only");
columns = Flag.Bool("columns", Platform.USER == "gri", &flags.columns, "print column info in error messages");
testmode = Flag.Bool("t", false, &flags.testmode, "test mode: interprets /* ERROR */ and /* SYNC */ comments");
tokenchan = Flag.Bool("token_chan", false, &flags.tokenchan, "use token channel for scanner-parser connection");
silent = Flag.Bool("s", false, "silent mode: no pretty print output");
)
func init() {
Flag.BoolVar(&flags.verbose, "v", false, "verbose mode: trace parsing");
Flag.BoolVar(&flags.sixg, "6g", true, "6g compatibility mode");
//TODO fix this code again
//Flag.BoolVar(&flags.deps, "d", false, "print dependency information only");
Flag.BoolVar(&flags.columns, "columns", Platform.USER == "gri", "print column info in error messages");
Flag.BoolVar(&flags.testmode, "t", false, "test mode: interprets /* ERROR */ and /* SYNC */ comments");
Flag.BoolVar(&flags.tokenchan, "token_chan", false, "use token channel for scanner-parser connection");
}
func Usage() {
print("usage: pretty { flags } { files }\n");
......@@ -51,7 +54,7 @@ func main() {
if nerrors > 0 {
return;
}
if !silent.BVal() && !flags.testmode {
if !*silent && !flags.testmode {
Printer.Print(prog);
}
}
......
......@@ -19,18 +19,18 @@ import (
)
var (
debug = flag.Bool("debug", false, nil, "print debugging information");
debug = flag.Bool("debug", false, "print debugging information");
// layout control
tabwidth = flag.Int("tabwidth", 8, nil, "tab width");
usetabs = flag.Bool("usetabs", true, nil, "align with tabs instead of blanks");
newlines = flag.Bool("newlines", true, nil, "respect newlines in source");
maxnewlines = flag.Int("maxnewlines", 3, nil, "max. number of consecutive newlines");
tabwidth = flag.Int("tabwidth", 8, "tab width");
usetabs = flag.Bool("usetabs", true, "align with tabs instead of blanks");
newlines = flag.Bool("newlines", true, "respect newlines in source");
maxnewlines = flag.Int("maxnewlines", 3, "max. number of consecutive newlines");
// formatting control
html = flag.Bool("html", false, nil, "generate html");
comments = flag.Bool("comments", true, nil, "print comments");
optsemicolons = flag.Bool("optsemicolons", false, nil, "print optional semicolons");
html = flag.Bool("html", false, "generate html");
comments = flag.Bool("comments", true, "print comments");
optsemicolons = flag.Bool("optsemicolons", false, "print optional semicolons");
)
......@@ -81,7 +81,7 @@ type Printer struct {
func (P *Printer) HasComment(pos int) bool {
return comments.BVal() && P.cpos < pos;
return *comments && P.cpos < pos;
}
......@@ -112,7 +112,7 @@ func (P *Printer) Init(text io.Write, comments *array.Array) {
// Printing support
func HtmlEscape(s string) string {
if html.BVal() {
if *html {
var esc string;
for i := 0; i < len(s); i++ {
switch s[i] {
......@@ -137,7 +137,7 @@ func (P *Printer) Printf(format string, s ...) {
func (P *Printer) Newline(n int) {
if n > 0 {
m := int(maxnewlines.IVal());
m := int(*maxnewlines);
if n > m {
n = m;
}
......@@ -208,7 +208,7 @@ func (P *Printer) TaggedString(pos int, tag, s, endtag string) {
// only white space before comment on this line
// or file starts with comment
// - indent
if !newlines.BVal() && P.cpos != 0 {
if !*newlines && P.cpos != 0 {
nlcount = 1;
}
P.Newline(nlcount);
......@@ -243,7 +243,7 @@ func (P *Printer) TaggedString(pos int, tag, s, endtag string) {
}
// print comment
if debug.BVal() {
if *debug {
P.Printf("[%d]", P.cpos);
}
P.Printf("%s", HtmlEscape(ctext));
......@@ -275,7 +275,7 @@ func (P *Printer) TaggedString(pos int, tag, s, endtag string) {
// --------------------------------
// print pending newlines
if newlines.BVal() && (P.newlines > 0 || P.state == inside_list) && nlcount > P.newlines {
if *newlines && (P.newlines > 0 || P.state == inside_list) && nlcount > P.newlines {
// Respect additional newlines in the source, but only if we
// enabled this feature (newlines.BVal()) and we are expecting
// newlines (P.newlines > 0 || P.state == inside_list).
......@@ -289,7 +289,7 @@ func (P *Printer) TaggedString(pos int, tag, s, endtag string) {
// --------------------------------
// print string
if debug.BVal() {
if *debug {
P.Printf("[%d]", pos);
}
P.Printf("%s%s%s", tag, HtmlEscape(s), endtag);
......@@ -337,7 +337,7 @@ func (P *Printer) Error(pos int, tok int, msg string) {
// HTML support
func (P *Printer) HtmlPrologue(title string) {
if html.BVal() {
if *html {
P.TaggedString(0,
"<html>\n"
"<head>\n"
......@@ -355,7 +355,7 @@ func (P *Printer) HtmlPrologue(title string) {
func (P *Printer) HtmlEpilogue() {
if html.BVal() {
if *html {
P.TaggedString(0,
"</pre>\n"
"</body>\n"
......@@ -371,7 +371,7 @@ func (P *Printer) HtmlIdentifier(x *AST.Expr) {
panic();
}
obj := x.obj;
if html.BVal() && obj.kind != Object.NONE {
if *html && obj.kind != Object.NONE {
// depending on whether we have a declaration or use, generate different html
// - no need to HtmlEscape ident
id := Utils.IntToString(obj.id, 10);
......@@ -647,7 +647,7 @@ func (P *Printer) Block(pos int, list *array.Array, end int, indent bool) {
if !indent {
P.indentation++;
}
if !optsemicolons.BVal() {
if !*optsemicolons {
P.separator = none;
}
P.state = closing_scope;
......@@ -874,10 +874,10 @@ export func Print(prog *AST.Program) {
// setup
var P Printer;
padchar := byte(' ');
if usetabs.BVal() {
if *usetabs {
padchar = '\t';
}
text := tabwriter.New(os.Stdout, int(tabwidth.IVal()), 1, padchar, true, html.BVal());
text := tabwriter.New(os.Stdout, *tabwidth, 1, padchar, true, *html);
P.Init(text, prog.comments);
// TODO would be better to make the name of the src file be the title
......
......@@ -14,8 +14,8 @@ import (
var (
tabwidth = flag.Int("tabwidth", 4, nil, "tab width");
usetabs = flag.Bool("usetabs", false, nil, "align with tabs instead of blanks");
tabwidth = flag.Int("tabwidth", 4, "tab width");
usetabs = flag.Bool("usetabs", false, "align with tabs instead of blanks");
)
......@@ -37,10 +37,10 @@ func Untab(name string, src *os.FD, dst *tabwriter.Writer) {
func main() {
flag.Parse();
padchar := byte(' ');
if usetabs.BVal() {
if *usetabs {
padchar = '\t';
}
dst := tabwriter.New(os.Stdout, int(tabwidth.IVal()), 1, padchar, true, false);
dst := tabwriter.New(os.Stdout, *tabwidth, 1, padchar, true, false);
if flag.NArg() > 0 {
for i := 0; i < flag.NArg(); i++ {
name := flag.Arg(i);
......
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