From 92fbc7ce57b4ab992c7c406ad271caefcaf3e616 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:31:10 +0000 Subject: [PATCH 01/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 83b6d4c..e14da90 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -8,6 +8,7 @@ import ( "github.com/Krishanu230/Flipbook-Language/object" "github.com/signintech/gopdf" + "github.com/disintegration/imaging" ) var ( @@ -42,11 +43,16 @@ func Eval(node ast.Node, env *object.Environment) object.Object { return val } + case *ast.SwirlEffectStatement: + val := evalSwirlEffect(node, env) + if isError(val) { + return val + } case *ast.SaveStatement: - val := evalSave(node, env) - if isError(val) { - return val - } + val := evalSave(node, env) + if isError(val) { + return val + } } //end of switch return nil } @@ -177,7 +183,8 @@ func evalKeyframe(inp *ast.KeyframeStatement, env *object.Environment) object.Ob } func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { - r := evalIdentifier(inp.Book, env) + rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) + pdf.Image(rotatedImg, float64(ix), float64(iy), nil) book, ok := r.(*object.Book) if !ok { return r From 31905262e7bb3e398030fc60c4a4a3d5d4458566 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:40:27 +0000 Subject: [PATCH 02/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index e14da90..f7831f0 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -183,6 +183,18 @@ func evalKeyframe(inp *ast.KeyframeStatement, env *object.Environment) object.Ob } func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { + r := evalIdentifier(inp.Book, env) + book, ok := r.(*object.Book) + if !ok { + return r + } + imgObj := evalIdentifier(inp.Image, env) + img, ok := imgObj.(*object.Image) + if !ok { + return imgObj + } + pdf := gopdf.GoPdf{} + pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: float64(book.DimX), H: float64(book.DimY)}}) rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) pdf.Image(rotatedImg, float64(ix), float64(iy), nil) book, ok := r.(*object.Book) From 38e9d874f197fc377349b019431ade672f8e1211 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:52:12 +0000 Subject: [PATCH 03/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index f7831f0..66774ef 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -3,6 +3,7 @@ package evaluator import ( "fmt" "strconv" + "image/color" "github.com/Krishanu230/Flipbook-Language/ast" "github.com/Krishanu230/Flipbook-Language/object" @@ -183,6 +184,8 @@ func evalKeyframe(inp *ast.KeyframeStatement, env *object.Environment) object.Ob } func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { + ix := 0 + iy := 0 r := evalIdentifier(inp.Book, env) book, ok := r.(*object.Book) if !ok { @@ -197,7 +200,7 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: float64(book.DimX), H: float64(book.DimY)}}) rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) pdf.Image(rotatedImg, float64(ix), float64(iy), nil) - book, ok := r.(*object.Book) + book, ok = r.(*object.Book) if !ok { return r } @@ -220,8 +223,8 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { println("ERROR1") return newError("Image " + fname + " size larger than the page " + strconv.Itoa(pno)) }*/ - ix := img.PosX - iy := img.PosY + ix = img.PosX + iy = img.PosY if (ix > pw) || (iy > ph) { return newError("Image " + fname + " position beyond the page " + strconv.Itoa(pno)) } From 1081208084050a8c426fe4582003c93c5167752f Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:53:48 +0000 Subject: [PATCH 04/12] Sandbox run evaluator/evaluator.go --- evaluator/evaluator.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 66774ef..b6874ac 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -2,21 +2,21 @@ package evaluator import ( "fmt" - "strconv" "image/color" + "strconv" "github.com/Krishanu230/Flipbook-Language/ast" "github.com/Krishanu230/Flipbook-Language/object" - "github.com/signintech/gopdf" "github.com/disintegration/imaging" + "github.com/signintech/gopdf" ) var ( NULL = &object.Null{} ) -//eval a ast node. recursive nature +// eval a ast node. recursive nature func Eval(node ast.Node, env *object.Environment) object.Object { switch node := node.(type) { @@ -45,20 +45,20 @@ func Eval(node ast.Node, env *object.Environment) object.Object { } case *ast.SwirlEffectStatement: - val := evalSwirlEffect(node, env) - if isError(val) { - return val - } + val := evalSwirlEffect(node, env) + if isError(val) { + return val + } case *ast.SaveStatement: - val := evalSave(node, env) - if isError(val) { - return val - } + val := evalSave(node, env) + if isError(val) { + return val + } } //end of switch return nil } -//eval a bunch of statements by passing them to Eval() +// eval a bunch of statements by passing them to Eval() func evalStatements(sts []ast.Statement, env *object.Environment) object.Object { var result object.Object @@ -69,7 +69,7 @@ func evalStatements(sts []ast.Statement, env *object.Environment) object.Object return result } -//resolve an identifier +// resolve an identifier func evalIdentifier(node *ast.Identifier, env *object.Environment) object.Object { val, ok := env.Get(node.Value) if ok { @@ -78,7 +78,7 @@ func evalIdentifier(node *ast.Identifier, env *object.Environment) object.Object return newError("identifier not found: " + node.Value) } -//evaluate New type of statements +// evaluate New type of statements func evalNew(inp *ast.NewStatement, env *object.Environment) object.Object { st := *inp if st.DType.Value == "image" { @@ -96,7 +96,7 @@ func evalNew(inp *ast.NewStatement, env *object.Environment) object.Object { return &object.Null{} } -//evaluate Insert statements +// evaluate Insert statements func evalInsert(inp *ast.InsertStatement, env *object.Environment) object.Object { //env.Print() r := evalIdentifier(inp.Image, env) @@ -132,7 +132,7 @@ func evalInsert(inp *ast.InsertStatement, env *object.Environment) object.Object return &object.Null{} } -//evaluate keyframe statements +// evaluate keyframe statements func evalKeyframe(inp *ast.KeyframeStatement, env *object.Environment) object.Object { r := evalIdentifier(inp.Image, env) img, ok := r.(*object.Image) @@ -237,7 +237,7 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { return &object.Null{} } -//Helper Functions +// Helper Functions func isError(obj object.Object) bool { if obj != nil { return obj.Type() == object.ERROR_OBJ From dc882ccda992c449889def081b8998e94ef08b6e Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:57:57 +0000 Subject: [PATCH 05/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 1 + 1 file changed, 1 insertion(+) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index b6874ac..2c90b2d 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -7,6 +7,7 @@ import ( "github.com/Krishanu230/Flipbook-Language/ast" "github.com/Krishanu230/Flipbook-Language/object" + "image/color" "github.com/disintegration/imaging" "github.com/signintech/gopdf" From 91415cfd353a0e0a9856610f98e6cd754b8c88b1 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:02:20 +0000 Subject: [PATCH 06/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 2c90b2d..82dc191 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -7,7 +7,6 @@ import ( "github.com/Krishanu230/Flipbook-Language/ast" "github.com/Krishanu230/Flipbook-Language/object" - "image/color" "github.com/disintegration/imaging" "github.com/signintech/gopdf" @@ -197,7 +196,7 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { if !ok { return imgObj } - pdf := gopdf.GoPdf{} + pdf = gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: float64(book.DimX), H: float64(book.DimY)}}) rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) pdf.Image(rotatedImg, float64(ix), float64(iy), nil) From cf3a12065999f5b0050f1f11f2e94e46ac8118fc Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:06:12 +0000 Subject: [PATCH 07/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 82dc191..308bbdf 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -2,7 +2,6 @@ package evaluator import ( "fmt" - "image/color" "strconv" "github.com/Krishanu230/Flipbook-Language/ast" @@ -200,7 +199,7 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: float64(book.DimX), H: float64(book.DimY)}}) rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) pdf.Image(rotatedImg, float64(ix), float64(iy), nil) - book, ok = r.(*object.Book) + book, ok := r.(*object.Book) if !ok { return r } From a964cd9d5e94c275061858ac98a06a76c0b21e03 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:08:21 +0000 Subject: [PATCH 08/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 308bbdf..14743fa 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -8,6 +8,15 @@ import ( "github.com/Krishanu230/Flipbook-Language/object" "github.com/disintegration/imaging" +) + +func SwirlEffect(pages []object.PageProperty, rotationRate int) { + for i, page := range pages { + rotationAngle := i * rotationRate + for _, img := range page.ImagesProps { + img.Image = imaging.Rotate(img.Image, float64(rotationAngle), color.Transparent) + } + } "github.com/signintech/gopdf" ) @@ -48,6 +57,16 @@ func Eval(node ast.Node, env *object.Environment) object.Object { if isError(val) { return val } + } + + func evalSwirlEffect(node *ast.SwirlEffectStatement, env *object.Environment) object.Object { + r := evalIdentifier(node.Book, env) + book, ok := r.(*object.Book) + if !ok { + return r + } + SwirlEffect(book.Pages, node.RotationRate.Value) + return &object.Null{} case *ast.SaveStatement: val := evalSave(node, env) if isError(val) { @@ -197,9 +216,13 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { } pdf = gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: float64(book.DimX), H: float64(book.DimY)}}) - rotatedImg := imaging.Rotate(img.Image, float64(img.Rotation), color.Transparent) - pdf.Image(rotatedImg, float64(ix), float64(iy), nil) - book, ok := r.(*object.Book) + for pno, page := range book.Pages { + pdf.AddPage() + for _, img := range page.ImagesProps { + rotatedImg := img.Image + pdf.Image(rotatedImg, float64(ix), float64(iy), nil) + } + } if !ok { return r } From bb69a1774147f05a152352e142732f960727e93c Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:10:53 +0000 Subject: [PATCH 09/12] feat: Updated lexer/lexer_test.go --- lexer/lexer_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 1e9156b..49d27e1 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -41,6 +41,10 @@ func TestNextToken(t *testing.T) { {token.SCALE, "scale"}, {token.INT, "10"}, {token.SEMICOLON, ";"}, + {token.SWIRL, "swirl"}, + {token.IDN, "bookone"}, + {token.INT, "10"}, + {token.SEMICOLON, ";"}, } l := NewLexer(input) From 145581863d7726f28ccce93cab7db85fe28854de Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:14:11 +0000 Subject: [PATCH 10/12] feat: Updated README.md --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e87a4e7..40d844b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ go run main.go flipbook.flip ## Language Design +5. SwirlEffect: This function applies a rotation transformation to each page of a flipbook, with the angle of rotation increasing for each subsequent page. The syntax is as follows: + +***swirl [Book ObjType variable] [Rotation Rate INT];*** + ### Rules: 1. There are two type of objects in this language: Book and Image. @@ -36,7 +40,7 @@ go run main.go flipbook.flip 3.2 Integers: They are any positive integer. -4. There are four types of Statements implemented right now: new, insert, keyframe, save. Every Statement must end with a semicolon. +4. There are five types of Statements implemented right now: new, insert, keyframe, save, swirl. Every Statement must end with a semicolon. 4.1 ***New***: It creates a new object Syntax - @@ -61,7 +65,16 @@ go run main.go flipbook.flip ***save [Book ObjType variable] outputNameString*** ## Example program -Lets animate apple falling over newton's head. +Lets animate apple falling over newton's head with a swirl effect. +``` +new book bookone = ((1600,1600), 25); +new image newton = ((100,100), "newton.png"); +insert newton bookone from page 1 to 25 at (600,1270); +new image melon = ((100,100), "melon.png"); +insert melon bookone from page 1 to 25 at (600,0); +keyframe melon bookone positionY (1,0) to (25, 1000); +swirl bookone 10; +save bookone "out.pdf"; ``` new book bookone = ((1600,1600), 25); new image newton = ((100,100), "newton.png"); @@ -90,5 +103,4 @@ KeyWords= { new, at, to, set, image, book, scale, insert, from, page, keyframe,, 1. Compelete the document: Due to the limitation of time, The documentation is not very resourceful. 2.Clean Up the Code: Due to the same reason, I couldnt get the chance to properly refactor code and comment it. 3. Add Expression pevaluation via Pratt's Parser technique -4. Add an analouge of Functions as Effects like swirl -5. Add more properties like opacity and more statements like 'set'. +4. Add more properties like opacity and more statements like 'set'. From 18981453e5286de7c9f8d587f24cdd80b30cb536 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:16:00 +0000 Subject: [PATCH 11/12] feat: Updated evaluator/evaluator.go --- evaluator/evaluator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 14743fa..e746b65 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -236,7 +236,7 @@ func evalSave(inp *ast.SaveStatement, env *object.Environment) object.Object { for pno, page := range book.Pages { pdf.AddPage() for _, img := range page.ImagesProps { - imgpath := img.Image.Filename + imgpath := img.Filename //// TODO: implement scale property change by using a better library for pdf /*iw := int(img.Image.DimX * (0)) ih := int(img.Image.DimX * (0)) From effb4a3f810febd90d25661fd4491a13cc493fd7 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:17:49 +0000 Subject: [PATCH 12/12] feat: Add tests for SwirlEffect and evalSwirlEffec --- evaluator/evaluator_test.go | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 evaluator/evaluator_test.go diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go new file mode 100644 index 0000000..74b5216 --- /dev/null +++ b/evaluator/evaluator_test.go @@ -0,0 +1,47 @@ +package evaluator + +import ( + "testing" + + "github.com/Krishanu230/Flipbook-Language/ast" + "github.com/Krishanu230/Flipbook-Language/object" +) + +func TestSwirlEffect(t *testing.T) { + pages := []object.PageProperty{ + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + } + SwirlEffect(pages, 10) + if pages[0].ImagesProps[0].Image.Rotation != 0 || pages[1].ImagesProps[0].Image.Rotation != 10 { + t.Errorf("SwirlEffect did not correctly update the rotation of the images") + } +} + +func TestEvalSwirlEffect(t *testing.T) { + env := object.NewEnvironment() + env.Set("bookone", &object.Book{Pages: []object.PageProperty{ + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + }}) + node := &ast.SwirlEffectStatement{Book: &ast.Identifier{Value: "bookone"}, RotationRate: &ast.IntegerLiteral{Value: 10}} + evalSwirlEffect(node, env) + book, _ := env.Get("bookone") + if book.(*object.Book).Pages[0].ImagesProps[0].Image.Rotation != 0 || book.(*object.Book).Pages[1].ImagesProps[0].Image.Rotation != 10 { + t.Errorf("evalSwirlEffect did not correctly update the rotation of the images") + } +} + +func TestEvalSave(t *testing.T) { + env := object.NewEnvironment() + env.Set("bookone", &object.Book{Pages: []object.PageProperty{ + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + {ImagesProps: []object.ImageProperty{{Image: object.Image{Rotation: 0}}}}, + }}) + node := &ast.SaveStatement{Book: &ast.Identifier{Value: "bookone"}, OutputName: &ast.StringLiteral{Value: "out.pdf"}} + evalSave(node, env) + book, _ := env.Get("bookone") + if book.(*object.Book).Pages[0].ImagesProps[0].Image.Rotation != 0 || book.(*object.Book).Pages[1].ImagesProps[0].Image.Rotation != 10 { + t.Errorf("evalSave did not correctly update the rotation of the images") + } +}