Skip to content

Commit ed4673a

Browse files
committed
Dump!
1 parent 2bb1d47 commit ed4673a

File tree

16 files changed

+183
-85
lines changed

16 files changed

+183
-85
lines changed

compiler/ast/clause_tree.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
package ast
1616

17-
import "github.com/RohitAwate/commaql/compiler"
17+
import (
18+
"github.com/RohitAwate/commaql/compiler/common"
19+
)
1820

1921
type ColumnForOrderByClause struct {
20-
ColumnToken compiler.Token
21-
OrderToken compiler.Token
22+
ColumnToken common.Token
23+
OrderToken common.Token
2224
Name string
2325
Ascending bool
2426
}
@@ -28,7 +30,7 @@ type OrderByClause struct {
2830
}
2931

3032
type GroupByClause struct {
31-
Columns []compiler.Token
33+
Columns []common.Token
3234
}
3335

3436
func (obc OrderByClause) amNode() {}

compiler/ast/expr_tree.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414

1515
package ast
1616

17-
import "github.com/RohitAwate/commaql/compiler"
17+
import (
18+
"github.com/RohitAwate/commaql/compiler/common"
19+
)
1820

1921
type Literal struct {
20-
Meta compiler.Token
22+
Meta common.Token
2123
}
2224

2325
type UnaryExpr struct {
24-
Operator compiler.Token
26+
Operator common.Token
2527
Operand Expr
2628
}
2729

2830
type BinaryExpr struct {
2931
LeftOperand Expr
30-
Operator compiler.Token
32+
Operator common.Token
3133
RightOperand Expr
3234
}
3335

compiler/ast/stmt_tree.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package ast
1616

17-
import "github.com/RohitAwate/commaql/compiler"
17+
import (
18+
"github.com/RohitAwate/commaql/compiler/common"
19+
)
1820

