about summary refs log tree commit diff
path: root/pkg/lang/compiler
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-06-02 01:09:33 +0000
committerGitHub <noreply@github.com>2022-06-02 01:09:33 +0000
commite57216d3b29ba9688918972c683da3ab0da3ad95 (patch)
treec78634bb2165bb2149848b86e0a5c411318fef2b /pkg/lang/compiler
parent87d115da565618b2f2f1cbbdcca883bbc0c57e60 (diff)
downloadjinx-e57216d3b29ba9688918972c683da3ab0da3ad95.tar.zst
jinx-e57216d3b29ba9688918972c683da3ab0da3ad95.zip
Create Lang compiler package
Diffstat (limited to 'pkg/lang/compiler')
-rw-r--r--pkg/lang/compiler/compiler.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
new file mode 100644
index 0000000..bf34487
--- /dev/null
+++ b/pkg/lang/compiler/compiler.go
@@ -0,0 +1,41 @@
+package compiler
+
+import (
+	"jinx/pkg/lang/ast"
+	"jinx/pkg/lang/vm/code"
+)
+
+type Compiler struct {
+	ast ast.Program
+}
+
+func New(ast ast.Program) *Compiler {
+	return &Compiler{
+		ast: ast,
+	}
+}
+
+func (comp *Compiler) Compile() (code.Code, error) {
+	bc := make([]byte, 0, 1024)
+
+	for _, stmt := range comp.ast.Stmts {
+		if stmt.Kind != ast.StmtKindExpr {
+			panic("statements other than expressions not implemented")
+		}
+
+		expr := stmt.Value.(ast.StmtExpr).Value
+
+		res, err := comp.compileExpr(expr)
+		if err != nil {
+			return code.Code{}, err
+		}
+
+		bc = append(bc, res...)
+	}
+
+	return code.New(bc, code.NewDebugInfo("unknown file")), nil
+}
+
+func (comp *Compiler) compileExpr(expr ast.Expr) ([]byte, error) {
+	panic("not implemented")
+}