Commit 032cfacc authored by mohamad.alturky's avatar mohamad.alturky

adding transpiler functionality

parent ecfe9d6e
package Languages
import (
Types "donut/types"
)
type Bridge struct {
TemplateCodeHandler Types.TextToTextConverter
InjectCodeHandler Types.TextToTextConverter
MainFunctionHandler Types.TextAppendUtil
ImportsHandler Types.TextAppendUtil
}
package main package main
import ( import (
Tokenizer "donut/tokenization" FileManagement "donut/fileManagement"
Transpilation "donut/transpilation"
"github.com/TwiN/go-color"
) )
func main() { func main() {
...@@ -22,6 +21,7 @@ func main() { ...@@ -22,6 +21,7 @@ func main() {
code code
}} }}
text text
@exec @exec
{{ 11111111 {{ 11111111
...@@ -32,113 +32,161 @@ func main() { ...@@ -32,113 +32,161 @@ func main() {
}} }}
code code
}} }}
}}
`
new := ""
startingIdx := 0
for {
if startingIdx == len(template) {
break
}
res := Tokenizer.ConvertNestingToConcatenation(startingIdx, template, "exec", "template")
startingIdx = res.TerminatedAt
new = new + res.Template
}
// println("------------------")
// println(template2.Template)
// println("------------------")
// println(template2.TerminatedAt)
// println("------------------")
// println(len(template))
println(color.Colorize(color.Blue, `
}}
|‾‾‾‾‾‾‾‾\ |‾‾‾‾‾‾‾‾‾| |‾‾‾‾‾\ |‾‾| |‾‾| |‾‾| |‾‾‾‾‾‾‾‾‾‾‾‾| @define {{
| |‾‾‾\ \ | |‾‾‾| | | |\ \ | | | | | | ‾‾‾‾| |‾‾‾‾ f()===>
| | | | | | | | | | \ \| | | | | | | | }}
| |___/ / | |___| | | | \ | | |___| | | |
|________/ |_________| |__| \____| |_________| |__|
`)) @import {{
println(new) "fmt"
// println(color.Colorize(color.Red, "This is also red")) }}
`
result := Transpilation.BuildTranspiler(template).GetExecutable()
FileManagement.WriteFile("./rs.go", result)
} }
// package main // package main
// // A simple program demonstrating the paginator component from the Bubbles
// // component library.
// import ( // import (
// "fmt" // Tokenizer "donut/tokenization"
// "log"
// "strings"
// "github.com/charmbracelet/bubbles/paginator"
// "github.com/charmbracelet/lipgloss"
// tea "github.com/charmbracelet/bubbletea" // "github.com/TwiN/go-color"
// ) // )
// func newModel() model { // func main() {
// var items []string // template := `
// for i := 1; i < 101; i++ { // @template
// text := fmt.Sprintf("Item %d", i) // {{
// items = append(items, text) // text
// } // @exec
// {{
// p := paginator.New() // code
// p.Type = paginator.Dots
// p.PerPage = 10 // @template
// p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•") // {{
// p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•") // text
// p.SetTotalPages(len(items)) // }}
// return model{ // code
// paginator: p, // }}
// items: items, // text
// } // @exec
// } // {{ 11111111
// code
// type model struct { // @template
// items []string // {{
// paginator paginator.Model // text
// } // }}
// code
// func (m model) Init() tea.Cmd { // }}
// return nil // }}
// } // `
// func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // new := ""
// var cmd tea.Cmd // startingIdx := 0
// switch msg := msg.(type) { // for {
// case tea.KeyMsg: // if startingIdx == len(template) {
// switch msg.String() { // break
// case "q", "esc", "ctrl+c":
// return m, tea.Quit
// } // }
// res := Tokenizer.ConvertNestingToConcatenation(startingIdx, template, "exec", "template")
// startingIdx = res.TerminatedAt
// new = new + res.Template
// } // }
// m.paginator, cmd = m.paginator.Update(msg) // // println("------------------")
// return m, cmd // // println(template2.Template)
// } // // println("------------------")
// // println(template2.TerminatedAt)
// func (m model) View() string { // // println("------------------")
// var b strings.Builder // // println(len(template))
// b.WriteString("\n Paginator Example\n\n")
// start, end := m.paginator.GetSliceBounds(len(m.items)) // println(color.Colorize(color.Blue, `
// for _, item := range m.items[start:end] {
// b.WriteString(" • " + item + "\n\n") // |‾‾‾‾‾‾‾‾\ |‾‾‾‾‾‾‾‾‾| |‾‾‾‾‾\ |‾‾| |‾‾| |‾‾| |‾‾‾‾‾‾‾‾‾‾‾‾|
// } // | |‾‾‾\ \ | |‾‾‾| | | |\ \ | | | | | | ‾‾‾‾| |‾‾‾‾
// b.WriteString(" " + m.paginator.View()) // | | | | | | | | | | \ \| | | | | | | |
// b.WriteString("\n\n h/l ←/→ page • q: quit\n") // | |___/ / | |___| | | | \ | | |___| | | |
// return b.String() // |________/ |_________| |__| \____| |_________| |__|
// `))
// println(new)
// // println(color.Colorize(color.Red, "This is also red"))
// } // }
// func main() { // // package main
// p := tea.NewProgram(newModel())
// if _, err := p.Run(); err != nil { // // // A simple program demonstrating the paginator component from the Bubbles
// log.Fatal(err) // // // component library.
// }
// } // // import (
// // "fmt"
// // "log"
// // "strings"
// // "github.com/charmbracelet/bubbles/paginator"
// // "github.com/charmbracelet/lipgloss"
// // tea "github.com/charmbracelet/bubbletea"
// // )
// // func newModel() model {
// // var items []string
// // for i := 1; i < 101; i++ {
// // text := fmt.Sprintf("Item %d", i)
// // items = append(items, text)
// // }
// // p := paginator.New()
// // p.Type = paginator.Dots
// // p.PerPage = 10
// // p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
// // p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
// // p.SetTotalPages(len(items))
// // return model{
// // paginator: p,
// // items: items,
// // }
// // }
// // type model struct {
// // items []string
// // paginator paginator.Model
// // }
// // func (m model) Init() tea.Cmd {
// // return nil
// // }
// // func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// // var cmd tea.Cmd
// // switch msg := msg.(type) {
// // case tea.KeyMsg:
// // switch msg.String() {
// // case "q", "esc", "ctrl+c":
// // return m, tea.Quit
// // }
// // }
// // m.paginator, cmd = m.paginator.Update(msg)
// // return m, cmd
// // }
// // func (m model) View() string {
// // var b strings.Builder
// // b.WriteString("\n Paginator Example\n\n")
// // start, end := m.paginator.GetSliceBounds(len(m.items))
// // for _, item := range m.items[start:end] {
// // b.WriteString(" • " + item + "\n\n")
// // }
// // b.WriteString(" " + m.paginator.View())
// // b.WriteString("\n\n h/l ←/→ page • q: quit\n")
// // return b.String()
// // }
// // func main() {
// // p := tea.NewProgram(newModel())
// // if _, err := p.Run(); err != nil {
// // log.Fatal(err)
// // }
// // }
package main
import(
"fmt"
)
func main() {
Code := ""
Code = Code + string( `
text
`)
code
Code = Code + string( `
text
`)
code
Code = Code + string( `
text
`)
11111111
code
Code = Code + string( `
text
`)
code
Code = Code + string( `
`)
}
f()===>
...@@ -139,7 +139,7 @@ func GetDirectiveContentWithTerminationIndex(template string, directiveName stri ...@@ -139,7 +139,7 @@ func GetDirectiveContentWithTerminationIndex(template string, directiveName stri
*/ */
directiveFinishedFrom := loopIndex + 2 directiveFinishedFrom := loopIndex + 2
return Types.DirectiveContentSpecification{directiveContent, directiveFinishedFrom} return Types.DirectiveContentSpecification{Content: directiveContent, TerminationIndex: directiveFinishedFrom}
} }
func FindTerminationIndex(template string, startingIndex int) int { func FindTerminationIndex(template string, startingIndex int) int {
......
...@@ -172,5 +172,5 @@ func ConvertNestingToConcatenation(startAt int, template string, executeToken st ...@@ -172,5 +172,5 @@ func ConvertNestingToConcatenation(startAt int, template string, executeToken st
} }
} }
return Types.RecursionResult{answer, terminationIndex} return Types.RecursionResult{Template: answer, TerminatedAt: terminationIndex}
} }
package Transpilation
type ITranspiler interface {
GetExecutable() func()
}
package Transpilation
import Tokenizer "donut/tokenization"
type Transpiler struct {
template string
}
func BuildTranspiler(template string) Transpiler {
return Transpiler{template: template}
}
func (transpiler Transpiler) GetExecutable() string {
importsParsingResult := Tokenizer.ExtractDirectiveContent(transpiler.template, "@import")
transpiler.template = importsParsingResult[1]
println("transpiler.template")
println(transpiler.template)
println("transpiler.template")
importsContent := importsParsingResult[0]
println(importsContent)
defineParsingResult := Tokenizer.ExtractDirectiveContent(transpiler.template, "@define")
transpiler.template = defineParsingResult[1]
defineContent := defineParsingResult[0]
println(defineContent)
templateAfterAdjustingInjectables := Tokenizer.ReplaceDirectiveContent(transpiler.template, "@inject", returnInjectedString)
transpiler.template = templateAfterAdjustingInjectables
println("templateAfterAdjustingInjectables")
println(templateAfterAdjustingInjectables)
println("templateAfterAdjustingInjectables")
resultAfterReplacingNesting := ""
startingIdx := 0
for {
println(startingIdx)
if startingIdx == len(transpiler.template) {
break
}
res := Tokenizer.ConvertNestingToConcatenation(startingIdx, transpiler.template, "exec", "template")
startingIdx = res.TerminatedAt
resultAfterReplacingNesting = resultAfterReplacingNesting + res.Template
}
transpiler.template = resultAfterReplacingNesting
templateSectionsParsed := Tokenizer.ReplaceDirectiveContent(transpiler.template, "@template", returnInjectedtemplate)
transpiler.template = templateSectionsParsed
execSectionsParsed := Tokenizer.ReplaceDirectiveContent(transpiler.template, "@exec", returnCode)
transpiler.template = execSectionsParsed
finalCode := injectImports(importsContent) + injectMain(transpiler.template) + defineContent
return finalCode
}
func returnInjectedString(text string) string {
return "\nCode = Code + string( " + text + " )\n"
}
func returnInjectedtemplate(text string) string {
return "\nCode = Code + string( `" + text + "`)\n"
}
func returnCode(text string) string {
return text
}
func injectImports(imports string) string {
return `
package main
import(
` + imports + `
)
`
}
func injectMain(main string) string {
return `
func main() {
Code := ""
` + main + `
}
`
}
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
"fmt" "fmt"
}} }}
using Go; using Go;
@template {{
}}
public class Main public class Main
{ {
@exec {{ @exec {{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment