From b707454bb1266e2f29c84a33c309fd2c7e8c602e Mon Sep 17 00:00:00 2001 From: andygeiss <4ndygeiss@gmail.com> Date: Tue, 1 May 2018 18:14:46 +0200 Subject: [PATCH] while loop added --- impl/worker/mapping.go | 3 ++- impl/worker/worker.go | 18 ++++++++++++++++++ impl/worker/worker_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/impl/worker/mapping.go b/impl/worker/mapping.go index 963fc2d..ff03d29 100644 --- a/impl/worker/mapping.go +++ b/impl/worker/mapping.go @@ -47,6 +47,7 @@ var ( "wifi.ScanNetworks": "WiFi.scanNetworks", "wifi.SetDNS": "WiFi.setDNS", "wifi.SSID": "WiFi.SSID", + "wifi.Status": "WiFi.status", "wifi.StatusConnected": "WL_CONNECTED", "wifi.StatusIdle": "WL_IDLE", "Loop": "void loop", @@ -55,7 +56,7 @@ var ( ) // Mapping specifies the api logic to apply transformation to a specific Golang identifier by reading simple JSON map. -type Mapping struct {} +type Mapping struct{} // NewMapping creates a new mapping and returns its address. func NewMapping() worker.Mapping { diff --git a/impl/worker/worker.go b/impl/worker/worker.go index ab3060c..8484547 100644 --- a/impl/worker/worker.go +++ b/impl/worker/worker.go @@ -339,6 +339,8 @@ func handleStmt(stmt ast.Stmt) string { case *ast.ExprStmt: code += handleExprStmt(s) code += ";" + case *ast.ForStmt: + code += handleForStmt(s) case *ast.IfStmt: code += handleIfStmt(s) case *ast.SwitchStmt: @@ -347,6 +349,22 @@ func handleStmt(stmt ast.Stmt) string { return code } +func handleForStmt(stmt *ast.ForStmt) string { + code := "" + if stmt.Init == nil && stmt.Post == nil { + code += "while" + } else { + code += "for" + } + code += "(" // stmt.Init + code += handleBinaryExpr(stmt.Cond) // stmt.Cond + code += "" // stmt.Post + code += ") {" + code += handleBlockStmt(stmt.Body) // stmt.Body + code += "}" + return code +} + func handleSwitchStmt(stmt *ast.SwitchStmt) string { code := "switch (" code += handleExpr(stmt.Tag) diff --git a/impl/worker/worker_test.go b/impl/worker/worker_test.go index 5e70587..19cc5ed 100644 --- a/impl/worker/worker_test.go +++ b/impl/worker/worker_test.go @@ -376,3 +376,32 @@ func Test_SwitchStmt_With_Break(t *testing.T) { ` Validate(source, expected, t) } + +func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) { + source := `package test + import wifi "github.com/andygeiss/esp32/api/controller/wifi" + func Setup() error { + serial.Begin(serial.BaudRate115200) + wifi.BeginEncrypted("SSID", "PASS") + for wifi.Status() != wifi.StatusConnected { + serial.Println("Connecting ...") + } + serial.Println("Connected!") + return nil + } + func Loop() error {} +` + expected := ` + #include + void setup() { + Serial.begin(115200); + WiFi.begin("SSID","PASS"); + while(WiFi.status()!=WL_CONNECTED){ + Serial.println("Connecting..."); + } + Serial.println("Connected!"); + } + void loop() {} +` + Validate(source, expected, t) +} \ No newline at end of file