Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get the end of the list
- func int endOfListSub(var int list) { // subfunction to provide usersafety
- if MEM_ReadInt(list+4) {
- return endOfList(MEM_ReadInt(list+4));
- };
- return list;
- };
- func int endOfList(var int list) {
- if (!list) {
- MEM_Error("endOfList: no valid pointer");
- };
- return endOfListSub(list);
- };
- func int endOfListSubS(var int list) {
- if MEM_ReadInt(list+8) {
- return endOfList(MEM_ReadInt(list+4));
- };
- return list;
- };
- func int endOfListS(var int list) {
- if (!list) {
- MEM_Error("endOfListS: no valid pointer");
- };
- return endOfListSubS(list);
- };
- // Get the length of the list
- func int lengthOfListSub(var int list, var int nr) { // subfunction to provide usersafety
- if MEM_ReadInt(list+4) {
- return lengthOfListSub(MEM_ReadInt(list+4), nr+1);
- };
- return nr;
- };
- func int lengthOfList(var int list) {
- if (!list) {
- MEM_Error("lengthOfList(): no valid pointer");
- };
- return lengthOfListSub(list, 0);
- };
- func int lengthOfListSubS(var int list, var int nr) {
- if MEM_ReadInt(list+8) {
- return lengthOfListSub(MEM_ReadInt(list+8), nr+1);
- };
- return nr;
- };
- func int lengthOfListS(var int list) {
- if (!list) {
- MEM_Error("lengthOfListS: no valid pointer");
- };
- return lengthOfListSubS(list, 0);
- };
- // Get a specific node of the list by number
- func int getNodeSub(var int list, var int nr) { // subfunction to provide usersafety
- if (!nr) {
- return list;
- };
- return getNodeSub(MEM_ReadInt(list+4), nr-1);
- };
- func int getNode(var int list, var int nr) { // nr. Node of list 'list'
- if nr >= lengthOfList(list) {
- MEM_Error("getNode(): nr is greater than the list");
- };
- return getNodeSub(list, nr);
- };
- func int getNodeSubS(var int list, var int nr) {
- if (!nr) {
- return list;
- };
- return getNodeSubS(MEM_ReadInt(list+8), nr-1);
- };
- func int getNodeS(var int list, var int nr) {
- if nr >= lengthOfListS(list) {
- MEM_Error("getNode: nr is greater than the list");
- };
- return getNodeSub(list, nr);
- };
- // add a node to a list
- func void addNode(var int list, var int data) {
- if (!list) {
- MEM_Error("addNode: no valid list");
- return;
- };
- var int ptr; ptr = MEM_Alloc(8);
- MEM_WriteInt(endOfList(list)+4, ptr);
- MEM_WriteInt(ptr, data);
- };
- func void addNodeS(var int list, var int data) {
- if (!list) {
- MEM_Error("addNode: no valid list");
- return;
- };
- var int ptr; ptr = MEM_Alloc(12);
- MEM_WriteInt(endOfList(list)+8, ptr);
- MEM_WriteInt(ptr+4, data);
- };
- // delete a node of a list by number
- func void deleteNode(var int list, var int nodeNr) {
- var int prev; prev = getNode(list, nodeNr-1);
- var int del; del = MEM_ReadInt(prev);
- MEM_WriteInt(prev, MEM_ReadInt(del));
- MEM_Free(del);
- };
- func void deleteNodeS(var int list, var int nodeNr) {
- var int prev; prev = getNodeS(list, nodeNr-1);
- var int del; del = MEM_ReadInt(prev+4);
- MEM_WriteInt(prev+4, MEM_ReadInt(del+4));
- MEM_Free(del);
- };
- // delete a complete list
- func void deleteList(var int list) {
- if (!list) {
- MEM_Error("deleteList: invalid list");
- return;
- };
- var int pos; pos = MEM_StackPos.position;
- if (!MEM_ReadInt(list)) {
- deleteNodeS(list, 0);
- return;
- };
- deleteNodeS(MEM_ReadInt(list), 0);
- MEM_StackPos.position = pos;
- };
- func void deleteListS(var int list) {
- if (!list) {
- MEM_Error("deleteListS: invalid list");
- return;
- };
- var int pos; pos = MEM_StackPos.position;
- if (!MEM_ReadInt(list+4)) {
- deleteNodeS(list, 0);
- return;
- };
- deleteNodeS(MEM_ReadInt(list+4), 0);
- MEM_StackPos.position = pos;
- };
- // Call a function for every node and pass the node with it
- func void ForList(var int list, var string function) {
- var int pos; pos = MEM_StackPos.position;
- if (list) {
- MEM_PushIntParam(list);
- MEM_CallByString(function);
- list = MEM_ReadInt(list+4);
- MEM_StackPos.position = pos;
- };
- };
- func void ForListS(var int list, var string function) {
- var int pos; pos = MEM_StackPos.position;
- if (list) {
- MEM_PushIntParam(list);
- MEM_CallByString(function);
- list = MEM_ReadInt(list+8);
- MEM_StackPos.position = pos;
- };
- };
- // copys a list to an array (memory of the size 4*nodes Bytes, contains the data in every word)
- var int ListArrayPtr;
- var int ListArrayNr;
- func void List_ToArraySub(var int list) {
- MEM_WriteInt(ListArrayPtr+(4*ListArrayNr), MEM_ReadInt(list));
- ListArrayNr += 1;
- };
- func int List_ToArray(var int list) {
- ListArrayPtr = MEM_Alloc(LengthOfList(list)*4);
- ListArrayNr = 0;
- ForList(list, "LIST_TOARRAYSUB");
- return ListArrayPtr;
- };
- func void List_ToArraySubS(var int list) {
- MEM_WriteInt(ListArrayPtr+(4*ListArrayNr), MEM_ReadInt(list+4));
- ListArrayNr += 1;
- };
- func int List_ToArrayS(var int list) {
- ListArrayPtr = MEM_Alloc(LengthOfListS(list)*4);
- ListArrayNr = 0;
- ForListS(list, "LIST_TOARRAYSUBS");
- return ListArrayPtr;
- };
- // copys a list to an static array
- var MEMINT_HELPERCLASS CopyToINT_MinusOne;
- func int List_CopyToINT() {
- MEMINT_StackPopInst();
- MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
- ListArrayPtr = MEMINT_StackPopInt();
- var int list; list = MEMINT_StackPopInt();
- ForList(list, "LIST_TOARRAYSUB");
- return ListArrayPtr;
- };
- var MEMINT_HELPERCLASS CopyTo_MinusOne;
- func int List_CopyTo(var int list, var int statArr) {
- MEM_Error ("List_CopyTo was called before List_Init");
- };
- var MEMINT_HELPERCLASS CopyToINT_MinusOneS;
- func int List_CopyToINTS() {
- MEMINT_StackPopInst();
- MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
- ListArrayPtr = MEMINT_StackPopInt();
- var int list; list = MEMINT_StackPopInt();
- ForList(list, "LIST_TOARRAYSUBS");
- return ListArrayPtr;
- };
- var MEMINT_HELPERCLASS CopyTo_MinusOneS;
- func int List_CopyToS() {
- MEM_Error ("List_CopyToS was called before List_Init");
- };
- func int List_Init() {
- MEMINT_StatArrs_ReplaceFunc (CopyTo_MinusOne + 1, CopyToInt_MinusOne + 1);
- MEMINT_StatArrs_ReplaceFunc(CopyTo_MinusOneS + 1, CopyToINT_MinusOneS + 1);
- };
- // sets the data 'data' of node 'node'
- func void List_Set(var int node, var int data) {
- MEM_WriteInt(node, data);
- };
- func void List_SetS(var int node, var int data) {
- MEM_WriteInt(node+4, data);
- };
- // returns the last node which contains the pointer 'data'
- var int List_ContainsNode;
- var int List_ContainsData;
- func void List_ContainsSub(var int list) {
- if List_ContainsData == MEM_ReadInt(list) {
- List_ContainsNode = list;
- };
- };
- func int List_Contains(var int list, var int data) {
- List_ContainsNode = -1;
- List_ContainsData = data;
- ForList(list, "LIST_CONTAINSSUB");
- return List_ContainsNode;
- };
- func void List_ContainsSubS(var int list) {
- if List_ContainsData == MEM_ReadInt(list+4) {
- List_ContainsNode = list;
- };
- };
- func int List_ContainsS(var int list, var int data) {
- List_ContainsNode = -1;
- List_ContainsData = data;
- ForListS(list, "LIST_CONTAINSSUBS");
- return List_ContainsNode;
- };
- // adds a node between node nr. 'offset' and node nr. 'offset + 1'
- func void List_AddOffset(var int list, var int offset, var int data) {
- var int prev; prev = getNode(list, offset);
- var int next; next = MEM_ReadInt(prev);
- MEM_WriteInt(prev, MEM_Alloc(8));
- MEM_WriteInt(MEM_ReadInt(prev), next);
- MEM_WriteInt(MEM_ReadInt(prev)+4, data);
- };
- func void List_AddOffsetS(var int list, var int offset, var int data) {
- var int prev; prev = getNodeS(list, offset);
- var int next; next = MEM_ReadInt(prev+4);
- MEM_WriteInt(prev, MEM_Alloc(12));
- MEM_WriteInt(MEM_ReadInt(prev+4), next);
- MEM_WriteInt(MEM_ReadInt(prev+8), data);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement