Skip to content

Commit

Permalink
ALL: Bunch of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 5, 2024
1 parent 3a15f63 commit cf59b93
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 56 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func buildProject() string {
path = filepath.Join(project.Path, "build", path[:len(path)-3]+".ll")

irFile, _ := os.Create(path)
codegen.Emit(file.Path, project, file.Ast, irFile)
codegen.Emit(file.AbsolutePath(), project, file.Ast, irFile)
_ = irFile.Close()

irPaths = append(irPaths, path)
Expand Down Expand Up @@ -148,7 +148,7 @@ func generateEntrypoint(project *workspace.Project, path string) error {
m := llvm.NewModule()
m.Source("__entrypoint")

function, _ := project.GetFunction("main")
function := project.GetFunction("main")

void := m.Void()
i32 := m.Primitive("i32", 32, llvm.SignedEncoding)
Expand Down
4 changes: 2 additions & 2 deletions cmd/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func getDefinitionToken(resolver fuckoff.Resolver, token *ast.Token) []protocol.
case *ast.Member:
if s, ok := asThroughPointer[*ast.Struct](parent.Value.Result().Type); ok {
if parentWantsFunction(parent) {
method, _ := resolver.GetMethod(s, token.String(), false)
method := resolver.GetMethod(s, token.String(), false)
if method == nil {
method, _ = resolver.GetMethod(s, token.String(), true)
method = resolver.GetMethod(s, token.String(), true)
}

if method != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func getHoverToken(resolver fuckoff.Resolver, token *ast.Token) *protocol.Hover
case *ast.Member:
if s, ok := asThroughPointer[*ast.Struct](parent.Value.Result().Type); ok {
if parentWantsFunction(parent) {
method, _ := resolver.GetMethod(s, token.String(), false)
method := resolver.GetMethod(s, token.String(), false)
if method == nil {
method, _ = resolver.GetMethod(s, token.String(), true)
method = resolver.GetMethod(s, token.String(), true)
}

if method != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (g *get) contains(node Node) bool {
range_ := node.Cst().Range

if g.pos.Line == range_.End.Line && g.pos.Column >= range_.End.Column && node.Token().IsEmpty() {
if node.Parent().Cst() != nil && (node.Parent().Cst().Range.End.Line > range_.End.Line || node.Parent().Cst().Range.End.Column == range_.End.Column) {
if node.Parent() != nil && node.Parent().Cst() != nil && (node.Parent().Cst().Range.End.Line > range_.End.Line || node.Parent().Cst().Range.End.Column == range_.End.Column) {
next := GetNextSibling(node)

if next == nil || (next.Cst() != nil && next.Cst().Range.Start.Line > g.pos.Line) {
Expand Down Expand Up @@ -160,7 +160,7 @@ func (g *getNextSibling) VisitNode(node Node) {
// GetParent()

func GetParent[T Node](node Node) T {
for node != nil {
for !IsNil(node) {
if n, ok := node.(T); ok {
return n
}
Expand Down
2 changes: 1 addition & 1 deletion core/ast/cst2ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *converter) convertExpr(node cst.Node) ast.Expr {
return c.convertAllocateArrayExpr(node)
case cst.IdentifierNode:
return c.convertIdentifierExpr(node)
case cst.BoolExprNode, cst.NumberExprNode, cst.CharacterExprNode, cst.StringExprNode:
case cst.NilExprNode, cst.BoolExprNode, cst.NumberExprNode, cst.CharacterExprNode, cst.StringExprNode:
return c.convertLiteral(node)

default:
Expand Down
10 changes: 8 additions & 2 deletions core/ast/cst2ast/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cst2ast
import (
"fireball/core/ast"
"fireball/core/cst"
"fireball/core/scanner"
)

func (c *converter) convertStmt(node cst.Node) ast.Stmt {
Expand Down Expand Up @@ -121,19 +122,24 @@ func (c *converter) convertForStmt(node cst.Node) ast.Stmt {
var increment ast.Expr
var body ast.Stmt

semicolons := 0

for _, child := range node.Children {
if child.Kind.IsStmt() {
if initializer == nil {
if semicolons == 0 {
initializer = c.convertStmt(child)
semicolons++
} else {
body = c.convertStmt(child)
}
} else if child.Kind.IsExpr() {
if condition == nil {
if semicolons < 2 {
condition = c.convertExpr(child)
} else {
increment = c.convertExpr(child)
}
} else if child.Token.Kind == scanner.Semicolon {
semicolons++
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/ast/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (i *Impl) GetMethod(name string, static bool) *Func {
}

for _, function := range i.Methods {
if function.Name.String() == name && function.Flags&Static == staticValue {
if function.Name != nil && function.Name.String() == name && function.Flags&Static == staticValue {
return function
}
}
Expand All @@ -57,7 +57,7 @@ func (e *Enum) GetCase(name string) *EnumCase {
}

func (f *Field) GetMangledName() string {
return fmt.Sprintf("fb$%s::%s", f.Parent, f.Name)
return fmt.Sprintf("fb$%s::%s", f.Parent(), f.Name)
}

// Func
Expand Down
36 changes: 19 additions & 17 deletions core/ast/types_manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ func (s *Struct) Align() uint32 {

func (s *Struct) Equals(other Type) bool {
if s2, ok := As[*Struct](other); ok {
return slices.EqualFunc(s.Fields, s2.Fields, fieldEquals)
return tokensEquals(s.Name, s2.Name) && slices.EqualFunc(s.Fields, s2.Fields, fieldEquals)
}

return false
}

func fieldEquals(v1, v2 *Field) bool {
return v1.Name.String() == v2.Name.String() && typesEquals(v1.Type, v2.Type)
return tokensEquals(v1.Name, v2.Name) && typesEquals(v1.Type, v2.Type)
}

func (s *Struct) CanAssignTo(other Type) bool {
return s.Equals(other)
if s2, ok := As[*Struct](other); ok {
return slices.EqualFunc(s.Fields, s2.Fields, fieldEquals)
}

return false
}

func (s *Struct) Resolved() Type {
Expand Down Expand Up @@ -205,7 +209,7 @@ func (e *Enum) Equals(other Type) bool {
}

func enumCaseEquals(v1, v2 *EnumCase) bool {
return v1.Name.String() == v2.Name.String() && v1.ActualValue == v2.ActualValue
return tokensEquals(v1.Name, v2.Name) && v1.ActualValue == v2.ActualValue
}

func (e *Enum) CanAssignTo(other Type) bool {
Expand All @@ -232,17 +236,7 @@ func (f *Func) Align() uint32 {

func (f *Func) Equals(other Type) bool {
if f2, ok := As[*Func](other); ok {
fName := ""
if f.Name != nil {
fName = f.Name.String()
}

f2Name := ""
if f2.Name != nil {
f2Name = f2.Name.String()
}

return fName == f2Name && typesEquals(f.Returns, f2.Returns) && slices.EqualFunc(f.Params, f2.Params, paramEquals)
return tokensEquals(f.Name, f2.Name) && typesEquals(f.Returns, f2.Returns) && slices.EqualFunc(f.Params, f2.Params, paramEquals)
}

return false
Expand Down Expand Up @@ -309,11 +303,15 @@ func (t *typePrinter) VisitResolvable(type_ *Resolvable) {
}

func (t *typePrinter) VisitStruct(type_ *Struct) {
t.str += type_.Name.String()
if type_.Name != nil {
t.str += type_.Name.String()
}
}

func (t *typePrinter) VisitEnum(type_ *Enum) {
t.str += type_.Name.String()
if type_.Name != nil {
t.str += type_.Name.String()
}
}

func (t *typePrinter) VisitFunc(type_ *Func) {
Expand All @@ -328,6 +326,10 @@ func (t *typePrinter) VisitNode(node Node) {

// Utils

func tokensEquals(t1, t2 *Token) bool {
return (t1 == nil && t2 == nil) || (t1 != nil && t2 != nil && t1.String() == t2.String())
}

func typesEquals(t1, t2 Type) bool {
return (t1 == nil && t2 == nil) || (t1 != nil && t2 != nil && t1.Equals(t2))
}
10 changes: 5 additions & 5 deletions core/checker/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (c *checker) VisitIdentifier(expr *ast.Identifier) {

// Function / function pointer
if parentWantsFunction(expr) {
if f, _ := c.resolver.GetFunction(expr.String()); f != nil {
if f := c.resolver.GetFunction(expr.String()); f != nil {
expr.Result().SetFunction(f)
expr.Kind = ast.FunctionKind

Expand All @@ -510,7 +510,7 @@ func (c *checker) VisitIdentifier(expr *ast.Identifier) {
}

// Type
if t, _ := c.resolver.GetType(expr.String()); t != nil {
if t := c.resolver.GetType(expr.String()); t != nil {
expr.Result().SetType(t)

if _, ok := t.(*ast.Enum); ok {
Expand Down Expand Up @@ -786,7 +786,7 @@ func (c *checker) VisitMember(expr *ast.Member) {
if v, ok := expr.Value.Result().Type.(*ast.Struct); ok {
// Check if parent expression wants a function
if parentWantsFunction(expr) {
function, _ := c.resolver.GetMethod(v, expr.Name.String(), true)
function := c.resolver.GetMethod(v, expr.Name.String(), true)

if function == nil {
c.error(expr.Name, "Struct '%s' does not contain static method with the name '%s'", ast.PrintType(v), expr.Name)
Expand Down Expand Up @@ -854,7 +854,7 @@ func (c *checker) VisitMember(expr *ast.Member) {

// Check if parent expression wants a function
if parentWantsFunction(expr) {
function, _ := c.resolver.GetMethod(s, expr.Name.String(), false)
function := c.resolver.GetMethod(s, expr.Name.String(), false)

if function != nil {
expr.Result().SetFunction(function)
Expand Down Expand Up @@ -901,7 +901,7 @@ func parentWantsFunction(expr ast.Expr) bool {
}

func (c *checker) checkMalloc(expr ast.Expr) {
function, _ := c.resolver.GetFunction("malloc")
function := c.resolver.GetFunction("malloc")

if function == nil {
c.error(expr, "Malloc function not found")
Expand Down
12 changes: 9 additions & 3 deletions core/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,13 @@ func (c *codegen) getFunction(function *ast.Func) exprValue {
}

// Resolve function from project
_, filePath := c.resolver.GetFunction(function.Name.String())
if f := c.resolver.GetFunction(function.Name.String()); f != nil {

if filePath == c.path {
panic("codegen.getFunction() - Local function not found in functions map")
filePath := ast.GetParent[*ast.File](f).Path

if filePath == c.path {
panic("codegen.getFunction() - Local function not found in functions map")
}
}

value := c.module.Declare(c.getType(function))
Expand Down Expand Up @@ -324,6 +327,9 @@ func (c *codegen) getType(type_ ast.Type) llvm.Type {
llvmType = c.module.Primitive("f32", 32, llvm.FloatEncoding)
case ast.F64:
llvmType = c.module.Primitive("f64", 64, llvm.FloatEncoding)

default:
panic("codegen.getType() - Not implemented")
}
} else if v, ok := ast.As[*ast.Array](type_); ok {
// Array
Expand Down
10 changes: 8 additions & 2 deletions core/codegen/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *codegen) VisitStructInitializer(expr *ast.StructInitializer) {

// Malloc
if expr.New {
mallocFunc, _ := c.resolver.GetFunction("malloc")
mallocFunc := c.resolver.GetFunction("malloc")
malloc := c.getFunction(mallocFunc)

pointer := c.block.Call(
Expand Down Expand Up @@ -152,7 +152,7 @@ func (c *codegen) VisitArrayInitializer(expr *ast.ArrayInitializer) {
func (c *codegen) VisitAllocateArray(expr *ast.AllocateArray) {
count := c.loadExpr(expr.Count)

mallocFunc, _ := c.resolver.GetFunction("malloc")
mallocFunc := c.resolver.GetFunction("malloc")
malloc := c.getFunction(mallocFunc)

a, _ := ast.As[*ast.Primitive](expr.Count.Result().Type)
Expand Down Expand Up @@ -442,6 +442,12 @@ func (c *codegen) VisitCast(expr *ast.Cast) {
c.exprResult = value
return
}

if _, ok := ast.As[*ast.Func](expr.Result().Type); ok {
// pointer to function pointer
c.exprResult = value
return
}
}

// Error
Expand Down
5 changes: 4 additions & 1 deletion core/cst/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
)

var canStartExpr = []scanner.TokenKind{
scanner.Nil,
scanner.True,
scanner.False,
scanner.Number,
scanner.Hex,
scanner.Binary,
scanner.String,
scanner.Identifier,

Expand Down Expand Up @@ -64,7 +67,7 @@ var canStartStructFieldExpr = []scanner.TokenKind{scanner.Identifier}

func parsePrefixExprPratt(p *parser) Node {
switch p.peek() {
case scanner.True, scanner.False, scanner.Number, scanner.Hex, scanner.Binary, scanner.Character, scanner.String:
case scanner.Nil, scanner.True, scanner.False, scanner.Number, scanner.Hex, scanner.Binary, scanner.Character, scanner.String:
return p.advanceGetLeaf()

case scanner.Identifier:
Expand Down
17 changes: 17 additions & 0 deletions core/cst/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ func (n Node) Leaf() bool {
return n.Token.Lexeme != ""
}

func (n Node) Count(kind scanner.TokenKind) int {
count := 0

for _, child := range n.Children {
if child.Token.Kind == kind {
count++
}
}

return count
}

func (n Node) Contains(kind scanner.TokenKind) bool {
for _, child := range n.Children {
if child.Token.Kind == kind {
Expand Down Expand Up @@ -89,6 +101,7 @@ const (
StructFieldExprNode
ArrayExprNode
AllocateArrayExprNode
NilExprNode
BoolExprNode
NumberExprNode
CharacterExprNode
Expand All @@ -107,6 +120,8 @@ func NodeKindFromToken(token scanner.Token) NodeKind {
case scanner.Identifier:
return IdentifierNode

case scanner.Nil:
return NilExprNode
case scanner.True, scanner.False:
return BoolExprNode
case scanner.Number, scanner.Hex, scanner.Binary:
Expand Down Expand Up @@ -207,6 +222,8 @@ func (n NodeKind) String() string {
return "Array"
case AllocateArrayExprNode:
return "Allocate array"
case NilExprNode:
return "Nil"
case BoolExprNode:
return "Bool"
case NumberExprNode:
Expand Down
6 changes: 3 additions & 3 deletions core/fuckoff/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

type Resolver interface {
GetType(name string) (ast.Type, string)
GetType(name string) ast.Type

GetFunction(name string) (*ast.Func, string)
GetFunction(name string) *ast.Func

GetMethod(type_ ast.Type, name string, static bool) (*ast.Func, string)
GetMethod(type_ ast.Type, name string, static bool) *ast.Func
GetMethods(type_ ast.Type, static bool) []*ast.Func

GetFileNodes() []*ast.File
Expand Down
Loading

0 comments on commit cf59b93

Please sign in to comment.