Merge pull request #13 from Productive2/master

Refactoring of object structures: create japlvalue.nim
This commit is contained in:
Mattia 2020-10-22 15:48:47 +02:00 committed by GitHub
commit 7649cf6e3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 33 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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]