Replies: 3 comments 1 reply
-
|
All good points! Polishing the emitted Go output is an ongoing process. I'll tackle some of these soon. Curious about the 1.1 MB bump though. I could not reproduce it locally and the prelude doesn't explain it. Are you able to share your Go and Lis projects so I can take a deeper look? |
Beta Was this translation helpful? Give feedback.
-
|
Go: package main
import (
"fmt"
"os"
"sort"
"strconv"
"time"
"go.bug.st/serial"
)
func userChoice(defaultOption int, options ...string) int {
for {
for i, option := range options {
fmt.Print(i + 1)
fmt.Print(" ")
fmt.Print(option)
if i == defaultOption {
fmt.Print(" [default]")
}
fmt.Println()
}
input := 0
_, err := fmt.Scanln(&input)
if err != nil {
if defaultOption >= 0 {
return defaultOption
}
continue
}
if input < 1 || input > len(options) {
continue
}
return input - 1
}
}
func configure(defaultPort string) string {
ports, err := serial.GetPortsList()
if err != nil {
fmt.Print("Error ")
fmt.Println(err)
return ""
}
if len(ports) == 0 {
fmt.Println("No serial port found")
return ""
}
sort.Slice(ports, func(a, b int) bool {
return ports[a] < ports[b]
})
defaultOption := -1
for i, port := range ports {
if port == defaultPort {
defaultOption = i
break
}
}
choice := userChoice(defaultOption, ports...)
return ports[choice]
}
func determineFile(prefix string) string {
_, err := os.Stat(prefix)
if err != nil {
return prefix
}
i := 2
for {
name := prefix + strconv.Itoa(i)
_, err := os.Stat(name)
if err != nil {
return name
}
i += 1
}
}
func receive(portName string) {
name := determineFile("received")
fmt.Print("Reading '")
fmt.Print(name)
fmt.Println("'...")
file, err := os.Create(name)
if err != nil {
fmt.Print("Error opening file '")
fmt.Print(name)
fmt.Println("'")
return
}
defer file.Close()
mode := &serial.Mode {
BaudRate: 4800,
Parity: serial.NoParity,
DataBits: 8,
StopBits: serial.OneStopBit,
}
port, err := serial.Open(portName, mode)
if err != nil {
fmt.Print("Failed to open port ")
fmt.Println(portName)
return
}
defer port.Close()
if err := port.ResetInputBuffer(); err != nil {
fmt.Print("Failed to clear input buffer for port ")
fmt.Println(portName)
return
}
if err := port.SetReadTimeout(60 * time.Second); err != nil {
fmt.Print("Failed to set timeout for port ")
fmt.Println(portName)
return
}
buffer := make([]byte, 1024)
readOverall := 0
for {
read, err := port.Read(buffer)
if err != nil {
fmt.Print("Failed to read port ")
fmt.Println(portName)
return
}
if read == 0 {
break
}
if readOverall == 0 {
if err := port.SetReadTimeout(time.Second); err != nil {
fmt.Print("Failed to set timeout for port ")
fmt.Println(portName)
return
}
}
written, err := file.Write(buffer[:read])
if written != read {
fmt.Printf("wrote only %v though it should write %v\n", written, read)
return
}
if err != nil {
fmt.Print("Error writing file '")
fmt.Print(name)
fmt.Println("'")
return
}
readOverall += read
}
fmt.Printf("Received %v bytes\n", readOverall)
}
func main() {
port := ""
ports, err := serial.GetPortsList()
if err == nil && len(ports) > 0 {
port = ports[0]
}
for {
fmt.Println()
fmt.Print("Port: ")
defaultOption := 0
if len(port) > 0 {
defaultOption = 1
fmt.Println(port)
} else {
fmt.Println("?")
}
choice := userChoice(defaultOption, "Configure", "Receive File", "Quit")
switch choice {
case 0:
p := configure(port)
if len(p) > 0 {
port = p
}
continue
case 1:
if len(port) == 0 {
port = configure(port)
}
if len(port) > 0 {
receive(port)
}
continue
default:
fmt.Println("Bye!")
return
}
}
}Lisette: |
Beta Was this translation helpful? Give feedback.
-
|
@tmssngr Thanks for following up. Re: cleaner emitted Go - All but one of the points you raised are now cleaner via #772 and #780 and will be out with Re: +1.1 MB binary size - Building your projects on my end I'm seeing:
So it appears you're comparing a stripped binary (your Go build) to an unstripped binary (your Lis build). To strip debug info in Lis: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This lisette code
is transpiled to this Go code
Not really bad, but still leaves room for improvements, if the user needs to debug with a Go debugger:
loop_1:is not neededcontinue loop_1can be replaced withcontinue, the second one can be removedsubject_2could be replaced byresult_5Resulttype to immediately use the parts of it could be inlined -> improved performance (though this does not matter for this particular code)My tiny test application which I've written in Go (215 lines) is 2.1MB large. I've reimplemented it in Lisette: the produced go file is 303 lines long and the binary is 3.2MB large.
Beta Was this translation helpful? Give feedback.
All reactions