Picol  Check-in [fb19e567ce]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:help.pcl: Return a list of `{result number}` instead of just a number from `match-bracket`. Avoid interpreting a number differently based on its sign.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1: fb19e567ce6675660decf2ea34fe4f90e89ef1c9
User & Date: dbohdan 2024-08-08 09:35:59
Context
2024-08-08
09:35
help.pcl: Return a list of `{result number}` instead of just a number from `match-bracket`. Avoid interpreting a number differently based on its sign. Leaf check-in: fb19e567ce user: dbohdan tags: trunk
07:15
shell: Switch from linenoise to bestline. Bestline has history search and other improvements. Replace `PICOL_SHELL_LINENOISE` with generic `PICOL_SHELL_LINE_EDIT` in case of future changes. Rename the binary to `picol-line-edit`. check-in: 55e2a82d3f user: dbohdan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to help.pcl.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


16
17
18
19
20
21
22
#! /usr/bin/env picolsh
# help.pcl: Extract usage messages from picol.h.
# 
# The PICOL_ARITY2 macro in picol.h is called to ensure the command is being
# used correctly and to display a usage message if it is not.  This script
# extracts the commands and their usage messages from picol.h, adds any missing
# commands from [info commands], and prints the result.  Its code demonstrates
# text manipulation, basic parsing, and the use of optional features: arrays,
# I/O, and regular expressions.  Could this be the most complex program written
# in Picol?  It's possible!

set macro PICOL_ARITY2  ;# The macro we shall look for.

# Find a matching closing bracket for an opening bracket in $s starting at
# $start.  Brackets escaped with $escape don't count.


proc match-bracket {opening closing escape s start} {
    set n 0

    if {[string index $s $start] ne $opening} {
        error {start isn't a bracket}
    }



|
|
|
|
|
|
|
|

|


|
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/env picolsh
# help.pcl: Extract usage messages from picol.h.
#
# The PICOL_ARITY2 macro in picol.h is called to ensure a command is being used
# correctly and display a usage message if it is not. This script extracts the
# commands and their usage messages from picol.h, adds any missing commands
# from [info commands], and prints the result. The code demonstrates text
# manipulation, basic parsing, and the use of optional features: arrays, I/O,
# and regular expressions. This might be the most complex program written in
# Picol.

set macro PICOL_ARITY2  ;# The macro we are going to look for.

# Find a matching closing bracket for an opening bracket in $s starting at
# $start. Brackets escaped with $escape don't count. Returns a list of
# {unmatched $n}, where $n is the number of unmatched opening brackets, or
# {matched $i}, where $i is the index of the match.
proc match-bracket {opening closing escape s start} {
    set n 0

    if {[string index $s $start] ne $opening} {
        error {start isn't a bracket}
    }

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            incr n -1
        }

        if {$n == 0} break
    }

    if {$n > 0} {
        return -$n
    }

    return $i
}

set file [if {$argc == 0} { lindex picol.h } else { lindex $argv 0 }]
set ch [open $file]
puts "Running $tcl_platform(engine) [info pa] with\
      [llength [info commands]] commands"

set count 0
while {![eof $ch]} {
    gets $ch statement
    # We don't want macro definitions.
    if {[string match #define* $statement]} continue

    set start [string first [set macro]( $statement]
    if {$start == -1} continue
    incr start [string length $macro]

    # Read up to five lines of the source code to get every argument to the
    # PICOL_ARITY2(...) macro.
    for {set lines 1} {$lines <= 5} {incr lines} {
        set end [match-bracket ( ) \\ $statement $start]
        if {$end > -1} break

        gets $ch line
        append statement $line
    }

    # The usage message is one or more C strings in the last argument to the
    # macro.
    set last_arg_start [string last , $statement]
    set message_start [string first \" $statement $last_arg_start]
    set message_end [string last \" $statement]

    set message [string range $statement \
                              [+ $message_start 1] \
                              [- $message_end 1]]

    # Separate the usage message into the command name and its possible
    # arguments.
    set cmd [lindex $message 0]
    if {[regexp ^\s*$ $cmd]} continue

    set usage [if {[info exists cmds($cmd)]} {
        set cmds($cmd)
    } else {







|


|




















|
|















|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
            incr n -1
        }

        if {$n == 0} break
    }

    if {$n > 0} {
        return [list unmatched $n]
    }

    return [list matched $i]
}

set file [if {$argc == 0} { lindex picol.h } else { lindex $argv 0 }]
set ch [open $file]
puts "Running $tcl_platform(engine) [info pa] with\
      [llength [info commands]] commands"

set count 0
while {![eof $ch]} {
    gets $ch statement
    # We don't want macro definitions.
    if {[string match #define* $statement]} continue

    set start [string first [set macro]( $statement]
    if {$start == -1} continue
    incr start [string length $macro]

    # Read up to five lines of the source code to get every argument to the
    # PICOL_ARITY2(...) macro.
    for {set lines 1} {$lines <= 5} {incr lines} {
        lassign [match-bracket ( ) \\ $statement $start] result
        if {$result eq {matched}} break

        gets $ch line
        append statement $line
    }

    # The usage message is one or more C strings in the last argument to the
    # macro.
    set last_arg_start [string last , $statement]
    set message_start [string first \" $statement $last_arg_start]
    set message_end [string last \" $statement]

    set message [string range $statement \
                              [+ $message_start 1] \
                              [- $message_end 1]]

    # Split the usage message into the command name and the possible
    # arguments.
    set cmd [lindex $message 0]
    if {[regexp ^\s*$ $cmd]} continue

    set usage [if {[info exists cmds($cmd)]} {
        set cmds($cmd)
    } else {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    }
}

foreach cmd [lsort -unique [array names cmds]] {
    foreach usage [set cmds($cmd)] {
        set msg "$cmd $usage"

        # Avoid printing the same usage muliple times.  Some commands are
        # defined identically twice because of conditional compilation.
        if {[info exists printed($msg)]} continue
        
        puts $msg
        set printed($msg) 1
    }
}







|


|




111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    }
}

foreach cmd [lsort -unique [array names cmds]] {
    foreach usage [set cmds($cmd)] {
        set msg "$cmd $usage"

        # Avoid printing the same usage muliple times. Some commands are
        # defined identically twice because of conditional compilation.
        if {[info exists printed($msg)]} continue

        puts $msg
        set printed($msg) 1
    }
}