Skip to content

Both left and right identifiers question #8

@asad-awadia

Description

@asad-awadia

Trying to understand how https://github.com/alexkappa/exp/blob/master/exp.go#L143= works. If both left and right are identifiers such as foo > bar then won't k get overwritten [right will overwrite left] and v be 0?

How will the match work then?

Given a tree like https://github.com/alexkappa/exp/blob/master/parse/parse_test.go#L16= - how would the GreaterThan work?

Example tree

			&tree{
				value: token{Type: T_IS_GREATER, Value: ">"},
				left: &tree{
					value: token{Type: T_IDENTIFIER, Value: "foo"},
				},
				right: &tree{
					value: token{Type: T_IDENTIFIER, Value: "bar"},
				},
			}

Code

	case parse.T_IS_GREATER:
		var (
			k string
			v float64
			l = t.Left()
			r = t.Right()
		)
		switch l.Value().Type {
		case parse.T_IDENTIFIER:
			k = l.Value().Value
		case parse.T_NUMBER:
			var err error
			v, err = strconv.ParseFloat(l.Value().Value, 10)
			if err != nil {
				return nil, err
			}
		}
		switch r.Value().Type {
		case parse.T_IDENTIFIER:
			k = r.Value().Value
		case parse.T_NUMBER:
			var err error
			v, err = strconv.ParseFloat(r.Value().Value, 10)
			if err != nil {
				return nil, err
			}
		}
		return GreaterThan(k, v), nil

@alexkappa

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions