ifstmt support added
Этот коммит содержится в:
родитель
abb26acefe
коммит
15babc8bf5
2 изменённых файлов: 98 добавлений и 20 удалений
|
@ -7,6 +7,7 @@ import (
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -74,7 +75,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,11 +118,19 @@ func handleBinaryExpr(expr ast.Expr) string {
|
||||||
e := expr.(*ast.BinaryExpr)
|
e := expr.(*ast.BinaryExpr)
|
||||||
code := ""
|
code := ""
|
||||||
switch x := e.X.(type) {
|
switch x := e.X.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
code += handleBasicLit(x)
|
||||||
|
case *ast.CallExpr:
|
||||||
|
code += handleCallExpr(x)
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
code += handleIdent(x)
|
code += handleIdent(x)
|
||||||
}
|
}
|
||||||
code += e.Op.String()
|
code += e.Op.String()
|
||||||
switch y := e.Y.(type) {
|
switch y := e.Y.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
code += handleBasicLit(y)
|
||||||
|
case *ast.CallExpr:
|
||||||
|
code += handleCallExpr(y)
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
code += handleIdent(y)
|
code += handleIdent(y)
|
||||||
}
|
}
|
||||||
|
@ -198,7 +207,7 @@ func handleFuncDecl(decl ast.Decl) string {
|
||||||
code += "("
|
code += "("
|
||||||
code += handleFuncDeclParams(fd.Type)
|
code += handleFuncDeclParams(fd.Type)
|
||||||
code += ") {"
|
code += ") {"
|
||||||
code += handleFuncDeclBody(fd.Body)
|
code += handleBlockStmt(fd.Body)
|
||||||
code += "}"
|
code += "}"
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
@ -223,7 +232,7 @@ func handleFuncDeclParams(t *ast.FuncType) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFuncDeclBody(body *ast.BlockStmt) string {
|
func handleBlockStmt(body *ast.BlockStmt) string {
|
||||||
code := ""
|
code := ""
|
||||||
if body == nil {
|
if body == nil {
|
||||||
return code
|
return code
|
||||||
|
@ -233,6 +242,8 @@ func handleFuncDeclBody(body *ast.BlockStmt) string {
|
||||||
case *ast.AssignStmt:
|
case *ast.AssignStmt:
|
||||||
code += handleAssignStmt(s)
|
code += handleAssignStmt(s)
|
||||||
code += ";"
|
code += ";"
|
||||||
|
case *ast.IfStmt:
|
||||||
|
code += handleIfStmt(s)
|
||||||
case *ast.DeclStmt:
|
case *ast.DeclStmt:
|
||||||
code += handleDeclStmt(s)
|
code += handleDeclStmt(s)
|
||||||
case *ast.ExprStmt:
|
case *ast.ExprStmt:
|
||||||
|
@ -243,6 +254,13 @@ func handleFuncDeclBody(body *ast.BlockStmt) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleIfStmt(stmt *ast.IfStmt) string {
|
||||||
|
cond := handleBinaryExpr(stmt.Cond)
|
||||||
|
body := handleBlockStmt(stmt.Body)
|
||||||
|
code := fmt.Sprintf(`if (%s) { %s }`, cond, body)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
func handleFuncDeclName(ident *ast.Ident) string {
|
func handleFuncDeclName(ident *ast.Ident) string {
|
||||||
code := ""
|
code := ""
|
||||||
if ident == nil {
|
if ident == nil {
|
||||||
|
|
|
@ -29,14 +29,14 @@ func Validate(source, expected string, t *testing.T) {
|
||||||
Assert(t, tcode, IsEqual(texpected))
|
Assert(t, tcode, IsEqual(texpected))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyPackage(t *testing.T) {
|
func Test_Empty_Package(t *testing.T) {
|
||||||
source := `package test`
|
source := `package test`
|
||||||
expected := `void loop(){}
|
expected := `void loop(){}
|
||||||
void setup() {} `
|
void setup() {} `
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionDeclaration(t *testing.T) {
|
func Test_Function_Declaration(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {}
|
func foo() {}
|
||||||
func bar() {}
|
func bar() {}
|
||||||
|
@ -45,7 +45,7 @@ func TestFunctionDeclaration(t *testing.T) {
|
||||||
void bar() {} `
|
void bar() {} `
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionDeclarationWithArgs(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) {}
|
||||||
func bar(y int) {}
|
func bar(y int) {}
|
||||||
|
@ -54,7 +54,7 @@ func TestFunctionDeclarationWithArgs(t *testing.T) {
|
||||||
void bar(int y) {} `
|
void bar(int y) {} `
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestConstStringDeclaration(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,7 +63,7 @@ func TestConstStringDeclaration(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithConstStringDeclaration(t *testing.T) {
|
func Test_Function_With_Const_String_Declaration(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
const foo string = "bar"
|
const foo string = "bar"
|
||||||
|
@ -76,7 +76,7 @@ func TestFunctionWithConstStringDeclaration(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithVarStringDeclaration(t *testing.T) {
|
func Test_Function_With_Var_String_Declaration(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
var foo string = "bar"
|
var foo string = "bar"
|
||||||
|
@ -89,7 +89,7 @@ func TestFunctionWithVarStringDeclaration(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithFunctionCall(t *testing.T) {
|
func Test_Function_With_Function_Call(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
bar()
|
bar()
|
||||||
|
@ -102,7 +102,7 @@ func TestFunctionWithFunctionCall(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithFunctionCallWithArgs(t *testing.T) {
|
func Test_Function_With_Function_Call_With_Args(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
bar(1,2,3)
|
bar(1,2,3)
|
||||||
|
@ -115,7 +115,7 @@ func TestFunctionWithFunctionCallWithArgs(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithFunctionCallWithString(t *testing.T) {
|
func Test_Function_With_Function_Call_With_String(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
bar("foo")
|
bar("foo")
|
||||||
|
@ -129,7 +129,7 @@ func TestFunctionWithFunctionCallWithString(t *testing.T) {
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionWithPackageFunctionCall(t *testing.T) {
|
func Test_Function_With_Package_Function_Call(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
foo.Bar(1,"2")
|
foo.Bar(1,"2")
|
||||||
|
@ -142,7 +142,7 @@ func TestFunctionWithPackageFunctionCall(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithAssignments(t *testing.T) {
|
func Test_Function_With_Assignments(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
x = 1
|
x = 1
|
||||||
|
@ -159,7 +159,7 @@ func TestFunctionWithAssignments(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithPackageSelectorAssignments(t *testing.T) {
|
func Test_Function_With_Package_Selector_Assignments(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
x = bar()
|
x = bar()
|
||||||
|
@ -177,7 +177,7 @@ func TestFunctionWithPackageSelectorAssignments(t *testing.T) {
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionIdentMapping(t *testing.T) {
|
func Test_Function_Ident_Mapping(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
serial.Begin()
|
serial.Begin()
|
||||||
|
@ -190,7 +190,7 @@ func TestFunctionIdentMapping(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
func TestFunctionWithIdentParam(t *testing.T) {
|
func Test_Function_With_Ident_Param(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
foo.Bar(1,"2",digital.Low)
|
foo.Bar(1,"2",digital.Low)
|
||||||
|
@ -204,7 +204,7 @@ func TestFunctionWithIdentParam(t *testing.T) {
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFunctionWithFunctionParam(t *testing.T) {
|
func Test_Function_With_Function_Param(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
func foo() {
|
func foo() {
|
||||||
serial.Println(wifi.LocalIP())
|
serial.Println(wifi.LocalIP())
|
||||||
|
@ -218,7 +218,7 @@ func TestFunctionWithFunctionParam(t *testing.T) {
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPackageImport(t *testing.T) {
|
func Test_Package_Import(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
import "github.com/andygeiss/esp32-mqtt/api/controller"
|
import "github.com/andygeiss/esp32-mqtt/api/controller"
|
||||||
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
|
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
|
||||||
|
@ -231,7 +231,7 @@ func TestPackageImport(t *testing.T) {
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPackageImport_ButIgnoreController(t *testing.T) {
|
func Test_Package_Import_But_Ignore_Controller(t *testing.T) {
|
||||||
source := `package test
|
source := `package test
|
||||||
import controller "github.com/andygeiss/esp32-controller"
|
import controller "github.com/andygeiss/esp32-controller"
|
||||||
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
|
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
|
||||||
|
@ -243,3 +243,63 @@ func TestPackageImport_ButIgnoreController(t *testing.T) {
|
||||||
`
|
`
|
||||||
Validate(source, expected, t)
|
Validate(source, expected, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_BlockStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
|
||||||
|
source := `package test
|
||||||
|
func Setup() error {}
|
||||||
|
func Loop() error {
|
||||||
|
if 1 == 1 {
|
||||||
|
serial.Println("1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
void setup() {}
|
||||||
|
void loop() {
|
||||||
|
if (1 == 1) {
|
||||||
|
Serial.println("1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
Validate(source, expected, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_BlockStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
|
||||||
|
source := `package test
|
||||||
|
func Setup() error {}
|
||||||
|
func Loop() error {
|
||||||
|
if x == 1 {
|
||||||
|
serial.Println("1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
void setup() {}
|
||||||
|
void loop() {
|
||||||
|
if (x == 1) {
|
||||||
|
Serial.println("1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
Validate(source, expected, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_BlockStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
|
||||||
|
source := `package test
|
||||||
|
func Setup() error {}
|
||||||
|
func Loop() error {
|
||||||
|
if x() == 1 {
|
||||||
|
serial.Println("1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
void setup() {}
|
||||||
|
void loop() {
|
||||||
|
if (x() == 1) {
|
||||||
|
Serial.println("1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
Validate(source, expected, t)
|
||||||
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче