func generateTypeDef(first bool, typeDef expressionParsing.TypeDefinition) string { if ftd, ok := typeDef.(expressionParsing.FuncTypeDefinition); ok { s := fmt.Sprintf("(%s %s) %s", ftd.ArgumentName(), ftd.ArgumentType().GenerateGo(), generateTypeDef(false, ftd.ReturnType())) if !first { return "func " + s } return s } else { return string(typeDef.GenerateGo()) } }
statementParser = NewLambdaStatementParser() factory = NewStatementFactory(declParser, statementParser, retParser) }) Context("Parse", func() { var expectedType expressionParsing.TypeDefinition BeforeEach(func() { expectedType, _, _ = expressionParsing.ParseTypeDef("func a A -> b B -> int32") }) It("Should distinguish a lambda statement", func() { code := "func a A -> b B -> int32\n\t5+9" f, err := statementParser.Parse(code, 0, nil, factory) Expect(f).ToNot(BeNil()) fs := f.(*LambdaStatement) Expect(err).To(BeNil()) Expect(fs).ToNot(BeNil()) Expect(fs.TypeDef.GenerateGo()).To(Equal(expectedType.GenerateGo())) Expect(fs.InnerStatements).ToNot(BeNil()) Expect(fs.InnerStatements).To(HaveLen(1)) }) It("Should distinguish a single line lambda statement", func() { code := "func a A -> b B -> int32 -> 5+9" f, err := statementParser.Parse(code, 0, nil, factory) Expect(f).ToNot(BeNil()) fs := f.(*LambdaStatement) Expect(err).To(BeNil()) Expect(fs).ToNot(BeNil()) Expect(fs.TypeDef.GenerateGo()).To(Equal(expectedType.GenerateGo())) Expect(fs.InnerStatements).ToNot(BeNil()) Expect(fs.InnerStatements).To(HaveLen(1)) }) It("Should distinguish a function as an argument", func() {
var funcType expressionParsing.TypeDefinition var twoArgFunc expressionParsing.TypeDefinition BeforeEach(func() { intType, _, _ = expressionParsing.ParseTypeDef("int32") funcType, _, _ = expressionParsing.ParseTypeDef("func a int32 -> int32") twoArgFunc, _, _ = expressionParsing.ParseTypeDef("func a int32 -> b int32 -> int32") }) It("Should generate the proper Go code with a definition", func() { code := "( 1 + 11 ) - a / 2" r, err := statementParser.Parse(code, 0, nil, factory) Expect(err).To(BeNil()) fm := expressionParsing.NewFunctionMap() name, _ := fm.AddFunction("a", intType) genGo, returnType, err := r.GenerateGo(fm) Expect(err).To(BeNil()) Expect(returnType.GenerateGo()).To(Equal(intType.GenerateGo())) Expect(genGo).To(Equal(fmt.Sprintf("((1+11)-(%s()/2))", name))) }) It("Should generate the proper Go code with definitions", func() { code := "a + b + c" r, err := statementParser.Parse(code, 0, nil, factory) Expect(err).To(BeNil()) fm := expressionParsing.NewFunctionMap() nameA, _ := fm.AddFunction("a", intType) nameB, _ := fm.AddFunction("b", intType) nameC, _ := fm.AddFunction("c", intType) genGo, returnType, err := r.GenerateGo(fm) Expect(err).To(BeNil()) Expect(returnType.GenerateGo()).To(Equal(intType.GenerateGo())) Expect(genGo).To(Equal(fmt.Sprintf("((%s()+%s())+%s())", nameA, nameB, nameC))) })