예제 #1
0
func splitEquals(line string) (bool, string, string) {
	var varName string
	var rest string
	var let string
	var equals string
	let, rest = parser.Tokenize(line)
	varName, rest = parser.Tokenize(rest)
	equals, rest = parser.Tokenize(rest)

	if let == "let" && equals == "=" {
		return true, varName, rest
	}

	return false, "", ""
}
예제 #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)
}
예제 #3
0
func (fs LambdaStatement) Parse(block string, lineNum int, nextBlockScanner *parser.ScanPeeker, factory *StatementFactory) (Statement, parser.SyntaxError) {
	lines := parser.Lines(block)
	factory = fetchNewFactory(factory)
	typeDef, err, rest := expressionParsing.ParseTypeDef(lines[0])
	if err != nil {
		return nil, parser.NewSyntaxError(err.Error(), lineNum, 0)
	}

	var ftd expressionParsing.FuncTypeDefinition
	var ok bool
	if ftd, ok = typeDef.(expressionParsing.FuncTypeDefinition); !ok {
		return nil, nil
	}

	var codeBlock []string
	if len(strings.TrimSpace(rest)) > 0 {
		var first string
		if len(lines) > 1 {
			return nil, parser.NewSyntaxError("Inline lambdas can only be one line", lineNum, 0)
		} else if first, rest = parser.Tokenize(rest); first != "->" {
			return nil, parser.NewSyntaxError("Misplaced tokens: "+rest, lineNum, 0)
		}
		codeBlock = []string{rest}
	} else {
		codeBlock = parser.RemoveTabs(lines[1:])
	}

	innerStatements, synErr := fetchInnerStatements(codeBlock, factory, lineNum+1)
	if synErr != nil {
		return nil, synErr
	}

	synErr = verifyInnerStatements(innerStatements, lineNum)
	if synErr != nil {
		return nil, synErr
	}

	return newLambdaStatement(lineNum, ftd, innerStatements, fs.packageLevel, fs.name), nil
}