Exemplo n.º 1
0
Arquivo: in.go Projeto: pschlump/lexie
func (Im *ImType) OutputDef() {
	fmt.Printf("Defs - OutputDef\n")
	for _, DefType := range []string{"Machines", "Errors", "ReservedWords", "Tokens"} {
		dd := Im.Def.DefsAre[DefType]
		fmt.Printf("==========================================================================\n")
		fmt.Printf("DefType: %s\n", DefType)
		fmt.Printf("==========================================================================\n")

		ss := com.SortMapStringString(dd.NameValueStr)
		// for AKey, ADef := range dd.NameValueStr {
		for _, AKey := range ss {
			ADef := dd.NameValueStr[AKey]
			fmt.Printf("    %s=%v\n", AKey, ADef)
		}
	}
}
Exemplo n.º 2
0
Arquivo: in.go Projeto: pschlump/lexie
// -----------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------
func (Im *ImType) FinializeFile() {
	ADef := ""
	AKey := ""

	// for DefType, dd := range Im.Def.DefsAre {
	for _, DefType := range []string{"Machines", "Errors", "Tokens", "ReservedWords"} {
		dd := Im.Def.DefsAre[DefType]
		// fmt.Printf("DefType (FinializeFiile): %s\n", DefType)
		// Pass 1 - Take Numbers and put in
		ss := com.SortMapStringString(dd.NameValueStr)
		// for AKey, ADef := range dd.NameValueStr {
		for _, AKey = range ss {
			ADef = dd.NameValueStr[AKey]
			if len(ADef) > 0 && IsNumeric(ADef) {
				// fmt.Printf("Found numeric for %s=%s\n", AKey, ADef)
				v, err := strconv.Atoi(ADef)
				if err != nil {
					fmt.Printf("Error: Invalid numeric value for a token, >%s<, error=%s\n", ADef, err)
				} else {
					dd.NameValue[AKey] = v
					dd.Reverse[v] = AKey
				}
			}
		}
		// Pass 2 - Assign All Others
		seq := dd.Seq
		// for AKey, ADef := range dd.NameValueStr {
		for _, AKey = range ss {
			ADef = dd.NameValueStr[AKey]
			if len(ADef) > 0 && IsNumeric(ADef) {
			} else if len(ADef) == 0 {
				// fmt.Printf("Found sequence assign for for %s=%s\n", AKey, ADef)
				for _, ok := dd.Reverse[seq]; ok; {
					seq++
					_, ok = dd.Reverse[seq]
				}
				dd.NameValue[AKey] = seq
				dd.Reverse[seq] = AKey
				dd.NameValueStr[AKey] = fmt.Sprintf("%d", seq)
				// fmt.Printf("     Assigned %d\n", seq)
				seq++
			}
		}
		dd.Seq = seq

		// fmt.Printf("dd.NameValue = %v\n", dd.NameValue)

		// Pass 3 - Assign Tokens
		// for AKey, ADef := range dd.NameValueStr {
		for _, AKey = range ss {
			ADef = dd.NameValueStr[AKey]
			if len(ADef) > 0 && IsNumeric(ADef) {
			} else if len(ADef) > 0 {
				// fmt.Printf("Found Name Assign for for %s=%s\n", AKey, ADef)
				if v, ok := dd.NameValue[ADef]; ok {
					dd.NameValue[AKey] = v
					dd.Reverse[v] = AKey
				}
			}
		}
		// Pass 4 - Look for any unsigned
		// for AKey, ADef := range dd.NameValueStr {
		for _, AKey = range ss {
			ADef = dd.NameValueStr[AKey]
			if len(ADef) > 0 && IsNumeric(ADef) {
			} else if len(ADef) > 0 {
				if v, ok := dd.NameValue[ADef]; ok {
					dd.NameValue[AKey] = v
					dd.Reverse[v] = AKey
				} else {
					v := Im.FindValueFor(ADef)
					if v < 0 {
						fmt.Printf("Warning: Token is not defined, Automatically defining!, ADef/AKey %s=%s=%d\n", ADef, AKey, seq) // !! !! Requries a 4th pass - after all defined !! !!
						dd.NameValue[AKey] = seq
						dd.Reverse[seq] = AKey
						seq++
					} else {
						dd.NameValue[AKey] = v
						dd.Reverse[v] = AKey
					}
				}
			}
		}
		Im.Def.DefsAre[DefType] = dd
	}

	for _, vv := range Im.Machine {
		for _, ww := range vv.Rules {
			if len(ww.RvName) > 0 {
				// fmt.Printf("%-20s", fmt.Sprintf(" Rv:%d=%s ", ww.Rv, ww.RvName))
				ww.Rv = Im.Lookup("Tokens", ww.RvName)
			}
			if len(ww.CallName) > 0 {
				// fmt.Printf("%-20s", fmt.Sprintf(" Call:%d=%s ", ww.Call, ww.CallName))
				ww.Call = Im.Lookup("Machines", ww.CallName)
			}
		}
	}

	// xyzzy-Machine--Mixins---
	for kk, vv := range Im.Machine {
		var tRules []*ImRuleType
		tRules = make([]*ImRuleType, 0, 100)
		for _, rr := range vv.Rules {
			p := Im.LocatePattern(rr, tRules) // A merge operation - if not found then append, else replace
			if p >= 0 {
				tRules[p] = rr
			} else {
				tRules = append(tRules, rr)
			}
		}
		for _, ww := range vv.Mixins {
			ii := Im.Lookup("Machines", ww)
			if ii >= 0 && ii < len(Im.Machine) {
				for _, uu := range Im.Machine[ii].Rules {
					p := Im.LocatePattern(uu, tRules) // A merge operation - if not found then append, else replace
					if p < 0 {
						tRules = append(tRules, uu)
					}
				}
			} else {
				fmt.Printf("Error: Mixin - did not find %s as a machine name\n", ww)
			}
		}
		Im.Machine[kk].Rules = tRules
	}

	dd := Im.Def.DefsAre["Tokens"]
	Tok_map = dd.Reverse
}