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

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

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

@ -14,13 +14,14 @@ const (
)
func main() {
mode, source, target := getFlags()
checkFlagsAreValid(source, target)
safeTranspile(mode, source, target)
mode, source, target, header_file := getFlags()
checkFlagsAreValid(source)
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")
header_file := flag.String("h", "", "Write headers to file also")
flag.Parse()
source := flag.Arg(0)
@ -31,10 +32,10 @@ func getFlags() (int, string, string) {
mode = PYTHON_MODE
}
return mode, source, target
return mode, source, target, *header_file
}
func checkFlagsAreValid(source, target string) {
func checkFlagsAreValid(source string) {
if source == "" {
printUsage()
os.Exit(1)
@ -50,7 +51,7 @@ func printUsage() {
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.
in, err := os.Open(source)
if err != nil {
@ -63,16 +64,27 @@ func safeTranspile(mode int, source, target string) {
if target != "" {
// Create the Arduino sketch file.
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 {
fmt.Fprintf(os.Stderr, "Arduino sketch file [%s] could not be opened! %v", target, err)
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 {
case CPP_MODE:
// Transpiles the Golang source into Arduino sketch.
service := transpile.NewService(in, out)
service.SetHeaderWriter(header_f)
if err := service.Start(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)

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

@ -15,6 +15,7 @@ import (
// Service specifies the api logic of transforming a source code format into another target format.
type Service interface {
Start() error
SetHeaderWriter(io.Writer) error
}
const (
@ -34,8 +35,9 @@ var (
// defaultService specifies the api logic of transforming a source code format into another target format.
type defaultService struct {
in io.Reader
out io.Writer
in io.Reader
out io.Writer
header io.Writer
}
// 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 ...
func (s *defaultService) Start() error {
if s.in == nil {
@ -89,8 +100,8 @@ func (s *defaultService) Start() error {
}
}
printFunctionDeclarations(s.out)
printGoHelperDeclarations(s.out)
s.printFunctionDeclarations()
s.printGoHelperDeclarations()
// Print the ordered result.
for i := 0; i < count; i++ {
@ -106,38 +117,41 @@ func (s *defaultService) Start() error {
return nil
}
func addFunctionDeclaration(f string) {
dlock.Lock()
defer dlock.Unlock()
funcDeclarations = append(funcDeclarations, f)
}
func printFunctionDeclarations(out io.Writer) {
func (s *defaultService) printFunctionDeclarations() {
dlock.Lock()
defer dlock.Unlock()
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 addGoHelperDeclaration(f string) {
dlock.Lock()
defer dlock.Unlock()
helpersForDeclarations = append(helpersForDeclarations, f)
}
func printGoHelperDeclarations(out io.Writer) {
func (s *defaultService) printGoHelperDeclarations() {
dlock.Lock()
defer dlock.Unlock()
for _, f := range helpersForDeclarations {
helper := fmt.Sprintf(`void %v%v(void*) { %v(); vTaskDelete(NULL); }`,
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 {