https://github.com/akkartik/mu/blob/master/001help.cc
  1 //: Everything this project/binary supports.
  2 //: This should give you a sense for what to look forward to in later layers.
  3 
  4 :(before "End Commandline Parsing")
  5 if (argc <= 1 || is_equal(argv[1], "--help")) {
  6   //: this is the functionality later layers will provide
  7   // currently no automated tests for commandline arg parsing
  8   if (argc <= 1) {
  9     cerr << "Please provide a Mu program to run.\n"
 10          << "\n";
 11   }
 12   cerr << "Usage:\n"
 13        << "  mu [options] [test] [files]\n"
 14        << "or:\n"
 15        << "  mu [options] [test] [files] -- [ingredients for function/recipe 'main']\n"
 16        << "Square brackets surround optional arguments.\n"
 17        << "\n"
 18        << "Examples:\n"
 19        << "  To load files and run 'main':\n"
 20        << "    mu file1.mu file2.mu ...\n"
 21        << "  To run 'main' and dump a trace of all operations at the end:\n"
 22        << "    mu --trace file1.mu file2.mu ...\n"
 23        << "  To run all tests:\n"
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
discard """
  cmd: '''nim c -d:nimAllocStats --gc:arc $file'''
  output: '''a b
70
hello
hello
hello
(allocCount: 3, deallocCount: 3)'''
"""

import system / ansi_c

proc main(): owned(proc()) =
  var a = "a"
  var b = "b"
  result = proc() =
    echo a, " ", b


proc foo(f: (iterator(): int)) =
  for i in f(): echo i

proc wrap =
  let p = main()
  p()

  let fIt = iterator(): int = yield 70
  foo fIt

wrap()

# bug #11533
proc say = echo "hello"

# Error: internal error: genAssignment: tyNil
var err0: proc() = say
err0()

var ok0: proc()
ok0 = say
ok0()

var ok1 = say
ok1()

when false:
  # bug #12443
  func newStringIterator(s: string): owned(iterator(): char) =
    result = iterator(): char =
      var pos = 0
      while pos < s.len:
        yield s[pos]
        inc pos

  proc stringIter() =
    let si = newStringIterator("foo")
    for i in si():
      echo i

  stringIter()

echo getAllocStats()
span class="Comment">//: avoid use-after-free issues (and memory leaks). 118 //: 119 //: 2. Avoid naked arrays to avoid out-of-bounds access. Never use operator[] 120 //: except with map. Use at() with STL vectors and so on. 121 //: 122 //: 3. Valgrind all the things. 123 //: 124 //: 4. Avoid unsigned numbers. Not strictly an undefined-behavior issue, but 125 //: the extra range doesn't matter, and it's one less confusing category of 126 //: interaction gotchas to worry about. 127 //: 128 //: Corollary: don't use the size() method on containers, since it returns an 129 //: unsigned and that'll cause warnings about mixing signed and unsigned, 130 //: yadda-yadda. Instead use this macro below to perform an unsafe cast to 131 //: signed. We'll just give up immediately if a container's ever too large. 132 //: Basically, Mu is not concerned about this being a little slower than it 133 //: could be. (https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7) 134 //: 135 //: Addendum to corollary: We're going to uniformly use int everywhere, to 136 //: indicate that we're oblivious to number size, and since Clang on 32-bit 137 //: platforms doesn't yet support multiplication over 64-bit integers, and 138 //: since multiplying two integers seems like a more common situation to end 139 //: up in than integer overflow. 140 :(before "End Includes") 141 #define SIZE(X) (assert((X).size() < (1LL<<(sizeof(int)*8-2))), static_cast<int>((X).size())) 142 143 //: 5. Integer overflow is guarded against at runtime using the -ftrapv flag 144 //: to the compiler, supported by Clang (GCC version only works sometimes: 145 //: http://stackoverflow.com/questions/20851061/how-to-make-gcc-ftrapv-work). 146 :(before "atexit(reset)") 147 initialize_signal_handlers(); // not always necessary, but doesn't hurt 148 //? cerr << INT_MAX+1 << '\n'; // test overflow 149 //? assert(false); // test SIGABRT 150 :(code) 151 // based on https://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c 152 void initialize_signal_handlers() { 153 struct sigaction action; 154 bzero(&action, sizeof(action)); 155 action.sa_sigaction = dump_and_exit; 156 sigemptyset(&action.sa_mask); 157 sigaction(SIGABRT, &action, NULL); // assert() failure or integer overflow on linux (with -ftrapv) 158 sigaction(SIGILL, &action, NULL); // integer overflow on OS X (with -ftrapv) 159 } 160 void dump_and_exit(int sig, siginfo_t* /*unused*/, void* /*unused*/) { 161 switch (sig) { 162 case SIGABRT: 163 #ifndef __APPLE__ 164 cerr << "SIGABRT: might be an integer overflow if it wasn't an assert() failure or exception\n"; 165 _Exit(1); 166 #endif 167 break; 168 case SIGILL: 169 #ifdef __APPLE__ 170 cerr << "SIGILL: most likely caused by integer overflow\n"; 171 _Exit(1); 172 #endif 173 break; 174 default: 175 break; 176 } 177 } 178 :(before "End Includes") 179 #include <signal.h> 180 181 //: For good measure we'll also enable SIGFPE. 182 :(before "atexit(reset)") 183 feenableexcept(FE_OVERFLOW | FE_UNDERFLOW); 184 //? assert(sizeof(int) == 4 && sizeof(float) == 4); 185 //? // | exp | mantissa 186 //? int smallest_subnormal = 0b00000000000000000000000000000001; 187 //? float smallest_subnormal_f = *reinterpret_cast<float*>(&smallest_subnormal); 188 //? cerr << "ε: " << smallest_subnormal_f << '\n'; 189 //? cerr << "ε/2: " << smallest_subnormal_f/2 << " (underflow)\n"; // test SIGFPE 190 :(before "End Includes") 191 #include <fenv.h> 192 :(code) 193 #ifdef __APPLE__ 194 // Public domain polyfill for feenableexcept on OS X 195 // http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c 196 int feenableexcept(unsigned int excepts) { 197 static fenv_t fenv; 198 unsigned int new_excepts = excepts & FE_ALL_EXCEPT; 199 unsigned int old_excepts; 200 if (fegetenv(&fenv)) return -1; 201 old_excepts = fenv.__control & FE_ALL_EXCEPT; 202 fenv.__control &= ~new_excepts; 203 fenv.__mxcsr &= ~(new_excepts << 7); 204 return fesetenv(&fenv) ? -1 : old_excepts; 205 } 206 #endif 207 208 //: 6. Map's operator[] being non-const is fucking evil. 209 :(before "Globals") // can't generate prototypes for these 210 // from http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map 211 template<typename T> typename T::mapped_type& get(T& map, typename T::key_type const& key) { 212 typename T::iterator iter(map.find(key)); 213 assert(iter != map.end()); 214 return iter->second; 215 } 216 template<typename T> typename T::mapped_type const& get(const T& map, typename T::key_type const& key) { 217 typename T::const_iterator iter(map.find(key)); 218 assert(iter != map.end()); 219 return iter->second; 220 } 221 template<typename T> typename T::mapped_type const& put(T& map, typename T::key_type const& key, typename T::mapped_type const& value) { 222 // map[key] requires mapped_type to have a zero-arg (default) constructor 223 map.insert(std::make_pair(key, value)).first->second = value; 224 return value; 225 } 226 template<typename T> bool contains_key(T& map, typename T::key_type const& key) { 227 return map.find(key) != map.end(); 228 } 229 template<typename T> typename T::mapped_type& get_or_insert(T& map, typename T::key_type const& key) { 230 return map[key]; 231 } 232 //: The contract: any container that relies on get_or_insert should never call 233 //: contains_key. 234 235 //: 7. istreams are a royal pain in the arse. You have to be careful about 236 //: what subclass you try to putback into. You have to watch out for the pesky 237 //: failbit and badbit. Just avoid eof() and use this helper instead. 238 :(code) 239 bool has_data(istream& in) { 240 return in && !in.eof(); 241 } 242 243 :(before "End Includes") 244 #include <assert.h> 245 246 #include <iostream> 247 using std::istream; 248 using std::ostream; 249 using std::iostream; 250 using std::cin; 251 using std::cout; 252 using std::cerr; 253 #include <iomanip> 254 255 #include <string.h> 256 #include <string> 257 using std::string; 258 259 #include <algorithm> 260 using std::min; 261 using std::max;