From 1b4b43e0ec6ee75e75ecb88fd080e0b0f99dcf49 Mon Sep 17 00:00:00 2001 From: Luca Morgado Date: Mon, 30 Sep 2024 15:10:46 +0200 Subject: [PATCH] added some test files and modified go.mod --- DEMO/Release/cube.ecla | 227 ++++++++++++++++++++++++++++++++++++++++ DEMO/Release/pixel.ecla | 96 +++++++++++++++++ go.mod | 14 ++- go.sum | 40 +++++-- interpreter/env.go | 8 ++ main.go | 3 +- 6 files changed, 377 insertions(+), 11 deletions(-) create mode 100644 DEMO/Release/cube.ecla create mode 100644 DEMO/Release/pixel.ecla diff --git a/DEMO/Release/cube.ecla b/DEMO/Release/cube.ecla new file mode 100644 index 0000000..8ed4f1d --- /dev/null +++ b/DEMO/Release/cube.ecla @@ -0,0 +1,227 @@ +import "pixel"; +import "console"; +import "time"; +import "math"; +import "cast"; + +struct Color { + R: int; + G: int; + B: int; + A: int; +} + +struct Point { + X: float; + Y: float; +} + +struct Point3D { + X: float; + Y: float; + Z: float; +} + +function Point3DTo2D(p: Point3D, c: Point3D) (Point) { + var point Point3D = Point3D{0.0,0.0,0.0}; + var camera Point3D = Point3D{0.0,0.0,0.0}; + camera = c; + point = p; + px := point.X; + py := point.Y; + pz := point.Z; + cx := camera.X; + cy := camera.Y; + cz := camera.Z; + var x float = px - cx; + var y float = py - cy; + var z float = pz - cz; + var scale float = 1.0 / z; + return Point{x * scale, y * scale}; +} + +function DegToRad(deg: float) (float) { + return deg * 3.141592 / 180.0; +} + +function ApplyRotation(p: Point3D, r: Point3D) (Point3D) { + var point Point3D = Point3D{0.0,0.0,0.0}; + var rotation Point3D = Point3D{0.0,0.0,0.0}; + point = p; + rotation = r; + px := point.X; + py := point.Y; + pz := point.Z; + # transform to radians + rx := DegToRad(rotation.X); + ry := DegToRad(rotation.Y); + rz := DegToRad(rotation.Z); + + cos1 := math.cos(rx); + sin1 := math.sin(rx); + + cos2 := math.cos(ry); + sin2 := math.sin(ry); + + cos3 := math.cos(rz); + sin3 := math.sin(rz); + + var y1 float = py * math.cos(rx) - pz * sin1; + var z1 float = py * sin1 + pz * cos1; + + var x1 float = px * cos2 + z1 * sin2; + var z2 float = -px * sin2 + z1 * cos2; + + var x2 float = x1 * cos3 - y1 * sin3; + var y2 float = x1 * sin3 + y1 * cos3; + + return Point3D{x2, y2, z2}; +} + +function ApplyTranslation(p: Point3D, t: Point3D) (Point3D) { + var point Point3D = Point3D{0.0,0.0,0.0}; + var translation Point3D = Point3D{0.0,0.0,0.0}; + point = p; + translation = t; + px := point.X; + py := point.Y; + pz := point.Z; + tx := translation.X; + ty := translation.Y; + tz := translation.Z; + return Point3D{px + tx, py + ty, pz + tz}; +} + +function ApplyScale(p: Point3D, s: Point3D) (Point3D) { + var point Point3D = Point3D{0.0,0.0,0.0}; + var scale Point3D = Point3D{0.0,0.0,0.0}; + point = p; + scale = s; + px := point.X; + py := point.Y; + pz := point.Z; + sx := scale.X; + sy := scale.Y; + sz := scale.Z; + return Point3D{px * sx, py * sy, pz * sz}; +} + + +pixel.start(); +var windowHandle int; +windowHandle = pixel.createWindow("testWindow1", 1000, 1000); +console.println("Created window with handle: ", windowHandle); + +# draw a simple cube +var vertices []Point3D = [ + # triangle 1 + Point3D{0.0, 0.0, 0.0}, + Point3D{1.0, 0.0, 0.0}, + Point3D{1.0, 1.0, 0.0}, + # triangle 2 + Point3D{0.0, 0.0, 0.0}, + Point3D{1.0, 1.0, 0.0}, + Point3D{0.0, 1.0, 0.0}, + # triangle 3 + Point3D{0.0, 0.0, 0.0}, + Point3D{0.0, 1.0, 0.0}, + Point3D{0.0, 1.0, 1.0}, + # triangle 4 + Point3D{0.0, 0.0, 0.0}, + Point3D{0.0, 1.0, 1.0}, + Point3D{0.0, 0.0, 1.0}, + # triangle 5 + Point3D{0.0, 0.0, 0.0}, + Point3D{0.0, 0.0, 1.0}, + Point3D{1.0, 0.0, 1.0}, + # triangle 6 + Point3D{0.0, 0.0, 0.0}, + Point3D{1.0, 0.0, 1.0}, + Point3D{1.0, 0.0, 0.0}, + # triangle 7 + Point3D{1.0, 0.0, 0.0}, + Point3D{1.0, 0.0, 1.0}, + Point3D{1.0, 1.0, 1.0}, + # triangle 8 + Point3D{1.0, 0.0, 0.0}, + Point3D{1.0, 1.0, 1.0}, + Point3D{1.0, 1.0, 0.0}, + # triangle 9 + Point3D{0.0, 1.0, 0.0}, + Point3D{1.0, 1.0, 0.0}, + Point3D{1.0, 1.0, 1.0}, + # triangle 10 + Point3D{0.0, 1.0, 0.0}, + Point3D{1.0, 1.0, 1.0}, + Point3D{0.0, 1.0, 1.0}, + # triangle 11 + Point3D{0.0, 0.0, 1.0}, + Point3D{1.0, 0.0, 1.0}, + Point3D{1.0, 1.0, 1.0}, + # triangle 12 + Point3D{0.0, 0.0, 1.0}, + Point3D{1.0, 1.0, 1.0}, + Point3D{0.0, 1.0, 1.0} +]; + + + +var camera Point3D = Point3D{0.0, 0.0, 0.0}; + +var rotation Point3D = Point3D{0.0, 0.0, 0.0}; + +var position Point3D = Point3D{2000.0, 5000.0, 10.0}; + +var scale Point3D = Point3D{1000.0, 1000.0, 1.0}; + + +var running bool = true; + +var velocityX float = 10.0; +var velocityY float = 10.0; + +while (running){ + time.sleep(0.1); + pixel.clear(windowHandle, Color{0, 0, 0, 255}); +# p1 := Point3DTo2D(ApplyTranslation(ApplyScale(ApplyRotation(vertices[0],rotation), scale), position), camera); +# p2 := Point3DTo2D(ApplyTranslation(ApplyScale(ApplyRotation(vertices[1],rotation), scale), position), camera); +# p3 := Point3DTo2D(ApplyTranslation(ApplyScale(ApplyRotation(vertices[2],rotation), scale), position), camera); + + var projected []Point; + + for (i := 0, i < len(vertices), i++) { + projected = append(projected, Point3DTo2D(ApplyTranslation(ApplyScale(ApplyRotation(vertices[i],rotation), scale), position), camera)); + } + + for (i := 0, i < len(projected), i+=3) { + pixel.drawTriangle(windowHandle, projected[i], projected[i+1], projected[i+2], Color{255,255,255, 255},false); + } + + # rotate the cube + rotation.X += 0.1; + rotation.Y += 0.1; + rotation.Z += 0.1; + + # change direction when hitting the edge + posx := position.X; + posy := position.Y; + if (posx > 10000 || posx < 0) { + velocityX = -velocityX; + } + if (posy > 10000 || posy < 0) { + velocityY = -velocityY; + } + position.X += velocityX; + position.Y += velocityY; + + + + + + + pixel.updateWindow(windowHandle); + if (pixel.windowShouldClose(windowHandle)){ + pixel.destroyWindow(windowHandle); + running = false; + } +} \ No newline at end of file diff --git a/DEMO/Release/pixel.ecla b/DEMO/Release/pixel.ecla new file mode 100644 index 0000000..cf17224 --- /dev/null +++ b/DEMO/Release/pixel.ecla @@ -0,0 +1,96 @@ +import "pixel"; +import "console"; +import "time"; +import "math"; +import "cast"; + +struct Color { + R: int; + G: int; + B: int; + A: int; +} + +struct Point { + X: float; + Y: float; +} + + +pixel.start(); +var windowHandle int; +windowHandle = pixel.createWindow("testWindow1", 1000, 1000); +console.println("Created window with handle: ", windowHandle); + +var balls []Point; +var ballSizes []float; +var ballSpeeds []Point; +var colors []Color; + +# generate balls +for (var i int = 0, i < 100, i++){ + x := math.random(0.0, 1000.0); + time.sleep(0.1); + y := math.random(0.0, 1000.0); + time.sleep(0.1); + p := Point{x, y}; + balls = append(balls, p); +} +# generate ball sizes +for (var i int = 0, i < 100, i++){ + time.sleep(0.1); + s := math.random(10.0, 20.0); + ballSizes = append(ballSizes, s); +} +# generate ball speeds +for (var i int = 0, i < 100, i++){ + x := math.random(-10.0, 10.0); + time.sleep(0.1); + y := math.random(x, 10.0); + time.sleep(0.1); + bs := Point{x, y}; + ballSpeeds = append(ballSpeeds, bs); +} + +# generate colors +for (var i int = 0, i < 100, i++){ + r := cast.floatToInt(math.random(0.0, 255.0)); + time.sleep(0.1); + g := cast.floatToInt(math.random(0.0, 255.0)); + time.sleep(0.1); + b := cast.floatToInt(math.random(0.0, 255.0)); + time.sleep(0.1); + c := Color{r, g, b, 255}; + colors = append(colors, c); +} + +var running bool = true; +while (running){ + time.sleep(0.1); + pixel.clear(windowHandle, Color{0, 0, 0, 255}); + + for (var i int = 0, i < 100, i++){ + balls[i].X += ballSpeeds[i].X; + balls[i].Y += ballSpeeds[i].Y; + + posX := balls[i].X; + posY := balls[i].Y; + + if (posX < 0 || posX > 1000){ + ballSpeeds[i].X = -ballSpeeds[i].X; + } + if (posY < 0 || posY > 1000){ + ballSpeeds[i].Y = -ballSpeeds[i].Y; + } + + pixel.drawCircle(windowHandle, balls[i], ballSizes[i], colors[i], true); + } + + + + pixel.updateWindow(windowHandle); + if (pixel.windowShouldClose(windowHandle)){ + pixel.destroyWindow(windowHandle); + running = false; + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index 3ffbbfc..911ed33 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,14 @@ module github.com/Eclalang/Ecla go 1.21.1 require ( - github.com/Eclalang/LibraryController v1.0.0 + github.com/Eclalang/LibraryController v1.0.1-0.20240809080138-46cfaf3ebdf0 github.com/Eclalang/mainthread v0.0.0-20171120011319-8b78f0a41ae3 ) require ( + github.com/Eclalang/array v1.0.0 // indirect github.com/Eclalang/cast v1.0.0 // indirect - github.com/Eclalang/console v1.0.1 // indirect + github.com/Eclalang/console v1.0.2 // indirect github.com/Eclalang/encoding v1.0.1 // indirect github.com/Eclalang/hash v1.0.0 // indirect github.com/Eclalang/json v1.0.1 // indirect @@ -17,6 +18,13 @@ require ( github.com/Eclalang/os v1.0.1 // indirect github.com/Eclalang/regex v1.0.1 // indirect github.com/Eclalang/strings v1.0.0 // indirect - github.com/Eclalang/time v1.0.1 // indirect + github.com/Eclalang/time v1.1.1 // indirect + github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 // indirect github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 // indirect + github.com/faiface/pixel v0.10.0 // indirect + github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 // indirect + github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect + github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 // indirect + github.com/pkg/errors v0.9.1 // indirect + golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect ) diff --git a/go.sum b/go.sum index 13ecc38..a2aab74 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,11 @@ -github.com/Eclalang/LibraryController v1.0.0 h1:D3z6DeVgck4xu28brEKV1UbglLk5mtO4JCqcLLVkpa8= -github.com/Eclalang/LibraryController v1.0.0/go.mod h1:yqLKnC2as6X/+zx4e2KYLpAm3rmKhfk6BksW7CMlIa4= +github.com/Eclalang/LibraryController v1.0.1-0.20240809080138-46cfaf3ebdf0 h1:RUh+s9vewH1RPEaSAUxi7Yz9Wv582g0yksweSDt5yxc= +github.com/Eclalang/LibraryController v1.0.1-0.20240809080138-46cfaf3ebdf0/go.mod h1:zp6fDAJoj0ebC7TDziuCqdoZGBL5KH0zml9wffr5psg= +github.com/Eclalang/array v1.0.0 h1:ywo2X3hGBcvRs3OUUy6Jds9sz+Euyogy98G5RLLFNGc= +github.com/Eclalang/array v1.0.0/go.mod h1:Hf8ZjZ8SrS2D2EuJSS0sS8u21uciYFo1I5jVps2Oi7Y= github.com/Eclalang/cast v1.0.0 h1:wEdtCFSLULp2Gm77/mM6mxiLOvLe/M5R3mgUfQKnzmc= github.com/Eclalang/cast v1.0.0/go.mod h1:RJAiwgqiEPT3kmoSYTBK+JhpGOI8YXRdxrQ99zJyA00= -github.com/Eclalang/console v1.0.1 h1:t2Jc9cH19wT4/polJKNnnUmasl9f4pbVt81+p5lm5Kg= -github.com/Eclalang/console v1.0.1/go.mod h1:RHRoUAVzxpcTVzX/rPm5VFjOUrshw4v9pBVDlCEmwQM= +github.com/Eclalang/console v1.0.2 h1:IQ5zY+7vvotHDDCXOsRsIu+waAwey+X4yItnM8Jr0kc= +github.com/Eclalang/console v1.0.2/go.mod h1:wXf4+hC8kNolIfLvENuTKvNuGEo/YumYztONSgPNSTY= github.com/Eclalang/encoding v1.0.1 h1:cntD/UAlHaHMxu/xRZiT5R37bJrJvGQ2dWKi3VePVDI= github.com/Eclalang/encoding v1.0.1/go.mod h1:fc3/K5xxbEf8fRSBhxunDFRerN3Wr6CXJ2lyA1nbxWA= github.com/Eclalang/hash v1.0.0 h1:GzD6XGvAdr7Z64weypuLxjuyxXAGIebkGQzRFnuB0eY= @@ -20,7 +22,33 @@ github.com/Eclalang/regex v1.0.1 h1:d0Uv0mA+1B3t0gknC1rZ7YwXEdY/03bMLm34yUgknXA= github.com/Eclalang/regex v1.0.1/go.mod h1://L9ebqTHyWDZWhmsvbmWFjfczjNUHGcRKpZj8dDR00= github.com/Eclalang/strings v1.0.0 h1:rONEmWsx5bu0cXjF9VkbLd1l2MGSB16plWYfuioawFk= github.com/Eclalang/strings v1.0.0/go.mod h1:2FgmYwPkfSu/cd2gZ8Wy37QfMzv2eraF86V/dDrLLhk= -github.com/Eclalang/time v1.0.1 h1:POiM/SNM6MxIhvbpNXa863LKgdIV8HWvXl9xi8ijKyQ= -github.com/Eclalang/time v1.0.1/go.mod h1://+Z1TD75utJJ7ob1E+D60sNezGOI3T7rhN17jYGnvM= +github.com/Eclalang/time v1.1.1 h1:LrlIytusJQdhN4xKTkRo66V5JPZHDegn5rTY0FDmCl4= +github.com/Eclalang/time v1.1.1/go.mod h1://+Z1TD75utJJ7ob1E+D60sNezGOI3T7rhN17jYGnvM= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0= +github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g= github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q= github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M= +github.com/faiface/pixel v0.10.0 h1:EHm3ZdQw2Ck4y51cZqFfqQpwLqNHOoXwbNEc9Dijql0= +github.com/faiface/pixel v0.10.0/go.mod h1:lU0YYcW77vL0F1CG8oX51GXurymL45MXd57otHNLK7A= +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a h1:vxnBhFDDT+xzxf1jTJKMKZw3H0swfWk9RpWbBbDK5+0= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 h1:THttjeRn1iiz69E875U6gAik8KTWk/JYAHoSVpUxBBI= +github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff h1:+2zgJKVDVAz/BWSsuniCmU1kLCjL88Z8/kv39xCI9NQ= +golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/interpreter/env.go b/interpreter/env.go index 9e1ceb4..1f4be69 100644 --- a/interpreter/env.go +++ b/interpreter/env.go @@ -1,6 +1,7 @@ package interpreter import ( + "encoding/json" "fmt" "github.com/Eclalang/Ecla/interpreter/eclaDecl" "os" @@ -159,6 +160,9 @@ func (env *Env) Execute() { // Parsing pars := parser.Parser{Tokens: env.Tokens, ErrorHandler: env.ErrorHandle} env.SyntaxTree = pars.Parse() + data, _ := json.Marshal(env.SyntaxTree) + os.WriteFile("syntaxTree.json", data, 0644) + fmt.Println(json.Marshal(env.SyntaxTree)) // Execute Run(env) @@ -291,6 +295,10 @@ func (lib *envLib) GetVar(name string) (*eclaType.Var, bool) { return lib.Var.Get(name) } +func (lib *envLib) GetVariables() map[string]eclaType.Type { + return map[string]eclaType.Type{} +} + // ConvertToLib converts the Env to a Lib. func (env *Env) ConvertToLib(MainEnv *Env) libs.Lib { return &envLib{ diff --git a/main.go b/main.go index 2145b18..2b11927 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "github.com/Eclalang/Ecla/interpreter" - "github.com/Eclalang/mainthread" "strings" "time" ) @@ -78,5 +77,5 @@ func eclaCli() { } func main() { - mainthread.Run(eclaCli) + eclaCli() }