if with else added
Этот коммит содержится в:
родитель
b707454bb1
коммит
eb79c94690
3 изменённых файлов: 89 добавлений и 4 удалений
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 <WiFi.h>
|
||||
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)
|
||||
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче