From eb79c94690cbd56f34cca9feeab4fc232903b74c Mon Sep 17 00:00:00 2001 From: andygeiss <4ndygeiss@gmail.com> Date: Mon, 18 Jun 2018 19:11:04 +0200 Subject: [PATCH] if with else added --- impl/worker/mapping.go | 1 + impl/worker/worker.go | 18 +++++++--- impl/worker/worker_test.go | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/impl/worker/mapping.go b/impl/worker/mapping.go index ff03d29..5da9ba8 100644 --- a/impl/worker/mapping.go +++ b/impl/worker/mapping.go @@ -32,6 +32,7 @@ var ( "serial.Println": "Serial.println", "timer.Delay": "delay", "wifi": "WiFi", + "wifi.Client": "WiFiClient", "wifi.Begin": "WiFi.begin", "wifi.BeginEncrypted": "WiFi.begin", "wifi.BSSID": "WiFi.BSSID", diff --git a/impl/worker/worker.go b/impl/worker/worker.go index 8484547..ba53701 100644 --- a/impl/worker/worker.go +++ b/impl/worker/worker.go @@ -74,7 +74,7 @@ func (w *Worker) Start() error { } } // Print the AST. - //ast.Fprint(os.Stderr, fset, file, nil) + // ast.Fprint(os.Stderr, fset, file, nil) return nil } @@ -259,6 +259,8 @@ func handleGenDecl(decl ast.Decl) string { switch gd.Tok { case token.CONST: code += "const " + case token.VAR: + code += "" } code += handleSpecs(gd.Specs) return code @@ -277,9 +279,13 @@ func handleIdent(expr ast.Expr) string { } func handleIfStmt(stmt *ast.IfStmt) string { - cond := handleBinaryExpr(stmt.Cond) + cond := handleExpr(stmt.Cond) body := handleBlockStmt(stmt.Body) code := fmt.Sprintf(`if (%s) { %s }`, cond, body) + if stmt.Else != nil { + tail := handleBlockStmt(stmt.Else.(*ast.BlockStmt)) + code += fmt.Sprintf(" else { %s }", tail) + } return code } @@ -380,8 +386,10 @@ func handleValueSpec(spec ast.Spec) string { code += handleValueSpecType(s.Type) code += " " code += handleValueSpecNames(s.Names) - code += " = " - code += handleValueSpecValues(s.Values) + if s.Values != nil { + code += " = " + code += handleValueSpecValues(s.Values) + } return code } @@ -396,6 +404,8 @@ func handleValueSpecNames(names []*ast.Ident) string { func handleValueSpecType(expr ast.Expr) string { code := "" switch t := expr.(type) { + case *ast.SelectorExpr: + code += handleSelectorExpr(t) case *ast.Ident: code += handleIdent(t) } diff --git a/impl/worker/worker_test.go b/impl/worker/worker_test.go index 19cc5ed..5ce4957 100644 --- a/impl/worker/worker_test.go +++ b/impl/worker/worker_test.go @@ -45,6 +45,7 @@ func Test_Function_Declaration(t *testing.T) { void bar() {} ` Validate(source, expected, t) } + func Test_Function_Declaration_With_Args(t *testing.T) { source := `package test func foo(x int) {} @@ -54,6 +55,7 @@ func Test_Function_Declaration_With_Args(t *testing.T) { void bar(int y) {} ` Validate(source, expected, t) } + func Test_Const_String_Declaration(t *testing.T) { source := `package test const foo string = "bar" @@ -63,6 +65,18 @@ func Test_Const_String_Declaration(t *testing.T) { ` Validate(source, expected, t) } + + +func Test_Var_String_Declaration(t *testing.T) { + source := `package test + var client wifi.Client + ` + expected := ` + WiFiClient client; + ` + Validate(source, expected, t) +} + func Test_Function_With_Const_String_Declaration(t *testing.T) { source := `package test func foo() { @@ -327,6 +341,32 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) { Validate(source, expected, t) } +func Test_IfStmt_With_Else(t *testing.T) { + source := `package test + const maxX = 1 + func Setup() error {} + func Loop() error { + if x == maxX { + serial.Println("1") + } else { + serial.Println("2") + } + } +` + expected := ` + const maxX = 1; + void setup() {} + void loop() { + if (x == maxX) { + Serial.println("1"); + } else { + Serial.println("2"); + } + } +` + Validate(source, expected, t) +} + func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) { source := `package test func Setup() error {} @@ -404,4 +444,38 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) { void loop() {} ` Validate(source, expected, t) +} + +func Test_WiFiWebClient(t *testing.T) { + source := `package test + import wifi "github.com/andygeiss/esp32/api/controller/wifi" + var client wifi.Client + func Setup() error {} + func Loop() error { + serial.Print("Connecting to ") + serial.Println(host) + serial.Print(" ...") + if (client.Connect(host, 443)) { + serial.Println(" Connected!") + return nil + } else { + serial.Println(" Failed!") + } + return nil + } +` + expected := `#include + WiFiClient client; + voidsetup(){} + voidloop(){ + Serial.print("Connecting to"); + Serial.println(host); + Serial.print(" ..."); + if(){ + Serial.println(" Connected!"); + } else { + Serial.println(" Failed!"); + } + }` + Validate(source, expected, t) } \ No newline at end of file