Example #1
0
func getFuncNames(filecontent []string) ([]string, []string) {
	r := make([]string, 0)
	r2 := make([]string, 0)
	var funcCounter int64
	functionRx := regexp.MustCompile(FUNCTION_NAME_REGEX)
	functionDefRx := regexp.MustCompile(FUNCTION_DEFINITION_REGEX)
	for lineno, line := range filecontent {
		if functionRx.MatchString(line) {
			funcnameB := functionRx.FindSubmatch([]byte(line))
			funcname := string(funcnameB[1])
			funcCounter = funcCounter + 1
			fmt.Printf("%v %d ", funcCounter, lineno)
			color.Print("@g", funcname)
			color.Println("@y|", line)
			r = append(r, funcname)

			if functionDefRx.MatchString(line) {
				funcdefB := functionDefRx.FindSubmatch([]byte(line))
				funcdef := string(funcdefB[1])
				r2 = append(r2, funcdef)
			} else {
				log.Fatal("Can detect the funciton name but not the function definition! That is not acceptable")
			}
		}
	}
	return r, r2
}
Example #2
0
func main() {
	originalFile := "./OriginalPlotClusters.m"
	className := "PC"

	b, err := ioutil.ReadFile(originalFile)
	if err != nil {
		log.Fatal("Cant read the original,", err)
	}

	lines := strings.Split(string(b), "\n")

	funcnames, funcdefs := getFuncNames(lines)
	//revese the funcnames, to reduce the change of collision, like a funcname being embded inside another function name
	funcnames = reversSortStringSlice(funcnames)

	//TODO: in all the function declaration lines, replace `=([A-z])` with '= \1'
	functionRx := regexp.MustCompile(FUNCTION_NAME_REGEX)

	var funcCounter int64
	//insidefunc := false
	//TODO: the assuption is the original file STARTS with a function declaration line, no comments of white space before it
	funclines := make([]string, 0)
	funcname := ""
	lineno := 0
	line := ""
	for lineno, line = range lines {
		if functionRx.MatchString(line) {
			if len(funclines) != 0 {
				patchedfunclines := renameFuncs(className, funclines, funcnames)
				color.Println("@rWriting: ", funcname)
				err = ioutil.WriteFile(funcname+".m", []byte(strings.Join(patchedfunclines, "")), os.ModePerm)
				if err != nil {
					log.Fatal("Error writing the file", err)
				}
				funclines = make([]string, 0)
			}
			funcnameB := functionRx.FindSubmatch([]byte(line))
			funcname = string(funcnameB[1])
			funcCounter = funcCounter + 1
			fmt.Printf("%v %d ", funcCounter, lineno)
			color.Print("@g", funcname)
			color.Println("@y|", line)
		}
		funclines = append(funclines, line)
	}
	//write the last function
	if len(funclines) != 0 {
		patchedfunclines := renameFuncs(className, funclines, funcnames)
		color.Println("@rWriting: ", funcname)
		err = ioutil.WriteFile(funcname+".m", []byte(strings.Join(patchedfunclines, "")), os.ModePerm)
		if err != nil {
			log.Fatal("Error writing the file", err)
		}
		funclines = make([]string, 0)
	}

	//TODO: do the main class def file
	classdeflines := ""
	classdeflines = classdeflines + "classdef " + className + "\r"
	classdeflines = classdeflines + `properties
        Version = '1'
    end
    methods (Static)        
`
	classdeflines = classdeflines + strings.Join(funcdefs, "\r")
	classdeflines = classdeflines + `

    end
end
`
	err = ioutil.WriteFile(className+".m", []byte(classdeflines), os.ModePerm)
	if err != nil {
		log.Fatal("Error writing the Classdefninition file", err)
	}
}