1921
type SelectStmt struct {
2022
Columns []SelectColumnNode
@@ -26,13 +28,13 @@ type SelectStmt struct {
2628
}
2729

2830
type SelectColumnNode struct {
29-
ColumnToken compiler.Token
30-
AliasToken compiler.Token
31+
ColumnToken common.Token
32+
AliasToken common.Token
3133
}
3234

3335
type TableNode struct {
34-
TableToken compiler.Token
35-
AliasToken compiler.Token
36+
TableToken common.Token
37+
AliasToken common.Token
3638
}
3739

3840
func (ss SelectStmt) amNode() {}

compiler/codegen/generator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ package codegen
1616

1717
import (
1818
"fmt"
19+
"github.com/RohitAwate/commaql/compiler/common"
1920

20-
"github.com/RohitAwate/commaql/compiler"
2121
"github.com/RohitAwate/commaql/compiler/ast"
2222
"github.com/RohitAwate/commaql/compiler/parser/tokenizer"
2323
"github.com/RohitAwate/commaql/vm"
@@ -26,7 +26,7 @@ import (
2626
type CodeGenerator struct {
2727
statements []ast.Stmt
2828
Code vm.Bytecode
29-
Errors []compiler.Error
29+
Errors []common.Error
3030
}
3131

3232
func NewCodeGenerator(statements []ast.Stmt) (*CodeGenerator, error) {
@@ -40,15 +40,15 @@ func NewCodeGenerator(statements []ast.Stmt) (*CodeGenerator, error) {
4040
}, nil
4141
}
4242

43-
func (cg *CodeGenerator) Run() compiler.PhaseStatus {
43+
func (cg *CodeGenerator) Run() common.PhaseStatus {
4444
for _, statement := range cg.statements {
4545
switch stmt := statement.(type) {
4646
case ast.SelectStmt:
4747
cg.visitSelectStmt(&stmt)
4848
}
4949
}
5050

51-
return compiler.PHASE_OK
51+
return common.PHASE_OK
5252
}
5353

5454
func (cg *CodeGenerator) visitSelectStmt(ss *ast.SelectStmt) {
@@ -114,7 +114,7 @@ func (cg *CodeGenerator) visitLiteral(lit *ast.Literal) {
114114
}
115115
}
116116

117-
var unaryOperatorToOpCode = map[compiler.TokenType]vm.OpCode{
117+
var unaryOperatorToOpCode = map[common.TokenType]vm.OpCode{
118118
tokenizer.MINUS: vm.OpNegate,
119119
tokenizer.NOT: vm.OpNot,
120120
}
@@ -124,7 +124,7 @@ func (cg *CodeGenerator) visitUnaryExpr(ue *ast.UnaryExpr) {
124124
cg.Code.Emit(unaryOperatorToOpCode[ue.Operator.Type])
125125
}
126126

127-
var binaryOperatorToOpCode = map[compiler.TokenType]vm.OpCode{
127+
var binaryOperatorToOpCode = map[common.TokenType]vm.OpCode{
128128
tokenizer.PLUS: vm.OpAdd,
129129
tokenizer.MINUS: vm.OpSubtract,
130130
tokenizer.STAR: vm.OpMultiply,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package compiler
15+
package common
1616

1717
type Location struct {
1818
Line uint

compiler/compiler.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2021-22 Rohit Awate
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package compiler
16+
17+
import (
18+
"fmt"
19+
"github.com/RohitAwate/commaql/compiler/codegen"
20+
"github.com/RohitAwate/commaql/compiler/parser"
21+
"github.com/RohitAwate/commaql/compiler/parser/tokenizer"
22+
"github.com/RohitAwate/commaql/disassembler"
23+
"github.com/RohitAwate/commaql/table"
24+
"os"
25+
)
26+
27+
type Compiler struct {
28+
Tables []table.Table
29+
}
30+
31+
func NewCompiler(filepath string) (*Compiler, error) {
32+
reader, err := os.Open(filepath)
33+
if err != nil {
34+
return nil, fmt.Errorf("file not found: %s", filepath)
35+
}
36+
37+
csvTable, err := table.NewCSVTable(reader)
38+
if err != nil {
39+
return nil, fmt.Errorf("could not read from file: %s", filepath)
40+
}
41+
42+
return &Compiler{Tables: []table.Table{csvTable}}, nil
43+
}
44+
45+
func (c *Compiler) Compile(query string) {
46+
t := tokenizer.Tokenizer{Query: query}
47+
tokens, _ := t.Run()
48+
49+
p := parser.Parser{Tokens: tokens}
50+
statements, _ := p.Run()
51+
52+
cg, _ := codegen.NewCodeGenerator(statements)
53+
cg.Run()
54+
55+
disassembler.Disassemble(&cg.Code)
56+
}

compiler/parser/parser.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ package parser
1616

1717
import (
1818
"fmt"
19+
"github.com/RohitAwate/commaql/compiler/common"
1920

20-
"github.com/RohitAwate/commaql/compiler"
2121
"github.com/RohitAwate/commaql/compiler/ast"
2222
"github.com/RohitAwate/commaql/compiler/parser/tokenizer"
2323
)
2424

2525
type Parser struct {
26-
Tokens []compiler.Token
26+
Tokens []common.Token
2727

2828
current uint
29-
errors []compiler.Error
29+
errors []common.Error
3030
}
3131

32-
func (p *Parser) Run() ([]ast.Stmt, []compiler.Error) {
32+
func (p *Parser) Run() ([]ast.Stmt, []common.Error) {
3333
var statements []ast.Stmt
3434

3535
for !p.eof() {
@@ -60,15 +60,15 @@ func (p *Parser) statement() ast.Node {
6060
return nil
6161
}
6262

63-
func (p *Parser) peek() compiler.Token {
63+
func (p *Parser) peek() common.Token {
6464
if p.current < uint(len(p.Tokens)) {
6565
return p.Tokens[p.current]
6666
}
6767

6868
return p.Tokens[len(p.Tokens)-1]
6969
}
7070

71-
func (p *Parser) previous() compiler.Token {
71+
func (p *Parser) previous() common.Token {
7272
return p.Tokens[p.current-1]
7373
}
7474

@@ -78,7 +78,7 @@ func (p *Parser) advance() {
7878
}
7979
}
8080

81-
func (p *Parser) match(tokenType compiler.TokenType) bool {
81+
func (p *Parser) match(tokenType common.TokenType) bool {
8282
if p.peek().Type == tokenType {
8383
p.advance()
8484
return true
@@ -96,9 +96,9 @@ func (p *Parser) eof() bool {
9696
}
9797

9898
func (p *Parser) emitError(msg string) {
99-
p.errors = append(p.errors, compiler.Error{Message: msg, Location: p.peek().Location})
99+
p.errors = append(p.errors, common.Error{Message: msg, Location: p.peek().Location})
100100
}
101101

102102
func (p *Parser) emitExpectedError(expectedWhat string) {
103-
p.emitError(fmt.Sprintf("Expected %s, found '%s'", expectedWhat, p.peek().Lexeme))
104-
}
103+
p.emitError(fmt.Sprintf("Expected %s, found '%s'", expectedWhat, p.peek().Lexeme))
104+
}

compiler/parser/tokenizer/tokenizer.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ package tokenizer
1616

1717
import (
1818
"fmt"
19+
"github.com/RohitAwate/commaql/compiler/common"
1920
"strings"
20-
21-
"github.com/RohitAwate/commaql/compiler"
2221
)
2322

2423
type Tokenizer struct {
@@ -28,13 +27,13 @@ type Tokenizer struct {
2827
anchor uint
2928
lookahead uint
3029

31-
errors []compiler.Error
30+
errors []common.Error
3231
}
3332

34-
func (t *Tokenizer) Run() ([]compiler.Token, []compiler.Error) {
33+
func (t *Tokenizer) Run() ([]common.Token, []common.Error) {
3534
t.Reset()
3635

37-
tokens := make([]compiler.Token, 0)
36+
tokens := make([]common.Token, 0)
3837

3938
for !t.eof() {
4039
t.skipWhitespace()
@@ -117,7 +116,7 @@ func (t *Tokenizer) Reset() {
117116
}
118117

119118
func (t *Tokenizer) emitError(msg string) {
120-
t.errors = append(t.errors, compiler.Error{Message: msg, Location: t.getLocationForWindow()})
119+
t.errors = append(t.errors, common.Error{Message: msg, Location: t.getLocationForWindow()})
121120
}
122121

123122
func (t *Tokenizer) eof() bool {
@@ -165,14 +164,14 @@ func (t *Tokenizer) getLexemeForWindow() string {
165164
return t.Query[t.anchor:t.lookahead]
166165
}
167166

168-
func (t *Tokenizer) getLocationForWindow() compiler.Location {
167+
func (t *Tokenizer) getLocationForWindow() common.Location {
169168
// TODO: Track line and columns
170-
return compiler.Location{Line: 0, Column: 0}
169+
return common.Location{Line: 0, Column: 0}
171170
}
172171

173-
func (t *Tokenizer) emitToken() compiler.Token {
172+
func (t *Tokenizer) emitToken() common.Token {
174173
defer t.advanceWindow()
175-
return compiler.Token{Lexeme: t.getLexemeForWindow(), Location: t.getLocationForWindow()}
174+
return common.Token{Lexeme: t.getLexemeForWindow(), Location: t.getLocationForWindow()}
176175
}
177176

178177
func (t *Tokenizer) skipWhitespace() {
@@ -191,7 +190,7 @@ func (t *Tokenizer) skipWhitespace() {
191190
}
192191
}
193192

194-
func (t *Tokenizer) number() compiler.Token {
193+
func (t *Tokenizer) number() common.Token {
195194
// TODO: Handle floats
196195
for isDigit(t.peek()) {
197196
t.advance()
@@ -203,7 +202,7 @@ func (t *Tokenizer) number() compiler.Token {
203202
return token
204203
}
205204

206-
func (t *Tokenizer) identifier() compiler.Token {
205+
func (t *Tokenizer) identifier() common.Token {
207206
for t.peek() == '_' || isAlpha(t.peek()) {
208207
t.advance()
209208
}
@@ -219,7 +218,7 @@ func (t *Tokenizer) identifier() compiler.Token {
219218
return token
220219
}
221220

222-
func (t *Tokenizer) stringLiteral() compiler.Token {
221+
func (t *Tokenizer) stringLiteral() common.Token {
223222
startingQuote := t.peek()
224223

225224
// Consume opening quote
@@ -238,7 +237,7 @@ func (t *Tokenizer) stringLiteral() compiler.Token {
238237
return stringToken
239238
}
240239

241-
func (t *Tokenizer) emitSingleCharToken(tokenType compiler.TokenType) compiler.Token {
240+
func (t *Tokenizer) emitSingleCharToken(tokenType common.TokenType) common.Token {
242241
// TODO: Try and get rid of this and use just the emitToken method with a new
243242
// parameter that accept the token type. That would involve playing around with advance
244243
// since that appears to be handle by respective logic just before calling emitToken.

compiler/parser/tokenizer/tokens.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
package tokenizer
1616

1717
import (
18+
"github.com/RohitAwate/commaql/compiler/common"
1819
"strings"
19-
20-
"github.com/RohitAwate/commaql/compiler"
2120
)
2221

2322
// TODO: Maybe move this to compiler/common.go
2423
const (
25-
AND compiler.TokenType = iota // SQL Keywords
24+
AND common.TokenType = iota // SQL Keywords
2625
ALL
2726
AS
2827
ASC
@@ -79,7 +78,7 @@ const (
7978
NUMBER
8079
)
8180

82-
var SQLKeywordsToTokenType = map[string]compiler.TokenType{
81+
var SQLKeywordsToTokenType = map[string]common.TokenType{
8382
"AND": AND,
8483
"ALL": ALL,
8584
"AS": AS,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/RohitAwate/commaql
22

3-
go 1.15
3+
go 1.18

0 commit comments

Comments
 (0)