rename old prefix kon* to nd*

This commit is contained in:
prod2 2022-01-27 05:37:10 +01:00
parent d69decebab
commit dd81e1151b
5 changed files with 94 additions and 93 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ nds
*.txt
callgrind*
test.nim
test
test
.vscode

View File

@ -19,7 +19,7 @@ type
Chunk* = object
code*: seq[uint8]
constants*: seq[KonValue]
constants*: seq[NdValue]
lines*: seq[int]
name*: string # name of the module/chunk/files
@ -62,11 +62,11 @@ proc toInt*(du8: DoubleUint8): int =
proc DU8ptrToInt*(du8: ptr uint8): int =
cast[ptr uint16](du8)[].int
proc addConstant*(ch: var Chunk, constant: KonValue): int =
proc addConstant*(ch: var Chunk, constant: NdValue): int =
ch.constants.add(constant)
ch.constants.high
proc writeConstant*(ch: var Chunk, constant: KonValue, line: int): int =
proc writeConstant*(ch: var Chunk, constant: NdValue, line: int): int =
result = ch.addConstant(constant)
ch.writeChunk(opConstant, line)
ch.writeChunk(result.toDU8, line)

View File

@ -155,7 +155,7 @@ proc writePops(comp: Compiler, count: int) =
comp.writeChunk(0, count.toDU8())
proc writeConstant(comp: Compiler, constant: KonValue) =
proc writeConstant(comp: Compiler, constant: NdValue) =
comp.stackIndex.inc
let index = comp.chunk.writeConstant(constant, comp.previous.line)
if index >= argMax:
@ -351,10 +351,10 @@ proc expression(comp: Compiler) =
proc number(comp: Compiler) =
# assume the number is already advanced through
let value = comp.previous.text.parseFloat.toKonValue
let value = comp.previous.text.parseFloat.toNdValue
comp.writeConstant(value)
when debugCompiler:
debugEcho &"Written constant (type: {value.konType}, str repr: {$value}) to chunk"
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
tkNumber.genRule(number, nop, pcNone)
@ -374,10 +374,10 @@ proc expNil(comp: Compiler) =
tkNil.genRule(expNil, nop, pcNone)
proc expString(comp: Compiler) =
let value = comp.previous.text[1..^2].toKonValue()
let value = comp.previous.text[1..^2].toNdValue()
comp.writeConstant(value)
when debugCompiler:
debugEcho &"Written constant (type: {value.konType}, str repr: {$value}) to chunk"
debugEcho &"Written constant (type: {value.ndType}, str repr: {$value}) to chunk"
tkString.genRule(expString, nop, pcNone)
@ -409,7 +409,7 @@ proc variable(comp: Compiler) =
setOp = opSetLocal
else:
# global
arg = comp.chunk.addConstant(name.toKonValue)
arg = comp.chunk.addConstant(name.toNdValue)
if comp.match(tkEqual):
# assignment (global/local)
@ -704,7 +704,7 @@ proc parseVariable(comp: Compiler, msg: string): int =
0 # index to the constant is irrelevant if the var is local
else:
# global
comp.chunk.addConstant(name.toKonValue())
comp.chunk.addConstant(name.toNdValue())
proc defineVariable(comp: Compiler, index: int) =
## Generate code that moves the variable on the stack

136
value.nim
View File

