Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit af5c952

Browse files
committed
feat: allow compilation to executable file
1 parent 2df4737 commit af5c952

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

compiler/compiler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"encoding/gob"
45
"github.com/looplanguage/compiler/code"
56
"github.com/looplanguage/loop/models/object"
67
)
@@ -179,3 +180,9 @@ type Bytecode struct {
179180
Instructions code.Instructions
180181
Constants []object.Object
181182
}
183+
184+
func RegisterGobTypes() {
185+
gob.Register(&object.String{})
186+
gob.Register(&object.Integer{})
187+
gob.Register(&object.CompiledFunction{})
188+
}

main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ func main() {
4848
log.Fatalln(err)
4949
}
5050

51-
var compiled bytes2.Buffer
52-
enc := gob.NewEncoder(&compiled)
51+
compiler.RegisterGobTypes()
5352

54-
enc.Encode(comp.Bytecode())
53+
var constantBytes bytes2.Buffer
5554

56-
ioutil.WriteFile(filepath.Dir(os.Args[1])+"/"+fileNameWithoutExtension(filepath.Base(os.Args[1]))+".lpx", compiled.Bytes(), 0644)
55+
enc := gob.NewEncoder(&constantBytes)
56+
err = enc.Encode(comp.Bytecode())
57+
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
62+
ioutil.WriteFile(filepath.Dir(os.Args[1])+"/"+fileNameWithoutExtension(filepath.Base(os.Args[1]))+".lpx", constantBytes.Bytes(), 0644)
5763
}
5864

5965
func fileNameWithoutExtension(fileName string) string {

0 commit comments

Comments
 (0)