-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the ability to run Go code when the game is running. No need to compile, and restart the game. Just write the code in terminal, hit enter, and it will be immediately executed. Go code is interpreted by traefik/yaegi interpreter. It is a Go interpreter compatible with standard Go compiler. Lines are read from terminal by peterh/liner package. For now I'm using my fork of github.com/peterh/liner, because the original does not work well in Goland/VSCode on Win11 (see peterh/liner#163). This is temporary, until peterh/liner is fixed properly. Limitations: * MidInt, MaxInt, MinInt - those functions can be executed in terminal but only accepts int's (not int64, byte etc.) * pi.Int cannot be used * The size of compiled game with devtools is increased by 3.7MB (+35% increase), the compilation takes 21% more time. But it is worth it. I plan to add a possibility to disable scripting in the future though.
- Loading branch information
Showing
47 changed files
with
2,692 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${fileDirname}", | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// (c) 2023 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package help | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/elgopher/pi/devtools/internal/lib" | ||
) | ||
|
||
var NotFound = fmt.Errorf("no help found") | ||
|
||
func PrintHelp(topic string) error { | ||
switch topic { | ||
case "": | ||
fmt.Println("This is interactive terminal. " + | ||
"You can write Go code here, which will run immediately. " + | ||
"You can use all Pi packages: pi, key, state, snap, font, image and " + | ||
"selection of standard packages: " + strings.Join(stdPackages(), ", ") + ". " + | ||
"\n\n" + | ||
"Type help topic for more information. For example: help pi or help pi.Spr" + | ||
"\n\n" + | ||
"Available commands: help [h], pause [p], resume [r], undo [u]", | ||
) | ||
return nil | ||
default: | ||
return goDoc(topic) | ||
} | ||
} | ||
|
||
func stdPackages() []string { | ||
var packages []string | ||
for _, p := range lib.AllPackages() { | ||
if p.IsStdPackage() { | ||
packages = append(packages, p.Alias) | ||
} | ||
} | ||
sort.Strings(packages) | ||
return packages | ||
} | ||
|
||
func goDoc(symbol string) error { | ||
symbol = completeSymbol(symbol) | ||
if symbolNotSupported(symbol) { | ||
return NotFound | ||
} | ||
|
||
fmt.Println("###############################################################################") | ||
|
||
var args []string | ||
args = append(args, "doc") | ||
if shouldShowDetailedDescriptionForSymbol(symbol) { | ||
args = append(args, "-all") | ||
} | ||
args = append(args, symbol) | ||
command := exec.Command("go", args...) | ||
command.Stdout = bufio.NewWriter(os.Stdout) | ||
|
||
if err := command.Run(); err != nil { | ||
var exitErr *exec.ExitError | ||
if isExitErr := errors.As(err, &exitErr); isExitErr && exitErr.ExitCode() == 1 { | ||
return NotFound | ||
} | ||
|
||
return fmt.Errorf("problem getting help: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func completeSymbol(symbol string) string { | ||
packages := lib.AllPackages() | ||
|
||
for _, p := range packages { | ||
if p.Alias == symbol { | ||
return p.Path | ||
} | ||
} | ||
|
||
for _, p := range packages { | ||
prefix := p.Alias + "." | ||
if strings.HasPrefix(symbol, prefix) { | ||
return p.Path + "." + symbol[len(prefix):] | ||
} | ||
} | ||
|
||
return symbol | ||
} | ||
|
||
func symbolNotSupported(symbol string) bool { | ||
packages := lib.AllPackages() | ||
|
||
for _, p := range packages { | ||
prefix := p.Path + "." | ||
if strings.HasPrefix(symbol, prefix) || symbol == p.Path { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
var symbolsWithDetailedDescription = []string{ | ||
"github.com/elgopher/pi.Button", | ||
"github.com/elgopher/pi.MouseButton", | ||
"github.com/elgopher/pi/key.Button", | ||
} | ||
|
||
func shouldShowDetailedDescriptionForSymbol(symbol string) bool { | ||
for _, s := range symbolsWithDetailedDescription { | ||
if symbol == s { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// (c) 2023 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
//go:build !js | ||
|
||
package help_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elgopher/pi/devtools/internal/help" | ||
"github.com/elgopher/pi/devtools/internal/test" | ||
) | ||
|
||
func TestPrintHelp(t *testing.T) { | ||
t.Run("should return error when trying to print help for not imported packages", func(t *testing.T) { | ||
topics := []string{ | ||
"io", "io.Writer", | ||
} | ||
for _, topic := range topics { | ||
t.Run(topic, func(t *testing.T) { | ||
// when | ||
err := help.PrintHelp(topic) | ||
// then | ||
assert.ErrorIs(t, err, help.NotFound) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("should return error when trying to print help for non-existent symbol", func(t *testing.T) { | ||
err := help.PrintHelp("pi.NonExistent") | ||
assert.ErrorIs(t, err, help.NotFound) | ||
}) | ||
|
||
t.Run("should print help for", func(t *testing.T) { | ||
tests := map[string]struct { | ||
topic string | ||
expected string | ||
}{ | ||
"package": { | ||
topic: "pi", | ||
expected: `Package pi`, | ||
}, | ||
"function": { | ||
topic: "pi.Spr", | ||
expected: `func Spr(n, x, y int)`, | ||
}, | ||
"struct": { | ||
topic: "pi.PixMap", | ||
expected: `type PixMap struct {`, | ||
}, | ||
} | ||
for testName, testCase := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
swapper := test.SwapStdout(t) | ||
// when | ||
err := help.PrintHelp(testCase.topic) | ||
// then | ||
swapper.BringStdoutBack() | ||
assert.NoError(t, err) | ||
output := swapper.ReadOutput(t) | ||
assert.Contains(t, output, testCase.expected) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("should show help for image.Image from github.com/elgopher/pi package, not from stdlib", func(t *testing.T) { | ||
topics := []string{ | ||
"image", "image.Image", | ||
} | ||
for _, topic := range topics { | ||
t.Run(topic, func(t *testing.T) { | ||
swapper := test.SwapStdout(t) | ||
// when | ||
err := help.PrintHelp("image.Image") | ||
// then | ||
swapper.BringStdoutBack() | ||
assert.NoError(t, err) | ||
output := swapper.ReadOutput(t) | ||
assert.Contains(t, output, `// import "github.com/elgopher/pi/image"`) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("should show detailed help for pi.Button", func(t *testing.T) { | ||
tests := map[string]string{ | ||
"pi.Button": "Keyboard mappings", | ||
"pi.MouseButton": "MouseRight MouseButton = 2", | ||
"key.Button": "func (b Button) String() string", | ||
} | ||
for topic, expected := range tests { | ||
t.Run(topic, func(t *testing.T) { | ||
swapper := test.SwapStdout(t) | ||
// when | ||
err := help.PrintHelp(topic) | ||
// then | ||
swapper.BringStdoutBack() | ||
assert.NoError(t, err) | ||
output := swapper.ReadOutput(t) | ||
assert.Contains(t, output, expected) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// (c) 2023 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package interpreter | ||
|
||
type ErrInvalidIdentifier struct { | ||
message string | ||
} | ||
|
||
func (e ErrInvalidIdentifier) Error() string { | ||
return e.message | ||
} |
Oops, something went wrong.