Implemented source tracking and type checking
All checks were successful
/ test (push) Successful in 21s
All checks were successful
/ test (push) Successful in 21s
This commit is contained in:
parent
70c2da4a4d
commit
329fcfff6f
30 changed files with 427 additions and 125 deletions
|
@ -17,6 +17,7 @@ var (
|
|||
UInt32 = &Base{name: "uint32", size: 4}
|
||||
UInt16 = &Base{name: "uint16", size: 2}
|
||||
UInt8 = &Base{name: "uint8", size: 1}
|
||||
Void = &Base{name: "void", size: 0}
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
47
src/types/Function.go
Normal file
47
src/types/Function.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package types
|
||||
|
||||
import "strings"
|
||||
|
||||
// Function transforms inputs to new outputs.
|
||||
type Function struct {
|
||||
Input []Type
|
||||
Output []Type
|
||||
}
|
||||
|
||||
// Name returns the type name.
|
||||
func (f *Function) Name() string {
|
||||
builder := strings.Builder{}
|
||||
builder.WriteString("(")
|
||||
|
||||
for i, input := range f.Input {
|
||||
builder.WriteString(input.Name())
|
||||
|
||||
if i != len(f.Input)-1 {
|
||||
builder.WriteString(", ")
|
||||
}
|
||||
}
|
||||
|
||||
builder.WriteString(")")
|
||||
|
||||
if len(f.Output) == 0 {
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
builder.WriteString(" -> (")
|
||||
|
||||
for i, output := range f.Output {
|
||||
builder.WriteString(output.Name())
|
||||
|
||||
if i != len(f.Output)-1 {
|
||||
builder.WriteString(",")
|
||||
}
|
||||
}
|
||||
|
||||
builder.WriteString(")")
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// Size returns the total size in bytes.
|
||||
func (f *Function) Size() int {
|
||||
return 8
|
||||
}
|
71
src/types/Parse.go
Normal file
71
src/types/Parse.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"git.urbach.dev/cli/q/src/token"
|
||||
)
|
||||
|
||||
// Parse returns the type with the given tokens or `nil` if it doesn't exist.
|
||||
func Parse[T ~[]token.Token](tokens T, source []byte) Type {
|
||||
if tokens[0].Kind == token.Mul {
|
||||
to := tokens[1:]
|
||||
typ := Parse(to, source)
|
||||
|
||||
if typ == Any {
|
||||
return AnyPointer
|
||||
}
|
||||
|
||||
return &Pointer{To: typ}
|
||||
}
|
||||
|
||||
if len(tokens) >= 2 && tokens[0].Kind == token.ArrayStart && tokens[1].Kind == token.ArrayEnd {
|
||||
to := tokens[2:]
|
||||
typ := Parse(to, source)
|
||||
|
||||
if typ == Any {
|
||||
return AnyArray
|
||||
}
|
||||
|
||||
return &Array{Of: typ}
|
||||
}
|
||||
|
||||
if tokens[0].Kind != token.Identifier {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tokens[0].String(source) {
|
||||
case "int":
|
||||
return Int
|
||||
case "int64":
|
||||
return Int64
|
||||
case "int32":
|
||||
return Int32
|
||||
case "int16":
|
||||
return Int16
|
||||
case "int8":
|
||||
return Int8
|
||||
case "uint":
|
||||
return UInt
|
||||
case "uint64":
|
||||
return UInt64
|
||||
case "uint32":
|
||||
return UInt32
|
||||
case "uint16":
|
||||
return UInt16
|
||||
case "uint8":
|
||||
return UInt8
|
||||
case "byte":
|
||||
return Byte
|
||||
case "bool":
|
||||
return Bool
|
||||
case "float":
|
||||
return Float
|
||||
case "float64":
|
||||
return Float64
|
||||
case "float32":
|
||||
return Float32
|
||||
case "any":
|
||||
return Any
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue