Convert Figma logo to code with AI

google logoskylark

Skylark in Go: the Skylark configuration language, implemented in Go [MOVED to go.starlark.net]

1,187
74
1,187
6

Top Related Projects

Starlark Language

Common Expression Language -- specification and binary representation

Maintainable configuration files

5,227

HCL is the HashiCorp configuration language.

19,408

Tom's Obvious, Minimal Language

Quick Overview

Skylark is a configuration language developed by Google, designed for use in build systems and other configuration tasks. It is a dialect of Python with a restricted feature set, aiming to provide a simple, deterministic, and hermetic language for configuration purposes.

Pros

  • Simple and easy to learn, especially for those familiar with Python
  • Deterministic execution, ensuring consistent results across different environments
  • Hermetic design, preventing unintended side effects and improving security
  • Extensible through custom built-in functions and modules

Cons

  • Limited feature set compared to full-fledged programming languages
  • Lack of widespread adoption outside of Google's ecosystem
  • Potential learning curve for those not familiar with Python-like syntax
  • Limited documentation and community support compared to more popular languages

Code Examples

  1. Defining a function and using it:
def greet(name):
    return "Hello, " + name + "!"

print(greet("Skylark"))
  1. Working with lists and dictionaries:
fruits = ["apple", "banana", "cherry"]
prices = {"apple": 1.0, "banana": 0.5, "cherry": 2.0}

for fruit in fruits:
    print(fruit + " costs $" + str(prices[fruit]))
  1. Conditional statements and loops:
def is_even(n):
    return n % 2 == 0

numbers = range(1, 11)
even_sum = 0

for num in numbers:
    if is_even(num):
        even_sum += num

print("Sum of even numbers:", even_sum)

Getting Started

To use Skylark, you typically need to integrate it into a build system or configuration tool that supports it. Here's a basic example of how to use Skylark in a Bazel BUILD file:

load("@bazel_skylib//lib:paths.bzl", "paths")

py_library(
    name = "my_library",
    srcs = glob(["*.py"]),
    deps = [
        "//some/other:library",
        "@external_repo//:dependency",
    ],
)

py_test(
    name = "my_test",
    srcs = ["test.py"],
    deps = [":my_library"],
)

To use Skylark standalone, you would need to implement a custom interpreter or use an existing one that supports Skylark syntax.

Competitor Comparisons

Starlark Language

Pros of Starlark

  • More actively maintained with frequent updates and contributions
  • Broader community support and adoption within the Bazel ecosystem
  • Enhanced documentation and examples for easier implementation

Cons of Starlark

  • Potentially more complex due to additional features and integrations
  • May have a steeper learning curve for newcomers to the Bazel ecosystem
  • Slightly larger codebase, which could impact performance in some cases

Code Comparison

Skylark:

def my_rule_impl(ctx):
    # Implementation details
    return struct(
        files = depset([ctx.outputs.out])
    )

my_rule = rule(
    implementation = my_rule_impl,
    attrs = {"deps": attr.label_list()},
    outputs = {"out": "%{name}.out"},
)

Starlark:

def my_rule_impl(ctx):
    # Implementation details
    return [DefaultInfo(files = depset([ctx.outputs.out]))]

my_rule = rule(
    implementation = my_rule_impl,
    attrs = {"deps": attr.label_list()},
    outputs = {"out": "%{name}.out"},
)

The code examples show minor syntax differences, with Starlark using DefaultInfo instead of struct for returning rule information.

Pros of go-jsonnet

  • Native Go implementation, offering better performance and easier integration with Go projects
  • Supports advanced Jsonnet features like import callbacks and native extensions
  • Actively maintained with regular updates and bug fixes

Cons of go-jsonnet

  • Limited to Jsonnet language, while Skylark is more versatile and Python-like
  • Steeper learning curve for those unfamiliar with Jsonnet syntax
  • Less extensive standard library compared to Skylark

Code Comparison

Skylark:

def greet(name):
    return "Hello, " + name + "!"

result = greet("World")
print(result)

go-jsonnet:

local greet(name) = "Hello, " + name + "!";

{
  result: greet("World")
}

Summary

go-jsonnet is a Go implementation of the Jsonnet language, offering better performance and integration with Go projects. It supports advanced Jsonnet features but has a steeper learning curve compared to Skylark. Skylark, on the other hand, provides a more Python-like syntax and a more extensive standard library, making it more versatile for general-purpose configuration and scripting tasks. The choice between the two depends on the specific project requirements and the team's familiarity with each language.

