Skip to content

Commit

Permalink
Merge pull request #41 from gopherds/refactor
Browse files Browse the repository at this point in the history
block importing
  • Loading branch information
dwhitena authored Aug 17, 2016
2 parents d4244cd + 7c6f07b commit 9f17203
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions internal/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,54 @@ func (s *Session) Eval(in string) (string, bytes.Buffer, error) {
// Split the lines of the input to check for special commands.
inLines := strings.Split(in, "\n")
var nonImportLines []string
for _, line := range inLines {
for idx, line := range inLines {

// Extract non-special lines.
if !strings.HasPrefix(line, "import") && !strings.HasPrefix(line, ":") {
trimLine := strings.TrimSpace(line)
if !strings.HasPrefix(line, "import") && !strings.HasPrefix(line, ":") && !strings.HasPrefix(trimLine, "\"") && !strings.HasPrefix(line, ")") {
nonImportLines = append(nonImportLines, line)
continue
}

// Process special commands.
var args []string
for _, command := range commands {

// Clear the args.
args = []string{}

// Extract any argument provided with the special command.
arg := strings.TrimPrefix(line, ":"+command.name)
if command.name == "import" {
arg = strings.TrimPrefix(arg, "import")
}
if arg == line {
switch {
case arg == line:
continue
case strings.HasPrefix(strings.TrimSpace(arg), "("):
advance := 1
currentLine := inLines[idx+advance]
for !strings.Contains(currentLine, ")") {
fmt.Println(currentLine)
args = append(args, currentLine)
advance++
currentLine = inLines[idx+advance]
}
default:
args = append(args, arg)
}

// Apply the action associated with the special command.
if arg == "" || strings.HasPrefix(arg, " ") {
arg = strings.TrimSpace(arg)
_, err := command.action(s, arg)
if err != nil {
if err == ErrQuit {
return "", bytes.Buffer{}, err
for _, arg = range args {
if arg == "" || strings.HasPrefix(arg, " ") {
arg = strings.TrimSpace(arg)
_, err := command.action(s, arg)
if err != nil {
if err == ErrQuit {
return "", bytes.Buffer{}, err
}
errorf("%s: %s", command.name, err)
}
errorf("%s: %s", command.name, err)
}
}
}
Expand Down

0 comments on commit 9f17203

Please sign in to comment.