Python: реализован инициализированный массив

Этот коммит содержится в:
Softonik 2022-12-08 04:53:07 +03:00 коммит произвёл Nikolay Kopitonenko
родитель c9d3b90caa
коммит 7414333f00
2 изменённых файлов: 32 добавлений и 3 удалений

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

@ -136,8 +136,19 @@ func handleBasicLit(bl *ast.BasicLit) string {
return bl.Value
}
func handleCompositeLit(bl *ast.CompositeLit) string {
return "[]"
func handleCompositeLit(cl *ast.CompositeLit) string {
code := "["
switch cl.Type.(type) {
case *ast.ArrayType:
args := make([]string, 0)
for _, arg := range cl.Elts {
args = append(args, handleExpr(arg))
}
code += strings.Join(args, ",")
}
code += "]"
return code
}
func handleUnaryExpr(expr *ast.UnaryExpr) string {
@ -210,6 +221,7 @@ func handleDeclStmt(stmt *ast.DeclStmt) string {
func handleExpr(expr ast.Expr) string {
code := ""
// spew.Dump(expr)
switch e := expr.(type) {
case *ast.BasicLit:
code += handleBasicLit(e)

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

@ -98,7 +98,7 @@ main()
Compare(source, expected)
})
It("Array assignment", func() {
It("Empty Array assignment", func() {
source := `package test
import "skidl"
@ -117,6 +117,23 @@ main()
Compare(source, expected)
})
It("Inited array assignment", func() {
source := `package test
import "skidl"
func main() {
a := []any{1, 2, x}
}
`
expected := `from skidl import *
def main():
a = [1,2,x]
main()
`
Compare(source, expected)
})
It("Array append", func() {
source := `package test
import "skidl"