xref: /aosp_15_r20/external/starlark-go/cmd/starlark/starlark.go (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
1// Copyright 2017 The Bazel 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// The starlark command interprets a Starlark file.
6// With no arguments, it starts a read-eval-print loop (REPL).
7package main // import "go.starlark.net/cmd/starlark"
8
9import (
10	"flag"
11	"fmt"
12	"log"
13	"os"
14	"runtime"
15	"runtime/pprof"
16	"strings"
17
18	"go.starlark.net/internal/compile"
19	"go.starlark.net/repl"
20	"go.starlark.net/resolve"
21	"go.starlark.net/starlark"
22	"go.starlark.net/starlarkjson"
23)
24
25// flags
26var (
27	cpuprofile = flag.String("cpuprofile", "", "gather Go CPU profile in this file")
28	memprofile = flag.String("memprofile", "", "gather Go memory profile in this file")
29	profile    = flag.String("profile", "", "gather Starlark time profile in this file")
30	showenv    = flag.Bool("showenv", false, "on success, print final global environment")
31	execprog   = flag.String("c", "", "execute program `prog`")
32)
33
34func init() {
35	flag.BoolVar(&compile.Disassemble, "disassemble", compile.Disassemble, "show disassembly during compilation of each function")
36
37	// non-standard dialect flags
38	flag.BoolVar(&resolve.AllowFloat, "float", resolve.AllowFloat, "obsolete; no effect")
39	flag.BoolVar(&resolve.AllowSet, "set", resolve.AllowSet, "allow set data type")
40	flag.BoolVar(&resolve.AllowLambda, "lambda", resolve.AllowLambda, "allow lambda expressions")
41	flag.BoolVar(&resolve.AllowRecursion, "recursion", resolve.AllowRecursion, "allow while statements and recursive functions")
42	flag.BoolVar(&resolve.AllowGlobalReassign, "globalreassign", resolve.AllowGlobalReassign, "allow reassignment of globals, and if/for/while statements at top level")
43}
44
45func main() {
46	os.Exit(doMain())
47}
48
49func doMain() int {
50	log.SetPrefix("starlark: ")
51	log.SetFlags(0)
52	flag.Parse()
53
54	if *cpuprofile != "" {
55		f, err := os.Create(*cpuprofile)
56		check(err)
57		err = pprof.StartCPUProfile(f)
58		check(err)
59		defer func() {
60			pprof.StopCPUProfile()
61			err := f.Close()
62			check(err)
63		}()
64	}
65	if *memprofile != "" {
66		f, err := os.Create(*memprofile)
67		check(err)
68		defer func() {
69			runtime.GC()
70			err := pprof.Lookup("heap").WriteTo(f, 0)
71			check(err)
72			err = f.Close()
73			check(err)
74		}()
75	}
76
77	if *profile != "" {
78		f, err := os.Create(*profile)
79		check(err)
80		err = starlark.StartProfile(f)
81		check(err)
82		defer func() {
83			err := starlark.StopProfile()
84			check(err)
85		}()
86	}
87
88	thread := &starlark.Thread{Load: repl.MakeLoad()}
89	globals := make(starlark.StringDict)
90
91	// Ideally this statement would update the predeclared environment.
92	// TODO(adonovan): plumb predeclared env through to the REPL.
93	starlark.Universe["json"] = starlarkjson.Module
94
95	switch {
96	case flag.NArg() == 1 || *execprog != "":
97		var (
98			filename string
99			src      interface{}
100			err      error
101		)
102		if *execprog != "" {
103			// Execute provided program.
104			filename = "cmdline"
105			src = *execprog
106		} else {
107			// Execute specified file.
108			filename = flag.Arg(0)
109		}
110		thread.Name = "exec " + filename
111		globals, err = starlark.ExecFile(thread, filename, src, nil)
112		if err != nil {
113			repl.PrintError(err)
114			return 1
115		}
116	case flag.NArg() == 0:
117		fmt.Println("Welcome to Starlark (go.starlark.net)")
118		thread.Name = "REPL"
119		repl.REPL(thread, globals)
120	default:
121		log.Print("want at most one Starlark file name")
122		return 1
123	}
124
125	// Print the global environment.
126	if *showenv {
127		for _, name := range globals.Keys() {
128			if !strings.HasPrefix(name, "_") {
129				fmt.Fprintf(os.Stderr, "%s = %s\n", name, globals[name])
130			}
131		}
132	}
133
134	return 0
135}
136
137func check(err error) {
138	if err != nil {
139		log.Fatal(err)
140	}
141}
142