Added multi-line support

This commit is contained in:
obscuren 2014-05-19 13:04:31 +02:00
parent 3b7707c3fd
commit 16421106d4
3 changed files with 35 additions and 9 deletions

@ -101,14 +101,15 @@ func (self *JSRE) Run(code string) (otto.Value, error) {
type JSRepl struct { type JSRepl struct {
re *JSRE re *JSRE
prompt string
} }
func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { func NewJSRepl(ethereum *eth.Ethereum) *JSRepl {
return &JSRepl{re: NewJSRE(ethereum)} return &JSRepl{re: NewJSRE(ethereum), prompt: "> "}
} }
func (self *JSRepl) Start() { func (self *JSRepl) Start() {
fmt.Println("Eth JavaScript console")
self.read() self.read()
} }

@ -6,7 +6,11 @@ package main
// #include <readline/readline.h> // #include <readline/readline.h>
// #include <readline/history.h> // #include <readline/history.h>
import "C" import "C"
import "unsafe"
import (
"strings"
"unsafe"
)
func readLine(prompt *string) *string { func readLine(prompt *string) *string {
var p *C.char var p *C.char
@ -37,19 +41,40 @@ func addHistory(s string) {
C.free(unsafe.Pointer(p)) C.free(unsafe.Pointer(p))
} }
func (self *JSRepl) read() { var indentCount = 0
prompt := "eth >>> " var str = ""
func (self *JSRepl) setIndent() {
open := strings.Count(str, "{")
open += strings.Count(str, "(")
closed := strings.Count(str, "}")
closed += strings.Count(str, ")")
indentCount = open - closed
if indentCount <= 0 {
self.prompt = "> "
} else {
self.prompt = strings.Join(make([]string, indentCount*2), "..")
self.prompt += " "
}
}
func (self *JSRepl) read() {
L: L:
for { for {
switch result := readLine(&prompt); true { switch result := readLine(&self.prompt); true {
case result == nil: case result == nil:
break L //exit loop break L //exit loop
case *result != "": //ignore blank lines case *result != "": //ignore blank lines
addHistory(*result) //allow user to recall this line str += *result + "\n"
self.parseInput(*result) self.setIndent()
if indentCount <= 0 {
addHistory(str) //allow user to recall this line
self.parseInput(str)
}
} }
} }
} }

@ -9,7 +9,7 @@ import (
func (self *JSRepl) read() { func (self *JSRepl) read() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
for { for {
fmt.Printf("eth >>> ") fmt.Printf(self.prompt)
str, _, err := reader.ReadLine() str, _, err := reader.ReadLine()
if err != nil { if err != nil {
fmt.Println("Error reading input", err) fmt.Println("Error reading input", err)