@ -1,25 +1,25 @@
import strformat
type
KonType* = enum
ktNil, ktBool, ktFloat, ktString,
ktFunct,
ktError,
NdType* = enum
ndNil, ndBool, ndFloat, ndString,
ndFunct,
ndError,
const errorTypes = {ktError}
const errorTypes = {ndError}
type
KonValue* = object
case konType*: KonType:
of ktNil:
NdValue* = object
case ndType*: NdType:
of ndNil:
discard
of ktBool:
of ndBool:
boolValue*: bool
of ktFloat:
of ndFloat:
floatValue*: float64
of ktString:
of ndString:
stringValue*: string
of ktFunct:
of ndFunct:
sdfga*: string # WHY IS THIS STRING WHEN HERE EVEN THO USELESS SPEEDING THE BENCHMARK UP
arity*: uint8 # number of arguments
entryII*: int # entry instruction index
@ -32,45 +32,45 @@ type
# KON VALUE HELPERS, MUST BE DEFINED FOR EVERY KONVALUE
proc `$`*(val: KonValue): string =
case val.konType:
of ktFloat:
proc `$`*(val: NdValue): string =
case val.ndType:
of ndFloat:
return $val.floatValue
of ktBool:
of ndBool:
return $val.boolValue
of ktNil:
of ndNil:
return "nil"
of ktString:
of ndString:
return val.stringValue
of ktFunct:
of ndFunct:
return &"Function object: {val.entryII}"
of errorTypes:
let ename = $val.konType
let ename = $val.ndType
return &"{ename[2..^1]}: {val.message}"
proc isError*(val: KonValue): bool =
val.konType in errorTypes
proc isError*(val: NdValue): bool =
val.ndType in errorTypes
proc isFalsey*(val: KonValue): bool =
val.konType in {ktNil} or (val.konType == ktBool and not val.boolValue)
proc isFalsey*(val: NdValue): bool =
val.ndType in {ndNil} or (val.ndType == ndBool and not val.boolValue)
template isTruthy*(val: KonValue): bool =
template isTruthy*(val: NdValue): bool =
not isFalsey(val)
proc equal*(val, right: KonValue): bool =
if val.konType != right.konType:
proc equal*(val, right: NdValue): bool =
if val.ndType != right.ndType:
false
else:
case val.konType:
of ktFloat:
case val.ndType:
of ndFloat:
val.floatValue == right.floatValue
of ktBool:
of ndBool:
val.boolValue == right.boolValue
of ktNil:
of ndNil:
true
of ktString:
of ndString:
val.stringValue == right.stringValue
of ktFunct:
of ndFunct:
val.entryII == right.entryII
# same entry II/module but diff arity is a bug
of errorTypes:
@ -78,23 +78,23 @@ proc equal*(val, right: KonValue): bool =
# NIM VALUE TO KON VALUE WRAPPERS
proc toKonValue*(val: float): KonValue =
KonValue(konType: ktFloat, floatValue: val)
proc toNdValue*(val: float): NdValue =
NdValue(ndType: ndFloat, floatValue: val)
proc toKonValue*(val: bool): KonValue =
KonValue(konType: ktBool, boolValue: val)
proc toNdValue*(val: bool): NdValue =
NdValue(ndType: ndBool, boolValue: val)
proc toKonValue*(val: string): KonValue =
KonValue(konType: ktString, stringValue: val)
proc toNdValue*(val: string): NdValue =
NdValue(ndType: ndString, stringValue: val)
proc newKonFunction*(ii: int, arity: uint8): KonValue =
KonValue(konType: ktFunct, entryII: ii, arity: arity)
proc newKonFunction*(ii: int, arity: uint8): NdValue =
NdValue(ndType: ndFunct, entryII: ii, arity: arity)
proc toKonValue*: KonValue =
KonValue(konType: ktNil)
proc toNdValue*: NdValue =
NdValue(ndType: ndNil)
proc konError*(msg: string): KonValue =
KonValue(konType: ktError, message: msg)
proc konError*(msg: string): NdValue =
NdValue(ndType: ndError, message: msg)
# NatReturn misc
@ -106,51 +106,51 @@ const natOk* = NatReturn(ok: true)
# OPERATIONS
# NOTE: these operations can return ktTypeError with a message if types are invalid
proc negate*(val: var KonValue): NatReturn {.inline.} =
if (val.konType != ktFloat):
proc negate*(val: var NdValue): NatReturn {.inline.} =
if (val.ndType != ndFloat):
return natError("Operand must be a number.")
else:
val.floatValue = -val.floatValue
return natOk
proc add*(val: var KonValue, right: KonValue): NatReturn {.inline.} =
if val.konType == ktFloat and right.konType == ktFloat:
proc add*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
val.floatValue += right.floatValue
elif val.konType == ktString and right.konType == ktString:
elif val.ndType == ndString and right.ndType == ndString:
val.stringValue &= right.stringValue
else:
return natError(&"Attempt to add types {val.konType} and {right.konType}.")
return natError(&"Attempt to add types {val.ndType} and {right.ndType}.")
return natOk
proc subtract*(val: var KonValue, right: KonValue): NatReturn {.inline.} =
if val.konType == ktFloat and right.konType == ktFloat:
proc subtract*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
val.floatValue -= right.floatValue
else:
return natError(&"Attempt to subtract types {val.konType} and {right.konType}.")
return natError(&"Attempt to subtract types {val.ndType} and {right.ndType}.")
return natOk
proc multiply*(val: var KonValue, right: KonValue): NatReturn {.inline.} =
if val.konType == ktFloat and right.konType == ktFloat:
proc multiply*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
val.floatValue *= right.floatValue
else:
return natError(&"Attempt to multiply types {val.konType} and {right.konType}.")
return natError(&"Attempt to multiply types {val.ndType} and {right.ndType}.")
return natOk
proc divide*(val: var KonValue, right: KonValue): NatReturn {.inline.} =
if val.konType == ktFloat and right.konType == ktFloat:
proc divide*(val: var NdValue, right: NdValue): NatReturn {.inline.} =
if val.ndType == ndFloat and right.ndType == ndFloat:
val.floatValue /= right.floatValue
else:
return natError(&"Attempt to divide types {val.konType} and {right.konType}.")
return natError(&"Attempt to divide types {val.ndType} and {right.ndType}.")
return natOk
proc `<`*(val: KonValue, right: KonValue): KonValue =
if val.konType == ktFloat and right.konType == ktFloat:
return toKonValue(val.floatValue < right.floatValue)
proc `<`*(val: NdValue, right: NdValue): NdValue =
if val.ndType == ndFloat and right.ndType == ndFloat:
return toNdValue(val.floatValue < right.floatValue)
else:
return konError(&"Attempt to compare types {val.konType} and {right.konType}.")
return konError(&"Attempt to compare types {val.ndType} and {right.ndType}.")
proc `>`*(val: KonValue, right: KonValue): KonValue =
if val.konType == ktFloat and right.konType == ktFloat:
return toKonValue(val.floatValue > right.floatValue)
proc `>`*(val: NdValue, right: NdValue): NdValue =
if val.ndType == ndFloat and right.ndType == ndFloat:
return toNdValue(val.floatValue > right.floatValue)
else:
return konError(&"Attempt to compare types {val.konType} and {right.konType}.")
return konError(&"Attempt to compare types {val.ndType} and {right.ndType}.")

