Added a few more tests

This commit is contained in:
Mattia Giambirtone 2022-08-01 13:16:07 +02:00
parent a370961218
commit da651355b9
6 changed files with 66 additions and 5 deletions

View File

@ -263,12 +263,11 @@ proc constReadUInt8(self: PeonVM, idx: int): PeonObject =
result = PeonObject(kind: UInt8, uTiny: self.chunk.consts[idx])
proc constReadString(self: PeonVM, idx: int): PeonObject =
proc constReadString(self: PeonVM, size: int, idx: int): PeonObject =
## Reads a constant from the
## chunk's constant table and
## returns a Peon object. Assumes
## the constant is a string
let size = self.readLong()
result = PeonObject(kind: String, str: self.chunk.consts[idx..<size].fromBytes())
@ -346,7 +345,7 @@ proc dispatch*(self: PeonVM) =
of LoadUInt8:
self.push(self.constReadUInt8(int(self.readLong())))
of LoadString:
self.push(self.constReadString(int(self.readLong())))
self.push(self.constReadString(int(self.readLong()), int(self.readLong())))
of LoadFloat32:
self.push(self.constReadFloat32(int(self.readLong())))
of LoadFloat64:

View File

@ -160,10 +160,16 @@ proc loadAddressInstruction(self: Debugger, instruction: OpCode) =
proc constantInstruction(self: Debugger, instruction: OpCode) =
## Debugs instructions that operate on the constant table
var size: uint
if instruction == LoadString:
size = [self.chunk.code[self.current + 1], self.chunk.code[self.current + 2], self.chunk.code[self.current + 3]].fromTriple()
self.current += 3
var constant = [self.chunk.code[self.current + 1], self.chunk.code[self.current + 2], self.chunk.code[self.current + 3]].fromTriple()
printInstruction(instruction)
stdout.styledWriteLine(fgGreen, &", points to constant at position ", fgYellow, $constant)
stdout.styledWrite(fgGreen, &", points to constant at position ", fgYellow, $constant)
self.current += 4
if instruction == LoadString:
stdout.styledWriteLine(fgGreen, " of length ", fgYellow, $size)
proc jumpInstruction(self: Debugger, instruction: OpCode) =
@ -179,7 +185,6 @@ proc jumpInstruction(self: Debugger, instruction: OpCode) =
self.checkFrameStart(i)
proc disassembleInstruction*(self: Debugger) =
## Takes one bytecode instruction and prints it
printDebug("Offset: ")

51
tests/fib.pn Normal file
View File

@ -0,0 +1,51 @@
operator `<`(a, b: int): bool {
#pragma[magic: "LessThanInt64", pure]
}
operator `-`(a, b: float): float {
#pragma[magic: "SubFloat64", pure]
}
operator `-`(a, b: int): int {
#pragma[magic: "SubInt64", pure]
}
operator `+`(a, b: int): int {
#pragma[magic: "AddInt64", pure]
}
operator `/`(a, b: int): int {
#pragma[magic: "DivInt64", pure]
}
fn clock: float {
#pragma[magic: "SysClock64", pure]
}
fn print(x: float) {
#pragma[magic: "GenericPrint"]
}
fn print(x: int) {
#pragma[magic: "GenericPrint"]
}
fn fib(n: int): int {
if n < 2 {
return n;
}
return fib(n - 2) + fib(n - 1);
}
var x = clock();
print(fib(25));
print(clock() - x);

0
tests/import/a.pn Normal file
View File

0
tests/import/b.pn Normal file
View File

6
tests/lt_gt.pn Normal file
View File

@ -0,0 +1,6 @@
operator `<`(a, b: int): bool {#pragma[magic: "LessThanInt64", pure]}
operator `>`(a, b: int): bool {return b < a;}
3 > 2;
2 < 3;