-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINALLY, (sometimes) working classes
- Loading branch information
Showing
9 changed files
with
534 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
for (var x:int = 0; x < 10; x = x + 1) { | ||
print x; | ||
} | ||
class Bek { | ||
a:int; | ||
|
||
Bek() { | ||
this.a = 1; | ||
} | ||
|
||
func sob() { | ||
print this.a; | ||
} | ||
} | ||
|
||
var lol: Bek = new Bek(); | ||
lol.sob(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package compiler | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/llir/llvm/ir" | ||
"github.com/llir/llvm/ir/types" | ||
"github.com/llir/llvm/ir/value" | ||
) | ||
|
||
type Class struct { | ||
Stmt | ||
Name string | ||
Constructor Method | ||
Fields []Field | ||
Methods []Method | ||
} | ||
|
||
type Field struct { | ||
Name string | ||
Type types.Type | ||
Private bool | ||
Index int | ||
} | ||
|
||
type Method struct { | ||
Name string | ||
Params []*ir.Param | ||
ReturnType types.Type | ||
Body []Stmt | ||
Private bool | ||
} | ||
|
||
func (ctx *Context) compileClassDeclaration(c *Class) { | ||
classType := types.NewStruct() | ||
for _, field := range c.Fields { | ||
classType.Fields = append(classType.Fields, field.Type) | ||
} | ||
ctx.Compiler.StructFields[c.Name] = c.Fields | ||
fmt.Println(c.Constructor.Body[0]) | ||
ctx.Module.NewTypeDef(c.Name, classType) | ||
if c.Constructor.Name != "" { | ||
fmt.Println("Declaring constructor " + c.Constructor.Name) | ||
constructor := ctx.compileMethodDeclaration(c.Constructor, c) | ||
// Add constructor to the symbol table | ||
ctx.Compiler.SymbolTable[c.Name] = constructor | ||
} | ||
for _, m := range c.Methods { | ||
fmt.Println("Declaring method " + m.Name) | ||
ctx.Compiler.SymbolTable[c.Name+"."+m.Name] = ctx.compileMethodDeclaration(m, c) | ||
} | ||
} | ||
|
||
func (ctx *Context) compileMethodDeclaration(m Method, c *Class) *ir.Func { | ||
params := m.Params | ||
var typeDef types.Type | ||
for _, t := range ctx.Module.TypeDefs { | ||
if t.Name() == c.Name { | ||
typeDef = t | ||
break | ||
} | ||
} | ||
|
||
if typeDef == nil { | ||
panic("type not found") | ||
} | ||
params = append(params, ir.NewParam("this", types.NewPointer(typeDef))) | ||
f := ctx.Module.NewFunc(m.Name, m.ReturnType) | ||
f.Params = params | ||
block := f.NewBlock("entry") | ||
classctx := ctx.NewContext(block) | ||
for _, stmt := range m.Body { | ||
classctx.compileStmt(stmt) | ||
} | ||
if classctx.Term == nil { | ||
if m.ReturnType.Equal(types.Void) { | ||
classctx.NewRet(nil) | ||
} else { | ||
panic(fmt.Errorf("function `%s` does not return a value", m.Name)) | ||
} | ||
} | ||
|
||
return f | ||
} | ||
|
||
func (ctx *Context) compileClassMethod(c SClassMethod) { | ||
// Get the class instance | ||
instance := ctx.lookupVariable(c.InstanceName) | ||
|
||
var classType types.Type | ||
for _, t := range ctx.Module.TypeDefs { | ||
if t.Equal(instance.Type().(*types.PointerType).ElemType) { | ||
classType = t | ||
break | ||
} | ||
} | ||
if classType == nil { | ||
panic(fmt.Errorf("type `%s` not found", instance.Type().String())) | ||
} | ||
// Get the method | ||
method := ctx.Compiler.SymbolTable[instance.Type().(*types.PointerType).ElemType.Name()+"."+c.MethodName].(*ir.Func) | ||
if method == nil { | ||
panic(fmt.Errorf("method '%s.%s' not found", instance.Type().(*types.PointerType).ElemType.Name(), c.MethodName)) | ||
} | ||
// Get the function arguments | ||
var args []value.Value | ||
for _, arg := range c.Args { | ||
args = append(args, ctx.compileExpr(arg)) | ||
} | ||
// Call the function | ||
ctx.NewCall(method, append([]value.Value{instance}, args...)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.