Exemplo n.º 1
0
func parseDefine(s string, def map[string]string) (string, error) {
	noquotes := exstrings.Stripquotes(s)
	regdef := regexp.MustCompile("(![a-zA-Z0-9]+) (=|equ) (.*)")
	regnum := regexp.MustCompile("([a-zA-Z0-9]{2})[ ]*\\$([a-zA-Z0-9]{2})")
	m := regdef.FindAllStringSubmatch(noquotes, -1)

	if m != nil {
		v := m[0]
		if def[v[1]] != "" {
			return "", fmt.Errorf("%s redefined", v[1])
		}
		if strings.Contains(v[3], "!") {
			split := exstrings.Split(v[3], " $#,")
			for _, str := range split {
				if strings.Contains(str, "!") {
					str = str[strings.Index(str, "!"):]

					if def[str] == "" {
						return "", fmt.Errorf("%s not defined before use.", str)
					}
					//	Replace the !define with its value
					v[3] = strings.Replace(v[3], str, def[str], -1)

					//	Replace any $xx$xx with $xxxx
					v[3] = regnum.ReplaceAllString(v[3], "$1$2")
				}
			}
		}
		def[v[1]] = v[3]
		return "", nil
	} else {
		regsym := regexp.MustCompile("(![a-zA-Z0-9]+)")
		matches := regsym.FindAllStringSubmatch(s, -1)
		submatches := make([]string, len(matches))

		for k, v := range matches {
			submatches[k] = v[1]
		}

		sortDescending(submatches)

		for _, v := range submatches {
			s = strings.Replace(s, v, def[v], -1)
		}
		s_split := strings.Split(s, " ")
		s = s_split[0] + " " + regnum.ReplaceAllString(strings.Join(s_split[1:], " "), "$1$2")
	}
	return s, nil
}
Exemplo n.º 2
0
func countBytes(str string) int {
	if str == "" {
		return 0
	}
	spl := strings.Split(str, " ")
	if spl[0] == "JSR" || spl[0] == "JMP" {
		if spl[1][0:1] != "$" {
			return 3
		}
	} else if len(spl) == 2 && isBranch(spl[0]) && len(spl[1]) > 5 && spl[1][0:6] == ".LABEL" {
		if spl[0] == "BRL" {
			return 3
		} else {
			return 2
		}
	} else if spl[0] == "BRK" && len(spl) > 2 {
		return strings.Count(str, "BRK") * 2
	}
	regnum, _ := regexp.Compile("[0-9a-fA-F]{2}")
	str = regnum.ReplaceAllString(strings.Join(exstrings.Split(str, " ,")[1:], " "), "x")
	return 1 + strings.Count(str, "x")
}