about summary refs log tree commit diff stats
path: root/html/073array.mu.html
blob: 433e2044419e08c02b2fa204dbbb4b6fbb25dd4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 073array.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muScenario { color: #00af00; }
.Delimiter { color: #800080; }
.muRecipe { color: #ff8700; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muControl { color: #c0a020; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="muScenario">scenario</span> array-from-args [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new-array <span class="Constant">0</span>, <span class="Constant">1</span>, <span class="Constant">2</span>
    <span class="Constant">2</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">1</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># array length</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
]

<span class="Comment"># create an array out of a list of scalar args</span>
<span class="muRecipe">def</span> new-array<span class="muRecipe"> -&gt; </span>result:address:array:character [
  <span class="Constant">local-scope</span>
  capacity:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while read curr-value</span>
    curr-value:character, exists?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
    <span class="muControl">break-unless</span> exists?
    capacity<span class="Special"> &lt;- </span>add capacity, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  result<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, capacity
  <span class="Constant">rewind-ingredients</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while read curr-value</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i, capacity
    <span class="muControl">break-if</span> done?
    curr-value:character, exists?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
    assert exists?, <span class="Constant">[error in rewinding ingredients to new-array]</span>
    *result<span class="Special"> &lt;- </span>put-index *result, i, curr-value
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">return</span> result
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
an class="w"> "select failed"); return 3; } } /*=========================================================================*\ * Internal functions \*=========================================================================*/ static t_socket getfd(lua_State *L) { t_socket fd = SOCKET_INVALID; lua_pushstring(L, "getfd"); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { lua_pushvalue(L, -2); lua_call(L, 1, 1); if (lua_isnumber(L, -1)) { double numfd = lua_tonumber(L, -1); fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; } } lua_pop(L, 1); return fd; } static int dirty(lua_State *L) { int is = 0; lua_pushstring(L, "dirty"); lua_gettable(L, -2); if (!lua_isnil(L, -1)) { lua_pushvalue(L, -2); lua_call(L, 1, 1); is = lua_toboolean(L, -1); } lua_pop(L, 1); return is; } static void collect_fd(lua_State *L, int tab, int itab, fd_set *set, t_socket *max_fd) { int i = 1, n = 0; /* nil is the same as an empty table */ if (lua_isnil(L, tab)) return; /* otherwise we need it to be a table */ luaL_checktype(L, tab, LUA_TTABLE); for ( ;; ) { t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { lua_pop(L, 1); break; } /* getfd figures out if this is a socket */ fd = getfd(L); if (fd != SOCKET_INVALID) { /* make sure we don't overflow the fd_set */ #ifdef _WIN32 if (n >= FD_SETSIZE) luaL_argerror(L, tab, "too many sockets"); #else if (fd >= FD_SETSIZE) luaL_argerror(L, tab, "descriptor too large for set size"); #endif FD_SET(fd, set); n++; /* keep track of the largest descriptor so far */ if (*max_fd == SOCKET_INVALID || *max_fd < fd) *max_fd = fd; /* make sure we can map back from descriptor to the object */ lua_pushnumber(L, (lua_Number) fd); lua_pushvalue(L, -2); lua_settable(L, itab); } lua_pop(L, 1); i = i + 1; } } static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { int ndirty = 0, i = 1; if (lua_isnil(L, tab)) return 0; for ( ;; ) { t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { lua_pop(L, 1); break; } fd = getfd(L); if (fd != SOCKET_INVALID && dirty(L)) { lua_pushnumber(L, ++ndirty); lua_pushvalue(L, -2); lua_settable(L, dtab); FD_CLR(fd, set); } lua_pop(L, 1); i = i + 1; } return ndirty; } static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, int itab, int tab, int start) { t_socket fd; for (fd = 0; fd < max_fd; fd++) { if (FD_ISSET(fd, set)) { lua_pushnumber(L, ++start); lua_pushnumber(L, (lua_Number) fd); lua_gettable(L, itab); lua_settable(L, tab); } } } static void make_assoc(lua_State *L, int tab) { int i = 1, atab; lua_newtable(L); atab = lua_gettop(L); for ( ;; ) { lua_pushnumber(L, i); lua_gettable(L, tab); if (!lua_isnil(L, -1)) { lua_pushnumber(L, i); lua_pushvalue(L, -2); lua_settable(L, atab); lua_pushnumber(L, i); lua_settable(L, atab); } else { lua_pop(L, 1); break; } i = i+1; } }