mirror of https://github.com/japl-lang/japl.git
parent
5d98814d7d
commit
fb11433477
|
@ -2,8 +2,7 @@
|
|||
// from nim itself (the nim compiler options are identical to those of
|
||||
// production.json)
|
||||
{"flags": {
|
||||
"gc": "none",
|
||||
"d": "danger"
|
||||
"gc": "none"
|
||||
},
|
||||
"verbose": true,
|
||||
"override_config": true,
|
||||
|
|
|
@ -36,6 +36,8 @@ import config
|
|||
when isMainModule:
|
||||
import util/debug
|
||||
import types/methods
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
import terminal
|
||||
|
||||
|
||||
type
|
||||
|
@ -212,7 +214,11 @@ proc emitByte(self: Compiler, byt: OpCode|uint8) =
|
|||
## Emits a single bytecode instruction and writes it
|
||||
## to the current chunk being compiled
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
echo "DEBUG - Compiler: Emitting " & $byt & " (uint8 value of " & $(uint8 byt) & ")"
|
||||
write stdout, &"DEBUG - Compiler: Emitting {$byt} (uint8 value of {$(uint8 byt)}"
|
||||
if byt.int() <= OpCode.high().int():
|
||||
write stdout, &"; opcode value of {$byt.OpCode}"
|
||||
write stdout, ")\n"
|
||||
|
||||
self.currentChunk.writeChunk(uint8 byt, self.parser.previous.line)
|
||||
|
||||
|
||||
|
@ -652,6 +658,10 @@ proc emitJump(self: Compiler, opcode: OpCode): int =
|
|||
self.emitByte(opcode)
|
||||
self.emitByte(0xff)
|
||||
self.emitByte(0xff)
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgYellow)
|
||||
write stdout, &"DEBUG - Compiler: emit jump @ {self.currentChunk.code.len-2}\n"
|
||||
setForegroundColor(fgDefault)
|
||||
return self.currentChunk.code.len - 2
|
||||
|
||||
|
||||
|
@ -669,14 +679,23 @@ proc patchJump(self: Compiler, offset: int) =
|
|||
## be jumped over, so the size of the if/else conditions
|
||||
## or loops is limited (hopefully 65 thousands and change
|
||||
## instructions are enough for everyone)
|
||||
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgYellow)
|
||||
write stdout, &"DEBUG - Compiler: patching jump @ {offset}"
|
||||
let jump = self.currentChunk.code.len - offset - 2
|
||||
if jump > (int uint16.high):
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgDefault)
|
||||
write stdout, "\n"
|
||||
self.compileError("too much code to jump over")
|
||||
else:
|
||||
let casted = toDouble(jump)
|
||||
self.currentChunk.code[offset] = casted[0]
|
||||
self.currentChunk.code[offset + 1] = casted[1]
|
||||
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
write stdout, &" points to {casted[0]}, {casted[1]} = {jump}\n"
|
||||
setForegroundColor(fgDefault)
|
||||
|
||||
proc ifStatement(self: Compiler) =
|
||||
## Parses if statements in a C-style fashion
|
||||
|
@ -703,14 +722,22 @@ proc ifStatement(self: Compiler) =
|
|||
|
||||
proc emitLoop(self: Compiler, start: int) =
|
||||
## Creates a loop and emits related instructions.
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgYellow)
|
||||
write stdout, &"DEBUG - Compiler: emitting loop at start {start} "
|
||||
self.emitByte(OpCode.Loop)
|
||||
var offset = self.currentChunk.code.len - start + 2
|
||||
if offset > (int uint16.high):
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgDefault)
|
||||
write stdout, "\n"
|
||||
self.compileError("loop body is too large")
|
||||
else:
|
||||
let offsetBytes = toDouble(offset)
|
||||
self.emitByte(offsetBytes[0])
|
||||
self.emitByte(offsetBytes[1])
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
write stdout, &"pointing to {offsetBytes[0]}, {offsetBytes[1]} = {offset}\n"
|
||||
|
||||
|
||||
proc endLooping(self: Compiler) =
|
||||
|
|
|
@ -21,6 +21,7 @@ import ../types/methods
|
|||
import ../types/arraylist
|
||||
import strformat
|
||||
import terminal
|
||||
import ../multibyte
|
||||
|
||||
proc printName(name: string) =
|
||||
setForegroundColor(fgGreen)
|
||||
|
@ -62,11 +63,9 @@ proc constantInstruction(name: string, chunk: Chunk, offset: int): int =
|
|||
|
||||
proc jumpInstruction(name: string, chunk: Chunk, offset: int): int =
|
||||
var jumpArray: array[2, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2]]
|
||||
var jump: int
|
||||
copyMem(jump.addr, jumpArray.addr, sizeof(uint16))
|
||||
write stdout, &"DEBUG - VM:\tInstruction -> "
|
||||
printName(name)
|
||||
write stdout, &"\nDEBUG - VM:\tJump size -> {jump}"
|
||||
write stdout, &"\nDEBUG - VM:\tJump size -> {jumpArray.fromDouble()} ( = {$jumpArray[0]}, {$jumpArray[1]})"
|
||||
nl()
|
||||
return offset + 3
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ import types/typeutils
|
|||
import types/function
|
||||
import types/native
|
||||
import types/arraylist
|
||||
import multibyte
|
||||
# We always import it to
|
||||
# avoid the compiler complaining
|
||||
# about functions not existing
|
||||
|
@ -281,9 +282,7 @@ proc readBytes(self: CallFrame): int =
|
|||
proc readShort(self: CallFrame): uint16 =
|
||||
## Reads a 16 bit number from the
|
||||
## given frame's chunk
|
||||
let arr = [self.readByte(), self.readByte()]
|
||||
copyMem(result.addr, unsafeAddr(arr), sizeof(uint16))
|
||||
|
||||
fromDouble([self.readByte(), self.readByte()])
|
||||
|
||||
proc readConstant(self: CallFrame): ptr Obj =
|
||||
## Reads a constant from the given
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
//[Test: defaults]
|
||||
//[skip]
|
||||
//[source:mixed]
|
||||
var a = 3;
|
||||
fun test(b = a) {
|
||||
print (b);
|
||||
}
|
||||
a = 9;
|
||||
test();//stdout:3
|
||||
//[end]
|
||||
//[end]
|
Loading…
Reference in New Issue