Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing the repl with autocomplete #50

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed build_cli.ps
Binary file not shown.
6 changes: 3 additions & 3 deletions build_cli.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/local/bin/bash
#!/usr/bin/env bash
set -x
CWD=$(pwd)
go-bindata -pkg main -nomemcopy -o $GOPATH/src/github.com/gen0cide/gscript/generator/bindata.go -prefix 'generator' generator/templates/...
go run generator/main.go generator/bindata.go generate --config $GOPATH/src/github.com/gen0cide/gscript/functions.yml --out $GOPATH/src/github.com/gen0cide/gscript/engine/vm_functions.go --docs $GOPATH/src/github.com/gen0cide/gscript/FUNCTIONS.md
go-bindata -pkg generator -nomemcopy -o $GOPATH/src/github.com/gen0cide/gscript/generator/bindata.go -prefix 'generator' generator/templates/...
go run generator/cmd/gscript-generator/main.go generate --config $GOPATH/src/github.com/gen0cide/gscript/functions.yml --out $GOPATH/src/github.com/gen0cide/gscript/engine/vm_functions.go --docs $GOPATH/src/github.com/gen0cide/gscript/FUNCTIONS.md
cd $GOPATH/src/github.com/gen0cide/gscript/cmd/gscript
go-bindata -pkg compiler -nomemcopy -o $GOPATH/src/github.com/gen0cide/gscript/compiler/bindata.go -prefix '../..' ../../templates/...
go build -o $GOPATH/bin/gscript
Expand Down
7 changes: 0 additions & 7 deletions build_gsegen.sh

This file was deleted.

46 changes: 46 additions & 0 deletions compiler/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion debug_windows.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/bin/bash
#!/usr/bin/env bash
set -x
CWD=$(pwd)
cd $GOPATH/src/github.com/gen0cide/gscript/cmd/gscript
Expand Down
74 changes: 63 additions & 11 deletions debugger/session.go → debugger/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,54 @@ import (
"github.com/fatih/color"
"github.com/gen0cide/gscript/compiler"
"github.com/gen0cide/gscript/engine"
"github.com/gen0cide/gscript/generator"
"github.com/gen0cide/gscript/logging"
"github.com/robertkrimen/otto"
"github.com/sirupsen/logrus"
)

var FunctionBlacklist = []string{
"arguments",
"Object",
"console",
}

type Debugger struct {
Engine *engine.Engine
Logger *logrus.Logger
Prompt *prompt.Prompt
Engine *engine.Engine
Logger *logrus.Logger
Prompt *prompt.Prompt
BuiltInFuncs map[string]*generator.FunctionDef
REPLSuggestions []prompt.Suggest
}

func New(name string) *Debugger {
logger := logrus.New()
logger.Formatter = &logging.GSEFormatter{}
logger.Out = logging.LogWriter{Name: name}
logger.Level = logrus.DebugLevel
g := generator.Generator{
Logger: logger,
}
funcDefs := map[string]*generator.FunctionDef{}
for name, funcObj := range g.ExtractFunctionList(compiler.MustAsset("templates/builtin.yml")) {
funcDefs[name] = funcObj
}
for name, funcObj := range g.ExtractFunctionList(compiler.MustAsset("templates/functions.yml")) {
funcDefs[name] = funcObj
}
suggestions := []prompt.Suggest{}
for name, funcObj := range funcDefs {
suggestions = append(suggestions, prompt.Suggest{
Text: name,
Description: funcObj.Description,
})
}
gse := engine.New(name)
return &Debugger{
Engine: gse,
Logger: logger,
Engine: gse,
Logger: logger,
BuiltInFuncs: funcDefs,
REPLSuggestions: suggestions,
}
}

Expand All @@ -41,7 +69,10 @@ func (d *Debugger) SetupDebugEngine() {
}

func (d *Debugger) SessionCompleter(p prompt.Document) []prompt.Suggest {
return []prompt.Suggest{}
if p.TextBeforeCursor() == "" {
return []prompt.Suggest{}
}
return prompt.FilterHasPrefix(d.REPLSuggestions, p.GetWordBeforeCursor(), true)
}

