Changeset 074444f in mainline for uspace/dist/src/sysel/demos/list.sy
- Timestamp:
- 2010-04-10T11:15:33Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1ef0fc3, 38aaacc2
- Parents:
- 23de644
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/dist/src/sysel/demos/list.sy
r23de644 r074444f 27 27 -- 28 28 29 -- Doubly-linked list implementation. 30 class List is 31 var head : ListNode; 32 33 -- Initialize list. 34 fun Init() is 35 head = new ListNode(); 36 head.prev = head; 37 head.next = head; 38 end 39 40 -- Append new entry at the end of the list. 41 fun Append(data : int) is 42 var n : ListNode; 43 var ntl : ListNode; 44 45 ntl = head.prev; 46 47 n = new ListNode(); 48 n.value = data; 49 50 n.prev = ntl; 51 n.next = head; 52 n.head = head; 53 54 ntl.next = n; 55 head.prev = n; 56 end 57 58 -- Return first node in the list or @c nil if there is none. 59 prop First : ListNode is 60 get is 61 return get_first(); 62 end 63 end 64 65 -- Return first node in the list or @c nil if there is none. 66 fun get_first() : ListNode is 67 if head.next == head then 68 return nil; 69 else 70 return head.next; 71 end 72 end 73 end 74 75 class ListNode is 76 var value : int; 77 78 var prev : ListNode; 79 var next : ListNode; 80 var head : ListNode; 81 82 -- Value stored in this node. 83 prop Value : int is 84 get is 85 return value; 86 end 87 end 88 89 -- Previous node in list. 90 prop Prev : ListNode is 91 get is 92 return get_prev(); 93 end 94 end 95 96 -- Next node in list. 97 prop Next : ListNode is 98 get is 99 return get_next(); 100 end 101 end 102 103 -- Get next node. 104 fun get_next() : ListNode is 105 if next != head then 106 return next; 107 else 108 return nil; 109 end 110 end 111 112 -- Get previous node. 113 fun get_prev() : ListNode is 114 if prev != head then 115 return next; 116 else 117 return nil; 118 end 119 end 120 121 end 122 29 -- Using the List class from the library. 123 30 class ListDemo is 124 31 fun Main() is 125 32 var list : List; 33 var i : Int; 126 34 127 35 list = new List(); 128 36 list.Init(); 129 37 130 list.Append(5); 131 list.Append(6); 132 list.Append(7); 133 list.Append(8); 38 -- We need autoboxing or generics to get rid of this mess. 39 i = new Int(); i.Value = 5; list.Append(i); 40 i = new Int(); i.Value = 6; list.Append(i); 41 i = new Int(); i.Value = 7; list.Append(i); 42 i = new Int(); i.Value = 8; list.Append(i); 134 43 135 44 var n : ListNode; … … 137 46 n = list.First; 138 47 while n != nil do 139 Builtin.WriteLine( n.value);48 Builtin.WriteLine((n.value as Int).Value); 140 49 n = n.Next; 141 50 end
Note:
See TracChangeset
for help on using the changeset viewer.