diff options
author | Alan Donovan <adonovan@google.com> | 2018-10-23 11:05:09 -0400 |
---|---|---|
committer | Alan Donovan <adonovan@google.com> | 2018-10-23 11:05:09 -0400 |
commit | e3deafefac22db7bfd4877d37614fe5db4b03881 (patch) | |
tree | e3de93e8b63abffb95767055fb392714cc8cc8a1 /cmd/starlark/starlark.go | |
parent | ce5c2fa1ad6a8fa4beb4eacdbd3bf9997162d144 (diff) | |
download | starlark-go-e3deafefac22db7bfd4877d37614fe5db4b03881.tar.gz |
rename skylark -> starlark
Change-Id: Iebd0e040ff674b2f9da39bf5242c8afaa7f4ddc8
Diffstat (limited to 'cmd/starlark/starlark.go')
-rw-r--r-- | cmd/starlark/starlark.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/cmd/starlark/starlark.go b/cmd/starlark/starlark.go new file mode 100644 index 0000000..3e74a75 --- /dev/null +++ b/cmd/starlark/starlark.go @@ -0,0 +1,87 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// The starlark command interprets a Starlark file. +// With no arguments, it starts a read-eval-print loop (REPL). +package main + +import ( + "flag" + "fmt" + "log" + "os" + "runtime/pprof" + "sort" + "strings" + + "github.com/google/starlark" + "github.com/google/starlark/repl" + "github.com/google/starlark/resolve" +) + +// flags +var ( + cpuprofile = flag.String("cpuprofile", "", "gather CPU profile in this file") + showenv = flag.Bool("showenv", false, "on success, print final global environment") +) + +// non-standard dialect flags +func init() { + flag.BoolVar(&resolve.AllowFloat, "fp", resolve.AllowFloat, "allow floating-point numbers") + flag.BoolVar(&resolve.AllowSet, "set", resolve.AllowSet, "allow set data type") + flag.BoolVar(&resolve.AllowLambda, "lambda", resolve.AllowLambda, "allow lambda expressions") + flag.BoolVar(&resolve.AllowNestedDef, "nesteddef", resolve.AllowNestedDef, "allow nested def statements") + flag.BoolVar(&resolve.AllowBitwise, "bitwise", resolve.AllowBitwise, "allow bitwise operations (&, |, ^, ~, <<, and >>)") +} + +func main() { + log.SetPrefix("starlark: ") + log.SetFlags(0) + flag.Parse() + + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + log.Fatal(err) + } + if err := pprof.StartCPUProfile(f); err != nil { + log.Fatal(err) + } + defer pprof.StopCPUProfile() + } + + thread := &starlark.Thread{Load: repl.MakeLoad()} + globals := make(starlark.StringDict) + + switch len(flag.Args()) { + case 0: + fmt.Println("Welcome to Starlark (github.com/google/starlark)") + repl.REPL(thread, globals) + case 1: + // Execute specified file. + filename := flag.Args()[0] + var err error + globals, err = starlark.ExecFile(thread, filename, nil, nil) + if err != nil { + repl.PrintError(err) + os.Exit(1) + } + default: + log.Fatal("want at most one Starlark file name") + } + + // Print the global environment. + if *showenv { + var names []string + for name := range globals { + if !strings.HasPrefix(name, "_") { + names = append(names, name) + } + } + sort.Strings(names) + for _, name := range names { + fmt.Fprintf(os.Stderr, "%s = %s\n", name, globals[name]) + } + } +} |