Exemplo n.º 1
0
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())
	}
}
Exemplo n.º 2
0
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("LambdaStatement", func() {
	var statementParser StatementParser
	var factory *StatementFactory
	BeforeEach(func() {
		retParser := NewReturnStatementParser()
		declParser := NewLetParser()
		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() {
Exemplo n.º 3
0
				Expect(returnType.GenerateGo()).To(Equal(intType.GenerateGo()))
				Expect(genGo).To(Equal("((1+11)-(10/2))"))
			})
			It("Should generate the proper Go code with proper numeric types", func() {
				code := "( 1b + 11b ) - 10b / 2b"
				r, err := statementParser.Parse(code, 0, nil, factory)
				Expect(err).To(BeNil())
				genGo, returnType, err := r.GenerateGo(funcMap)
				Expect(err).To(BeNil())
				intType, _, _ := expressionParsing.ParseTypeDef("int8")
				Expect(returnType.GenerateGo()).To(Equal(intType.GenerateGo()))
				Expect(genGo).To(Equal("((int8(1)+int8(11))-(int8(10)/int8(2)))"))
			})
		})
		Context("With functions", func() {
			var intType expressionParsing.TypeDefinition
			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())