From ecc188de14f05bdad3e27a8ac8e88a6d569034bf Mon Sep 17 00:00:00 2001 From: TakshakRamteke Date: Sun, 24 Nov 2024 23:34:30 +0530 Subject: [PATCH 1/3] FIX : assigned value for buffer length --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 9598072..75d4415 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func readConsole(inputBuffer inputBuffer) inputBuffer { fmt.Println("Error reading input") os.Exit(1) } + inputBuffer.bufferLength = len(inputBuffer.buffer) inputBuffer.inputLength = bytesRead - 1 return inputBuffer From b5a31e7775b31c943389ec90ee849944ab7c3811 Mon Sep 17 00:00:00 2001 From: TakshakRamteke Date: Fri, 29 Nov 2024 01:42:48 +0530 Subject: [PATCH 2/3] FEAT : added a simple compiler and support for commands --- main.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 75d4415..e14f472 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,30 @@ package main import ( + "bufio" "fmt" + "io" "os" + "strings" +) + +type metaCommandResults int +type prepareResults int +type statementType int + +const ( + metaCommandSuccess metaCommandResults = iota + metaCommandUnrecognised +) + +const ( + prepareSuccess prepareResults = iota + prepareUnrecognised +) + +const ( + statementSelect statementType = iota + statementInsert ) type inputBuffer struct { @@ -11,6 +33,44 @@ type inputBuffer struct { inputLength int } +type statement struct { + stype statementType +} + +func doMetaCommands(inputBuffer inputBuffer) metaCommandResults { + metacommand := metaCommandUnrecognised + if inputBuffer.buffer == ".exit" { + fmt.Println("bye!") + os.Exit(0) + } else { + metacommand = metaCommandUnrecognised + } + return metacommand +} + +func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareResults { + if strings.Split(inputBuffer.buffer, " ")[0] == "insert" { + statement.stype = statementInsert + return prepareSuccess + } + if strings.Split(inputBuffer.buffer, " ")[0] == "select" { + statement.stype = statementSelect + return prepareSuccess + } + return prepareUnrecognised +} + +func executeStatement(statement *statement) { + switch statement.stype { + case statementInsert: + fmt.Println("This is where the logic of insert statement should go") + break + case statementSelect: + fmt.Println("This is where the logic of select statement should go") + break + } +} + func createInputBuffer() inputBuffer { newInputBuffer := inputBuffer{"", 0, 0} return newInputBuffer @@ -21,7 +81,21 @@ func printConsole() { } func readConsole(inputBuffer inputBuffer) inputBuffer { - fmt.Scanf("%s", &inputBuffer.buffer) + var stdin *bufio.Reader + var line []rune + stdin = bufio.NewReader(os.Stdin) + for { + c, _, err := stdin.ReadRune() + if err == io.EOF || c == '\n' { + break + } + if err != nil { + fmt.Println("Error reading input") + os.Exit(1) + } + line = append(line, c) + } + inputBuffer.buffer = string(line) bytesRead := len(inputBuffer.buffer) if bytesRead <= 0 { fmt.Println("Error reading input") @@ -35,17 +109,31 @@ func readConsole(inputBuffer inputBuffer) inputBuffer { func main() { inputBuffer := createInputBuffer() + statement := statement{0} condition := true for condition { printConsole() inputBuffer = readConsole(inputBuffer) - - if inputBuffer.buffer == ".exit" { - fmt.Println("bye!") - os.Exit(0) - } else { - fmt.Printf("unrecognised command %v\n", inputBuffer.buffer) + if strings.Split(inputBuffer.buffer, "")[0] == "." { + switch doMetaCommands(inputBuffer) { + case metaCommandSuccess: + continue + case metaCommandUnrecognised: + fmt.Printf("unrecognised command %v\n", inputBuffer.buffer) + continue + } } + switch prepareStatements(inputBuffer, &statement) { + case prepareSuccess: + break + case prepareUnrecognised: + fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.buffer) + continue + } + + executeStatement(&statement) + fmt.Println("Executed") + } } From d2a5477217fd620c63da3b43f6cedd5c4355a9ec Mon Sep 17 00:00:00 2001 From: TakshakRamteke Date: Fri, 29 Nov 2024 02:08:45 +0530 Subject: [PATCH 3/3] FEAT : skeleton for a in memory table --- main.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e14f472..2a9f3c6 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ const ( const ( prepareSuccess prepareResults = iota prepareUnrecognised + prepareSyntaxError ) const ( @@ -33,8 +34,15 @@ type inputBuffer struct { inputLength int } +type row struct { + id uint + username string + email string +} + type statement struct { stype statementType + row row } func doMetaCommands(inputBuffer inputBuffer) metaCommandResults { @@ -50,6 +58,14 @@ func doMetaCommands(inputBuffer inputBuffer) metaCommandResults { func prepareStatements(inputBuffer inputBuffer, statement *statement) prepareResults { if strings.Split(inputBuffer.buffer, " ")[0] == "insert" { + argsAssigned, err := fmt.Sscanf(inputBuffer.buffer, "insert %v %s %s", &statement.row.id, &statement.row.username, &statement.row.email) + if err != nil { + fmt.Println("error parsing insert statement") + fmt.Println(err.Error()) + } + if argsAssigned > 3 { + return prepareSyntaxError + } statement.stype = statementInsert return prepareSuccess } @@ -109,7 +125,7 @@ func readConsole(inputBuffer inputBuffer) inputBuffer { func main() { inputBuffer := createInputBuffer() - statement := statement{0} + statement := statement{0, row{0, "", ""}} condition := true for condition { printConsole()