Beispiel #1
0
func ParseRpnValue(token string) RpnValue {
	isArg := strings.HasPrefix(token, "a:")
	if isArg {
		token = token[2:]
	}

	if parser.IsNumber(token) || (parser.ValidFunctionName(token) && isArg) {
		rpn := newPrimRpnValue(token)
		rpn.argument = isArg || parser.IsNumber(token)
		return rpn
	} else {
		var prec int
		if parser.ValidFunctionName(token) {
			prec = FuncCall
		} else {
			prec = opPrec(token)
		}
		return newOpRpnValue(token, prec)
	}
}
Beispiel #2
0
func toRpn(line string, outputQueue []RpnValue, opStack []RpnValue, fm FunctionMap) ([]RpnValue, error) {
	token, rest := parser.Tokenize(line)
	if parser.IsOperator(token) {
		return toRpnOperator(token, rest, outputQueue, opStack, fm)
	} else if token == "(" {
		return toRpn(rest, outputQueue, append(opStack, newParenRpnValue()), fm)
	} else if token == ")" {
		return toRpnRightParen(token, rest, outputQueue, opStack, fm)
	} else if _, ok := getFunction(token, fm); ok && !topOfStackIsFunc(opStack, fm) {
		return toRpn(rest, outputQueue, append(opStack, newOpRpnValue(token, FuncCall)), fm)
	} else if parser.IsPrimitive(token) || parser.ValidFunctionName(token) {
		return toRpn(rest, append(outputQueue, newPrimRpnValue(token)), opStack, fm)
	} else if token == "" {
		for i := len(opStack) - 1; i > -1; i-- {
			outputQueue = append(outputQueue, opStack[i])
		}
		return outputQueue, nil
	}

	return nil, errors.New("Unknown token: " + token)
}