mirror of
https://github.com/RoBaertschi/tt.git
synced 2025-04-16 05:53:30 +00:00
split up checker and inferer and added booleans
This commit is contained in:
parent
62c1e46e17
commit
cefa2698c6
@ -11,3 +11,8 @@ AST --> Type Checking --> TAST --> IR Emission --> TTIR --> Codegen --> TTASM --
|
|||||||
|
|
||||||
TTIR: TT Intermediate Representation is the Representation that the AST gets turned into. This will be mostly be used for optimissing and abstracting away from Assembly
|
TTIR: TT Intermediate Representation is the Representation that the AST gets turned into. This will be mostly be used for optimissing and abstracting away from Assembly
|
||||||
TAST: Typed Ast
|
TAST: Typed Ast
|
||||||
|
|
||||||
|
## Type Checking
|
||||||
|
Passes:
|
||||||
|
- Type Inference
|
||||||
|
- Type Checking
|
||||||
|
15
ast/ast.go
15
ast/ast.go
@ -74,6 +74,15 @@ func (ie *IntegerExpression) expressionNode() {}
|
|||||||
func (ie *IntegerExpression) TokenLiteral() string { return ie.Token.Literal }
|
func (ie *IntegerExpression) TokenLiteral() string { return ie.Token.Literal }
|
||||||
func (ie *IntegerExpression) String() string { return ie.Token.Literal }
|
func (ie *IntegerExpression) String() string { return ie.Token.Literal }
|
||||||
|
|
||||||
|
type BooleanExpression struct {
|
||||||
|
Token token.Token // The token.TRUE or token.FALSE
|
||||||
|
Value bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (be *BooleanExpression) expressionNode() {}
|
||||||
|
func (be *BooleanExpression) TokenLiteral() string { return be.Token.Literal }
|
||||||
|
func (be *BooleanExpression) String() string { return be.Token.Literal }
|
||||||
|
|
||||||
//go:generate stringer -type=BinaryOperator
|
//go:generate stringer -type=BinaryOperator
|
||||||
type BinaryOperator int
|
type BinaryOperator int
|
||||||
|
|
||||||
@ -86,6 +95,10 @@ const (
|
|||||||
NotEqual
|
NotEqual
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (bo BinaryOperator) IsBooleanOperator() bool {
|
||||||
|
return bo == Equal || bo == NotEqual
|
||||||
|
}
|
||||||
|
|
||||||
func (bo BinaryOperator) SymbolString() string {
|
func (bo BinaryOperator) SymbolString() string {
|
||||||
switch bo {
|
switch bo {
|
||||||
case Add:
|
case Add:
|
||||||
@ -113,5 +126,5 @@ type BinaryExpression struct {
|
|||||||
func (be *BinaryExpression) expressionNode() {}
|
func (be *BinaryExpression) expressionNode() {}
|
||||||
func (be *BinaryExpression) TokenLiteral() string { return be.Token.Literal }
|
func (be *BinaryExpression) TokenLiteral() string { return be.Token.Literal }
|
||||||
func (be *BinaryExpression) String() string {
|
func (be *BinaryExpression) String() string {
|
||||||
return fmt.Sprintf("%s %s %s", be.Lhs, be.Operator.SymbolString(), be.Rhs)
|
return fmt.Sprintf("(%s %s %s)", be.Lhs, be.Operator.SymbolString(), be.Rhs)
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ func Compile(args Arguments) {
|
|||||||
|
|
||||||
tprogram, err := typechecker.New().CheckProgram(program)
|
tprogram, err := typechecker.New().CheckProgram(program)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Typechecker failed with %e\n", err)
|
fmt.Printf("Typechecker failed with: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if (args.ToPrint & PrintTAst) != 0 {
|
if (args.ToPrint & PrintTAst) != 0 {
|
||||||
|
@ -48,6 +48,8 @@ func New(l *lexer.Lexer) *Parser {
|
|||||||
|
|
||||||
p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
|
p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
|
||||||
p.registerPrefixFn(token.Int, p.parseIntegerExpression)
|
p.registerPrefixFn(token.Int, p.parseIntegerExpression)
|
||||||
|
p.registerPrefixFn(token.True, p.parseBooleanExpression)
|
||||||
|
p.registerPrefixFn(token.False, p.parseBooleanExpression)
|
||||||
|
|
||||||
p.infixParseFns = make(map[token.TokenType]infixParseFn)
|
p.infixParseFns = make(map[token.TokenType]infixParseFn)
|
||||||
p.registerInfixFn(token.Plus, p.parseBinaryExpression)
|
p.registerInfixFn(token.Plus, p.parseBinaryExpression)
|
||||||
@ -234,6 +236,23 @@ func (p *Parser) parseIntegerExpression() ast.Expression {
|
|||||||
return int
|
return int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseBooleanExpression() ast.Expression {
|
||||||
|
var value bool
|
||||||
|
switch p.curToken.Type {
|
||||||
|
case token.True:
|
||||||
|
value = true
|
||||||
|
case token.False:
|
||||||
|
value = false
|
||||||
|
default:
|
||||||
|
return p.exprError(p.curToken, "invalid token for boolean expression %s", p.curToken.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ast.BooleanExpression{
|
||||||
|
Token: p.curToken,
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Parser) parseBinaryExpression(lhs ast.Expression) ast.Expression {
|
func (p *Parser) parseBinaryExpression(lhs ast.Expression) ast.Expression {
|
||||||
var op ast.BinaryOperator
|
var op ast.BinaryOperator
|
||||||
switch p.curToken.Type {
|
switch p.curToken.Type {
|
||||||
|
20
tast/tast.go
20
tast/tast.go
@ -58,6 +58,8 @@ type FunctionDeclaration struct {
|
|||||||
ReturnType types.Type
|
ReturnType types.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Declaration = &FunctionDeclaration{}
|
||||||
|
|
||||||
func (fd *FunctionDeclaration) declarationNode() {}
|
func (fd *FunctionDeclaration) declarationNode() {}
|
||||||
func (fd *FunctionDeclaration) TokenLiteral() string { return fd.Token.Literal }
|
func (fd *FunctionDeclaration) TokenLiteral() string { return fd.Token.Literal }
|
||||||
func (fd *FunctionDeclaration) String() string {
|
func (fd *FunctionDeclaration) String() string {
|
||||||
@ -69,6 +71,8 @@ type IntegerExpression struct {
|
|||||||
Value int64
|
Value int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Expression = &IntegerExpression{}
|
||||||
|
|
||||||
func (ie *IntegerExpression) expressionNode() {}
|
func (ie *IntegerExpression) expressionNode() {}
|
||||||
func (ie *IntegerExpression) Type() types.Type {
|
func (ie *IntegerExpression) Type() types.Type {
|
||||||
return types.I64
|
return types.I64
|
||||||
@ -76,6 +80,20 @@ func (ie *IntegerExpression) Type() types.Type {
|
|||||||
func (ie *IntegerExpression) TokenLiteral() string { return ie.Token.Literal }
|
func (ie *IntegerExpression) TokenLiteral() string { return ie.Token.Literal }
|
||||||
func (ie *IntegerExpression) String() string { return ie.Token.Literal }
|
func (ie *IntegerExpression) String() string { return ie.Token.Literal }
|
||||||
|
|
||||||
|
type BooleanExpression struct {
|
||||||
|
Token token.Token // The token.TRUE or token.FALSE
|
||||||
|
Value bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Expression = &BooleanExpression{}
|
||||||
|
|
||||||
|
func (be *BooleanExpression) expressionNode() {}
|
||||||
|
func (ie *BooleanExpression) Type() types.Type {
|
||||||
|
return types.Bool
|
||||||
|
}
|
||||||
|
func (be *BooleanExpression) TokenLiteral() string { return be.Token.Literal }
|
||||||
|
func (be *BooleanExpression) String() string { return be.Token.Literal }
|
||||||
|
|
||||||
type BinaryExpression struct {
|
type BinaryExpression struct {
|
||||||
Token token.Token // The operator
|
Token token.Token // The operator
|
||||||
Lhs, Rhs Expression
|
Lhs, Rhs Expression
|
||||||
@ -83,6 +101,8 @@ type BinaryExpression struct {
|
|||||||
ResultType types.Type
|
ResultType types.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Expression = &BinaryExpression{}
|
||||||
|
|
||||||
func (be *BinaryExpression) expressionNode() {}
|
func (be *BinaryExpression) expressionNode() {}
|
||||||
func (be *BinaryExpression) Type() types.Type {
|
func (be *BinaryExpression) Type() types.Type {
|
||||||
return be.ResultType
|
return be.ResultType
|
||||||
|
@ -16,7 +16,9 @@ type Token struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var keywords = map[string]TokenType{
|
var keywords = map[string]TokenType{
|
||||||
"fn": Fn,
|
"fn": Fn,
|
||||||
|
"true": True,
|
||||||
|
"false": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -40,7 +42,9 @@ const (
|
|||||||
NotEqual TokenType = "!="
|
NotEqual TokenType = "!="
|
||||||
|
|
||||||
// Keywords
|
// Keywords
|
||||||
Fn TokenType = "FN"
|
Fn TokenType = "FN"
|
||||||
|
True TokenType = "TRUE"
|
||||||
|
False TokenType = "FALSE"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LookupKeyword(literal string) TokenType {
|
func LookupKeyword(literal string) TokenType {
|
||||||
|
@ -40,6 +40,12 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) {
|
|||||||
switch expr := expr.(type) {
|
switch expr := expr.(type) {
|
||||||
case *tast.IntegerExpression:
|
case *tast.IntegerExpression:
|
||||||
return &Constant{Value: expr.Value}, []Instruction{}
|
return &Constant{Value: expr.Value}, []Instruction{}
|
||||||
|
case *tast.BooleanExpression:
|
||||||
|
value := int64(0)
|
||||||
|
if expr.Value {
|
||||||
|
value = 1
|
||||||
|
}
|
||||||
|
return &Constant{Value: value}, []Instruction{}
|
||||||
case *tast.BinaryExpression:
|
case *tast.BinaryExpression:
|
||||||
switch expr.Operator {
|
switch expr.Operator {
|
||||||
default:
|
default:
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
package typechecker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"robaertschi.xyz/robaertschi/tt/ast"
|
||||||
|
"robaertschi.xyz/robaertschi/tt/tast"
|
||||||
|
"robaertschi.xyz/robaertschi/tt/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Checker struct {
|
||||||
|
foundMain bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Checker {
|
||||||
|
return &Checker{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) error(t token.Token, format string, args ...any) error {
|
||||||
|
return fmt.Errorf("%s:%d:%d %s", t.Loc.File, t.Loc.Line, t.Loc.Col, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) CheckProgram(program *ast.Program) (*tast.Program, error) {
|
||||||
|
newProgram, err := c.inferTypes(program)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
errs := []error{}
|
||||||
|
|
||||||
|
for _, decl := range newProgram.Declarations {
|
||||||
|
err := c.checkDeclaration(decl)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs) > 0 {
|
||||||
|
return nil, errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.foundMain {
|
||||||
|
// TODO(Robin): Add support for libraries
|
||||||
|
errs = append(errs, errors.New("no function called 'main' found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return newProgram, errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) checkDeclaration(decl tast.Declaration) error {
|
||||||
|
switch decl := decl.(type) {
|
||||||
|
case *tast.FunctionDeclaration:
|
||||||
|
err := c.checkExpression(decl.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if decl.Name == "main" {
|
||||||
|
c.foundMain = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("unhandled declaration in type checker")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) checkExpression(expr tast.Expression) error {
|
||||||
|
switch expr := expr.(type) {
|
||||||
|
case *tast.IntegerExpression:
|
||||||
|
return nil
|
||||||
|
case *tast.BooleanExpression:
|
||||||
|
return nil
|
||||||
|
case *tast.BinaryExpression:
|
||||||
|
lhsErr := c.checkExpression(expr.Lhs)
|
||||||
|
rhsErr := c.checkExpression(expr.Rhs)
|
||||||
|
var operandErr error
|
||||||
|
if lhsErr == nil && rhsErr == nil {
|
||||||
|
if !expr.Lhs.Type().IsSameType(expr.Rhs.Type()) {
|
||||||
|
operandErr = fmt.Errorf("the lhs of the expression does not have the same type then the rhs, lhs=%q, rhs=%q", expr.Lhs.Type().Name(), expr.Rhs.Type().Name())
|
||||||
|
} else if !expr.Lhs.Type().SupportsBinaryOperator(expr.Operator) {
|
||||||
|
operandErr = fmt.Errorf("the operator %q is not supported by the type %q", expr.Operator, expr.Lhs.Type().Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Join(lhsErr, rhsErr, operandErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unhandled expression in type checker")
|
||||||
|
}
|
@ -6,28 +6,15 @@ import (
|
|||||||
|
|
||||||
"robaertschi.xyz/robaertschi/tt/ast"
|
"robaertschi.xyz/robaertschi/tt/ast"
|
||||||
"robaertschi.xyz/robaertschi/tt/tast"
|
"robaertschi.xyz/robaertschi/tt/tast"
|
||||||
"robaertschi.xyz/robaertschi/tt/token"
|
|
||||||
"robaertschi.xyz/robaertschi/tt/types"
|
"robaertschi.xyz/robaertschi/tt/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Checker struct {
|
func (c *Checker) inferTypes(program *ast.Program) (*tast.Program, error) {
|
||||||
foundMain bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Checker {
|
|
||||||
return &Checker{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Checker) error(t token.Token, format string, args ...any) error {
|
|
||||||
return fmt.Errorf("%s:%d:%d %s", t.Loc.File, t.Loc.Line, t.Loc.Col, fmt.Sprintf(format, args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Checker) CheckProgram(program *ast.Program) (*tast.Program, error) {
|
|
||||||
decls := []tast.Declaration{}
|
decls := []tast.Declaration{}
|
||||||
errs := []error{}
|
errs := []error{}
|
||||||
|
|
||||||
for _, decl := range program.Declarations {
|
for _, decl := range program.Declarations {
|
||||||
decl, err := c.checkDeclaration(decl)
|
decl, err := c.inferDeclaration(decl)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
decls = append(decls, decl)
|
decls = append(decls, decl)
|
||||||
} else {
|
} else {
|
||||||
@ -35,52 +22,44 @@ func (c *Checker) CheckProgram(program *ast.Program) (*tast.Program, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.foundMain {
|
|
||||||
// TODO(Robin): Add support for libraries
|
|
||||||
errs = append(errs, errors.New("no function called 'main' found"))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &tast.Program{Declarations: decls}, errors.Join(errs...)
|
return &tast.Program{Declarations: decls}, errors.Join(errs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkDeclaration(decl ast.Declaration) (tast.Declaration, error) {
|
func (c *Checker) inferDeclaration(decl ast.Declaration) (tast.Declaration, error) {
|
||||||
switch decl := decl.(type) {
|
switch decl := decl.(type) {
|
||||||
case *ast.FunctionDeclaration:
|
case *ast.FunctionDeclaration:
|
||||||
body, err := c.checkExpression(decl.Body)
|
body, err := c.inferExpression(decl.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if decl.Name == "main" {
|
|
||||||
c.foundMain = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return &tast.FunctionDeclaration{Token: decl.Token, Body: body, ReturnType: body.Type(), Name: decl.Name}, nil
|
return &tast.FunctionDeclaration{Token: decl.Token, Body: body, ReturnType: body.Type(), Name: decl.Name}, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("unhandled declaration in type checker")
|
return nil, errors.New("unhandled declaration in type inferer")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkExpression(expr ast.Expression) (tast.Expression, error) {
|
func (c *Checker) inferExpression(expr ast.Expression) (tast.Expression, error) {
|
||||||
switch expr := expr.(type) {
|
switch expr := expr.(type) {
|
||||||
case *ast.IntegerExpression:
|
case *ast.IntegerExpression:
|
||||||
return &tast.IntegerExpression{Token: expr.Token, Value: expr.Value}, nil
|
return &tast.IntegerExpression{Token: expr.Token, Value: expr.Value}, nil
|
||||||
|
case *ast.BooleanExpression:
|
||||||
|
return &tast.BooleanExpression{Token: expr.Token, Value: expr.Value}, nil
|
||||||
case *ast.ErrorExpression:
|
case *ast.ErrorExpression:
|
||||||
return nil, c.error(expr.InvalidToken, "invalid expression")
|
return nil, c.error(expr.InvalidToken, "invalid expression")
|
||||||
case *ast.BinaryExpression:
|
case *ast.BinaryExpression:
|
||||||
lhs, lhsErr := c.checkExpression(expr.Lhs)
|
lhs, lhsErr := c.inferExpression(expr.Lhs)
|
||||||
rhs, rhsErr := c.checkExpression(expr.Rhs)
|
rhs, rhsErr := c.inferExpression(expr.Rhs)
|
||||||
var operandErr error
|
|
||||||
var resultType types.Type
|
var resultType types.Type
|
||||||
if lhsErr == nil && rhsErr == nil {
|
if lhsErr == nil && rhsErr == nil {
|
||||||
if !lhs.Type().IsSameType(rhs.Type()) {
|
if expr.Operator.IsBooleanOperator() {
|
||||||
operandErr = fmt.Errorf("the lhs of the expression does not have the same type then the rhs, lhs=%q, rhs=%q", lhs.Type(), rhs.Type())
|
resultType = types.Bool
|
||||||
} else {
|
} else {
|
||||||
resultType = lhs.Type()
|
resultType = lhs.Type()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tast.BinaryExpression{Lhs: lhs, Rhs: rhs, Operator: expr.Operator, Token: expr.Token, ResultType: resultType}, errors.Join(lhsErr, rhsErr, operandErr)
|
return &tast.BinaryExpression{Lhs: lhs, Rhs: rhs, Operator: expr.Operator, Token: expr.Token, ResultType: resultType}, errors.Join(lhsErr, rhsErr)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unhandled expression in type checker")
|
return nil, fmt.Errorf("unhandled expression in type inferer")
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import "robaertschi.xyz/robaertschi/tt/ast"
|
||||||
|
|
||||||
type Type interface {
|
type Type interface {
|
||||||
// Checks if the two types are the same
|
// Checks if the two types are the same
|
||||||
IsSameType(Type) bool
|
IsSameType(Type) bool
|
||||||
|
SupportsBinaryOperator(op ast.BinaryOperator) bool
|
||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,12 +16,21 @@ type TypeId struct {
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
I64Id int64 = iota
|
I64Id int64 = iota
|
||||||
|
BoolId
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
I64 = New(I64Id, "i64")
|
I64 = New(I64Id, "i64")
|
||||||
|
Bool = New(BoolId, "bool")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (ti *TypeId) SupportsBinaryOperator(op ast.BinaryOperator) bool {
|
||||||
|
if ti == Bool && !op.IsBooleanOperator() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (ti *TypeId) IsSameType(t Type) bool {
|
func (ti *TypeId) IsSameType(t Type) bool {
|
||||||
if ti2, ok := t.(*TypeId); ok {
|
if ti2, ok := t.(*TypeId); ok {
|
||||||
return ti.id == ti2.id
|
return ti.id == ti2.id
|
||||||
|
Loading…
x
Reference in New Issue
Block a user