Skip to content

Commit fd9dd22

Browse files
committed
Get rid of generics
1 parent ed4673a commit fd9dd22

File tree

6 files changed

+31
-52
lines changed

6 files changed

+31
-52
lines changed

compiler/codegen/generator.go

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

1717
import (
1818
"fmt"
19-
"github.com/RohitAwate/commaql/compiler/common"
20-
2119
"github.com/RohitAwate/commaql/compiler/ast"
20+
"github.com/RohitAwate/commaql/compiler/common"
2221
"github.com/RohitAwate/commaql/compiler/parser/tokenizer"
2322
"github.com/RohitAwate/commaql/vm"
23+
"github.com/RohitAwate/commaql/vm/values"
2424
)
2525

2626
type CodeGenerator struct {
@@ -53,7 +53,7 @@ func (cg *CodeGenerator) Run() common.PhaseStatus {
5353

5454
func (cg *CodeGenerator) visitSelectStmt(ss *ast.SelectStmt) {
5555
for _, tableNode := range ss.Tables {
56-
val := vm.String{Meta: tableNode.TableToken.Lexeme}
56+
val := values.String{Meta: tableNode.TableToken.Lexeme}
5757
loc := cg.Code.AddConstant(val)
5858

5959
cg.Code.EmitWithArg(vm.OpLoadConst, loc)
@@ -96,17 +96,17 @@ func (cg *CodeGenerator) visitLiteral(lit *ast.Literal) {
9696
switch lit.Meta.Type {
9797
case tokenizer.NUMBER:
9898
// TODO: Write a helper for this, lots of duplication between these cases
99-
val := vm.NewNumber(lit.Meta.Lexeme)
99+
val := values.NewNumber(lit.Meta.Lexeme)
100100
loc := cg.Code.AddConstant(val)
101101
cg.Code.EmitWithArg(vm.OpLoadConst, loc)
102102
case tokenizer.TRUE:
103103
fallthrough
104104
case tokenizer.FALSE:
105-
val := vm.NewBoolean(lit.Meta.Type)
105+
val := values.NewBoolean(lit.Meta.Type)
106106
loc := cg.Code.AddConstant(val)
107107
cg.Code.EmitWithArg(vm.OpLoadConst, loc)
108108
case tokenizer.STRING:
109-
val := vm.NewString(lit.Meta.Lexeme)
109+
val := values.NewString(lit.Meta.Lexeme)
110110
loc := cg.Code.AddConstant(val)
111111
cg.Code.EmitWithArg(vm.OpLoadConst, loc)
112112
default:

table/csv.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package table
22

33
import (
44
"encoding/csv"
5+
"github.com/RohitAwate/commaql/vm/values"
56
"os"
67
"strings"
78
)
89

9-
type CSVTable[T SQLType] struct {
10+
type CSVTable struct {
1011
name string
11-
columns []Column[T]
12+
columns []Column
1213
reader *csv.Reader
1314
}
1415

15-
func NewCSVTable[T SQLType](file *os.File) (*CSVTable[T], error) {
16-
table := CSVTable[T]{
16+
func NewCSVTable(file *os.File) (*CSVTable, error) {
17+
table := CSVTable{
1718
reader: csv.NewReader(file), name: file.Name(),
18-
columns: []Column[T]{},
19+
columns: []Column{},
1920
}
2021

2122
// Scan the first row of the CSVs.
@@ -51,17 +52,8 @@ func NewCSVTable[T SQLType](file *os.File) (*CSVTable[T], error) {
5152
columnName = getColumnAlias(uint(index))
5253
}
5354

54-
switch deduceTypeForColumn(dataValue) {
55-
case SqlInt:
56-
newColumn := Column[int]{Name: columnName}
57-
table.columns = append(table.columns, newColumn)
58-
case SqlFloat:
59-
newColumn := Column[float64]{Name: columnName}
60-
case SqlBool:
61-
newColumn := Column[bool]{Name: columnName}
62-
default:
63-
newColumn := Column[string]{Name: columnName}
64-
}
55+
newColumn := Column{Name: columnName, Type: deduceTypeForColumn(dataValue)}
56+
table.columns = append(table.columns, newColumn)
6557
}
6658

6759
return &table, nil
@@ -71,14 +63,18 @@ func (ct CSVTable) Name() string {
7163
return ct.name
7264
}
7365

74-
func (ct CSVTable) Columns() []Column[T] {
66+
func (ct CSVTable) Columns() []Column {
7567
return ct.columns
7668
}
7769

7870
func (ct CSVTable) LoadData() {
7971
// TODO
8072
}
8173

74+
func (ct CSVTable) NextRow() ([]values.Value, error) {
75+
return make([]values.Value, 10), nil
76+
}
77+
8278
func (ct CSVTable) nextRow() ([]string, error) {
8379
row, err := ct.reader.Read()
8480
if err != nil {

table/storage.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

table/table.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414

1515
package table
1616

17-
type SQLType interface {
18-
int | float64 | string | bool
19-
}
17+
import "github.com/RohitAwate/commaql/vm/values"
2018

21-
type Column[T SQLType] struct {
19+
type Column struct {
2220
Name string
23-
Data Storage[T]
21+
Type SQLTypeHint
2422
}
2523

26-
type Table[T SQLType] interface {
24+
type Table interface {
2725
Name() string
28-
Columns() []Column[T]
26+
Columns() []Column
2927
LoadData()
28+
NextRow() ([]values.Value, error)
3029
}

vm/bytecode.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package vm
1616

17+
import "github.com/RohitAwate/commaql/vm/values"
18+
1719
type OpCode uint
1820

1921
const (
@@ -69,17 +71,17 @@ func GetOpCodeInfo(opCode OpCode) OpCodeInfo {
6971

7072
type Bytecode struct {
7173
Blob []OpCode
72-
ConstantsPool []Value
74+
ConstantsPool []values.Value
7375
}
7476

7577
func NewBytecode() Bytecode {
7678
return Bytecode{
7779
Blob: []OpCode{},
78-
ConstantsPool: []Value{},
80+
ConstantsPool: []values.Value{},
7981
}
8082
}
8183

82-
func (b *Bytecode) AddConstant(v Value) uint {
84+
func (b *Bytecode) AddConstant(v values.Value) uint {
8385
b.ConstantsPool = append(b.ConstantsPool, v)
8486
return uint(len(b.ConstantsPool) - 1)
8587
}

vm/value.go renamed to vm/values/value.go

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 vm
15+
package values
1616

1717
import (
1818
"fmt"

0 commit comments

Comments
 (0)