Сценарий: Последовательность блоков: константы
Этот коммит содержится в:
родитель
0516d8178a
коммит
8e3a1a3d7a
5 изменённых файлов: 84 добавлений и 7 удалений
41
pkg/service/const.go
Обычный файл
41
pkg/service/const.go
Обычный файл
|
@ -0,0 +1,41 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import "go/ast"
|
||||||
|
|
||||||
|
var (
|
||||||
|
constDeclarations []*Const
|
||||||
|
)
|
||||||
|
|
||||||
|
type Const struct {
|
||||||
|
s *ast.ValueSpec
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConst(s *ast.ValueSpec) *Const {
|
||||||
|
return &Const{s: s}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Const) String() (code string) {
|
||||||
|
// return c.s.Names[0].Name
|
||||||
|
return c.generate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Const) generate() (code string) {
|
||||||
|
code += "const "
|
||||||
|
code += handleValueSpec(c.s)
|
||||||
|
code += ";"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConstSpecs(specs []ast.Spec) string {
|
||||||
|
for _, spec := range specs {
|
||||||
|
switch s := spec.(type) {
|
||||||
|
case *ast.ValueSpec:
|
||||||
|
addConstant(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
func addConstant(s *ast.ValueSpec) {
|
||||||
|
c := NewConst(s)
|
||||||
|
constDeclarations = append(constDeclarations, c)
|
||||||
|
}
|
|
@ -345,4 +345,19 @@ return new Device();
|
||||||
void main() {
|
void main() {
|
||||||
dev1=NewDevice();
|
dev1=NewDevice();
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Сценарий: Последовательность блоков: константы, типы, абстрактные классы, классы, переменные, функции
|
||||||
|
* Исходник:
|
||||||
|
```
|
||||||
|
package test
|
||||||
|
|
||||||
|
type Mera int
|
||||||
|
|
||||||
|
const c1 = 4
|
||||||
|
```
|
||||||
|
* Результат:
|
||||||
|
```
|
||||||
|
const int c1 = 4;
|
||||||
|
typedef int Mera;
|
||||||
```
|
```
|
||||||
|
|
|
@ -86,6 +86,7 @@ func (s *defaultService) Start() error {
|
||||||
dst[i] = make(chan string, 1)
|
dst[i] = make(chan string, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constDeclarations = nil
|
||||||
classDeclarations = nil
|
classDeclarations = nil
|
||||||
funcDeclarations = nil
|
funcDeclarations = nil
|
||||||
helpersForDeclarations = nil
|
helpersForDeclarations = nil
|
||||||
|
@ -104,6 +105,7 @@ func (s *defaultService) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
s.printIncludeHeaders()
|
s.printIncludeHeaders()
|
||||||
|
s.printConstDeclarations()
|
||||||
s.printClassDeclarations()
|
s.printClassDeclarations()
|
||||||
s.printFunctionDeclarations()
|
s.printFunctionDeclarations()
|
||||||
s.printGoHelperDeclarations()
|
s.printGoHelperDeclarations()
|
||||||
|
@ -130,6 +132,19 @@ func (s *defaultService) printIncludeHeaders() {
|
||||||
h := "#include <Arduino.h>\n\n"
|
h := "#include <Arduino.h>\n\n"
|
||||||
s.out.Write([]byte(h))
|
s.out.Write([]byte(h))
|
||||||
}
|
}
|
||||||
|
func (s *defaultService) printConstDeclarations() {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
for _, c := range constDeclarations {
|
||||||
|
d := c.String()
|
||||||
|
s.out.Write([]byte(d + "\n"))
|
||||||
|
if s.header != nil {
|
||||||
|
s.header.Write([]byte(d + "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.out.Write([]byte("\n"))
|
||||||
|
}
|
||||||
func (s *defaultService) printClassDeclarations() {
|
func (s *defaultService) printClassDeclarations() {
|
||||||
dlock.Lock()
|
dlock.Lock()
|
||||||
defer dlock.Unlock()
|
defer dlock.Unlock()
|
||||||
|
|
|
@ -80,10 +80,11 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
const std::string foo = "bar";
|
||||||
|
|
||||||
void foo();
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
const std::string foo = "bar";
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
Compare(source, expected)
|
Compare(source, expected)
|
||||||
|
@ -396,10 +397,11 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
const int maxX = 1;
|
||||||
|
|
||||||
void setup();
|
void setup();
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
const int maxX = 1;
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x == maxX) {
|
if (x == maxX) {
|
||||||
|
@ -423,10 +425,11 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
const int maxX = 1;
|
||||||
|
|
||||||
void setup();
|
void setup();
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
const int maxX = 1;
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x == maxX) {
|
if (x == maxX) {
|
||||||
|
|
|
@ -22,11 +22,14 @@ func handleGenDecl(decl ast.Decl) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSpecs(specs []ast.Spec) (code string) {
|
func handleSpecs(specs []ast.Spec) (code string) {
|
||||||
for _, spec := range specs {
|
|
||||||
if InConstBlock {
|
if InConstBlock {
|
||||||
code += "const "
|
return handleConstSpecs(specs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return handleSpecsVariables(specs)
|
||||||
|
}
|
||||||
|
func handleSpecsVariables(specs []ast.Spec) (code string) {
|
||||||
|
for _, spec := range specs {
|
||||||
switch s := spec.(type) {
|
switch s := spec.(type) {
|
||||||
case *ast.ImportSpec:
|
case *ast.ImportSpec:
|
||||||
code += handleImportSpec(spec)
|
code += handleImportSpec(spec)
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче