2022-04-29 16:57:14 +02:00
|
|
|
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-07-10 13:19:57 +02:00
|
|
|
import strformat
|
|
|
|
import strutils
|
2022-04-29 16:57:14 +02:00
|
|
|
|
|
|
|
|
2022-05-18 13:32:32 +02:00
|
|
|
type
|
2022-04-29 16:57:14 +02:00
|
|
|
ObjectKind* = enum
|
2022-04-29 17:15:57 +02:00
|
|
|
## Enumeration of Peon
|
|
|
|
## types
|
2022-05-02 23:19:17 +02:00
|
|
|
Int8, UInt8, Int16, UInt16, Int32,
|
|
|
|
UInt32, Int64, UInt64, Float32, Float64,
|
|
|
|
Char, Byte, String, Function, CustomType,
|
2022-06-14 12:12:56 +02:00
|
|
|
Nil, Nan, Bool, Inf, Reference, Pointer
|
2022-05-02 23:19:17 +02:00
|
|
|
PeonObject* = object
|
2022-04-29 23:04:53 +02:00
|
|
|
## A generic Peon object
|
2022-05-02 23:19:17 +02:00
|
|
|
case kind*: ObjectKind:
|
2022-06-02 01:33:56 +02:00
|
|
|
of String:
|
|
|
|
str*: string
|
2022-05-02 23:19:17 +02:00
|
|
|
of Bool:
|
|
|
|
boolean*: bool
|
|
|
|
of Inf:
|
|
|
|
positive*: bool
|
|
|
|
of Byte:
|
|
|
|
`byte`*: byte
|
|
|
|
of Int8:
|
2022-06-19 14:44:14 +02:00
|
|
|
tiny*: int8
|
2022-05-02 23:19:17 +02:00
|
|
|
of UInt8:
|
|
|
|
uTiny*: uint8
|
|
|
|
of Int16:
|
|
|
|
short*: int16
|
|
|
|
of UInt16:
|
|
|
|
uShort*: uint16
|
|
|
|
of Int32:
|
|
|
|
`int`*: int32
|
|
|
|
of UInt32:
|
|
|
|
uInt*: uint32
|
|
|
|
of Int64:
|
|
|
|
long*: int64
|
|
|
|
of UInt64:
|
|
|
|
uLong*: uint64
|
|
|
|
of Nil, Nan:
|
|
|
|
discard
|
|
|
|
of CustomType:
|
|
|
|
fields*: seq[PeonObject]
|
2022-06-02 01:33:56 +02:00
|
|
|
of Float32:
|
|
|
|
halfFloat*: float32
|
|
|
|
of Float64:
|
|
|
|
`float`*: float
|
|
|
|
of Function:
|
|
|
|
ip*: uint32
|
2022-06-14 12:12:56 +02:00
|
|
|
of Reference, Pointer:
|
|
|
|
value*: ptr PeonObject
|
2022-05-02 23:19:17 +02:00
|
|
|
else:
|
2022-05-18 13:32:32 +02:00
|
|
|
discard # TODO
|
2022-07-10 13:19:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc `$`*(self: PeonObject): string =
|
|
|
|
## Returns a string representation
|
|
|
|
## of a peon object
|
|
|
|
case self.kind:
|
|
|
|
of Int64:
|
|
|
|
result = &"{self.long}'i64"
|
|
|
|
of UInt64:
|
|
|
|
result = &"{self.uLong}'u64"
|
|
|
|
of Int32:
|
|
|
|
result = &"{self.`int`}'i32"
|
|
|
|
of UInt32:
|
|
|
|
result = &"{self.uInt}'u32"
|
|
|
|
of Int16:
|
|
|
|
result = &"{self.short}'i16"
|
|
|
|
of UInt16:
|
|
|
|
result = &"{self.uShort}'u16"
|
|
|
|
of Int8:
|
|
|
|
result = &"{self.tiny}'i8"
|
|
|
|
of UInt8:
|
|
|
|
result = &"{self.uTiny}'u8"
|
|
|
|
of Float32:
|
|
|
|
result = &"{self.halfFloat}'f32"
|
|
|
|
of Float64:
|
|
|
|
result = &"{self.`float`}'f64"
|
|
|
|
of ObjectKind.Inf:
|
|
|
|
if self.positive:
|
|
|
|
result ="inf"
|
|
|
|
else:
|
|
|
|
result ="-inf"
|
|
|
|
of ObjectKind.Nan, Nil:
|
|
|
|
result =($self.kind).toLowerAscii()
|
|
|
|
of Function:
|
|
|
|
result = &"fn(ip: {self.ip})"
|
|
|
|
of CustomType:
|
|
|
|
result = "typevar"
|
|
|
|
of String:
|
|
|
|
result = self.str
|
2022-08-01 10:36:06 +02:00
|
|
|
of Bool:
|
|
|
|
if self.boolean:
|
|
|
|
result = "true"
|
|
|
|
else:
|
|
|
|
result = "false"
|
2022-07-10 13:19:57 +02:00
|
|
|
else:
|
|
|
|
discard
|