Skip to content

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
vyPal committed Nov 28, 2023
1 parent bc6e38b commit 07648ec
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 197 deletions.
150 changes: 13 additions & 137 deletions ast_dump.json
Original file line number Diff line number Diff line change
@@ -1,151 +1,27 @@
[
{
"Stmt": null,
"Name": "So",
"Constructor": {
"Name": "So",
"Params": null,
"ReturnType": {
"Typ": {
"TypeName": ""
},
"CustomType": ""
"Name": "a",
"Args": null,
"ReturnType": {
"Typ": {
"TypeName": ""
},
"Body": [
{
"Stmt": null,
"Name": {
"Expr": null,
"Struct": {
"Expr": null,
"Name": "this"
},
"Name": {
"Expr": null,
"Name": "dejv"
}
},
"Expr": {
"EConst": null,
"Value": 1
}
}
],
"Private": false
"CustomType": ""
},
"Fields": [
{
"Name": "dejv",
"Type": {
"TypeName": "",
"BitSize": 64
},
"Private": false,
"Index": 0
}
],
"Methods": [
"Body": [
{
"Name": "bek",
"Params": null,
"ReturnType": {
"Typ": {
"TypeName": ""
},
"CustomType": ""
},
"Body": [
{
"Stmt": null,
"Name": {
"Expr": null,
"Struct": {
"Expr": null,
"Name": "this"
},
"Name": {
"Expr": null,
"Name": "dejv"
}
},
"Expr": {
"Expr": null,
"Left": {
"Expr": null,
"Struct": {
"Expr": null,
"Name": "this"
},
"Name": {
"Expr": null,
"Name": "dejv"
}
},
"Right": {
"EConst": null,
"Value": 1
}
}
}
],
"Private": false
},
{
"Name": "david",
"Params": null,
"ReturnType": {
"Typ": {
"TypeName": "",
"BitSize": 64
},
"CustomType": ""
},
"Body": [
{
"Stmt": null,
"Val": {
"Expr": null,
"Struct": {
"Expr": null,
"Name": "this"
},
"Name": {
"Expr": null,
"Name": "dejv"
}
}
}
],
"Private": false
"Stmt": null,
"Expr": {
"EConst": null,
"Value": "bek"
}
}
]
},
{
"Stmt": null,
"Name": "so",
"Typ": {
"Typ": null,
"CustomType": "So"
},
"Expr": {
"Expr": null,
"Name": "So",
"Args": null
}
},
{
"Stmt": null,
"InstanceName": "so",
"MethodName": "bek",
"Name": "a",
"Args": null
},
{
"Stmt": null,
"Expr": {
"Expr": null,
"InstanceName": "so",
"MethodName": "david",
"Args": null
}
}
]
19 changes: 3 additions & 16 deletions example.cffc
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
class So {
dejv:int;
So() {
this.dejv = 1;
}
func bek() {
this.dejv = this.dejv + 1;
}
func david(): int {
return this.dejv
}
func a() {
print "bek";
}

var so:So = new So();

so.bek();
print so.david();
a();
3 changes: 0 additions & 3 deletions src/compiler/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ func (ctx *Context) compileClassDeclaration(c *Class) {
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)
}
}
Expand Down Expand Up @@ -86,7 +84,6 @@ func (ctx *Context) compileMethodDeclaration(m Method, c *Class) *ir.Func {
func (ctx *Context) compileClassMethod(c SClassMethod) {
// Get the class instance
instance := ctx.lookupVariable(c.InstanceName)
fmt.Println("Instance:", instance)

var classType types.Type
for _, t := range ctx.Module.TypeDefs {
Expand Down
13 changes: 0 additions & 13 deletions src/compiler/compiler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package compiler

import (
"fmt"

"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
Expand Down Expand Up @@ -51,7 +49,6 @@ func (c Context) lookupVariable(name string) value.Value {
if c.Compiler.VarsCanBeNumbers {
return nil
}
fmt.Printf("variable: `%s`\n", name)
panic("no such variable")
}
}
Expand All @@ -73,21 +70,11 @@ func (c *Compiler) Compile() {
block := funcMain.NewBlock("entry")
c.Context = NewContext(block, c)
for _, stmt := range c.AST {
fmt.Printf("%T: %v\n", stmt, stmt)
c.Context.compileStmt(stmt)
}
if c.Context.Block.Term == nil {
c.Context.Block.NewRet(constant.NewInt(types.I32, 0))
}
fmt.Println("Symbol table:")
for k, v := range c.SymbolTable {
fmt.Printf("%s: %v\n", k, v)
}

fmt.Println("Vars:")
for k, v := range c.Context.vars {
fmt.Printf("%s: %v\n", k, v)
}
}

func (c *Compiler) defineBuiltinFunctions() {
Expand Down
24 changes: 18 additions & 6 deletions src/compiler/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (c *Context) compileFunctionCallExpr(e ECall) value.Value {

func (c *Context) compileFunctionDecl(s SFuncDecl) {
// Create a temporary context and block for analysis
tmpBlock := c.Module.NewFunc("tmp", types.Void)
tmpCtx := c.NewContext(tmpBlock.NewBlock("entry"))
tmpBlock := c.Module.NewFunc("", types.Void)
tmpCtx := c.NewContext(tmpBlock.NewBlock("tmp-entry"))

var argsUsed []string
for _, arg := range s.Args {
Expand All @@ -75,20 +75,32 @@ func (c *Context) compileFunctionDecl(s SFuncDecl) {
continue
}
}
fmt.Println("Used var:", name)
c.usedVars[name] = true
value := tmpCtx.lookupVariable(name)
s.Args = append(s.Args, &CParam{Typ: CType{Typ: value.Type()}})
}
if tmpCtx.Term == nil {
if c.toType(s.ReturnType).Equal(types.Void) {
tmpCtx.NewRet(nil)
} else {
panic(fmt.Errorf("function `%s` does not return a value", s.Name))
}
}

// Remove the temporary function from the module
c.Module.Funcs = c.Module.Funcs[:len(c.Module.Funcs)-1]
funcs := []*ir.Func{}
for _, f := range c.Module.Funcs {
if f.Name() != tmpBlock.Name() {
funcs = append(funcs, f)
}
}
c.Module.Funcs = funcs

f := c.Module.NewFunc(s.Name, c.toType(s.ReturnType), c.toParams(s.Args)...)
f.Sig.Variadic = false
f.Sig.RetType = c.toType(s.ReturnType)
block := f.NewBlock("entry")
ctx := c.NewContext(block)
block := f.NewBlock("function-entry")
ctx := NewContext(block, c.Compiler)
for _, stmt := range s.Body {
ctx.compileStmt(stmt)
}
Expand Down
5 changes: 4 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ go 1.21.3
require github.com/llir/llvm v0.3.6

require (
github.com/fatih/color v1.16.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mewmew/float v0.0.0-20201204173432-505706aa38fa // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/tools v0.1.4 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
11 changes: 11 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -9,6 +11,11 @@ github.com/llir/ll v0.0.0-20220802044011-65001c0fb73c h1:UwtWiaR7Zg/IItv2hEN1EAT
github.com/llir/ll v0.0.0-20220802044011-65001c0fb73c/go.mod h1:2F+W9dmrXLYy3UZXnii5UM7QDRiVsz4QkMpC0vaBU7M=
github.com/llir/llvm v0.3.6 h1:Zh9vd8EOMDgwRAg43+VkOwnXISXIPyTzoNH89LLX5eM=
github.com/llir/llvm v0.3.6/go.mod h1:2vIck7uj3cIuZqx5cLXxB9lD6bT2JtgXcMD0u3WbfOo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mewmew/float v0.0.0-20201204173432-505706aa38fa h1:R27wrYHe8Zik4z/EV8xxfoH3cwMJw3qI4xsI3yYkGDQ=
github.com/mewmew/float v0.0.0-20201204173432-505706aa38fa/go.mod h1:O+xb+8ycBNHzJicFVs7GRWtruD4tVZI0huVnw5TM01E=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -28,8 +35,12 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
2 changes: 0 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,4 @@ func main() {
os.Remove(tmpDir + "/sleep.c")
os.Remove(tmpDir)
}

fmt.Println("Done!")
}
5 changes: 2 additions & 3 deletions src/parser/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"fmt"

"github.com/fatih/color"
"github.com/llir/llvm/ir/types"
"github.com/vyPal/CaffeineC/compiler"
)
Expand Down Expand Up @@ -69,7 +70,6 @@ func (p *Parser) parseClassConstructor() compiler.Method {
}
p.Pos++ // ")"
p.Pos++
fmt.Println("Start of function", name)
var body []compiler.Stmt
for p.Tokens[p.Pos].Type != "PUNCT" || p.Tokens[p.Pos].Value != "}" {
token := p.Tokens[p.Pos]
Expand Down Expand Up @@ -103,11 +103,10 @@ func (p *Parser) parseClassConstructor() compiler.Method {
body = append(body, p.parseAssignment())
}
default:
fmt.Println("[W]", token.Location, "Unexpected token:", token.Value)
color.Yellow(token.Location.String(), "Unexpected token:", token.Value)
p.Pos++
}
}
fmt.Println("End of function", name)
p.Pos++ // "}"
return compiler.Method{Name: name, Params: params, ReturnType: &compiler.CType{Typ: types.Void}, Body: body, Private: false}
}
Expand Down
Loading

0 comments on commit 07648ec

Please sign in to comment.