28
vm.nim
View File

@ -28,9 +28,9 @@ proc run*(chunk: Chunk): InterpretResult =
var
ip: ptr uint8 = chunk.code[0].unsafeAddr
stack: seq[KonValue] = newSeqOfCap[KonValue](256)
stack: seq[NdValue] = newSeqOfCap[NdValue](256)
hadError: bool
globals: Table[string, KonValue]
globals: Table[string, NdValue]
frames: seq[Frame] = newSeqOfCap[Frame](4)
proc runtimeError(msg: string) =
@ -39,14 +39,14 @@ proc run*(chunk: Chunk): InterpretResult =
write stderr, &"[line: {line}] {msg}\n"
hadError = true
template peek(stack: seq[KonValue]): KonValue =
template peek(stack: seq[NdValue]): NdValue =
stack[stack.high]
proc popn(stack: var seq[KonValue], amt: int) {.inline.} =
proc popn(stack: var seq[NdValue], amt: int) {.inline.} =
for i in countup(1, amt):
discard stack.pop()
proc pushSafe(stack: var seq[KonValue], val: KonValue): bool =
proc pushSafe(stack: var seq[NdValue], val: NdValue): bool =
## DEPRECATED
## returns if the value is not a runtime error
## prints the error if it is a runtime error
@ -70,7 +70,7 @@ proc run*(chunk: Chunk): InterpretResult =
result = ip.DU8ptrToInt
ip = ip.padd(argSize)
proc readConstant(): KonValue =
proc readConstant(): NdValue =
let index = readDU8()
chunk.constants[index]
@ -109,7 +109,7 @@ proc run*(chunk: Chunk): InterpretResult =
let amt = readUI8()
stack.popn(amt)
of opConstant:
let val: KonValue = readConstant()
let val: NdValue = readConstant()
stack.add(val)
of opNegate:
let res = stack.peek().negate
@ -146,15 +146,15 @@ proc run*(chunk: Chunk): InterpretResult =
else:
ip = frames.pop().returnIp # remove frame that's over
of opTrue:
stack.add(toKonValue(true))
stack.add(toNdValue(true))
of opFalse:
stack.add(toKonValue(false))
stack.add(toNdValue(false))
of opNil:
stack.add(toKonValue())
stack.add(toNdValue())
of opNot:
stack.add(toKonValue(stack.pop().isFalsey))
stack.add(toNdValue(stack.pop().isFalsey))
of opEqual:
stack.add(toKonValue(stack.pop().equal(stack.pop())))
stack.add(toNdValue(stack.pop().equal(stack.pop())))
of opLess:
if not stack.pushSafe(stack.pop() > stack.pop()):
break
@ -213,14 +213,14 @@ proc run*(chunk: Chunk): InterpretResult =
let argcountUint8 = readUI8Noconv()
let argcount = argcountUint8.int
let funct = stack[stack.high - argcount]
if funct.konType != ktFunct:
if funct.ndType != ndFunct:
runtimeError("Attempt to call a non-funct (a defunct?).") # here is a bad defunct joke
break
if funct.arity != argcountUint8:
runtimeError(&"Wrong number of arguments, expected {funct.arity}, got {argcount}.")
break
stack[stack.high - argcount] = toKonValue() # replace the function with nil: this is the return value slot
stack[stack.high - argcount] = toNdValue() # replace the function with nil: this is the return value slot
# create new frame
frames.add(Frame(stackBottom: stack.high - argcount, returnIp: ip))