starlark_compiler 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +21 -0
- data/.vscode/launch.json +23 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +36 -8
- data/lib/starlark_compiler/ast.rb +37 -1
- data/lib/starlark_compiler/build_file.rb +10 -1
- data/lib/starlark_compiler/version.rb +1 -1
- data/lib/starlark_compiler/writer.rb +34 -0
- data/starlark_compiler.gemspec +1 -1
- metadata +11 -9
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b487b7ec8f95d4d08b340c2e6872d38606a3cda406c44189e04cfcc55829ae7b
|
4
|
+
data.tar.gz: ebf0df91ab7dffdab555150b9f0e6ecb44b6faea915fb55bd052bd482da4e0be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8db37b777d13cbf3b29ecd9e138d6dc80240f2338226b9b57a29d4abae31f420935b25ed151ec14d975e724103e547b29d8d8cc3ebd965dbcfc62d11e6b23d00
|
7
|
+
data.tar.gz: '0040583536df2b938c70f3c2ce39f3230be444adfa6bd9c365b9ae42808ee2db1dbc895bc66a8b2d000e9eee0d5d6879cb08a3354ee8be1b32d227b0bc106d49'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
name: Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
push:
|
6
|
+
branches:
|
7
|
+
- master
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
integration_tests:
|
11
|
+
name: Build and Test
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v1
|
15
|
+
- uses: ruby/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: "2.6" # Version range or exact version of a Ruby version to use, using semvers version range syntax.
|
18
|
+
- name: Install Gems
|
19
|
+
run: bundle install
|
20
|
+
- name: Build and Test
|
21
|
+
run: bundle exec rake
|
data/.vscode/launch.json
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
3
|
+
// Hover to view descriptions of existing attributes.
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
5
|
+
"version": "0.2.0",
|
6
|
+
"configurations": [
|
7
|
+
{
|
8
|
+
"name": "RSpec - active spec file only",
|
9
|
+
"type": "Ruby",
|
10
|
+
"request": "launch",
|
11
|
+
"useBundler": true,
|
12
|
+
"pathToBundler": "/Users/yuanfeng/.rvm/gems/ruby-2.6.1/wrappers/bundle",
|
13
|
+
"pathToRDebugIDE": "/Users/yuanfeng/.rvm/gems/ruby-2.6.1/gems/ruby-debug-ide-0.7.2",
|
14
|
+
"debuggerPort": "1240",
|
15
|
+
"program": "/Users/yuanfeng/.rvm/gems/ruby-2.6.1/bin/rspec",
|
16
|
+
"args": [
|
17
|
+
"-I",
|
18
|
+
"${workspaceRoot}",
|
19
|
+
"${file}"
|
20
|
+
]
|
21
|
+
}
|
22
|
+
]
|
23
|
+
}
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,9 +20,9 @@ Or install it yourself as:
|
|
20
20
|
$ gem install starlark_compiler
|
21
21
|
|
22
22
|
## Usage
|
23
|
-
|
23
|
+
The following code:
|
24
24
|
```ruby
|
25
|
-
|
25
|
+
ast = StarlarkCompiler::AST.build do
|
26
26
|
ast = StarlarkCompiler::AST.new(toplevel: [])
|
27
27
|
ast << function_call(
|
28
28
|
'load',
|
@@ -39,20 +39,48 @@ starlark_string = StarlarkCompiler::AST.build do
|
|
39
39
|
'ios_library',
|
40
40
|
name: string('App_Objc'),
|
41
41
|
srcs: function_call('glob', array([string('Sources/**/*.swift')])) +
|
42
|
-
|
42
|
+
['A.swift']
|
43
43
|
)
|
44
|
+
ast << variable_assignment('deps', array([':App_Objc'])
|
45
|
+
ast << variable_assignment('numbers', array([1,2,3]))
|
44
46
|
ast << function_call(
|
45
47
|
'ios_application',
|
46
48
|
name: string('App'),
|
47
|
-
deps:
|
48
|
-
string(':App_Objc')
|
49
|
-
]),
|
49
|
+
deps: variable_reference('deps'),
|
50
50
|
entitlements: array([string(':App.entitlements')])
|
51
51
|
)
|
52
|
-
|
52
|
+
ast
|
53
53
|
end
|
54
|
+
starlark_string = StarlarkCompiler::Writer.write(ast: ast, io: +'')
|
55
|
+
```
|
56
|
+
will populate `starlark_string` with:
|
57
|
+
```python
|
58
|
+
load(
|
59
|
+
"@bazel_build_rules_apple//rules:ios.bzl",
|
60
|
+
"ios_application",
|
61
|
+
_ios_application = "ios_application",
|
62
|
+
)
|
63
|
+
load("@bazel_build_rules_apple//rules:ios.bzl", "ios_application")
|
64
|
+
|
65
|
+
ios_library(
|
66
|
+
name = "App_Objc",
|
67
|
+
srcs = glob(["Sources/**/*.swift"]) + ["A.swift"],
|
68
|
+
)
|
69
|
+
|
70
|
+
deps = [":App_Objc"]
|
71
|
+
|
72
|
+
numbers = [
|
73
|
+
1,
|
74
|
+
2,
|
75
|
+
3,
|
76
|
+
]
|
77
|
+
|
78
|
+
ios_application(
|
79
|
+
name = "App",
|
80
|
+
deps = deps,
|
81
|
+
entitlements = [":App.entitlements"],
|
82
|
+
)
|
54
83
|
```
|
55
|
-
|
56
84
|
## Development
|
57
85
|
|
58
86
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -46,6 +46,8 @@ module StarlarkCompiler
|
|
46
46
|
end
|
47
47
|
|
48
48
|
class Node
|
49
|
+
TYPE = :Expression
|
50
|
+
|
49
51
|
%i[- + / * % == < <= >= >].each do |op|
|
50
52
|
define_method(op) { |rhs| BinaryOperator.new(self, rhs, operator: op) }
|
51
53
|
end
|
@@ -73,7 +75,7 @@ module StarlarkCompiler
|
|
73
75
|
when Numeric
|
74
76
|
Number.new(obj)
|
75
77
|
else
|
76
|
-
raise Error, "#{obj.inspect} not convertible to Node"
|
78
|
+
raise Error, "Ruby stdlib type #{obj.inspect} not convertible to Node"
|
77
79
|
end
|
78
80
|
end
|
79
81
|
end
|
@@ -101,6 +103,17 @@ module StarlarkCompiler
|
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
106
|
+
class VariableReference < Node
|
107
|
+
attr_reader :var
|
108
|
+
def initialize(var)
|
109
|
+
unless var.is_a?(::String)
|
110
|
+
raise "Variable Reference must be a string, not #{var.class}"
|
111
|
+
end
|
112
|
+
|
113
|
+
@var = var
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
104
117
|
class Array < Node
|
105
118
|
attr_reader :elements
|
106
119
|
def initialize(elements)
|
@@ -108,6 +121,19 @@ module StarlarkCompiler
|
|
108
121
|
end
|
109
122
|
end
|
110
123
|
|
124
|
+
class VariableAssignment < Node
|
125
|
+
TYPE = :Statement
|
126
|
+
attr_reader :name, :var
|
127
|
+
def initialize(name, var)
|
128
|
+
@name = name
|
129
|
+
if node(var).class::TYPE != :Expression
|
130
|
+
raise "Unsupported type on rhs for assignment: #{var.class}"
|
131
|
+
end
|
132
|
+
|
133
|
+
@var = node(var)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
111
137
|
class FunctionCall < Node
|
112
138
|
attr_reader :name, :args, :kwargs
|
113
139
|
def initialize(name, *args, **kwargs)
|
@@ -117,6 +143,16 @@ module StarlarkCompiler
|
|
117
143
|
end
|
118
144
|
end
|
119
145
|
|
146
|
+
class FunctionDeclaration < Node
|
147
|
+
attr_reader :name, :args, :body, :kwargs
|
148
|
+
def initialize(name, args, body, **kwargs)
|
149
|
+
@name = name
|
150
|
+
@args = args.map(&method(:node))
|
151
|
+
@body = body.map(&method(:node))
|
152
|
+
@kwargs = kwargs.transform_values(&method(:node))
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
120
156
|
class MethodCall < Node
|
121
157
|
end
|
122
158
|
|
@@ -8,6 +8,7 @@ module StarlarkCompiler
|
|
8
8
|
def initialize(package:, workspace: Dir.pwd)
|
9
9
|
@loads = Hash.new { |h, k| h[k] = Set.new }
|
10
10
|
@targets = {}
|
11
|
+
@variable_assignments = {}
|
11
12
|
@package = package
|
12
13
|
@workspace = workspace
|
13
14
|
@path = File.join(@workspace, @package, 'BUILD.bazel')
|
@@ -26,6 +27,10 @@ module StarlarkCompiler
|
|
26
27
|
@targets[name] = function_call
|
27
28
|
end
|
28
29
|
|
30
|
+
def add_variable_assignment(name:, var:)
|
31
|
+
@variable_assignments[name] = var
|
32
|
+
end
|
33
|
+
|
29
34
|
def save!
|
30
35
|
File.open(@path, 'w') do |f|
|
31
36
|
Writer.write(ast: to_starlark, io: f)
|
@@ -36,10 +41,14 @@ module StarlarkCompiler
|
|
36
41
|
loads = @loads
|
37
42
|
.sort_by { |k, _| k }
|
38
43
|
.map { |f, fn| AST.build { function_call('load', f, *fn.sort) } }
|
44
|
+
variable_assignments = @variable_assignments
|
45
|
+
.map do |name, var|
|
46
|
+
AST.build { variable_assignment(name, var) }
|
47
|
+
end
|
39
48
|
targets = @targets
|
40
49
|
.sort_by { |k, _| k }
|
41
50
|
.map { |_f, fn| normalize_function_call_kwargs(fn) }
|
42
|
-
AST.new(toplevel: loads + targets)
|
51
|
+
AST.new(toplevel: loads + variable_assignments + targets)
|
43
52
|
end
|
44
53
|
|
45
54
|
private
|
@@ -97,6 +97,36 @@ module StarlarkCompiler
|
|
97
97
|
io << ')'
|
98
98
|
end
|
99
99
|
|
100
|
+
def write_function_declaration(declaration)
|
101
|
+
io << 'def ' << declaration.name << '('
|
102
|
+
final_index = declaration.args.size.pred
|
103
|
+
declaration.args.each_with_index do |arg, idx|
|
104
|
+
indented(single_line: false) do |indenter|
|
105
|
+
indenter.write_newline
|
106
|
+
write_node(arg)
|
107
|
+
indenter.write_comma unless final_index == idx
|
108
|
+
end
|
109
|
+
end
|
110
|
+
write_newline
|
111
|
+
io << '):'
|
112
|
+
final_index = declaration.body.size.pred
|
113
|
+
declaration.body.each do |arg|
|
114
|
+
indented(single_line: false) do |indenter|
|
115
|
+
indenter.write_newline
|
116
|
+
write_node(arg)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def write_variable_reference(variable)
|
122
|
+
io << variable.var
|
123
|
+
end
|
124
|
+
|
125
|
+
def write_variable_assignment(assignment)
|
126
|
+
io << assignment.name << ' = '
|
127
|
+
write_node(assignment.var)
|
128
|
+
end
|
129
|
+
|
100
130
|
def write_array(array)
|
101
131
|
single_line = single_line?(array)
|
102
132
|
io << '['
|
@@ -203,5 +233,9 @@ module StarlarkCompiler
|
|
203
233
|
dictionary.elements.size <= 1 &&
|
204
234
|
dictionary.elements.each_key.all?(&method(:single_line?))
|
205
235
|
end
|
236
|
+
|
237
|
+
def single_line_variable_reference?(_)
|
238
|
+
true
|
239
|
+
end
|
206
240
|
end
|
207
241
|
end
|
data/starlark_compiler.gemspec
CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
26
|
spec.require_paths = ['lib']
|
27
27
|
|
28
|
-
spec.required_ruby_version = '
|
28
|
+
spec.required_ruby_version = '>= 2.6'
|
29
29
|
|
30
30
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
31
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: starlark_compiler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Giddins
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,17 +24,19 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.0'
|
27
|
-
description:
|
27
|
+
description:
|
28
28
|
email:
|
29
29
|
- segiddins@segiddins.me
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- ".github/workflows/tests.yml"
|
34
35
|
- ".gitignore"
|
35
36
|
- ".rspec"
|
36
37
|
- ".rubocop.yml"
|
37
|
-
- ".
|
38
|
+
- ".vscode/launch.json"
|
39
|
+
- CHANGELOG.md
|
38
40
|
- CODE_OF_CONDUCT.md
|
39
41
|
- Gemfile
|
40
42
|
- Gemfile.lock
|
@@ -55,13 +57,13 @@ licenses:
|
|
55
57
|
metadata:
|
56
58
|
homepage_uri: https://github.com/segiddins/starlark_compiler
|
57
59
|
source_code_uri: https://github.com/segiddins/starlark_compiler
|
58
|
-
post_install_message:
|
60
|
+
post_install_message:
|
59
61
|
rdoc_options: []
|
60
62
|
require_paths:
|
61
63
|
- lib
|
62
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
65
|
requirements:
|
64
|
-
- - "
|
66
|
+
- - ">="
|
65
67
|
- !ruby/object:Gem::Version
|
66
68
|
version: '2.6'
|
67
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -70,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
72
|
- !ruby/object:Gem::Version
|
71
73
|
version: '0'
|
72
74
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
74
|
-
signing_key:
|
75
|
+
rubygems_version: 3.4.1
|
76
|
+
signing_key:
|
75
77
|
specification_version: 4
|
76
78
|
summary: A starlark gem
|
77
79
|
test_files: []
|