about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler.go
blob: bf3448733037079ec6cd25dd81cf7c17cf4aecb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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")
}