示例#1
0
func GofToGo(reader io.Reader, writer io.Writer) error {
	bs := parser.NewBlockScanner(reader, 1)
	bp := parser.NewScanPeeker(bs)
	factory := statementTypes.NewStatementFactory(fetchStatementParsers()...)
	funcMap := expressionParsing.NewFunctionMap()

	io.WriteString(writer, "package tests\n\n")

	return writeGeneratedBlocks(factory, bp, funcMap, writer)
}
示例#2
0
		It("Should distinguish a function as an argument", func() {
			code := "func a func x int32 -> int32 -> 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())
			expectedType, _, _ := expressionParsing.ParseTypeDef("func a func x int32 -> int32 -> b B -> int32")
			Expect(fs.TypeDef.GenerateGo()).To(Equal(expectedType.GenerateGo()))
			Expect(fs.InnerStatements).ToNot(BeNil())
			Expect(fs.InnerStatements).To(HaveLen(1))
		})
	})
	Context("GenerateGo", func() {
		It("Should generate a proper Go function with single line statement", func() {
			fm := expressionParsing.NewFunctionMap()
			code := "func a int32 -> int32 -> a + 5"
			f, err := statementParser.Parse(code, 0, nil, factory)
			Expect(f).ToNot(BeNil())
			Expect(err).To(BeNil())
			code, _, err = f.GenerateGo(fm)
			Expect(err).To(BeNil())
			Expect(matchCode(code, "func (a int32) int32{return (a+5)}")).To(BeTrue())
		})
		It("Should generate a proper Go function with one inner statement", func() {
			fm := expressionParsing.NewFunctionMap()
			code := "func a int32 -> b int32 -> c int32 -> int32\n\ta + b + c"
			f, err := statementParser.Parse(code, 0, nil, factory)
			Expect(f).ToNot(BeNil())
			Expect(err).To(BeNil())
			code, _, err = f.GenerateGo(fm)