@@ -9,21 +9,31 @@ import (
99 "github.com/monstermichl/typeshell/transpiler"
1010)
1111
12+ type helperName = string
13+
14+ const (
15+ sliceAssignmentHelper helperName = "_sah" // Slice assignment
16+ sliceCopyHelper helperName = "_sch" // Slice copy
17+ structAssignmentHelper helperName = "_stah" // Struct assignment
18+ stringSubscriptHelper helperName = "_stsh" // String subscript
19+ )
20+
1221type funcInfo struct {
1322 name string
1423}
1524
1625type converter struct {
17- interpreter string
18- startCode []string
19- code []string
20- varCounter int
21- forCounter int
22- funcs []funcInfo
23- funcCounter int
24- sliceAssignmentHelperRequired bool
25- sliceCopyHelperRequired bool
26- stringSubscriptHelperRequired bool
26+ interpreter string
27+ startCode []string
28+ code []string
29+ varCounter int
30+ forCounter int
31+ funcs []funcInfo
32+ funcCounter int
33+ sliceAssignmentHelperRequired bool
34+ structAssignmentHelperRequired bool
35+ sliceCopyHelperRequired bool
36+ stringSubscriptHelperRequired bool
2737}
2838
2939func New () * converter {
@@ -62,7 +72,7 @@ func (c *converter) ProgramEnd() error {
6272 // $2: Assignment index
6373 // $3: Assignment value
6474 // $4: Default value
65- c .addHelper ("slice assignment" , "_sah" ,
75+ c .addHelper ("slice assignment" , sliceAssignmentHelper ,
6676 "local _i=${2}" ,
6777 fmt .Sprintf (`local _l=%s` , c .sliceLenString ("${1}" )),
6878 `for ((_c=${_l};_c<${_i};_c++)); do` ,
@@ -73,7 +83,7 @@ func (c *converter) ProgramEnd() error {
7383 }
7484
7585 if c .sliceCopyHelperRequired {
76- c .addHelper ("slice copy" , "_sch" ,
86+ c .addHelper ("slice copy" , sliceCopyHelper ,
7787 "local _i=0" ,
7888 fmt .Sprintf (`local _l=%s` , c .sliceLenString ("${2}" )),
7989 "local _n=$(eval \" echo \\ ${${1}}\" )" ,
@@ -85,8 +95,17 @@ func (c *converter) ProgramEnd() error {
8595 )
8696 }
8797
98+ if c .structAssignmentHelperRequired {
99+ // $1: Slice name
100+ // $2: Assignment field
101+ // $3: Assignment value
102+ c .addHelper ("struct assignment" , structAssignmentHelper ,
103+ c .sliceAssignmentString ("${1}" , "${2}" , "${3}" , false ),
104+ )
105+ }
106+
88107 if c .stringSubscriptHelperRequired {
89- c .addHelper ("substring" , "_ssh" ,
108+ c .addHelper ("substring" , stringSubscriptHelper ,
90109 `_ls=$((${2}))` ,
91110 `_ll=$(((${3}-${2})+1))` ,
92111 `_ret="${1:${_ls}:${_ll}}"` ,
@@ -106,7 +125,13 @@ func (c *converter) VarAssignment(name string, value string, global bool) error
106125
107126func (c * converter ) SliceAssignment (name string , index string , value string , defaultValue string , global bool ) error {
108127 c .sliceAssignmentHelperRequired = true
109- c .addLine (fmt .Sprintf (`_sah %s %s "%s" "%s"` , c .varEvaluationString (name , global ), index , value , defaultValue ))
128+ c .callFunc (sliceAssignmentHelper , c .varEvaluationString (name , global ), index , value , defaultValue )
129+ return nil
130+ }
131+
132+ func (c * converter ) StructAssignment (name string , field string , value string , global bool ) error {
133+ c .structAssignmentHelperRequired = true
134+ c .callFunc (structAssignmentHelper , c .varEvaluationString (name , global ), field , value )
110135 return nil
111136}
112137
@@ -418,7 +443,7 @@ func (c *converter) StructDefinition(values []transpiler.StructValue, valueUsed
418443func (c * converter ) StringSubscript (value string , startIndex string , endIndex string , valueUsed bool ) (string , error ) {
419444 helper := c .nextHelperVar ()
420445
421- c .addLine ( fmt . Sprintf ( `_ssh "%s" %s %s` , value , startIndex , endIndex ) )
446+ c .callFunc ( stringSubscriptHelper , value , startIndex , endIndex )
422447 c .VarAssignment (helper , c .varEvaluationString ("_ret" , true ), false ) // https://www.baeldung.com/linux/bash-substring#1-using-thecut-command
423448 c .stringSubscriptHelperRequired = true
424449
@@ -518,7 +543,7 @@ func (c *converter) Input(prompt string, valueUsed bool) (string, error) {
518543func (c * converter ) Copy (destination string , source string , valueUsed bool , global bool ) (string , error ) {
519544 destination = c .varName (destination , global )
520545
521- c .addLine ( fmt . Sprintf ( "_sch %s %s" , destination , source ) )
546+ c .callFunc ( sliceCopyHelper , destination , source )
522547 c .sliceAssignmentHelperRequired = true
523548 c .sliceCopyHelperRequired = true
524549
@@ -548,6 +573,10 @@ func (c *converter) ReadFile(path string, valueUsed bool) (string, error) {
548573 return c .VarEvaluation (helper , valueUsed , false )
549574}
550575
576+ func (c * converter ) callFunc (name string , args ... string ) {
577+ c .addLine (fmt .Sprintf (`%s "%s"` , name , strings .Join (args , `" "` )))
578+ }
579+
551580func (c * converter ) mustCurrentForVar () string {
552581 return fmt .Sprintf ("_fv%d" , c .forCounter )
553582}
0 commit comments