Guest User

Untitled

a guest
May 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. )
  10.  
  11. func readArg(scanner *bufio.Scanner, regex *regexp.Regexp) string {
  12. (*scanner).Scan()
  13. argument := scanner.Text()
  14. matches := regex.FindAllStringIndex(argument, -1)
  15. return strconv.Itoa(len(matches))
  16. }
  17. func main() {
  18. args := os.Args[1:2]
  19. file, err := os.Open(args[0])
  20. if err != nil {
  21. fmt.Println("Invalid file supplied")
  22. os.Exit(1)
  23. }
  24. defer file.Close()
  25. regex := regexp.MustCompile("mors")
  26. scanner := bufio.NewScanner(file)
  27. for scanner.Scan() {
  28. opcode := scanner.Text()
  29. matches := regex.FindAllStringIndex(opcode, -1)
  30. output := ""
  31. switch len(matches) {
  32. case 0:
  33. output += "exit"
  34. case 1:
  35. output += "add "
  36. output += readArg(scanner, regex)
  37. case 2:
  38. output += "sub "
  39. output += readArg(scanner, regex)
  40. case 3:
  41. output += "sel "
  42. output += readArg(scanner, regex)
  43. case 4:
  44. output += "mov "
  45. output += readArg(scanner, regex)
  46. case 5:
  47. output += "prnt"
  48. case 6:
  49. output += "get"
  50. case 7:
  51. output += "goto "
  52. output += readArg(scanner, regex)
  53. case 8:
  54. output += "if"
  55. case 9:
  56. output += "mif"
  57. case 10:
  58. output += "lif"
  59. case 11:
  60. output += "eif"
  61. case 12:
  62. output += "sh"
  63. }
  64. fmt.Println(output)
  65. }
  66. }
Add Comment
Please, Sign In to add comment