Example #1
0
func eval(node *parser.Node) string {
	switch node.Name {
	case "Env":
		name := eval(node.Children[0])
		if ret := os.Getenv(name); ret == "" {
			return name + "_NOT_SET"
		} else {
			return ret
		}
	case "Folder":
		return filepath.Dir(eval(node.Children[0]))
	case "String", "NonOperation", "RecNonOp":
		return node.Data()
	case "Home":
		if u, err := user.Current(); err != nil {
			log4go.Warn("Couldn't lookup current user: %s", err)
		} else {
			return u.HomeDir
		}
	case "EXPAND_PATH", "RecOp":
		buf := bytes.NewBuffer(nil)
		for _, c := range node.Children {
			buf.WriteString(eval(c))
		}
		return buf.String()
	}
	return ""
}
Example #2
0
func convert(node *parser.Node) string {
	ret := ""
	switch node.Name {
	case "MethodDescriptor":
		ri := len(node.Children) - 1
		ret += convert(node.Children[ri]) + " ("
		for i := range node.Children[:ri] {
			if i > 0 {
				ret += ", "
			}
			ret += convert(node.Children[i])
		}
		ret += ")"
		return ret
	case "BaseType":
		fallthrough
	case "VoidDescriptor":
		return lut[node.Data()]
	case "Classname":
		return strings.Replace(node.Data(), "/", ".", -1)
	}
	for i := range node.Children {
		if ret != "" {
			ret += " "
		}
		ret += convert(node.Children[i])
	}
	if node.Name == "ArrayType" {
		ret += "[]"
	}
	return ret
}
Example #3
0
func data(n *parser.Node) string {
	switch n.Name {
	default:
		return n.Data()
	case "InheritedName":
		return data(n.Children[1])
	case "OptionalArgument":
		return data(n.Children[0])
	}
}
Example #4
0
func (c *Net) variable(n *parser.Node) string {
	switch n.Name {
	case "Access":
	default:
		if len(n.Children) == 0 {
			return n.Data()
		}
		for _, child := range n.Children {
			if v := c.variable(child); v != "" {
				return v
			}
		}
	}
	return ""
}
Example #5
0
func ToContentType(node *parser.Node) (ret content.Type) {
	if node.Name == "ArrayType" {
		ret.Flags |= content.FLAG_TYPE_ARRAY
		ret.Specialization = append(ret.Specialization, ToContentType(node.Children[0]))
		return
	}
	switch node.Name {
	case "BaseType":
		fallthrough
	case "VoidDescriptor":
		ret.Name.Relative = lut[node.Data()]
		ret.Name.Absolute = ret.Name.Relative
	case "Classname":
		ret.Name = ToContentFQN(node.Data())
	default:
		return ToContentType(node.Children[0])
	}

	return
}
Example #6
0
func typeresolve(td *TypeDef, node *parser.Node) (*content.Type, error) {
	switch n := node.Name; n {
	case "MethodCall":
		if methods, err := td.Methods(); err != nil {
			return nil, err
		} else {
			for _, method := range methods {
				if method.Name.Relative == node.Children[0].Data() {
					return &method.Returns[0].Type, nil
				}
			}
		}
	case "Identifier":
		if fields, err := td.Fields(); err != nil {
			return nil, err
		} else {
			for _, field := range fields {
				if field.Name.Relative == node.Data() {
					return &field.Type, nil
				}
			}
		}

		// Is it a Property then perhaps?
		name := "get_" + node.Data()
		if methods, err := td.Methods(); err != nil {
			return nil, err
		} else {
			for _, method := range methods {
				if method.Name.Relative == name {
					return &method.Returns[0].Type, nil
				}
			}
		}
		// TODO: could also be an inner class
	}

	// Found nothing.
	// TODO: Try parents
	return nil, fmt.Errorf("No such type found: %s, %s", td.Name(), node)
}
Example #7
0
func Eval(v *reflect.Value, node *parser.Node) (int, error) {
	switch node.Name {
	case "EXPRESSION":
		if l := len(node.Children); l != 2 {
			return 0, fmt.Errorf("Unexpected child length: %d, %s", l, node)
		}
		return Eval(v, node.Children[0])
	case "DotIdentifier":
		curr := v.Type().Name()
		children := node.Children
		if len(children) > 0 {
			// The last one will be handled by the fallthrough instead
			children = node.Children[:len(node.Children)-1]
		}
		for _, child := range children {
			f := v.FieldByName(child.Data())
			if !f.IsValid() {
				return 0, fmt.Errorf("No field by name %s in struct %s", node.Data(), curr)
			}
			v = &f
		}
		node = node.Children[len(node.Children)-1]
		fallthrough
	case "Identifier":
		if f := v.FieldByName(node.Data()); !f.IsValid() {
			return 0, fmt.Errorf("No field by name %s in struct %s", node.Data(), v)
		} else {
			switch f.Kind() {
			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				return int(f.Uint()), nil
			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				return int(f.Int()), nil
			case reflect.Bool:
				if f.Bool() {
					return 1, nil
				} else {
					return 0, nil
				}
			default:
				return 0, fmt.Errorf("Unexpected identifier kind: %v %v", f, f.Kind())
			}
		}
	case "Constant":
		i, err := strconv.ParseInt(node.Data(), 0, 32)
		return int(i), err
	case "EndOfFile":
		return 0, nil
	default:
		if l := len(node.Children); l != 2 {
			return 0, fmt.Errorf("Unexpected child length: %d, %s", l, node)
		}
		if a, err := Eval(v, node.Children[0]); err != nil {
			return 0, err
		} else if b, err := Eval(v, node.Children[1]); err != nil {
			return 0, err
		} else {
			switch node.Name {
			case "Ne":
				if a != b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Eq":
				if a == b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Lt":
				if a < b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Gt":
				if a > b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Le":
				if a <= b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Ge":
				if a >= b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Add":
				return a + b, nil
			case "Sub":
				return a - b, nil
			case "Mul":
				return a * b, nil
			case "ShiftLeft":
				return int(uint(a) << uint(b)), nil
			case "ShiftRight":
				return int(uint(a) >> uint(b)), nil
			case "Mask":
				return a & b, nil
			case "AndNot":
				return a &^ b, nil
			default:
				return 0, fmt.Errorf("Unimplemented operation: %s", node.Name)
			}
		}
	}
}
Example #8
0
func Eval(v *reflect.Value, node *parser.Node) (int, error) {
	switch node.Name {
	case "EXPRESSION":
		if l := len(node.Children); l != 2 {
			return 0, fmt.Errorf("Unexpected child length: %d, %s", l, node)
		}
		return Eval(v, node.Children[0])
	case "Identifier":
		if f := v.FieldByName(node.Data()); !f.IsValid() {
			return 0, fmt.Errorf("No field by name %s in struct %s", node.Data(), v)
		} else {
			switch f.Kind() {
			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				return int(f.Uint()), nil
			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				return int(f.Int()), nil
			default:
				return 0, fmt.Errorf("Unexpected identifier kind: %v %v", f, f.Kind())
			}
		}
	case "Constant":
		i, err := strconv.ParseInt(node.Data(), 0, 32)
		return int(i), err
	case "EndOfFile":
		return 0, nil
	default:
		if l := len(node.Children); l != 2 {
			return 0, fmt.Errorf("Unexpected child length: %d, %s", l, node)
		}
		if a, err := Eval(v, node.Children[0]); err != nil {
			return 0, err
		} else if b, err := Eval(v, node.Children[1]); err != nil {
			return 0, err
		} else {
			switch node.Name {
			case "Eq":
				if a == b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Lt":
				if a < b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Gt":
				if a > b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Le":
				if a <= b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Ge":
				if a >= b {
					return 1, nil
				} else {
					return 0, nil
				}
			case "Add":
				return a + b, nil
			case "Sub":
				return a - b, nil
			case "Mul":
				return a * b, nil
			case "ShiftLeft":
				return int(uint(a) << uint(b)), nil
			case "ShiftRight":
				return int(uint(a) >> uint(b)), nil
			case "Mask":
				return a & b, nil
			case "AndNot":
				return a &^ b, nil
			default:
				return 0, fmt.Errorf("Unimplemented operation: %s", node.Name)
			}
		}
	}
}