2018-01-15 18:42:40 +00:00
|
|
|
import
|
2018-01-24 13:31:24 +00:00
|
|
|
strformat, strutils, sequtils, macros,
|
2018-02-20 17:27:43 +00:00
|
|
|
value, ../errors, ../validation, ../utils_numeric, ../constants, ttmath, ../logging, .. / utils / bytes
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
Stack* = ref object of RootObj
|
|
|
|
logger*: Logger
|
2018-02-20 17:27:43 +00:00
|
|
|
values*: seq[UInt256]
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
template ensureStackLimit: untyped =
|
|
|
|
if len(stack.values) > 1023:
|
|
|
|
raise newException(FullStack, "Stack limit reached")
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc len*(stack: Stack): int =
|
2018-01-15 18:42:40 +00:00
|
|
|
len(stack.values)
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
template toType(i: UInt256, _: typedesc[UInt256]): UInt256 =
|
|
|
|
i
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
template toType(i: UInt256, _: typedesc[string]): string =
|
|
|
|
i.intToBigEndian.toString
|
|
|
|
|
|
|
|
template toType(i: UInt256, _: typedesc[Bytes]): Bytes =
|
|
|
|
i.intToBigEndian
|
|
|
|
|
|
|
|
template toType(b: string, _: typedesc[UInt256]): UInt256 =
|
|
|
|
b.toBytes.bigEndianToInt
|
|
|
|
|
|
|
|
template toType(b: string, _: typedesc[string]): string =
|
|
|
|
b
|
|
|
|
|
|
|
|
template toType(b: string, _: typedesc[Bytes]): Bytes =
|
|
|
|
b.toBytes
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
template toType(b: Bytes, _: typedesc[UInt256]): UInt256 =
|
|
|
|
b.bigEndianToInt
|
|
|
|
|
|
|
|
template toType(b: Bytes, _: typedesc[string]): string =
|
|
|
|
b.toString
|
|
|
|
|
|
|
|
template toType(b: Bytes, _: typedesc[Bytes]): Bytes =
|
|
|
|
b
|
|
|
|
|
|
|
|
proc push*(stack: var Stack, value: uint) =
|
2018-01-16 17:05:20 +00:00
|
|
|
## Push an integer onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
stack.values.add(value.u256)
|
|
|
|
|
|
|
|
proc push*(stack: var Stack, value: UInt256) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Push an integer onto the stack
|
|
|
|
ensureStackLimit()
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
stack.values.add(value)
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc push*(stack: var Stack, value: string) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Push a binary onto the stack
|
|
|
|
ensureStackLimit()
|
2018-02-06 19:20:06 +00:00
|
|
|
validateStackItem(value)
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
stack.values.add(value.toType(UInt256))
|
|
|
|
|
|
|
|
proc push*(stack: var Stack, value: Bytes) =
|
|
|
|
ensureStackLimit()
|
|
|
|
validateStackItem(value)
|
|
|
|
|
|
|
|
stack.values.add(value.toType(UInt256))
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc internalPop(stack: var Stack, numItems: int): seq[UInt256] =
|
2018-01-15 18:42:40 +00:00
|
|
|
if len(stack) < numItems:
|
|
|
|
result = @[]
|
|
|
|
else:
|
|
|
|
result = stack.values[^numItems .. ^1]
|
|
|
|
stack.values = stack.values[0 ..< ^numItems]
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc internalPop(stack: var Stack, numItems: int, T: typedesc): seq[T] =
|
2018-01-15 18:42:40 +00:00
|
|
|
result = @[]
|
|
|
|
if len(stack) < numItems:
|
|
|
|
return
|
|
|
|
|
|
|
|
for z in 0 ..< numItems:
|
|
|
|
var value = stack.values.pop()
|
2018-02-20 17:27:43 +00:00
|
|
|
result.add(toType(value, T))
|
2018-01-15 18:42:40 +00:00
|
|
|
|
|
|
|
template ensurePop(elements: untyped, a: untyped): untyped =
|
|
|
|
if len(`elements`) < `a`:
|
|
|
|
raise newException(InsufficientStack, "No stack items")
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc popInt*(stack: var Stack): UInt256 =
|
|
|
|
var elements = stack.internalPop(1, UInt256)
|
2018-01-16 18:42:38 +00:00
|
|
|
ensurePop(elements, 1)
|
|
|
|
result = elements[0]
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
macro internalPopTuple(numItems: static[int]): untyped =
|
2018-01-22 22:23:07 +00:00
|
|
|
var name = ident(&"internalPopTuple{numItems}")
|
2018-01-16 17:05:20 +00:00
|
|
|
var typ = nnkPar.newTree()
|
|
|
|
var t = ident("T")
|
2018-01-16 18:42:38 +00:00
|
|
|
var resultNode = ident("result")
|
|
|
|
var stackNode = ident("stack")
|
2018-01-16 17:05:20 +00:00
|
|
|
for z in 0 ..< numItems:
|
|
|
|
typ.add(t)
|
|
|
|
result = quote:
|
2018-01-16 18:42:38 +00:00
|
|
|
proc `name`*(`stackNode`: var Stack, `t`: typedesc): `typ`
|
|
|
|
result[^1] = nnkStmtList.newTree()
|
|
|
|
for z in 0 ..< numItems:
|
|
|
|
var zNode = newLit(z)
|
|
|
|
var element = quote:
|
|
|
|
var value = `stackNode`.values.pop()
|
2018-02-20 17:27:43 +00:00
|
|
|
`resultNode`[`zNode`] = toType(value, `t`)
|
2018-01-16 18:42:38 +00:00
|
|
|
result[^1].add(element)
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
# define pop<T> for tuples
|
|
|
|
internalPopTuple(2)
|
|
|
|
internalPopTuple(3)
|
|
|
|
internalPopTuple(4)
|
|
|
|
internalPopTuple(5)
|
|
|
|
internalPopTuple(6)
|
|
|
|
internalPopTuple(7)
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
macro popInt*(stack: typed, numItems: static[int]): untyped =
|
2018-01-16 17:05:20 +00:00
|
|
|
var resultNode = ident("result")
|
|
|
|
if numItems >= 8:
|
|
|
|
result = quote:
|
2018-02-20 17:27:43 +00:00
|
|
|
`stack`.internalPop(`numItems`, UInt256)
|
2018-01-16 17:05:20 +00:00
|
|
|
else:
|
2018-01-22 22:23:07 +00:00
|
|
|
var name = ident(&"internalPopTuple{numItems}")
|
2018-01-16 17:05:20 +00:00
|
|
|
result = quote:
|
2018-02-20 17:27:43 +00:00
|
|
|
`name`(`stack`, UInt256)
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc popBinary*(stack: var Stack): Bytes =
|
|
|
|
var elements = stack.internalPop(1, Bytes)
|
|
|
|
ensurePop(elements, 1)
|
|
|
|
result = elements[0]
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc popBinary*(stack: var Stack, numItems: int): seq[Bytes] =
|
|
|
|
result = stack.internalPop(numItems, Bytes)
|
|
|
|
ensurePop(result, numItems)
|
|
|
|
|
|
|
|
proc popString*(stack: var Stack): string =
|
2018-01-31 12:57:05 +00:00
|
|
|
var elements = stack.internalPop(1, string)
|
2018-01-15 18:42:40 +00:00
|
|
|
ensurePop(elements, 1)
|
|
|
|
result = elements[0]
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc popString*(stack: var Stack, numItems: int): seq[string] =
|
2018-01-31 12:57:05 +00:00
|
|
|
result = stack.internalPop(numItems, string)
|
2018-01-15 18:42:40 +00:00
|
|
|
ensurePop(result, numItems)
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
proc newStack*(): Stack =
|
|
|
|
new(result)
|
2018-02-07 09:50:15 +00:00
|
|
|
result.logger = logging.getLogger("stack.Stack")
|
2018-01-15 18:42:40 +00:00
|
|
|
result.values = @[]
|
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc swap*(stack: var Stack, position: int) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Perform a SWAP operation on the stack
|
|
|
|
var idx = position + 1
|
|
|
|
if idx < len(stack) + 1:
|
|
|
|
(stack.values[^1], stack.values[^idx]) = (stack.values[^idx], stack.values[^1])
|
|
|
|
else:
|
|
|
|
raise newException(InsufficientStack,
|
2018-01-22 22:23:07 +00:00
|
|
|
&"Insufficient stack items for SWAP{position}")
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc dup*(stack: var Stack, position: int | UInt256) =
|
2018-01-15 18:42:40 +00:00
|
|
|
## Perform a DUP operation on the stack
|
2018-02-06 19:20:06 +00:00
|
|
|
if (position != 0 and position.getInt < stack.len + 1) or (position == 0 and position.getInt < stack.len):
|
|
|
|
stack.push(stack.values[^position.getInt])
|
2018-01-15 18:42:40 +00:00
|
|
|
else:
|
|
|
|
raise newException(InsufficientStack,
|
2018-01-22 22:23:07 +00:00
|
|
|
&"Insufficient stack items for DUP{position}")
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-24 13:31:24 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc getInt*(stack: Stack, position: int): UInt256 =
|
|
|
|
stack.values[position]
|
2018-01-29 13:56:51 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc getBinary*(stack: Stack, position: int): Bytes =
|
|
|
|
stack.values[position].toType(Bytes)
|
|
|
|
|
|
|
|
proc getString*(stack: Stack, position: int): string =
|
|
|
|
stack.values[position].toType(string)
|
2018-01-29 13:56:51 +00:00
|
|
|
|
2018-01-24 13:31:24 +00:00
|
|
|
proc `$`*(stack: Stack): string =
|
|
|
|
let values = stack.values.mapIt(&" {$it}").join("\n")
|
|
|
|
&"Stack:\n{values}"
|