Skip to content

Commit

Permalink
feat: better formatting for bytecode disassembly log message
Browse files Browse the repository at this point in the history
  • Loading branch information
tucats committed Jan 25, 2025
1 parent 310f7f3 commit b402493
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app-cli/settings/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func Set(key string, value string) {
c.Modified = time.Now().Format(time.RFC1123Z)
c.Dirty = true

ui.Log(ui.AppLogger, "config.set", "name", key, "value", value)
ui.Log(ui.AppLogger, "config.set", ui.A{
"name": key,
"value": value})
}

// SetDefault puts a profile entry in the current Configuration structure. It is
Expand Down
14 changes: 6 additions & 8 deletions bytecode/disassembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ func (b *ByteCode) Disasm(ranges ...int) {
}

op, operand := FormatInstruction(i)
if ui.LogFormat == ui.TextFormat {
ui.Log(ui.ByteCodeLogger, "%4d: %s%s", n, strings.Repeat("| ", scopePad), op+" "+operand)
} else {
ui.Log(ui.ByteCodeLogger, "bytecode.instruction",
"addr", n,
"op", strings.TrimSpace(op),
"operand", operand)
}

ui.Log(ui.ByteCodeLogger, "bytecode.instruction", ui.A{
"addr": n,
"depth": scopePad,
"op": strings.TrimSpace(op),
"operand": operand})

if i.Operation == PushScope {
scopePad = scopePad + 1
Expand Down
2 changes: 1 addition & 1 deletion i18n/languages/messages_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ auth.authorized=Authorized as user {{user}}{{flag}}


bytecode.disasm=*** Disassembly of {{name}}
bytecode.instruction={{addr}}: {{op}} {{operand}}
bytecode.instruction={{addr|%4d}}: {{depth|pad "| "}}{{op|%-12s}} {{operand}}
bytecode.count=Disassembled {{count}} instructions


Expand Down
67 changes: 66 additions & 1 deletion i18n/subs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func splitOutFormats(text string) []string {
}

func handleFormat(text string, subs map[string]interface{}) string {
var err error

if !strings.HasPrefix(text, "{{") || !strings.HasSuffix(text, "}}") {
return text
}
Expand All @@ -83,7 +85,8 @@ func handleFormat(text string, subs map[string]interface{}) string {
value = normalizeNumericValues(value)

// Check for special cases in the format string
formatParts := strings.Split(format, "|")
formatParts := barUnescape(strings.Split(barEscape(format), "|"))

label := ""
format = "%v"

Expand All @@ -110,6 +113,37 @@ func handleFormat(text string, subs map[string]interface{}) string {
value = ""
}

case strings.HasPrefix(part, "pad "):
pad := strings.TrimSpace(part[len("pad "):])
if strings.HasPrefix(pad, "\"") {
pad, _ = strconv.Unquote(pad)
}

var count int

switch v := value.(type) {
case int:
count = v

case float64:
count = int(math.Round(v))

case string:
count, err = strconv.Atoi(v)
if err != nil || count < 0 {
return "!Invalid pad count: " + part + "!"
}

default:
return "!Invalid pad type: " + part + "!"
}

if err != nil || count < 0 {
return "!Invalid pad count: " + part + "!"
}

value = strings.Repeat(pad, count)

case strings.HasPrefix(part, "format"):
format = strings.TrimSpace(part[len("format"):])

Expand Down Expand Up @@ -151,6 +185,37 @@ func handleFormat(text string, subs map[string]interface{}) string {
return result
}

// Search a string value for a "|" in quotes and if found convert it to "!BAR!"
func barEscape(text string) string {
quote := false
result := ""

for _, char := range text {
if char == '"' {
quote = !quote
}

if char == '|' && quote {
result += "!BAR!"
} else {
result += string(char)
}
}

return result
}

// Search an array of strings and if any contain "!BAR!" convert it bacak to "|"
func barUnescape(parts []string) []string {
result := make([]string, len(parts))

for i, part := range parts {
result[i] = strings.ReplaceAll(part, "!BAR!", "|")
}

return result
}

func makeList(values interface{}) string {
var result []string

Expand Down
75 changes: 75 additions & 0 deletions i18n/subs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ func Test_handleSub(t *testing.T) {
subs map[string]interface{}
want string
}{
{
name: "simple pad",
text: `{{size|pad "*"}}`,
subs: map[string]interface{}{"size": 3},
want: "***",
},
{
name: "complex pad",
text: `{{size|pad "XO"}}`,
subs: map[string]interface{}{"size": 2},
want: "XOXO",
},
{
name: "zero pad",
text: `{{size|pad "*"}}`,
subs: map[string]interface{}{"size": 0},
want: "",
},
{
name: "label zero value",
text: `{{item|label "flag="}}`,
Expand Down Expand Up @@ -141,6 +159,17 @@ func Test_handleSubMap(t *testing.T) {
subs map[string]interface{}
want string
}{
{
name: "using pad",
text: `{{addr|%4d}}: {{depth|pad "| "}}{{op}} {{operand}}`,
subs: map[string]interface{}{
"addr": 1234,
"depth": 3,
"op": "LOAD_FAST",
"operand": "42",
},
want: `1234: | | | LOAD_FAST 42`,
},
{
name: "complex case 1",
text: `{{method}} {{endpoint}} {{file}} {{admin|empty|nonempty admin}} {{auth|empty|nonempty auth}}{{perms|label permissions=}}`,
Expand Down Expand Up @@ -253,3 +282,49 @@ func Test_normalizeNumericValues(t *testing.T) {
})
}
}

func Test_barEscape(t *testing.T) {
tests := []struct {
name string
arg string
want string
}{
{
name: "quoted sub",
arg: `one "|" two`,
want: `one "!BAR!" two`,
},
{
name: "multiple quoted sub",
arg: `one "|" two "|"`,
want: `one "!BAR!" two "!BAR!"`,
},
{
name: "mixed quoted sub",
arg: `one "|" two | three`,
want: `one "!BAR!" two | three`,
},
{
name: "no sub",
arg: "this is a simple string",
want: "this is a simple string",
},
{
name: "single sub",
arg: "one|two",
want: "one|two",
},
{
name: "multiple sub",
arg: "one|two|three",
want: "one|two|three",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := barEscape(tt.arg); got != tt.want {
t.Errorf("barEscape() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion tools/buildver.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5-1209
1.5-1210

0 comments on commit b402493

Please sign in to comment.