about summary refs log tree commit diff
path: root/pkg/lang/vm/text/op.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm/text/op.go')
-rw-r--r--pkg/lang/vm/text/op.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/pkg/lang/vm/text/op.go b/pkg/lang/vm/text/op.go
new file mode 100644
index 0000000..a8f3663
--- /dev/null
+++ b/pkg/lang/vm/text/op.go
@@ -0,0 +1,56 @@
+package text
+
+import "jinx/pkg/lang/vm/code"
+
+var (
+	opToString = map[code.Op]string{
+		code.OpNop:          "nop",
+		code.OpHalt:         "halt",
+		code.OpPushInt:      "push_int",
+		code.OpPushFloat:    "push_float",
+		code.OpPushString:   "push_string",
+		code.OpPushTrue:     "push_true",
+		code.OpPushFalse:    "push_false",
+		code.OpPushNull:     "push_null",
+		code.OpPushArray:    "push_array",
+		code.OpPushFunction: "push_function",
+		code.OpPushObject:   "push_object",
+		code.OpGetGlobal:    "get_global",
+		code.OpGetLocal:     "get_local",
+		code.OpGetMember:    "get_member",
+		code.OpGetArg:       "get_arg",
+		code.OpGetEnv:       "get_env",
+		code.OpAdd:          "add",
+		code.OpSub:          "sub",
+		code.OpIndex:        "index",
+		code.OpCall:         "call",
+		code.OpJmp:          "jmp",
+		code.OpJez:          "jez",
+		code.OpRet:          "ret",
+	}
+	stringToOp = reverseMap(opToString)
+)
+
+func OpToString(op code.Op) string {
+	str, ok := opToString[op]
+	if !ok {
+		return "unknown"
+	}
+	return str
+}
+
+func StringToOp(str string) (code.Op, error) {
+	op, ok := stringToOp[str]
+	if !ok {
+		return 0, ErrUnknownOp{Op: str}
+	}
+	return op, nil
+}
+
+func reverseMap[K comparable, V comparable](m map[K]V) map[V]K {
+	r := make(map[V]K)
+	for k, v := range m {
+		r[v] = k
+	}
+	return r
+}