-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode.go
58 lines (48 loc) · 1.35 KB
/
bytecode.go
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
// Copyright (c) 2020-2023 Ozan Hacıbekiroğlu.
// Use of this source code is governed by a MIT License
// that can be found in the LICENSE file.
package charlang
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/topxeq/charlang/parser"
)
// Bytecode holds the compiled functions and constants.
type Bytecode struct {
FileSet *parser.SourceFileSet
Main *CompiledFunction
Constants []Object
NumModules int
CompilerOptionsM *CompilerOptions
}
// Fprint writes constants and instructions to given Writer in a human readable form.
func (bc *Bytecode) Fprint(w io.Writer) {
_, _ = fmt.Fprintln(w, "Bytecode")
_, _ = fmt.Fprintf(w, "Modules:%d\n", bc.NumModules)
bc.putConstants(w)
bc.Main.Fprint(w)
}
func (bc *Bytecode) String() string {
var buf bytes.Buffer
bc.Fprint(&buf)
return buf.String()
}
func (bc *Bytecode) putConstants(w io.Writer) {
_, _ = fmt.Fprintf(w, "Constants:\n")
for i := range bc.Constants {
if cf, ok := bc.Constants[i].(*CompiledFunction); ok {
_, _ = fmt.Fprintf(w, "%4d: CompiledFunction\n", i)
var b bytes.Buffer
cf.Fprint(&b)
_, _ = fmt.Fprint(w, "\t")
str := b.String()
c := strings.Count(str, "\n")
_, _ = fmt.Fprint(w, strings.Replace(str, "\n", "\n\t", c-1))
continue
}
_, _ = fmt.Fprintf(w, "%4d: %#v|%s\n",
i, bc.Constants[i], bc.Constants[i].TypeName())
}
}