mirror of https://github.com/japl-lang/japl.git
Consirerable refactoring needed
This commit is contained in:
parent
84db1ce890
commit
c31fcd1142
|
@ -0,0 +1,30 @@
|
|||
# Copyright 2020 Mattia Giambirtone
|
||||
#
|
||||
# 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.
|
||||
|
||||
## Base structure for values and objects in JAPL, all
|
||||
## types inherit from this simple structure
|
||||
|
||||
import tables
|
||||
import ../types/objecttype
|
||||
|
||||
|
||||
type
|
||||
Chunk* = ref object
|
||||
## A piece of bytecode.
|
||||
## Consts represents (TODO newdoc)
|
||||
## Code represents (TODO newdoc)
|
||||
## Lines represents (TODO newdoc)
|
||||
consts*: seq[ptr Obj]
|
||||
code*: seq[uint8]
|
||||
lines*: seq[int]
|
|
@ -15,6 +15,7 @@
|
|||
## The module dedicated to the Chunk type
|
||||
## A chunk is a piece of bytecode.
|
||||
|
||||
import ../types/objecttype
|
||||
|
||||
type
|
||||
OpCode* {.pure.} = enum
|
||||
|
@ -90,7 +91,7 @@ const jumpInstructions* = {OpCode.JumpIfFalse, OpCode.Jump, OpCode.Loop}
|
|||
|
||||
proc newChunk*(): Chunk =
|
||||
## The constructor for the type Chunk
|
||||
result = Chunk(consts: ValueArray(values: @[]), code: @[], lines: @[])
|
||||
result = Chunk(consts: @[], code: @[], lines: @[])
|
||||
|
||||
|
||||
proc writeChunk*(self: Chunk, newByte: uint8, line: int) =
|
||||
|
@ -107,18 +108,18 @@ proc writeChunk*(self: Chunk, bytes: array[3, uint8], line: int) =
|
|||
|
||||
proc freeChunk*(self: Chunk) =
|
||||
## Resets a chunk to its initial value.
|
||||
self.consts = ValueArray(values: @[])
|
||||
self.consts = @[]
|
||||
self.code = @[]
|
||||
self.lines = @[]
|
||||
|
||||
|
||||
proc addConstant*(self: Chunk, constant: Value): int =
|
||||
proc addConstant*(self: Chunk, constant: ptr Obj): int =
|
||||
## Adds a constant to a chunk. Returns its index.
|
||||
self.consts.values.add(constant)
|
||||
return self.consts.values.high() # The index of the constant
|
||||
self.consts.add(constant)
|
||||
return self.consts.high() # The index of the constant
|
||||
|
||||
|
||||
proc writeConstant*(self: Chunk, constant: Value): array[3, uint8] =
|
||||
proc writeConstant*(self: Chunk, constant: ptr Obj): array[3, uint8] =
|
||||
## Writes a constant to a chunk. Returns its index casted to an array.
|
||||
## TODO newdoc
|
||||
let index = self.addConstant(constant)
|
|
@ -0,0 +1,133 @@
|
|||
# Copyright 2020 Mattia Giambirtone
|
||||
#
|
||||
# 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.
|
||||
|
||||
## This module represents the generic interface that JAPL uses internally
|
||||
## to represent types. Small-sized entities such as numbers and booleans are
|
||||
## treated differently with respect to bigger and more complex ones such as
|
||||
## strings and functions. That is because those more comolex entities are
|
||||
## allocated on the heap, while the simpler ones live on the stack
|
||||
|
||||
# import ../types/functiontype
|
||||
import japlvalue
|
||||
import ../types/stringtype
|
||||
import strformat
|
||||
|
||||
|
||||
type
|
||||
ValueArray* = ref object
|
||||
values*: seq[Value]
|
||||
|
||||
|
||||
func newValueArray*(): ValueArray =
|
||||
## Creates a new ValueArray
|
||||
result = ValueArray(values: @[])
|
||||
|
||||
|
||||
func writeValueArray*(arr: var ValueArray, value: Value) =
|
||||
## Adds a value to a ValueArray object
|
||||
arr.values.add(value)
|
||||
|
||||
|
||||
func isNil*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL nil object
|
||||
result = value.kind == ValueType.Nil
|
||||
|
||||
|
||||
func isBool*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL bool
|
||||
result = value.kind == ValueType.Bool
|
||||
|
||||
|
||||
func isInt*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL integer
|
||||
result = value.kind == ValueType.Integer
|
||||
|
||||
|
||||
func isFloat*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL float
|
||||
result = value.kind == ValueType.Double
|
||||
|
||||
|
||||
func isInf*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL inf object
|
||||
result = value.kind == ValueType.Inf or value.kind == ValueType.Minf
|
||||
|
||||
|
||||
func isNan*(value: Value): bool =
|
||||
## Returns true if the given value
|
||||
## is a JAPL nan object
|
||||
result = value.kind == ValueType.Nan
|
||||
|
||||
|
||||
func isNum*(value: Value): bool =
|
||||
## Returns true if the given value is
|
||||
## either a JAPL number, nan or inf
|
||||
result = isInt(value) or isFloat(value) or isInf(value) or isNan(value)
|
||||
|
||||
|
||||
func isObj*(value: Value): bool =
|
||||
## Returns if the current value is a JAPL object
|
||||
result = value.kind == ValueType.Object
|
||||
|
||||
|
||||
func isStr*(value: Value): bool =
|
||||
## Returns true if the given object is a JAPL string
|
||||
result = isObj(value) and value.obj.kind == ObjectType.String
|
||||
|
||||
|
||||
func toBool*(value: Value): bool =
|
||||
## Converts a JAPL bool to a nim bool
|
||||
result = value.boolValue
|
||||
|
||||
|
||||
func toInt*(value: Value): int =
|
||||
## Converts a JAPL int to a nim int
|
||||
result = value.intValue
|
||||
|
||||
|
||||
func toFloat*(value: Value): float =
|
||||
## Converts a JAPL float to a nim float
|
||||
result = value.floatValue
|
||||
|
||||
|
||||
func toStr*(value: Value): string =
|
||||
## Converts a JAPL string into a nim string
|
||||
var strObj = cast[ptr String](value.obj)
|
||||
for i in 0..strObj.str.len - 1:
|
||||
result.add(strObj.str[i])
|
||||
|
||||
|
||||
func asInt*(n: int): Value =
|
||||
## Creates an int object
|
||||
result = Value(kind: ValueType.Integer, intValue: n)
|
||||
|
||||
|
||||
func asFloat*(n: float): Value =
|
||||
## Creates a float object (double)
|
||||
result = Value(kind: ValueType.Double, floatValue: n)
|
||||
|
||||
|
||||
func asBool*(b: bool): Value =
|
||||
## Creates a boolean object
|
||||
result = Value(kind: ValueType.Bool, boolValue: b)
|
||||
|
||||
|
||||
proc asStr*(s: string): Value =
|
||||
## Creates a string object
|
||||
result = Value(kind: ValueType.Object, obj: newString(s))
|
|
@ -16,7 +16,7 @@
|
|||
## types inherit from this simple structure
|
||||
|
||||
import tables
|
||||
import ../meta/chunk
|
||||
import ../meta/japlvalue
|
||||
|
||||
|
||||
type
|
||||
|
@ -105,8 +105,13 @@ proc bool*(obj: ptr Obj): bool =
|
|||
|
||||
|
||||
proc eq*(a: ptr Obj, b: ptr Obj): bool =
|
||||
## Compares two
|
||||
result = a.kind == b.kind
|
||||
## Compares two objects for equality
|
||||
|
||||
if obj.kind != ObjectType.BaseObject:
|
||||
var newObj = convert obj
|
||||
result = newObj.eq()
|
||||
else:
|
||||
result = a.kind == b.kind
|
||||
|
||||
|
||||
proc hash*(self: ptr Obj): uint32 =
|
||||
|
|
|
@ -42,7 +42,7 @@ proc hash*(self: ptr String): uint32 =
|
|||
return result
|
||||
|
||||
|
||||
proc valuesEqual*(a: ptr String, b: ptr String): bool =
|
||||
proc eq*(a: ptr String, b: ptr String): bool =
|
||||
if a.len != b.len:
|
||||
return false
|
||||
elif a.hash != b.hash:
|
||||
|
|
Loading…
Reference in New Issue