Common Expression Language -- specification and binary representation

Pros of cel-spec

  • Designed specifically for expression evaluation in configuration and policy scenarios
  • Supports gradual typing, allowing for more flexible and dynamic expressions
  • Offers built-in support for protocol buffers and common data types

Cons of cel-spec

  • Less mature and less widely adopted compared to Skylark
  • More limited in scope, focusing primarily on expression evaluation rather than full scripting capabilities
  • Smaller community and ecosystem of tools and libraries

Code Comparison

Skylark:

def greet(name):
    return "Hello, " + name + "!"

result = greet("World")
print(result)

cel-spec:

name = "World"
greeting = "Hello, " + name + "!"

Summary

Skylark is a more established and feature-rich language for configuration and scripting, while cel-spec focuses on expression evaluation for specific use cases. Skylark offers a Python-like syntax and broader scripting capabilities, making it suitable for more complex configuration scenarios. cel-spec, on the other hand, provides a simpler and more constrained environment optimized for expression evaluation in configuration and policy contexts. The choice between the two depends on the specific requirements of the project and the desired balance between flexibility and simplicity.

Maintainable configuration files

Pros of Dhall

  • Strongly typed, providing better safety and error detection
  • Supports importing from URLs, allowing for distributed configuration
  • Has built-in functions for JSON conversion, making integration easier

Cons of Dhall

  • Steeper learning curve due to its more complex type system
  • Smaller community and ecosystem compared to Skylark
  • Less integration with existing build systems and tools

Code Comparison

Skylark example:

def greet(name):
    return "Hello, " + name + "!"

result = greet("World")
print(result)

Dhall example:

let greet = \(name : Text) -> "Hello, ${name}!"

in  greet "World"

Key Differences

  • Syntax: Skylark uses Python-like syntax, while Dhall has a more functional style
  • Type System: Dhall has a more advanced type system with features like union types
  • Evaluation: Skylark is dynamically evaluated, whereas Dhall is statically evaluated
  • Use Cases: Skylark is primarily used for build configurations, while Dhall is more general-purpose for configuration files

Conclusion

Both Skylark and Dhall aim to provide better solutions for configuration management, but they take different approaches. Skylark focuses on build systems and is more familiar to Python developers, while Dhall offers a more robust type system and functional programming paradigm for general configuration needs.

5,227

HCL is the HashiCorp configuration language.

Pros of HCL

  • Designed specifically for configuration, with a focus on human-readability
  • Native support for JSON-compatible data structures
  • Extensive use in HashiCorp's ecosystem, including Terraform

Cons of HCL

  • Less flexible for general-purpose programming tasks
  • Limited built-in functions compared to Skylark
  • Steeper learning curve for users unfamiliar with HashiCorp tools

Code Comparison

HCL:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "ExampleInstance"
  }
}

Skylark:

aws_instance(
    name = "example",
    ami = "ami-0c55b159cbfafe1f0",
    instance_type = "t2.micro",
    tags = {"Name": "ExampleInstance"},
)

Summary

HCL is tailored for configuration management, particularly within the HashiCorp ecosystem. It offers a readable syntax and native support for JSON-like structures. However, it may be less versatile for general programming tasks compared to Skylark.

Skylark, being more Python-like, provides greater flexibility for scripting and rule definition. It has a broader range of built-in functions but may require more setup for configuration-specific tasks.

The choice between HCL and Skylark depends on the specific use case and ecosystem integration requirements.

19,408

Tom's Obvious, Minimal Language

Pros of TOML

  • Simple and human-readable configuration format
  • Widely adopted across various programming languages
  • Supports a variety of data types, including dates and times

Cons of TOML

  • Less flexible for complex configurations
  • Limited support for nested structures compared to Skylark
  • No built-in support for expressions or functions

Code Comparison

TOML example:

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

Skylark example:

database = struct(
    server = "192.168.1.1",
    ports = [8001, 8001, 8002],
    connection_max = 5000,
    enabled = True,
)

Key Differences

  • Syntax: TOML uses a more INI-like syntax, while Skylark is Python-inspired
  • Functionality: Skylark offers more programming capabilities, including functions and conditionals
  • Use cases: TOML is primarily for configuration files, while Skylark is designed for build systems and more complex scripting

