aboutsummaryrefslogtreecommitdiff
path: root/cmd/vm
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-06 01:52:04 +0200
committerMel <einebeere@gmail.com>2022-07-06 14:05:28 +0200
commit7ab4e6290d7131e4a401d36d8480b3c2fec9ae41 (patch)
tree380351f1dd080d4cfe2e6ecc9c5ce25a7cd20ed5 /cmd/vm
parent5d00068f228322a95895e7f92dd48e6cbb206832 (diff)
downloadjinx-7ab4e6290d7131e4a401d36d8480b3c2fec9ae41.tar.zst
jinx-7ab4e6290d7131e4a401d36d8480b3c2fec9ae41.zip
Add lang binary and decompiler to VM
Diffstat (limited to 'cmd/vm')
-rw-r--r--cmd/vm/main.go26
1 files changed, 23 insertions, 3 deletions
diff --git a/cmd/vm/main.go b/cmd/vm/main.go
index 19ba160..013899a 100644
--- a/cmd/vm/main.go
+++ b/cmd/vm/main.go
@@ -13,6 +13,7 @@ import (
func main() {
// Take the first argument as the path to the file to be parsed.
compile := flag.String("c", "", "compile to file (if not bytecode)")
+ decompile := flag.String("d", "", "decompile bytecode to file")
run := flag.Bool("r", false, "run")
bytecode := flag.Bool("b", false, "interpret bytecode, without this flag the program will be interpreted in Lang VM text format.")
@@ -22,12 +23,16 @@ func main() {
exit("-c and -b are mutually exclusive")
}
+ if *compile != "" && *decompile != "" {
+ exit("-c and -d are mutually exclusive")
+ }
+
if *bytecode && !*run {
exit("-b requires -r")
}
- if *compile == "" && !*run {
- exit("nothing to do, either -c or -r is required")
+ if *compile == "" && *decompile == "" && !*run {
+ exit("nothing to do, either -c, -d or -r is required")
}
path := flag.Arg(0)
@@ -42,7 +47,7 @@ func main() {
defer file.Close()
var bc code.Code
- if !*bytecode {
+ if !*bytecode && *decompile == "" {
comp := text.NewCompiler(file)
bc, err = comp.Compile()
if err != nil {
@@ -67,6 +72,21 @@ func main() {
}
bc = code.New(content, code.NewDebugInfo(path))
+
+ if *decompile != "" {
+ decomp := text.NewDecompiler(bc)
+ result := decomp.Decompile()
+
+ output, err := os.Create(*decompile)
+ if err != nil {
+ exit("could not create file: %v", err)
+ }
+ defer output.Close()
+
+ if _, err := output.Write([]byte(result)); err != nil {
+ exit("could not write to file: %v", err)
+ }
+ }
}
if *run {