diff --git a/code/__DATASTRUCTURES/linked_lists.dm b/code/__DATASTRUCTURES/linked_lists.dm index 7a7f6519800..eccc3c422a8 100644 --- a/code/__DATASTRUCTURES/linked_lists.dm +++ b/code/__DATASTRUCTURES/linked_lists.dm @@ -2,54 +2,54 @@ //Ok so it's technically a double linked list, bite me. /datum/linked_list - var/datum/linked_element/head - var/datum/linked_element/tail - var/element_amt = 0 + var/datum/linked_node/head + var/datum/linked_node/tail + var/node_amt = 0 -/datum/linked_element +/datum/linked_node var/value = null var/datum/linked_list/linked_list = null - var/datum/linked_element/next_element_pointer = null - var/datum/linked_element/previous_element_pointer = null + var/datum/linked_node/next_node = null + var/datum/linked_node/previous_node = null -/datum/linked_list/proc/is_empty() - . = element_amt ? 0 : 1 +/datum/linked_list/proc/IsEmpty() + . = (node_amt <= 0) -//Add a linked_element (or value, creating a linked_element) at position -//the added element BECOMES the position-th element, -//eg: add("Test",5), the 5th element is now "Test", the previous 5th moves up to become the 6th -/datum/linked_list/proc/add(element, position) - var/datum/linked_element/adding - if(istype(element, /datum/linked_element)) - adding = element +//Add a linked_node (or value, creating a linked_node) at position +//the added node BECOMES the position-th element, +//eg: add("Test",5), the 5th node is now "Test", the previous 5th moves up to become the 6th +/datum/linked_list/proc/Add(node, position) + var/datum/linked_node/adding + if(istype(node, /datum/linked_node)) + adding = node else adding = new() - adding.value = element + adding.value = node if(!adding.linked_list || (adding.linked_list && (adding.linked_list != src))) - element_amt++ + node_amt++ adding.linked_list = src - if(position && position < element_amt) + if(position && position < node_amt) //Replacing head if(position == 1) if(head) - head.previous_element_pointer = adding - adding.next_element_pointer = head + head.previous_node = adding + adding.next_node = head head = adding - //Replacing any middle element + //Replacing any middle node else var/location = 0 - var/datum/linked_element/at - while((location != position) && (location <= element_amt)) + var/datum/linked_node/at + while((location != position) && (location <= node_amt)) if(at) - if(at.next_element_pointer) - at = at.next_element_pointer + if(at.next_node) + at = at.next_node else break else @@ -57,29 +57,29 @@ location++ //Push at up and assume it's place as the position-th element - if(at && at.previous_element_pointer) - at.previous_element_pointer.next_element_pointer = adding - adding.previous_element_pointer = at.previous_element_pointer - at.previous_element_pointer = adding - adding.next_element_pointer = at + if(at && at.previous_node) + at.previous_node.next_node = adding + adding.previous_node = at.previous_node + at.previous_node = adding + adding.next_node = at return //Replacing tail if(tail) - tail.next_element_pointer = adding - adding.previous_element_pointer = tail - if(!tail.previous_element_pointer) + tail.next_node = adding + adding.previous_node = tail + if(!tail.previous_node) head = tail tail = adding -//Remove a linked_element or the linked_element of a value +//Remove a linked_node or the linked_node of a value //If you specify a value the FIRST ONE is removed -/datum/linked_list/proc/remove(element) - var/datum/linked_element/removing - if(istype(element,/datum/linked_element)) - removing = element +/datum/linked_list/proc/Remove(node) + var/datum/linked_node/removing + if(istype(node,/datum/linked_node)) + removing = node else //optimise removing head and tail, no point looping for them, especially the tail if(removing == head) @@ -89,97 +89,103 @@ else var/location = 1 var/current_value = null - var/datum/linked_element/at = null - while((current_value != element) && (location <= element_amt)) + var/datum/linked_node/at = null + while((current_value != node) && (location <= node_amt)) if(at) - if(at.next_element_pointer) - at = at.next_element_pointer + if(at.next_node) + at = at.next_node else at = head location++ if(at) current_value = at.value - if(current_value == element) + if(current_value == node) removing = at break //Adjust pointers of where removing -was- in the chain. if(removing) - if(removing.previous_element_pointer) + if(removing.previous_node) if(removing == tail) - tail = removing.previous_element_pointer - if(removing.next_element_pointer) + tail = removing.previous_node + if(removing.next_node) if(removing == head) - head = removing.next_element_pointer - removing.next_element_pointer.previous_element_pointer = removing.previous_element_pointer - removing.previous_element_pointer.next_element_pointer = removing.next_element_pointer + head = removing.next_node + removing.next_node.previous_node = removing.previous_node + removing.previous_node.next_node = removing.next_node else - removing.previous_element_pointer.next_element_pointer = null + removing.previous_node.next_node = null else - if(removing.next_element_pointer) + if(removing.next_node) if(removing == head) - head = removing.next_element_pointer - removing.next_element_pointer.previous_element_pointer = null + head = removing.next_node + removing.next_node.previous_node = null - //if this is still true at this point, there's no more elements to replace them with + //if this is still true at this point, there's no more nodes to replace them with if(removing == head) head = null if(removing == tail) tail = null - removing.next_element_pointer = null - removing.previous_element_pointer = null + removing.next_node = null + removing.previous_node = null if(removing.linked_list == src) - element_amt-- + node_amt-- removing.linked_list = null return removing return 0 -//Removes and deletes a element -/datum/linked_list/proc/removeDelete(element) - var/datum/linked_element/dead = remove(element) +//Removes and deletes a node or value +/datum/linked_list/proc/RemoveDelete(node) + var/datum/linked_node/dead = Remove(node) if(dead) qdel(dead) return 1 return 0 -//Empty the linked_list, deleting all elements -/datum/linked_list/proc/empty() - var/datum/linked_element/e = head - while(e) - var/next = e.next_element_pointer - remove(e) - qdel(e) - e = next - element_amt = 0 +//Empty the linked_list, deleting all nodes +/datum/linked_list/proc/Empty() + var/datum/linked_node/n = head + while(n) + var/next = n.next_node + Remove(n) + qdel(n) + n = next + node_amt = 0 //Some debugging tools -/datum/linked_list/proc/check_node_links() - var/datum/linked_element/e = head - while(e) - . = "|[e.value]|" - if(e.previous_element_pointer) - . = "[e.previous_element_pointer.value]<-" + . - if(e.next_element_pointer) - . += "->[e.next_element_pointer.value]" - e = e.next_element_pointer +/datum/linked_list/proc/CheckNodeLinks() + var/datum/linked_node/n = head + while(n) + . = "|[n.value]|" + if(n.previous_node) + . = "[n.previous_node.value]<-" + . + if(n.next_node) + . += "->[n.next_node.value]" + n = n.next_node . += "
" -/datum/linked_list/proc/draw_node_links() + +/datum/linked_list/proc/DrawNodeLinks() . = "|<-" - var/datum/linked_element/e = head - while(e) - if(e.previous_element_pointer) + var/datum/linked_node/n = head + while(n) + if(n.previous_node) . += "<-" - . += "[e.value]" - if(e.next_element_pointer) + . += "[n.value]" + if(n.next_node) . += "->" - e = e.next_element_pointer + n = n.next_node . += "->|" - +/datum/linked_list/proc/ToList() + . = list() + var/datum/linked_node/n = head + while(n) + . += n + n = n.next_node \ No newline at end of file diff --git a/code/__DATASTRUCTURES/stacks.dm b/code/__DATASTRUCTURES/stacks.dm index 21f079e48df..64280ec7006 100644 --- a/code/__DATASTRUCTURES/stacks.dm +++ b/code/__DATASTRUCTURES/stacks.dm @@ -1,9 +1,9 @@ //Both count as failures, and they don't equate to each other -//this lets us do if(pop()) without having to specifically check for underflow -//same for if(push()) and overflow -#define STACK_OVERFLOW "" -#define STACK_UNDERFLOW null +//this lets us do if(Pop()) without having to specifically check for underflow +//same for if(Push()) and overflow +#define STACK_OVERFLOW -1 +#define STACK_UNDERFLOW -2 /datum/stack @@ -17,35 +17,35 @@ if(max) max_elements = max -/datum/stack/proc/pop() +/datum/stack/proc/Pop() if(is_empty()) return STACK_UNDERFLOW . = stack[stack.len] stack.Cut(stack.len,0) -/datum/stack/proc/push(element) +/datum/stack/proc/Push(element) if(max_elements && (stack.len+1 > max_elements)) return STACK_OVERFLOW stack += element -/datum/stack/proc/peek() +/datum/stack/proc/Top() if(is_empty()) return STACK_UNDERFLOW . = stack[stack.len] /datum/stack/proc/is_empty() - . = stack.len ? 0 : 1 + . = (stack.len > 0) //Rotate entire stack left with the leftmost looping around to the right -/datum/stack/proc/rotate_left() +/datum/stack/proc/RotateLeft() if(is_empty()) return 0 . = stack[1] stack.Cut(1,2) - push(.) + Push(.) //Rotate entire stack to the right with the rightmost looping around to the left -/datum/stack/proc/rotate_right() +/datum/stack/proc/RotateRight() if(is_empty()) return 0 . = stack[stack.len] @@ -53,3 +53,12 @@ stack.Insert(1,.) +/datum/stack/proc/Copy() + var/datum/stack/S=new() + S.stack = stack.Copy() + S.max_elements = max_elements + return S + + +/datum/stack/proc/Clear() + stack.Cut() diff --git a/code/modules/scripting/Errors.dm b/code/modules/scripting/Errors.dm index ed9d73b4e31..3b2b5a7de70 100644 --- a/code/modules/scripting/Errors.dm +++ b/code/modules/scripting/Errors.dm @@ -81,7 +81,7 @@ A basic description as to what went wrong. */ message - stack/stack + datum/stack/stack proc /* diff --git a/code/modules/scripting/Interpreter/Interpreter.dm b/code/modules/scripting/Interpreter/Interpreter.dm index 9aacf4f19f0..7b1b34ca81c 100644 --- a/code/modules/scripting/Interpreter/Interpreter.dm +++ b/code/modules/scripting/Interpreter/Interpreter.dm @@ -21,7 +21,7 @@ node BlockDefinition/program statement/FunctionDefinition/curFunction - stack + datum/stack scopes = new() functions = new() diff --git a/code/modules/scripting/Parser/Expressions.dm b/code/modules/scripting/Parser/Expressions.dm index 18245cac9c3..9386786a376 100644 --- a/code/modules/scripting/Parser/Expressions.dm +++ b/code/modules/scripting/Parser/Expressions.dm @@ -56,7 +56,7 @@ var token/accessor/A=T node/expression/value/variable/E//=new(A.member) - stack/S=new() + datum/stack/S = new() while(istype(A.object, /token/accessor)) S.Push(A) A=A.object @@ -130,7 +130,7 @@ Takes the operator on top of the opr stack and assigns its operand(s). Then this proc pushes the value of that operation to the top of the val stack. */ - Reduce(stack/opr, stack/val) + Reduce(datum/stack/opr, datum/stack/val) var/node/expression/operator/O=opr.Pop() if(!O) return if(!istype(O)) @@ -181,7 +181,7 @@ - */ ParseExpression(list/end=list(/token/end), list/ErrChars=list("{", "}"), check_functions = 0) - var/stack + var/datum/stack opr=new val=new src.expecting=VALUE diff --git a/code/modules/scripting/Parser/Parser.dm b/code/modules/scripting/Parser/Parser.dm index b3c6b8ac3c9..e2459cc9202 100644 --- a/code/modules/scripting/Parser/Parser.dm +++ b/code/modules/scripting/Parser/Parser.dm @@ -37,7 +37,7 @@ The token at in . */ curToken - stack + datum/stack blocks=new node/BlockDefinition GlobalBlock/global_block=new diff --git a/code/modules/scripting/stack.dm b/code/modules/scripting/stack.dm deleted file mode 100644 index efea746e47a..00000000000 --- a/code/modules/scripting/stack.dm +++ /dev/null @@ -1,23 +0,0 @@ -/stack - var/list - contents=new - proc - Push(value) - contents+=value - - Pop() - if(!contents.len) return null - . = contents[contents.len] - contents.len-- - - Top() //returns the item on the top of the stack without removing it - if(!contents.len) return null - return contents[contents.len] - - Copy() - var/stack/S=new() - S.contents=src.contents.Copy() - return S - - Clear() - contents.Cut() \ No newline at end of file diff --git a/tgstation.dme b/tgstation.dme index 55262d42579..8b493692762 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -1476,7 +1476,6 @@ #include "code\modules\scripting\Errors.dm" #include "code\modules\scripting\IDE.dm" #include "code\modules\scripting\Options.dm" -#include "code\modules\scripting\stack.dm" #include "code\modules\scripting\AST\AST Nodes.dm" #include "code\modules\scripting\AST\Blocks.dm" #include "code\modules\scripting\AST\Statements.dm"