Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "regexp"
- "strconv"
- )
- func readArg(scanner *bufio.Scanner, regex *regexp.Regexp) string {
- (*scanner).Scan()
- argument := scanner.Text()
- matches := regex.FindAllStringIndex(argument, -1)
- return strconv.Itoa(len(matches))
- }
- func main() {
- args := os.Args[1:2]
- file, err := os.Open(args[0])
- if err != nil {
- fmt.Println("Invalid file supplied")
- os.Exit(1)
- }
- defer file.Close()
- regex := regexp.MustCompile("mors")
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- opcode := scanner.Text()
- matches := regex.FindAllStringIndex(opcode, -1)
- output := ""
- switch len(matches) {
- case 0:
- output += "exit"
- case 1:
- output += "add "
- output += readArg(scanner, regex)
- case 2:
- output += "sub "
- output += readArg(scanner, regex)
- case 3:
- output += "sel "
- output += readArg(scanner, regex)
- case 4:
- output += "mov "
- output += readArg(scanner, regex)
- case 5:
- output += "prnt"
- case 6:
- output += "get"
- case 7:
- output += "goto "
- output += readArg(scanner, regex)
- case 8:
- output += "if"
- case 9:
- output += "mif"
- case 10:
- output += "lif"
- case 11:
- output += "eif"
- case 12:
- output += "sh"
- }
- fmt.Println(output)
- }
- }
Add Comment
Please, Sign In to add comment