about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler.go
blob: e299fdc5e0a06bfd283e16e31ef263be45c4cecc (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package compiler

import (
	"jinx/pkg/lang/ast"
	"jinx/pkg/lang/vm/code"
)

type Compiler struct {
	ast     ast.Program
	builder code.Builder
}

func New(ast ast.Program) *Compiler {
	return &Compiler{
		ast:     ast,
		builder: code.NewBuilder(),
	}
}

func (comp *Compiler) Compile() (code.Code, error) {
	for _, stmt := range comp.ast.Stmts {
		if stmt.Kind == ast.StmtKindEmpty {
			continue
		}

		if stmt.Kind != ast.StmtKindExpr {
			panic("statements other than expressions and empties not implemented")
		}

		expr := stmt.Value.(ast.StmtExpr).Value

		if err := comp.compileExpr(expr); err != nil {
			return code.Code{}, err
		}
	}

	return comp.builder.Build(), nil
}

func (comp *Compiler) compileExpr(expr ast.Expr) error {
	switch expr.Kind {
	case ast.ExprKindBinary:
		return comp.compileBinaryExpr(expr.Value.(ast.ExprBinary))
	case ast.ExprKindUnary:
		return comp.compileUnaryExpr(expr.Value.(ast.ExprUnary))
	case ast.ExprKindCall:
		return comp.compileCallExpr(expr.Value.(ast.ExprCall))
	case ast.ExprKindSubscription:
		return comp.compileSubscriptionExpr(expr.Value.(ast.ExprSubscription))

	case ast.ExprKindGroup:
		return comp.compileGroupExpr(expr.Value.(ast.ExprGroup))
	case ast.ExprKindFnLit:
		panic("not implemented")
	case ast.ExprKindArrayLit:
		panic("not implemented")
	case ast.ExprKindIdent:
		panic("not implemented")
	case ast.ExprKindIntLit:
		return comp.compileIntLitExpr(expr.Value.(ast.ExprIntLit))
	case ast.ExprKindFloatLit:
		return comp.compileFloatLitExpr(expr.Value.(ast.ExprFloatLit))
	case ast.ExprKindStringLit:
		return comp.compileStringLitExpr(expr.Value.(ast.ExprStringLit))
	case ast.ExprKindBoolLit:
		return comp.compileBoolLitExpr(expr.Value.(ast.ExprBoolLit))
	case ast.ExprKindNullLit:
		return comp.compileNullLitExpr(expr.Value.(ast.ExprNullLit))
	case ast.ExprKindThis:
		panic("not implemented")
	default:
		panic("unknown expression kind")
	}
}

func (comp *Compiler) compileBinaryExpr(expr ast.ExprBinary) error {
	if err := comp.compileExpr(expr.Right); err != nil {
		return err
	}

	if err := comp.compileExpr(expr.Left); err != nil {
		return err
	}

	switch expr.Op {
	case ast.BinOpPlus:
		comp.builder.AppendOp(code.OpAdd)
	case ast.BinOpMinus:
		comp.builder.AppendOp(code.OpSub)
	case ast.BinOpStar:
		// comp.builder.AppendOp(code.OpMul)
		panic("not implemented")
	case ast.BinOpSlash:
		// comp.builder.AppendOp(code.OpDiv)
		panic("not implemented")
	case ast.BinOpPercent:
		// comp.builder.AppendOp(code.OpMod)
		panic("not implemented")

	case ast.BinOpAssign:
		panic("not implemented")
	case ast.BinOpEq:
		// comp.builder.AppendOp(code.OpEq)
		panic("not implemented")
	case ast.BinOpNeq:
		// comp.builder.AppendOp(code.OpNeq)
		panic("not implemented")
	case ast.BinOpLt:
		// comp.builder.AppendOp(code.OpLt)
		panic("not implemented")
	case ast.BinOpLte:
		comp.builder.AppendOp(code.OpLte)
	case ast.BinOpGt:
		// comp.builder.AppendOp(code.OpGt)
		panic("not implemented")
	case ast.BinOpGte:
		// comp.builder.AppendOp(code.OpGte)
		panic("not implemented")
	default:
		panic("unknown binary operator")
	}

	return nil
}

func (comp *Compiler) compileUnaryExpr(expr ast.ExprUnary) error {
	if err := comp.compileExpr(expr.Value); err != nil {
		return err
	}

	switch expr.Op {
	case ast.UnOpBang:
		panic("not implemented")
	case ast.UnOpMinus:
		panic("not implemented")
	default:
		panic("unknown unary operator")
	}

	return nil
}

func (comp *Compiler) compileCallExpr(expr ast.ExprCall) error {
	for i := len(expr.Args) - 1; i >= 0; i-- {
		if err := comp.compileExpr(expr.Args[i]); err != nil {
			return err
		}
	}

	if err := comp.compileExpr(expr.Callee); err != nil {
		return err
	}

	comp.builder.AppendOp(code.OpCall)
	comp.builder.AppendInt(int64(len(expr.Args)))

	return nil
}

func (comp *Compiler) compileSubscriptionExpr(expr ast.ExprSubscription) error {
	if err := comp.compileExpr(expr.Key); err != nil {
		return err
	}

	if err := comp.compileExpr(expr.Obj); err != nil {
		return err
	}

	comp.builder.AppendOp(code.OpIndex)
	return nil
}

func (comp *Compiler) compileGroupExpr(expr ast.ExprGroup) error {
	return comp.compileExpr(expr.Value)
}

func (comp *Compiler) compileIntLitExpr(expr ast.ExprIntLit) error {
	comp.builder.AppendOp(code.OpPushInt)
	comp.builder.AppendInt(int64(expr.Value))
	return nil
}

func (comp *Compiler) compileFloatLitExpr(expr ast.ExprFloatLit) error {
	comp.builder.AppendOp(code.OpPushFloat)
	comp.builder.AppendFloat(expr.Value)
	return nil
}

func (comp *Compiler) compileStringLitExpr(expr ast.ExprStringLit) error {
	comp.builder.AppendOp(code.OpPushString)
	comp.builder.AppendString(expr.Value)
	return nil
}

func (comp *Compiler) compileBoolLitExpr(expr ast.ExprBoolLit) error {
	if expr.Value {
		comp.builder.AppendOp(code.OpPushTrue)
	} else {
		comp.builder.AppendOp(code.OpPushFalse)
	}
	return nil
}

func (comp *Compiler) compileNullLitExpr(expr ast.ExprNullLit) error {
	comp.builder.AppendOp(code.OpPushNull)
	return nil
}