Skip to content

Commit 1065217

Browse files
authoredFeb 28, 2024··
refactor(gnovm): rename precompiler to transpiler, move to own package (#1681)
Part 1 of several _transpiler_ refactors I intend to perform. This PR tackles one of the first pet peeves I have about the `precompile` tool, which is that the name is not intuitive to what it does. Transpile is a name that not everyone likes. Ultimately, converting one source code to another is also an act of "compilation"; in fact, the TypeScript tool that converts it into JavaScript is called the "TypeScript Compiler". The distinction between transpiler and compiler is blurry; but I think it is useful to call what we have as a _transpiler_ to outline that it is doing a "source-to-source" translation, which I don't think a name like `compile` or `precompile` could. Additionally, this PR moves the source files for the transpiler into its own package, gnovm/pkg/transpiler. One question is to whether the "constants" of the precompiler, like `GnoRealmPkgsPrefixBefore`, should deserve their own package or not.
1 parent f8e67c1 commit 1065217

37 files changed

+171
-166
lines changed
 

‎.github/workflows/examples.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
with:
3333
go-version: ${{ matrix.goversion }}
3434
- run: go install -v ./gnovm/cmd/gno
35-
- run: go run ./gnovm/cmd/gno precompile --verbose --gobuild ./examples
35+
- run: go run ./gnovm/cmd/gno transpile --verbose --gobuild ./examples
3636
test:
3737
strategy:
3838
fail-fast: false

‎PLAN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ the Discord server.
1616
### Gnolang
1717

1818
* Get basic Go tests working _COMPLETE_
19-
* Implement minimal toolsuite (precompile, gnodev)
19+
* Implement minimal toolsuite (transpile, gnodev)
2020
* Tweak/enforce gas limitations
2121
* Implement flat-as-struct supported
2222
* Implement ownership/realm logic; phase 1: no cycles

‎docs/getting-started/local-setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ git clone https://github.com/gnolang/gno.git
2828
## 2. Installing the `gno` development toolkit
2929

3030
Next, we are going to build and install the `gno` development toolkit.
31-
`gno` provides ample functionality to the user, among which is running, precompiling, testing and building `.gno` files.
31+
`gno` provides ample functionality to the user, among which is running, transpiling, testing and building `.gno` files.
3232

3333
To install the toolkit, navigate to the `gnovm` folder from the repository root, and run the `build` make directive:
3434

‎docs/gno-tooling/cli/gno.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ gno {SUB_COMMAND}
1919
| Name | Description |
2020
| ------------ | ------------------------------------------ |
2121
| `test` | Tests a gno package. |
22-
| `precompile` | Precompiles a `.gno` file to a `.go` file. |
22+
| `transpile` | Transpiles a `.gno` file to a `.go` file. |
2323
| `repl` | Starts a GnoVM REPL. |
2424

2525
### `test`
@@ -32,9 +32,9 @@ gno {SUB_COMMAND}
3232
| `root-dir` | String | Clones location of github.com/gnolang/gno (gno tries to guess it). |
3333
| `run` | String | Test name filtering pattern. |
3434
| `timeout` | time.Duration | The maximum execution time in ns. |
35-
| `precompile` | Boolean | Precompiles a `.gno` file to a `.go` file before testing. |
35+
| `transpile` | Boolean | Transpiles a `.gno` file to a `.go` file before testing. |
3636

37-
### `precompile`
37+
### `transpile`
3838

3939
#### **Options**
4040

‎docs/reference/go-gno-compatibility.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Legend:
303303
| + go mod init | gno mod init | same behavior |
304304
| + go mod download | gno mod download | same behavior |
305305
| + go mod tidy | gno mod tidy | same behavior |
306-
| | gno precompile | |
306+
| | gno transpile | |
307307
| go work | | |
308308
| | gno repl | |
309309
| go run | gno run | |

‎examples/Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ help:
99
@echo "Available make commands:"
1010
@cat Makefile | grep '^[a-z][^:]*:' | cut -d: -f1 | sort | sed 's/^/ /'
1111

12-
.PHONY: precompile
13-
precompile:
14-
go run ../gnovm/cmd/gno precompile --verbose .
12+
.PHONY: transpile
13+
transpile:
14+
go run ../gnovm/cmd/gno transpile --verbose .
1515

1616
.PHONY: build
17-
build: precompile
18-
go run ../gnovm/cmd/gno build --verbose .
17+
build:
18+
go run ../gnovm/cmd/gno transpile --verbose --gobuild .
1919

2020
.PHONY: test
2121
test:

‎gno.land/pkg/gnoclient/client_txs.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package gnoclient
22

33
import (
44
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
5-
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
5+
"github.com/gnolang/gno/gnovm/pkg/transpiler"
66
"github.com/gnolang/gno/tm2/pkg/amino"
77
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
88
"github.com/gnolang/gno/tm2/pkg/crypto"
@@ -138,8 +138,8 @@ func (c *Client) Run(cfg BaseTxCfg, msgs ...MsgRun) (*ctypes.ResultBroadcastTxCo
138138

139139
caller := c.Signer.Info().GetAddress()
140140

141-
// Precompile and validate Gno syntax
142-
if err = gno.PrecompileAndCheckMempkg(msg.Package); err != nil {
141+
// Transpile and validate Gno syntax
142+
if err = transpiler.TranspileAndCheckMempkg(msg.Package); err != nil {
143143
return nil, err
144144
}
145145

‎gno.land/pkg/keyscli/addpkg.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
99
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
10+
"github.com/gnolang/gno/gnovm/pkg/transpiler"
1011
"github.com/gnolang/gno/tm2/pkg/amino"
1112
"github.com/gnolang/gno/tm2/pkg/commands"
1213
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
@@ -101,8 +102,8 @@ func execMakeAddPkg(cfg *MakeAddPkgCfg, args []string, io commands.IO) error {
101102
panic(fmt.Sprintf("found an empty package %q", cfg.PkgPath))
102103
}
103104

104-
// precompile and validate syntax
105-
err = gno.PrecompileAndCheckMempkg(memPkg)
105+
// transpile and validate syntax
106+
err = transpiler.TranspileAndCheckMempkg(memPkg)
106107
if err != nil {
107108
panic(err)
108109
}

‎gno.land/pkg/keyscli/run.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
1111
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
12+
"github.com/gnolang/gno/gnovm/pkg/transpiler"
1213
"github.com/gnolang/gno/tm2/pkg/amino"
1314
"github.com/gnolang/gno/tm2/pkg/commands"
1415
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
@@ -108,8 +109,8 @@ func execMakeRun(cfg *MakeRunCfg, args []string, cmdio commands.IO) error {
108109
if memPkg.IsEmpty() {
109110
panic(fmt.Sprintf("found an empty package %q", memPkg.Path))
110111
}
111-
// precompile and validate syntax
112-
err = gno.PrecompileAndCheckMempkg(memPkg)
112+
// transpile and validate syntax
113+
err = transpiler.TranspileAndCheckMempkg(memPkg)
113114
if err != nil {
114115
panic(err)
115116
}

‎gnovm/cmd/gno/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
* `gno run` - run a Gno file
1212
* `gno build` - build a gno package
13-
* `gno precompile` - precompile .gno to .go
13+
* `gno transpile` - transpile .gno to .go
1414
* `gno test` - test a gno package
1515
* `gno mod` - manages dependencies
1616
* `gno repl` start a GnoVM REPL

‎gnovm/cmd/gno/lint_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestLintApp(t *testing.T) {
3131
// TODO: is gno source valid?
3232
// TODO: are dependencies valid?
3333
// TODO: is gno source using unsafe/discouraged features?
34-
// TODO: consider making `gno precompile; go lint *gen.go`
34+
// TODO: consider making `gno transpile; go lint *gen.go`
3535
// TODO: check for imports of native libs from non _test.gno files
3636
}
3737
testMainCaseRun(t, tc)

‎gnovm/cmd/gno/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
2828
newTestCmd(io),
2929
newLintCmd(io),
3030
newRunCmd(io),
31-
newPrecompileCmd(io),
31+
newTranspileCmd(io),
3232
newCleanCmd(io),
3333
newReplCmd(),
3434
newDocCmd(io),

‎gnovm/cmd/gno/test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
2121
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
2222
"github.com/gnolang/gno/gnovm/pkg/gnomod"
23+
"github.com/gnolang/gno/gnovm/pkg/transpiler"
2324
"github.com/gnolang/gno/gnovm/tests"
2425
"github.com/gnolang/gno/tm2/pkg/commands"
2526
"github.com/gnolang/gno/tm2/pkg/errors"
@@ -33,7 +34,7 @@ type testCfg struct {
3334
rootDir string
3435
run string
3536
timeout time.Duration
36-
precompile bool // TODO: precompile should be the default, but it needs to automatically precompile dependencies in memory.
37+
transpile bool // TODO: transpile should be the default, but it needs to automatically transpile dependencies in memory.
3738
updateGoldenTests bool
3839
printRuntimeMetrics bool
3940
withNativeFallback bool
@@ -110,10 +111,10 @@ func (c *testCfg) RegisterFlags(fs *flag.FlagSet) {
110111
)
111112

112113
fs.BoolVar(
113-
&c.precompile,
114-
"precompile",
114+
&c.transpile,
115+
"transpile",
115116
false,
116-
"precompile gno to go before testing",
117+
"transpile gno to go before testing",
117118
)
118119

119120
fs.BoolVar(
@@ -166,15 +167,15 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
166167

167168
verbose := cfg.verbose
168169

169-
tempdirRoot, err := os.MkdirTemp("", "gno-precompile")
170+
tempdirRoot, err := os.MkdirTemp("", "gno-transpile")
170171
if err != nil {
171172
log.Fatal(err)
172173
}
173174
defer os.RemoveAll(tempdirRoot)
174175

175176
// go.mod
176177
modPath := filepath.Join(tempdirRoot, "go.mod")
177-
err = makeTestGoMod(modPath, gno.ImportPrefix, "1.21")
178+
err = makeTestGoMod(modPath, transpiler.ImportPrefix, "1.21")
178179
if err != nil {
179180
return fmt.Errorf("write .mod file: %w", err)
180181
}
@@ -208,14 +209,14 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
208209
buildErrCount := 0
209210
testErrCount := 0
210211
for _, pkg := range subPkgs {
211-
if cfg.precompile {
212+
if cfg.transpile {
212213
if verbose {
213214
io.ErrPrintfln("=== PREC %s", pkg.Dir)
214215
}
215-
precompileOpts := newPrecompileOptions(&precompileCfg{
216+
transpileOpts := newTranspileOptions(&transpileCfg{
216217
output: tempdirRoot,
217218
})
218-
err := precompilePkg(importPath(pkg.Dir), precompileOpts)
219+
err := transpilePkg(importPath(pkg.Dir), transpileOpts)
219220
if err != nil {
220221
io.ErrPrintln(err)
221222
io.ErrPrintln("FAIL")
@@ -233,7 +234,7 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
233234
if err != nil {
234235
return errors.New("cannot resolve build dir")
235236
}
236-
err = goBuildFileOrPkg(tempDir, defaultPrecompileCfg)
237+
err = goBuildFileOrPkg(tempDir, defaultTranspileCfg)
237238
if err != nil {
238239
io.ErrPrintln(err)
239240
io.ErrPrintln("FAIL")
@@ -317,7 +318,7 @@ func gnoTestPkg(
317318
gnoPkgPath = pkgPathFromRootDir(pkgPath, rootDir)
318319
if gnoPkgPath == "" {
319320
// unable to read pkgPath from gno.mod, generate a random realm path
320-
gnoPkgPath = gno.GnoRealmPkgsPrefixBefore + random.RandStr(8)
321+
gnoPkgPath = transpiler.GnoRealmPkgsPrefixBefore + random.RandStr(8)
321322
}
322323
}
323324
memPkg := gno.ReadMemPackage(pkgPath, gnoPkgPath)

‎gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar

-6
This file was deleted.

‎gnovm/cmd/gno/testdata/gno_precompile/02_empty_dir.txtar

-6
This file was deleted.

‎gnovm/cmd/gno/testdata/gno_test/empty_gno1.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gno test .
55
! stdout .+
66
stderr '\? \. \[no test files\]'
77

8-
! gno test --precompile .
8+
! gno test --transpile .
99

1010
! stdout .+
1111
stderr 'expected ''package'', found ''EOF'''

‎gnovm/cmd/gno/testdata/gno_test/empty_gno2.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
! stdout .+
66
stderr 'expected ''package'', found ''EOF'''
77

8-
! gno test --precompile .
8+
! gno test --transpile .
99

1010
! stdout .+
1111
stderr 'expected ''package'', found ''EOF'''

‎gnovm/cmd/gno/testdata/gno_test/empty_gno3.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
! stdout .+
66
stderr 'expected ''package'', found ''EOF'''
77

8-
! gno test --precompile .
8+
! gno test --transpile .
99

1010
! stdout .+
1111
stderr 'expected ''package'', found ''EOF'''

‎gnovm/cmd/gno/testdata/gno_test/failing_filetest.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ stdout 'Machine.RunMain\(\) panic: beep boop'
66
stderr '=== RUN file/failing_filetest.gno'
77
stderr 'panic: fail on failing_filetest.gno: got unexpected error: beep boop'
88

9-
! gno test -verbose --precompile .
9+
! gno test -verbose --transpile .
1010

1111
stdout 'Machine.RunMain\(\) panic: beep boop'
1212
stderr '=== PREC \.'

‎gnovm/cmd/gno/testdata/gno_test/failing_test.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ stderr '=== RUN TestAlwaysFailing'
77
stderr '--- FAIL: TestAlwaysFailing'
88
stderr 'FAIL: 0 build errors, 1 test errors'
99

10-
! gno test -verbose --precompile .
10+
! gno test -verbose --transpile .
1111

1212
! stdout .+
1313
stderr '=== RUN TestAlwaysFailing'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Run gno transpile without args
2+
3+
! gno transpile
4+
5+
! stdout .+
6+
stderr 'USAGE'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Run gno transpile on an empty dir
2+
3+
gno transpile .
4+
5+
! stdout .+
6+
! stderr .+

‎gnovm/cmd/gno/testdata/gno_precompile/03_gno_files_parse_error.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/03_gno_files_parse_error.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Run gno precompile with gno files with parse errors
1+
# Run gno transpile with gno files with parse errors
22

3-
! gno precompile .
3+
! gno transpile .
44

55
! stdout .+
66
stderr '^main.gno:3:1: expected declaration, found invalid$'
77
stderr '^main.gno:7:2: expected declaration, found wrong$'
88
stderr '^sub/sub.gno:3:1: expected declaration, found invalid$'
9-
stderr '^3 precompile error\(s\)$'
9+
stderr '^3 transpile error\(s\)$'
1010

1111
# no *.gen.go files are created
1212
! exec test -f main.gno.gen.go

‎gnovm/cmd/gno/testdata/gno_precompile/04_valid_gno_files.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/04_valid_gno_files.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Run gno precompile with valid gno files
1+
# Run gno transpile with valid gno files
22

3-
gno precompile .
3+
gno transpile .
44

55
! stdout .+
66
! stderr .+

‎gnovm/cmd/gno/testdata/gno_precompile/05_skip_fmt_flag.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/05_skip_fmt_flag.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Run gno precompile with -skip-fmt flag
1+
# Run gno transpile with -skip-fmt flag
22
# NOTE(tb): this flag doesn't actually prevent the code format, because
3-
# `gnolang.Precompile()` calls `format.Node()`.
3+
# `gnolang.Transpile()` calls `format.Node()`.
44

5-
gno precompile -skip-fmt .
5+
gno transpile -skip-fmt .
66

77
! stdout .+
88
! stderr .+

‎gnovm/cmd/gno/testdata/gno_precompile/06_build_flag.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/06_build_flag.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Run gno precompile with -gobuild flag
1+
# Run gno transpile with -gobuild flag
22

3-
gno precompile -gobuild .
3+
gno transpile -gobuild .
44

55
! stdout .+
66
! stderr .+

‎gnovm/cmd/gno/testdata/gno_precompile/07_build_flag_with_build_error.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/07_build_flag_with_build_error.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Run gno precompile with -gobuild flag
1+
# Run gno transpile with -gobuild flag
22

3-
! gno precompile -gobuild .
3+
! gno transpile -gobuild .
44

55
! stdout .+
66
stderr '^main.gno:4:6: x declared and not used$'
77
stderr '^main.gno:5:6: y declared and not used$'
8-
stderr '^2 precompile error\(s\)$'
8+
stderr '^2 transpile error\(s\)$'
99

1010
cmp main.gno.gen.go main.gno.gen.go.golden
1111
cmp sub/sub.gno.gen.go sub/sub.gno.gen.go.golden

‎gnovm/cmd/gno/testdata/gno_precompile/08_build_flag_with_parse_error.txtar ‎gnovm/cmd/gno/testdata/gno_transpile/08_build_flag_with_parse_error.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Run gno precompile with -gobuild flag on file with parse error
1+
# Run gno transpile with -gobuild flag on file with parse error
22

3-
! gno precompile -gobuild .
3+
! gno transpile -gobuild .
44

55
! stdout .+
66
stderr '^main.gno:3:1: expected declaration, found invalid$'

0 commit comments

Comments
 (0)
Please sign in to comment.