From 3abe18ffca484efc3553aa4ec9cb677eb19cdaf4 Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 18 May 2022 01:37:09 +0200 Subject: Create compiler for Lang bytecode assembly --- pkg/lang/vm/text/op.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 pkg/lang/vm/text/op.go (limited to 'pkg/lang/vm/text/op.go') 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 +} -- cgit 1.4.1