xref: /aosp_15_r20/external/starlark-go/README.md (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
1*4947cdc7SCole Faust
2*4947cdc7SCole Faust<!-- This file is the project homepage for go.starlark.net -->
3*4947cdc7SCole Faust
4*4947cdc7SCole Faust# Starlark in Go
5*4947cdc7SCole Faust
6*4947cdc7SCole Faust[![Travis CI](https://travis-ci.org/google/starlark-go.svg)](https://travis-ci.org/google/starlark-go)
7*4947cdc7SCole Faust[![GoDoc](https://godoc.org/go.starlark.net/starlark?status.svg)](https://godoc.org/go.starlark.net/starlark)
8*4947cdc7SCole Faust
9*4947cdc7SCole FaustThis is the home of the _Starlark in Go_ project.
10*4947cdc7SCole FaustStarlark in Go is an interpreter for Starlark, implemented in Go.
11*4947cdc7SCole FaustStarlark was formerly known as Skylark.
12*4947cdc7SCole FaustThe new import path for Go packages is `"go.starlark.net/starlark"`.
13*4947cdc7SCole Faust
14*4947cdc7SCole FaustStarlark is a dialect of Python intended for use as a configuration language.
15*4947cdc7SCole FaustLike Python, it is an untyped dynamic language with high-level data
16*4947cdc7SCole Fausttypes, first-class functions with lexical scope, and garbage collection.
17*4947cdc7SCole FaustUnlike CPython, independent Starlark threads execute in parallel, so
18*4947cdc7SCole FaustStarlark workloads scale well on parallel machines.
19*4947cdc7SCole FaustStarlark is a small and simple language with a familiar and highly
20*4947cdc7SCole Faustreadable syntax. You can use it as an expressive notation for
21*4947cdc7SCole Fauststructured data, defining functions to eliminate repetition, or you
22*4947cdc7SCole Faustcan use it to add scripting capabilities to an existing application.
23*4947cdc7SCole Faust
24*4947cdc7SCole FaustA Starlark interpreter is typically embedded within a larger
25*4947cdc7SCole Faustapplication, and the application may define additional domain-specific
26*4947cdc7SCole Faustfunctions and data types beyond those provided by the core language.
27*4947cdc7SCole FaustFor example, Starlark was originally developed for the
28*4947cdc7SCole Faust[Bazel build tool](https://bazel.build).
29*4947cdc7SCole FaustBazel uses Starlark as the notation both for its BUILD files (like
30*4947cdc7SCole FaustMakefiles, these declare the executables, libraries, and tests in a
31*4947cdc7SCole Faustdirectory) and for [its macro
32*4947cdc7SCole Faustlanguage](https://docs.bazel.build/versions/master/skylark/language.html),
33*4947cdc7SCole Faustthrough which Bazel is extended with custom logic to support new
34*4947cdc7SCole Faustlanguages and compilers.
35*4947cdc7SCole Faust
36*4947cdc7SCole Faust
37*4947cdc7SCole Faust## Documentation
38*4947cdc7SCole Faust
39*4947cdc7SCole Faust* Language definition: [doc/spec.md](doc/spec.md)
40*4947cdc7SCole Faust
41*4947cdc7SCole Faust* About the Go implementation: [doc/impl.md](doc/impl.md)
42*4947cdc7SCole Faust
43*4947cdc7SCole Faust* API documentation: [godoc.org/go.starlark.net/starlark](https://godoc.org/go.starlark.net/starlark)
44*4947cdc7SCole Faust
45*4947cdc7SCole Faust* Mailing list: [starlark-go](https://groups.google.com/forum/#!forum/starlark-go)
46*4947cdc7SCole Faust
47*4947cdc7SCole Faust* Issue tracker: [https://github.com/google/starlark-go/issues](https://github.com/google/starlark-go/issues)
48*4947cdc7SCole Faust
49*4947cdc7SCole Faust### Getting started
50*4947cdc7SCole Faust
51*4947cdc7SCole FaustBuild the code:
52*4947cdc7SCole Faust
53*4947cdc7SCole Faust```shell
54*4947cdc7SCole Faust# check out the code and dependencies,
55*4947cdc7SCole Faust# and install interpreter in $GOPATH/bin
56*4947cdc7SCole Faust$ go get -u go.starlark.net/cmd/starlark
57*4947cdc7SCole Faust```
58*4947cdc7SCole Faust
59*4947cdc7SCole FaustRun the interpreter:
60*4947cdc7SCole Faust
61*4947cdc7SCole Faust```console
62*4947cdc7SCole Faust$ cat coins.star
63*4947cdc7SCole Faustcoins = {
64*4947cdc7SCole Faust  'dime': 10,
65*4947cdc7SCole Faust  'nickel': 5,
66*4947cdc7SCole Faust  'penny': 1,
67*4947cdc7SCole Faust  'quarter': 25,
68*4947cdc7SCole Faust}
69*4947cdc7SCole Faustprint('By name:\t' + ', '.join(sorted(coins.keys())))
70*4947cdc7SCole Faustprint('By value:\t' + ', '.join(sorted(coins.keys(), key=coins.get)))
71*4947cdc7SCole Faust
72*4947cdc7SCole Faust$ starlark coins.star
73*4947cdc7SCole FaustBy name:	dime, nickel, penny, quarter
74*4947cdc7SCole FaustBy value:	penny, nickel, dime, quarter
75*4947cdc7SCole Faust```
76*4947cdc7SCole Faust
77*4947cdc7SCole FaustInteract with the read-eval-print loop (REPL):
78*4947cdc7SCole Faust
79*4947cdc7SCole Faust```pycon
80*4947cdc7SCole Faust$ starlark
81*4947cdc7SCole Faust>>> def fibonacci(n):
82*4947cdc7SCole Faust...    res = list(range(n))
83*4947cdc7SCole Faust...    for i in res[2:]:
84*4947cdc7SCole Faust...        res[i] = res[i-2] + res[i-1]
85*4947cdc7SCole Faust...    return res
86*4947cdc7SCole Faust...
87*4947cdc7SCole Faust>>> fibonacci(10)
88*4947cdc7SCole Faust[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
89*4947cdc7SCole Faust>>>
90*4947cdc7SCole Faust```
91*4947cdc7SCole Faust
92*4947cdc7SCole FaustWhen you have finished, type `Ctrl-D` to close the REPL's input stream.
93*4947cdc7SCole Faust
94*4947cdc7SCole FaustEmbed the interpreter in your Go program:
95*4947cdc7SCole Faust
96*4947cdc7SCole Faust```go
97*4947cdc7SCole Faustimport "go.starlark.net/starlark"
98*4947cdc7SCole Faust
99*4947cdc7SCole Faust// Execute Starlark program in a file.
100*4947cdc7SCole Faustthread := &starlark.Thread{Name: "my thread"}
101*4947cdc7SCole Faustglobals, err := starlark.ExecFile(thread, "fibonacci.star", nil, nil)
102*4947cdc7SCole Faustif err != nil { ... }
103*4947cdc7SCole Faust
104*4947cdc7SCole Faust// Retrieve a module global.
105*4947cdc7SCole Faustfibonacci := globals["fibonacci"]
106*4947cdc7SCole Faust
107*4947cdc7SCole Faust// Call Starlark function from Go.
108*4947cdc7SCole Faustv, err := starlark.Call(thread, fibonacci, starlark.Tuple{starlark.MakeInt(10)}, nil)
109*4947cdc7SCole Faustif err != nil { ... }
110*4947cdc7SCole Faustfmt.Printf("fibonacci(10) = %v\n", v) // fibonacci(10) = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
111*4947cdc7SCole Faust```
112*4947cdc7SCole Faust
113*4947cdc7SCole FaustSee [starlark/example_test.go](starlark/example_test.go) for more examples.
114*4947cdc7SCole Faust
115*4947cdc7SCole Faust### Contributing
116*4947cdc7SCole Faust
117*4947cdc7SCole FaustWe welcome submissions but please let us know what you're working on
118*4947cdc7SCole Faustif you want to change or add to the Starlark repository.
119*4947cdc7SCole Faust
120*4947cdc7SCole FaustBefore undertaking to write something new for the Starlark project,
121*4947cdc7SCole Faustplease file an issue or claim an existing issue.
122*4947cdc7SCole FaustAll significant changes to the language or to the interpreter's Go
123*4947cdc7SCole FaustAPI must be discussed before they can be accepted.
124*4947cdc7SCole FaustThis gives all participants a chance to validate the design and to
125*4947cdc7SCole Faustavoid duplication of effort.
126*4947cdc7SCole Faust
127*4947cdc7SCole FaustDespite some differences, the Go implementation of Starlark strives to
128*4947cdc7SCole Faustmatch the behavior of [the Java implementation](https://github.com/bazelbuild/bazel)
129*4947cdc7SCole Faustused by Bazel and maintained by the Bazel team.
130*4947cdc7SCole FaustFor that reason, proposals to change the language itself should
131*4947cdc7SCole Faustgenerally be directed to [the Starlark site](
132*4947cdc7SCole Fausthttps://github.com/bazelbuild/starlark/), not to the maintainers of this
133*4947cdc7SCole Faustproject.
134*4947cdc7SCole FaustOnly once there is consensus that a language change is desirable may
135*4947cdc7SCole Faustits Go implementation proceed.
136*4947cdc7SCole Faust
137*4947cdc7SCole FaustWe use GitHub pull requests for contributions.
138*4947cdc7SCole Faust
139*4947cdc7SCole FaustPlease complete Google's contributor license agreement (CLA) before
140*4947cdc7SCole Faustsending your first change to the project.  If you are the copyright
141*4947cdc7SCole Faustholder, you will need to agree to the
142*4947cdc7SCole Faust[individual contributor license agreement](https://cla.developers.google.com/about/google-individual),
143*4947cdc7SCole Faustwhich can be completed online.
144*4947cdc7SCole FaustIf your organization is the copyright holder, the organization will
145*4947cdc7SCole Faustneed to agree to the [corporate contributor license agreement](https://cla.developers.google.com/about/google-corporate).
146*4947cdc7SCole FaustIf the copyright holder for your contribution has already completed
147*4947cdc7SCole Faustthe agreement in connection with another Google open source project,
148*4947cdc7SCole Faustit does not need to be completed again.
149*4947cdc7SCole Faust
150*4947cdc7SCole Faust### Stability
151*4947cdc7SCole Faust
152*4947cdc7SCole FaustWe reserve the right to make breaking language and API changes at this
153*4947cdc7SCole Fauststage in the project, although we will endeavor to keep them to a minimum.
154*4947cdc7SCole FaustOnce the Bazel team has finalized the version 1 language specification,
155*4947cdc7SCole Faustwe will be more rigorous with interface stability.
156*4947cdc7SCole Faust
157*4947cdc7SCole Faust### Credits
158*4947cdc7SCole Faust
159*4947cdc7SCole FaustStarlark was designed and implemented in Java by
160*4947cdc7SCole FaustUlf Adams,
161*4947cdc7SCole FaustLukács Berki,
162*4947cdc7SCole FaustJon Brandvein,
163*4947cdc7SCole FaustJohn Field,
164*4947cdc7SCole FaustLaurent Le Brun,
165*4947cdc7SCole FaustDmitry Lomov,
166*4947cdc7SCole FaustDamien Martin-Guillerez,
167*4947cdc7SCole FaustVladimir Moskva, and
168*4947cdc7SCole FaustFlorian Weikert,
169*4947cdc7SCole Fauststanding on the shoulders of the Python community.
170*4947cdc7SCole FaustThe Go implementation was written by Alan Donovan and Jay Conrod;
171*4947cdc7SCole Faustits scanner was derived from one written by Russ Cox.
172*4947cdc7SCole Faust
173*4947cdc7SCole Faust### Legal
174*4947cdc7SCole Faust
175*4947cdc7SCole FaustStarlark in Go is Copyright (c) 2018 The Bazel Authors.
176*4947cdc7SCole FaustAll rights reserved.
177*4947cdc7SCole Faust
178*4947cdc7SCole FaustIt is provided under a 3-clause BSD license:
179*4947cdc7SCole Faust[LICENSE](https://github.com/google/starlark-go/blob/master/LICENSE).
180*4947cdc7SCole Faust
181*4947cdc7SCole FaustStarlark in Go is not an official Google product.
182