forked from ShubhamNegi4/DaemonDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (95 loc) · 2.57 KB
/
main.go
File metadata and controls
113 lines (95 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
executor "DaemonDB/query_executor"
codegen "DaemonDB/query_parser/code-generator"
lex "DaemonDB/query_parser/lexer"
"DaemonDB/query_parser/parser"
storageengine "DaemonDB/storage_engine"
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
)
func main() {
const DB_ROOT = "./database"
engine, err := storageengine.NewStorageEngine(DB_ROOT)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize storage engine: %v\n", err)
os.Exit(1)
}
vm := executor.NewVM(engine)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
fmt.Println("\nShutting down...")
if engine.BufferPool != nil {
engine.BufferPool.FlushAllPages()
}
if engine.DiskManager != nil {
engine.DiskManager.CloseAll()
}
os.Exit(0)
}()
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Welcome to DaemonDB!")
fmt.Println("Please use 'USE <database>' or 'SHOW DATABASES' or 'CREATE DATABASE <database>' to begin.")
fmt.Println("Type 'help' for more commands.")
// REPL
for {
fmt.Print("daemon> ")
if !scanner.Scan() { // Ctrl+D pressed
break
}
line := strings.TrimSpace(scanner.Text())
if strings.EqualFold(line, "exit") {
break
}
if line == "" {
continue
}
if line == "?" || strings.EqualFold(line, "help") {
printHelp()
continue
}
query := line
// Lexer + Parser
l := lex.New(query)
p := parser.New(l)
stmt, err := p.ParseStatement()
if err != nil {
fmt.Printf("Parse error: %v\n", err)
continue
}
fmt.Println("\n=== AST ===")
fmt.Printf("%#v", stmt)
fmt.Println("\n\n=== Bytecode ===")
instructions, err := codegen.EmitBytecode(stmt)
if err != nil {
fmt.Printf("Codegen error: %v\n", err)
continue
}
for i, instr := range instructions {
fmt.Printf("%d: OP=%v, VALUE=%v\n", i, instr.Op, instr.Value)
}
fmt.Println("\n=== Execution ===")
if err := vm.Execute(instructions); err != nil {
fmt.Printf("Execution error: %v\n", err)
}
}
}
func printHelp() {
fmt.Println("Supported commands:")
fmt.Println(" SHOW DATABASES")
fmt.Println(" CREATE DATABASE <name>")
fmt.Println(" USE <database>")
fmt.Println(" CREATE TABLE <name> ( col type [primary key], ... )")
fmt.Println(" INSERT INTO <table> VALUES ( val1, val2, ... )")
fmt.Println(" SELECT * FROM <table> [ WHERE col = value ]")
fmt.Println(" SELECT * FROM t1 [ INNER|LEFT|RIGHT|FULL ] JOIN t2 ON col1 = col2 [ WHERE ... ]")
fmt.Println(" BEGIN; COMMIT; ROLLBACK")
fmt.Println(" exit")
fmt.Println("Note: UPDATE/DELETE/DROP are parsed but not executed yet.")
}