From 58c5a9d9b6d72741e87422b79c6d182dcd46d51f Mon Sep 17 00:00:00 2001 From: Productive2 <48047721+Productive2@users.noreply.github.com> Date: Thu, 22 Oct 2020 15:45:36 +0200 Subject: [PATCH] Refactoring of object structures: create japlvalue.nim --- common.nim | 4 +-- types/objecttype.nim => meta/japlvalue.nim | 29 ++++++++++++++++++++-- meta/{chunk.nim => opcode.nim} | 13 ++-------- meta/{valueobject.nim => valuearray.nim} | 19 +------------- 4 files changed, 32 insertions(+), 33 deletions(-) rename types/objecttype.nim => meta/japlvalue.nim (81%) rename meta/{chunk.nim => opcode.nim} (92%) rename meta/{valueobject.nim => valuearray.nim} (84%) diff --git a/common.nim b/common.nim index a516874..f03661e 100644 --- a/common.nim +++ b/common.nim @@ -15,9 +15,9 @@ import tables import strutils -import meta/valueobject +import meta/valuearray import meta/tokenobject -import types/objecttype +import types/japlvalue const FRAMES_MAX* = 400 # TODO: Inspect why the VM crashes if this exceeds 400 diff --git a/types/objecttype.nim b/meta/japlvalue.nim similarity index 81% rename from types/objecttype.nim rename to meta/japlvalue.nim index de35678..b2b8fa4 100644 --- a/types/objecttype.nim +++ b/meta/japlvalue.nim @@ -12,13 +12,38 @@ # See the License for the specific language governing permissions and # limitations under the License. -## Base structure for objects in JAPL, all +## Base structure for values and objects in JAPL, all ## types inherit from this simple structure import tables - type + Chunk* = ref object + ## A piece of bytecode. + ## Consts represents (TODO newdoc) + ## Code represents (TODO newdoc) + ## Lines represents (TODO newdoc) + consts*: ValueArray + code*: seq[uint8] + lines*: seq[int] + + ValueType* {.pure.} = enum + # All possible value types (this is the VM's notion of 'type', not the end user's) + Integer, Double, Bool, Nil, Object, Nan, Inf, Minf + Value* = object + ## Represents an internal JAPL type + case kind*: ValueType + of ValueType.Integer: + intValue*: int + of ValueType.Double: + floatValue*: float + of ValueType.Bool: + boolValue*: bool + of ValueType.Nil, ValueType.Inf, ValueType.Nan, ValueType.Minf: + discard + of ValueType.Object: + obj*: ptr Obj + ObjectType* {.pure.} = enum ## The type of the object ## (Also see meta/valueobject/ValueType) diff --git a/meta/chunk.nim b/meta/opcode.nim similarity index 92% rename from meta/chunk.nim rename to meta/opcode.nim index fe95beb..4201915 100644 --- a/meta/chunk.nim +++ b/meta/opcode.nim @@ -15,8 +15,7 @@ ## The module dedicated to the Chunk type ## A chunk is a piece of bytecode. -import valueobject - +import japlvalue type OpCode* {.pure.} = enum @@ -63,14 +62,6 @@ type Bnot - Chunk* = ref object - ## A piece of bytecode. - ## Consts represents (TODO newdoc) - ## Code represents (TODO newdoc) - ## Lines represents (TODO newdoc) - consts*: ValueArray - code*: seq[uint8] - lines*: seq[int] const simpleInstructions* = {OpCode.Return, OpCode.Add, OpCode.Multiply, OpCode.Divide, OpCode.Subtract, @@ -124,4 +115,4 @@ proc writeConstant*(self: Chunk, constant: Value): array[3, uint8] = ## TODO newdoc let index = self.addConstant(constant) result = cast[array[3, uint8]](index) - + diff --git a/meta/valueobject.nim b/meta/valuearray.nim similarity index 84% rename from meta/valueobject.nim rename to meta/valuearray.nim index eee103c..c456e10 100644 --- a/meta/valueobject.nim +++ b/meta/valuearray.nim @@ -19,29 +19,12 @@ ## allocated on the heap, while the simpler ones live on the stack # import ../types/functiontype -import ../types/objecttype +import japlvalue import ../types/stringtype import strformat type - ValueType* {.pure.} = enum - # All possible value types (this is the VM's notion of 'type', not the end user's) - Integer, Double, Bool, Nil, Object, Nan, Inf, Minf - Value* = object - ## Represents an internal JAPL type - case kind*: ValueType - of ValueType.Integer: - intValue*: int - of ValueType.Double: - floatValue*: float - of ValueType.Bool: - boolValue*: bool - of ValueType.Nil, ValueType.Inf, ValueType.Nan, ValueType.Minf: - discard - of ValueType.Object: - obj*: ptr Obj - ValueArray* = ref object values*: seq[Value]