Добавлена поддержка записи .h файлов

Этот коммит содержится в:
Softonik 2022-11-19 22:05:39 +03:00 коммит произвёл Nikolay Kopitonenko
родитель d69afaa742
коммит 08dc7bad19
2 изменённых файлов: 57 добавлений и 31 удалений

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

@ -14,13 +14,14 @@ const (
) )
func main() { func main() {
mode, source, target := getFlags() mode, source, target, header_file := getFlags()
checkFlagsAreValid(source, target) checkFlagsAreValid(source)
safeTranspile(mode, source, target) safeTranspile(mode, source, target, header_file)
} }
func getFlags() (int, string, string) { func getFlags() (int, string, string, string) {
pmode := flag.Bool("p", false, "Mode: C++ or Python") pmode := flag.Bool("p", false, "Mode: C++ or Python")
header_file := flag.String("h", "", "Write headers to file also")
flag.Parse() flag.Parse()
source := flag.Arg(0) source := flag.Arg(0)
@ -31,10 +32,10 @@ func getFlags() (int, string, string) {
mode = PYTHON_MODE mode = PYTHON_MODE
} }
return mode, source, target return mode, source, target, *header_file
} }
func checkFlagsAreValid(source, target string) { func checkFlagsAreValid(source string) {
if source == "" { if source == "" {
printUsage() printUsage()
os.Exit(1) os.Exit(1)
@ -50,7 +51,7 @@ func printUsage() {
fmt.Printf("\tgo-tr [-p] controller.go controller.ino\n\n") fmt.Printf("\tgo-tr [-p] controller.go controller.ino\n\n")
} }
func safeTranspile(mode int, source, target string) { func safeTranspile(mode int, source, target, header_file string) {
// Read the Golang source file. // Read the Golang source file.
in, err := os.Open(source) in, err := os.Open(source)
if err != nil { if err != nil {
@ -63,16 +64,27 @@ func safeTranspile(mode int, source, target string) {
if target != "" { if target != "" {
// Create the Arduino sketch file. // Create the Arduino sketch file.
os.Remove(target) os.Remove(target)
out, err = os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0666) out, err = os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0644)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Arduino sketch file [%s] could not be opened! %v", target, err) fmt.Fprintf(os.Stderr, "Arduino sketch file [%s] could not be opened! %v", target, err)
os.Exit(1) os.Exit(1)
} }
} }
var header_f *os.File
if header_file != "" {
os.Remove(header_file)
header_f, err = os.OpenFile(header_file, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Header file [%s] could not be opened! %v", header_file, err)
os.Exit(1)
}
}
switch mode { switch mode {
case CPP_MODE: case CPP_MODE:
// Transpiles the Golang source into Arduino sketch. // Transpiles the Golang source into Arduino sketch.
service := transpile.NewService(in, out) service := transpile.NewService(in, out)
service.SetHeaderWriter(header_f)
if err := service.Start(); err != nil { if err := service.Start(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err) fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1) os.Exit(1)

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

@ -15,6 +15,7 @@ import (
// Service specifies the api logic of transforming a source code format into another target format. // Service specifies the api logic of transforming a source code format into another target format.
type Service interface { type Service interface {
Start() error Start() error
SetHeaderWriter(io.Writer) error
} }
const ( const (
@ -36,6 +37,7 @@ var (
type defaultService struct { type defaultService struct {
in io.Reader in io.Reader
out io.Writer out io.Writer
header io.Writer
} }
// NewService creates a a new transpile and returns its address. // NewService creates a a new transpile and returns its address.
@ -46,6 +48,15 @@ func NewService(in io.Reader, out io.Writer) Service {
} }
} }
func (s *defaultService) SetHeaderWriter(w io.Writer) error {
if w == nil {
return fmt.Errorf("Empty")
}
s.header = w
return nil
}
// Start ... // Start ...
func (s *defaultService) Start() error { func (s *defaultService) Start() error {
if s.in == nil { if s.in == nil {
@ -89,8 +100,8 @@ func (s *defaultService) Start() error {
} }
} }
printFunctionDeclarations(s.out) s.printFunctionDeclarations()
printGoHelperDeclarations(s.out) s.printGoHelperDeclarations()
// Print the ordered result. // Print the ordered result.
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
@ -106,38 +117,41 @@ func (s *defaultService) Start() error {
return nil return nil
} }
func addFunctionDeclaration(f string) { func (s *defaultService) printFunctionDeclarations() {
dlock.Lock()
defer dlock.Unlock()
funcDeclarations = append(funcDeclarations, f)
}
func printFunctionDeclarations(out io.Writer) {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()
for _, f := range funcDeclarations { for _, f := range funcDeclarations {
out.Write([]byte(f + "\n")) s.out.Write([]byte(f + "\n"))
if s.header != nil {
s.header.Write([]byte(f + "\n"))
} }
out.Write([]byte("\n")) }
s.out.Write([]byte("\n"))
} }
func (s *defaultService) printGoHelperDeclarations() {
func addGoHelperDeclaration(f string) {
dlock.Lock()
defer dlock.Unlock()
helpersForDeclarations = append(helpersForDeclarations, f)
}
func printGoHelperDeclarations(out io.Writer) {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()
for _, f := range helpersForDeclarations { for _, f := range helpersForDeclarations {
helper := fmt.Sprintf(`void %v%v(void*) { %v(); vTaskDelete(NULL); }`, helper := fmt.Sprintf(`void %v%v(void*) { %v(); vTaskDelete(NULL); }`,
FunctionGoHelperPrefix, f, f) FunctionGoHelperPrefix, f, f)
out.Write([]byte(helper + "\n")) s.out.Write([]byte(helper + "\n"))
} }
out.Write([]byte("\n")) s.out.Write([]byte("\n"))
}
func addFunctionDeclaration(f string) {
dlock.Lock()
defer dlock.Unlock()
funcDeclarations = append(funcDeclarations, f)
}
func addGoHelperDeclaration(f string) {
dlock.Lock()
defer dlock.Unlock()
helpersForDeclarations = append(helpersForDeclarations, f)
} }
func handleAssignStmt(as *ast.AssignStmt) string { func handleAssignStmt(as *ast.AssignStmt) string {