func (d *Debugger) DebugConsole(call otto.FunctionCall) otto.Value {
Expand All @@ -54,13 +85,27 @@ func (d *Debugger) SessionExecutor(in string) {
if newIn == "exit" || newIn == "quit" {
os.Exit(0)
}
val, err := d.Engine.VM.Eval(newIn)
if newIn == "_symboldebug" {
c := d.Engine.VM.Context()
for k := range c.Symbols {
fmt.Printf("SYMBOL: %s\n", k)
}
return
}
s, err := d.Engine.VM.Compile("repl", newIn)
if err != nil {
d.Engine.Logger.Errorf("Console Error: %s", err.Error())
}
retVal, _ := val.Export()
if retVal != nil {
fmt.Printf(">>> %v\n", retVal)
} else {
v, err := d.Engine.VM.Eval(s)
if err != nil {
if oerr, ok := err.(*otto.Error); ok {
d.Engine.Logger.Errorf("Runtime Error: %s", oerr.String())
} else {
d.Engine.Logger.Errorf("Runtime Error: %s", err.Error())
}
} else {
fmt.Printf(">>> %s\n", v.String())
}
}
}

Expand Down Expand Up @@ -105,6 +150,13 @@ func (d *Debugger) InteractiveSession() {
prompt.OptionPrefix("gscript> "),
prompt.OptionPrefixTextColor(prompt.Red),
prompt.OptionTitle("Genesis Scripting Engine Console"),
prompt.OptionDescriptionBGColor(prompt.DarkGray),
prompt.OptionDescriptionTextColor(prompt.White),
prompt.OptionSuggestionBGColor(prompt.Black),
prompt.OptionSuggestionTextColor(prompt.LightGray),
prompt.OptionSelectedSuggestionBGColor(prompt.DarkRed),
prompt.OptionSelectedSuggestionTextColor(prompt.White),
prompt.OptionSelectedDescriptionBGColor(prompt.Red),
)
d.Prompt = p
entryText := []string{
Expand Down
2 changes: 2 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Engine struct {
DebuggerEnabled bool
Timeout int
Halted bool
Paused bool
}

func New(name string) *Engine {
Expand All @@ -31,6 +32,7 @@ func New(name string) *Engine {
Logger: logger,
DebuggerEnabled: false,
Halted: false,
Paused: false,
Timeout: 30,
}
}
Expand Down
13 changes: 9 additions & 4 deletions engine/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ func (e *Engine) RunWithTimeout(command string) (otto.Value, error) {
).Infof("%s Execution Time: %v", command, duration)
}()

e.VM.Interrupt = make(chan func(), 1) // The buffer prevents blocking
e.VM.Interrupt = make(chan func(), 1)

go func() {
time.Sleep(time.Duration(e.Timeout) * time.Second) // Stop after two seconds
e.VM.Interrupt <- func() {
panic(errTimeout)
for {
time.Sleep(time.Duration(e.Timeout) * time.Second)
if e.Paused {
e.VM.Interrupt <- func() {
panic(errTimeout)
}
return
}
}
}()

Expand Down
22 changes: 2 additions & 20 deletions engine/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,8 @@ function ByteArrayToString(a) {
return String.fromCharCode.apply(String, a);
}

function DumpObjectIndented(obj, indent) {
var result = "";
if (indent == null) indent = "";

for (var property in obj) {
var value = obj[property];
if (typeof value == 'string') {
value = "'" + value + "'";
}
else if (typeof value == 'object') {
if (value instanceof Array) {
value = "[ " + value + " ]";
} else {
var od = DumpObjectIndented(value, indent + " ");
value = "\n" + indent + "{\n" + od + "\n" + indent + "}";
}
}
result += indent + "'" + property + "' : " + value + ",\n";
}
return result.replace(/,\n$/, "");
function Dump(obj) {
return "\n" + JSON.stringify(obj, null, 2);
}

function BeforeDeploy() {
Expand Down
2 changes: 1 addition & 1 deletion generator/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading