feat: split into code for buffer and vm
This commit is contained in:
+31
-21
@@ -1,34 +1,44 @@
|
||||
package repl
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
. "godb/internals/buffer"
|
||||
. "godb/internals/vm"
|
||||
)
|
||||
|
||||
type InputBuffer struct {
|
||||
Buffer string
|
||||
Reader *bufio.Reader
|
||||
}
|
||||
|
||||
func NewInputBuffer() *InputBuffer {
|
||||
return &InputBuffer{
|
||||
Buffer: "",
|
||||
Reader: bufio.NewReader(os.Stdin),
|
||||
}
|
||||
}
|
||||
|
||||
func PrintConsole() {
|
||||
fmt.Print("godb > ")
|
||||
}
|
||||
|
||||
func (inputBuffer *InputBuffer) ReadConsole() {
|
||||
line, err := inputBuffer.Reader.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
fmt.Println("Error reading input", err)
|
||||
os.Exit(1)
|
||||
func Run() {
|
||||
|
||||
inputBuffer := NewInputBuffer()
|
||||
statement := NewStatement()
|
||||
for {
|
||||
PrintConsole()
|
||||
inputBuffer.ReadConsole()
|
||||
if len(inputBuffer.Buffer) > 0 && strings.HasPrefix(inputBuffer.Buffer, ".") {
|
||||
switch DoMetaCommands(inputBuffer) {
|
||||
case MetaCommandSuccess:
|
||||
continue
|
||||
case MetaCommandUnrecognised:
|
||||
fmt.Printf("unrecognised command %v\n", inputBuffer.Buffer)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
switch statement.PrepareStatements(inputBuffer) {
|
||||
case PrepareSuccess:
|
||||
//todo
|
||||
case PrepareUnrecognised:
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.Buffer)
|
||||
continue
|
||||
}
|
||||
|
||||
statement.ExecuteStatement()
|
||||
fmt.Println("Executed")
|
||||
|
||||
}
|
||||
inputBuffer.Buffer = strings.TrimSpace(line)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user