comparison keiko/util.tcl @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 #
2 # util.tcl
3 #
4 # This file is part of the Oxford Oberon-2 compiler
5 # Copyright (c) 2006--2016 J. M. Spivey
6 # All rights reserved
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # 3. The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30
31 # max -- maximum of any number of args
32 proc max {x args} {
33 set max $x
34 foreach y $args {
35 if {$y > $max} {set max $y}
36 }
37 return $max
38 }
39
40 # count -- number of occurrences of a char in a string
41 proc count {ch s} {return [regsub -all $ch $s "" dummy]}
42
43 # lmember -- test for list membership
44 proc lmember {x xs} {
45 return [expr {[lsearch -exact $xs $x] >= 0}]
46 }
47
48 # ladd -- add values to a set
49 proc ladd {sv args} {
50 upvar $sv s
51
52 foreach x $args {
53 if {! [lmember $x $s]} {lappend s $x}
54 }
55 }
56
57 # lsplit -- split a list into components
58 proc lsplit {xs args} {
59 set n [llength $args]
60
61 if {[llength $xs] != $n} {
62 error "expected $n fields, got [llength $xs]"
63 }
64
65 for {set i 0} {$i < $n} {incr i} {
66 uplevel [list set [lindex $args $i] [lindex $xs $i]]
67 }
68 }
69
70 # with -- bind names to record fields in body
71 proc with {record fields body} {
72 uplevel [concat [list lsplit $record] $fields]
73 uplevel $body
74 }
75
76 # filter -- filter a list by a predicate
77 proc filter {p xs} {
78 set ys {}
79 foreach x $xs {if {[uplevel $p $x]} {lappend ys $x}}
80 return $ys
81 }
82
83 # exists -- test is a list has any member that satisfies a predicate
84 proc exists {p xs} {
85 foreach x $xs {if {[uplevel $p $x]} {return 1}}
86 return 0
87 }
88
89 # map -- apply a function to all elements of a list
90 proc map {f xs} {
91 set ys {}
92 foreach x $xs {lappend ys [uplevel [concat $f [list $x]]]}
93 return $ys
94 }
95
96 # flatmap -- apply a function to all elements of a list, concatenate results
97 proc flatmap {f xs} {
98 set ys {}
99 foreach x $xs {set ys [concat $ys [uplevel $f $x]]}
100 return $ys
101 }
102
103 # remdups -- remove adjecent duplicates from a list
104 proc remdups {xs} {
105 if {$xs == ""} {
106 return {}
107 } else {
108 set y [lindex $xs 0]
109 set ys [list $y]
110 foreach x $xs {
111 if {$x != $y} {lappend ys $x}
112 set y $x
113 }
114 return $ys
115 }
116 }
117
118 # nth_char -- return n'th character of string
119 proc nth_char {n string} {
120 return [string index $string $n]
121 }
122
123 # nth_char_is -- test if n'th character of string is a given char
124 proc nth_char_is {n ch string} {
125 return [expr {[string index $string $n] == $ch}]
126 }
127
128 # trycatch -- handle exceptions by evaluating alternative script
129 proc trycatch {s1 s2} {
130 if {[catch {uplevel $s1}]} {uplevel $s2}
131 }
132
133 set charcode() 0
134 for {set i 1} {$i < 128} {incr i} {
135 set ch [format "%c" $i]
136 set charcode($ch) $i
137 }
138
139 # trim -- trim leading and trailing spaces
140 proc trim {s} { regexp {^ *(.*[^ ]) *$} $s dummy s; return $s }
141
142 # csym -- make C symbol
143 proc csym {k s} {
144 if {$k == ""} {
145 return [regsub "\\." $s ""]
146 } else {
147 return ${k}_[regsub "\\." $s ""]
148 }
149 }