if with else added
Этот коммит содержится в:
родитель
b707454bb1
коммит
eb79c94690
3 изменённых файлов: 89 добавлений и 4 удалений
|
@ -32,6 +32,7 @@ var (
|
||||||
"serial.Println": "Serial.println",
|
"serial.Println": "Serial.println",
|
||||||
"timer.Delay": "delay",
|
"timer.Delay": "delay",
|
||||||
"wifi": "WiFi",
|
"wifi": "WiFi",
|
||||||
|
"wifi.Client": "WiFiClient",
|
||||||
"wifi.Begin": "WiFi.begin",
|
"wifi.Begin": "WiFi.begin",
|
||||||
"wifi.BeginEncrypted": "WiFi.begin",
|
"wifi.BeginEncrypted": "WiFi.begin",
|
||||||
"wifi.BSSID": "WiFi.BSSID",
|
"wifi.BSSID": "WiFi.BSSID",
|
||||||
|
|
|
@ -74,7 +74,7 @@ func (w *Worker) Start() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Print the AST.
|
// Print the AST.
|
||||||
//ast.Fprint(os.Stderr, fset, file, nil)
|
// ast.Fprint(os.Stderr, fset, file, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,6 +259,8 @@ func handleGenDecl(decl ast.Decl) string {
|
||||||
switch gd.Tok {
|
switch gd.Tok {
|
||||||
case token.CONST:
|
case token.CONST:
|
||||||
code += "const "
|
code += "const "
|
||||||
|
case token.VAR:
|
||||||
|
code += ""
|
||||||
}
|
}
|
||||||
code += handleSpecs(gd.Specs)
|
code += handleSpecs(gd.Specs)
|
||||||
return code
|
return code
|
||||||
|
@ -277,9 +279,13 @@ func handleIdent(expr ast.Expr) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleIfStmt(stmt *ast.IfStmt) string {
|
func handleIfStmt(stmt *ast.IfStmt) string {
|
||||||
cond := handleBinaryExpr(stmt.Cond)
|
cond := handleExpr(stmt.Cond)
|
||||||
body := handleBlockStmt(stmt.Body)
|
body := handleBlockStmt(stmt.Body)
|
||||||
code := fmt.Sprintf(`if (%s) { %s }`, cond, 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
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,8 +386,10 @@ func handleValueSpec(spec ast.Spec) string {
|
||||||
code += handleValueSpecType(s.Type)
|
code += handleValueSpecType(s.Type)
|
||||||
code += " "
|
code += " "
|
||||||
code += handleValueSpecNames(s.Names)
|
code += handleValueSpecNames(s.Names)
|
||||||
code += " = "
|
if s.Values != nil {
|
||||||
code += handleValueSpecValues(s.Values)
|
code += " = "
|
||||||
|
code += handleValueSpecValues(s.Values)
|
||||||
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -396,6 +404,8 @@ func handleValueSpecNames(names []*ast.Ident) string {
|
||||||
func handleValueSpecType(expr ast.Expr) string {
|
func handleValueSpecType(expr ast.Expr) string {
|
||||||
code := ""
|
code := ""
|
||||||
switch t := expr.(type) {
|
switch t := expr.(type) {
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
code += handleSelectorExpr(t)
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
code += handleIdent(t)
|
code += handleIdent(t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,7 @@ func Test_Function_Declaration(t *testing.T) {
|
||||||
void bar() {} `
|
void bar() {} `
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Function_Declaration_With_Args(t *testing.T) {
|
func Test_Function_Declaration_With_Args(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo(x int) {}
|
func foo(x int) {}
|
||||||
|
@ -54,6 +55,7 @@ func Test_Function_Declaration_With_Args(t *testing.T) {
|
||||||
void bar(int y) {} `
|
void bar(int y) {} `
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Const_String_Declaration(t *testing.T) {
|
func Test_Const_String_Declaration(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
const foo string = "bar"
|
const foo string = "bar"
|
||||||
|
@ -63,6 +65,18 @@ func Test_Const_String_Declaration(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, 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) {
|
func Test_Function_With_Const_String_Declaration(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
|
@ -327,6 +341,32 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
|
||||||
Validate(source, expected, 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) {
|
func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func Setup() error {}
|
func Setup() error {}
|
||||||
|
@ -405,3 +445,37 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
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)
|
||||||
|
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче