Этот коммит содержится в:
andygeiss 2018-05-01 18:14:46 +02:00
родитель a1450b8c80
коммит b707454bb1
3 изменённых файлов: 49 добавлений и 1 удалений

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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)

Просмотреть файл

@ -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 <WiFi.h>
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)
}