Conclusion

TOML is ideal for simple configuration needs with its easy-to-read format. Skylark, being more powerful and flexible, is better suited for complex build systems and scenarios requiring programmatic logic.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Skylark in Go

Skylark is now called Starlark. The project has moved to go.starlark.net.

This is the home of the Skylark in Go project. Skylark in Go is an interpreter for Skylark, implemented in Go.

Skylark is a dialect of Python intended for use as a configuration language. Like Python, it is an untyped dynamic language with high-level data types, first-class functions with lexical scope, and garbage collection. Unlike CPython, independent Skylark threads execute in parallel, so Skylark workloads scale well on parallel machines. Skylark is a small and simple language with a familiar and highly readable syntax. You can use it as an expressive notation for structured data, defining functions to eliminate repetition, or you can use it to add scripting capabilities to an existing application.

A Skylark interpreter is typically embedded within a larger application, and the application may define additional domain-specific functions and data types beyond those provided by the core language. For example, Skylark was originally developed for the Bazel build tool. Bazel uses Skylark as the notation both for its BUILD files (like Makefiles, these declare the executables, libraries, and tests in a directory) and for its macro language, through which Bazel is extended with custom logic to support new languages and compilers.

Documentation

Getting started

Build the code:

$ go get github.com/google/skylark/...
$ go build github.com/google/skylark/cmd/skylark

Run the interpreter:

$ cat coins.sky
coins = {
  'dime': 10,
  'nickel': 5,
  'penny': 1,
  'quarter': 25,
}
print('By name:\t' + ', '.join(sorted(coins.keys())))
print('By value:\t' + ', '.join(sorted(coins.keys(), key=coins.get)))

$ ./skylark coins.sky
By name:	dime, nickel, penny, quarter
By value:	penny, nickel, dime, quarter

Interact with the read-eval-print loop (REPL):

$ ./skylark
>>> def fibonacci(n):
...    res = list(range(n))
...    for i in res[2:]:
...        res[i] = res[i-2] + res[i-1]
...    return res
...
>>> fibonacci(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>>

When you have finished, type Ctrl-D to close the REPL's input stream.

Contributing

We welcome submissions but please let us know what you're working on if you want to change or add to the Skylark repository.

Before undertaking to write something new for the Skylark project, please file an issue or claim an existing issue. All significant changes to the language or to the interpreter's Go API must be discussed before they can be accepted. This gives all participants a chance to validate the design and to avoid duplication of effort.

Despite some differences, the Go implementation of Skylark strives to match the behavior of the Java implementation used by Bazel. For that reason, proposals to change the language itself should generally be directed to the Bazel team, not to the maintainers of this project. Only once there is consensus that a language change is desirable may its Go implementation proceed.

We use GitHub pull requests for contributions.

Please complete Google's contributor license agreement (CLA) before sending your first change to the project. If you are the copyright holder, you will need to agree to the individual contributor license agreement, which can be completed online. If your organization is the copyright holder, the organization will need to agree to the corporate contributor license agreement. If the copyright holder for your contribution has already completed the agreement in connection with another Google open source project, it does not need to be completed again.

Stability

We reserve the right to make breaking language and API changes at this stage in the project, although we will endeavor to keep them to a minimum. Now that the project's long-term name ("Starlark") has been decided, we plan to copy this repository to github.com/google/starlark and change the canonical import path for all packages to starlark.net/starlark. The current github.com/google/skylark repository will be frozen. Once the Bazel team has finalized the version 1 language specification, we will be more rigorous with interface stability.

Credits

Skylark was designed and implemented in Java by Ulf Adams, Lukács Berki, Jon Brandvein, John Field, Laurent Le Brun, Dmitry Lomov, Damien Martin-Guillerez, Vladimir Moskva, and Florian Weikert, standing on the shoulders of the Python community. The Go implementation was written by Alan Donovan and Jay Conrod; its scanner was derived from one written by Russ Cox.

Legal

Skylark in Go is Copyright (c) 2017 The Bazel Authors. All rights reserved.

It is provided under a 3-clause BSD license: LICENSE.

The name "Skylark" is a code name of the Bazel project. The Bazel team plans to rename the language to "Starlark" to reflect its applicability to projects unrelated to Bazel.

Skylark in Go is not an official Google product.