-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (95 loc) · 2.25 KB
/
main.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
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
package main
import (
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"github.com/PhakornKiong/go-vm/compiler"
"github.com/PhakornKiong/go-vm/vm"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "Input file containing bytecode instructions",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "Type of the return value (int64, uint64, string)",
},
},
Action: func(c *cli.Context) error {
inputFilePath := c.String("input")
if inputFilePath == "" {
return errors.New("input file is required")
}
fileContent, err := os.ReadFile(inputFilePath)
if err != nil {
return err
}
a := vm.NewVM()
compiler := compiler.NewCompiler()
compiler.Compile(string(fileContent))
a.LoadBytecode(compiler.Output())
res, err := a.Execute()
if err != nil {
return err
}
// Handle representation of the data type
retType := c.String("type")
switch retType {
case "int64":
var val int64
if len(res) > 8 {
return fmt.Errorf("invalid result length: %d, expected between 0 and 8", len(res))
}
if len(res) == 0 {
val = 0
} else {
padded := make([]byte, 8)
copy(padded[8-len(res):], res)
// Sign extension for negative numbers
// Check if MSB is 1 for negative number
if res[0]&0x80 == 0x80 {
for i := 0; i < 8-len(res); i++ {
padded[i] = 0xFF
}
}
val = int64(binary.BigEndian.Uint64(padded))
}
fmt.Println("Result in int64 format")
fmt.Println(val)
case "uint64":
var val uint64
if len(res) > 8 {
return fmt.Errorf("invalid result length: %d, expected between 0 and 8", len(res))
}
if len(res) == 0 {
val = 0
} else {
padded := make([]byte, 8)
copy(padded[8-len(res):], res)
val = binary.BigEndian.Uint64(padded)
}
fmt.Println("Result in uint64 format")
fmt.Println(val)
case "string":
fmt.Println("Result in string format")
fmt.Println(string(res))
default:
fmt.Println("Result in byte array format")
fmt.Println(res)
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}