umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs | README | LICENSE

opcode.go (967B)


      1 package asm
      2 
      3 import (
      4 	"log"
      5 )
      6 
      7 //go:generate go run golang.org/x/tools/cmd/stringer@latest -type=Opcode
      8 type Opcode uint8
      9 
     10 const (
     11 	CMOV Opcode = iota
     12 	ARRI
     13 	ARRA
     14 	ADD
     15 	MUL
     16 	DIV
     17 	NOTA
     18 	HALT
     19 	ALLO
     20 	ABAN
     21 	OUTP
     22 	INP
     23 	LOAD
     24 	ORTH
     25 )
     26 
     27 func NewOpcode(input string) Opcode {
     28 	switch {
     29 	case input == CMOV.String():
     30 		return CMOV
     31 	case input == ARRI.String():
     32 		return ARRI
     33 	case input == ARRA.String():
     34 		return ARRA
     35 	case input == ADD.String():
     36 		return ADD
     37 	case input == MUL.String():
     38 		return MUL
     39 	case input == DIV.String():
     40 		return DIV
     41 	case input == NOTA.String():
     42 		return NOTA
     43 	case input == HALT.String():
     44 		return HALT
     45 	case input == ALLO.String():
     46 		return ALLO
     47 	case input == ABAN.String():
     48 		return ABAN
     49 	case input == OUTP.String():
     50 		return OUTP
     51 	case input == INP.String():
     52 		return INP
     53 	case input == LOAD.String():
     54 		return LOAD
     55 	case input == ORTH.String():
     56 		return ORTH
     57 	default:
     58 		log.Fatalf("invalid opcode %s", input)
     59 	}
     60 	return CMOV // thanks Go
     61 }