a few minor changes, so it compiles

This commit is contained in:
Productive2 2020-10-25 17:47:53 +01:00
parent 26f035a998
commit 44aab6f5fc
5 changed files with 55 additions and 21 deletions

View File

@ -24,8 +24,6 @@ import meta/opcode
import meta/token
import meta/looptype
import types/jobject
import types/jstring
import types/function
import tables
import config
when isMainModule:
@ -1081,6 +1079,9 @@ proc getRule(kind: TokenType): ParseRule =
proc compile*(self: ref Compiler, source: string): ptr Function =
when DEBUG_TRACE_COMPILER:
echo "==== COMPILER debugger starts ===="
echo ""
## Compiles a source string into a function
## object. This wires up all the code
## inside the parser and the lexer
@ -1090,10 +1091,19 @@ proc compile*(self: ref Compiler, source: string): ptr Function =
self.parser = initParser(tokens, self.file)
while not self.parser.match(EOF):
self.declaration()
var function = self.endCompiler()
when DEBUG_TRACE_COMPILER:
echo "==== COMPILER debugger ends ===="
echo ""
if not self.parser.hadError:
when DEBUG_TRACE_COMPILER:
echo "Result: Ok"
return function
else:
when DEBUG_TRACE_COMPILER:
echo "Result: Fail"
return nil
else:
return nil
@ -1126,10 +1136,10 @@ proc initCompiler*(context: FunctionType, enclosing: ref Compiler = nil, parser:
# This way the compiler can be executed on its own
# without the VM
when isMainModule:
var compiler: ref Compiler = initCompiler(SCRIPT, file="test")
echo "JAPL Compiler REPL"
while true:
try:
var compiler: ref Compiler = initCompiler(SCRIPT, file="test")
stdout.write("=> ")
var compiled = compiler.compile(stdin.readLine())
if compiled != nil:

View File

@ -17,8 +17,8 @@
const FRAMES_MAX* = 400 # TODO: Inspect why the VM crashes if this exceeds 400
const JAPL_VERSION* = "0.2.0"
const JAPL_RELEASE* = "alpha"
const DEBUG_TRACE_VM* = false # Traces VM execution
const DEBUG_TRACE_VM* = false # Traces VM execution
const DEBUG_TRACE_GC* = true # Traces the garbage collector (TODO)
const DEBUG_TRACE_ALLOCATION* = true # Traces memory allocation/deallocation (WIP)
const DEBUG_TRACE_COMPILER* = true # Traces the compiler (TODO)
const DEBUG_TRACE_COMPILER* = false # Traces the compiler (WIP)

View File

@ -80,11 +80,6 @@ const byteInstructions* = {OpCode.SetLocal, OpCode.GetLocal, OpCode.DeleteLocal,
const jumpInstructions* = {OpCode.JumpIfFalse, OpCode.Jump, OpCode.Loop}
proc newChunk*(): Chunk =
## The constructor for the type Chunk
result = Chunk(consts: @[], code: @[], lines: @[])
proc writeChunk*(self: Chunk, newByte: uint8, line: int) =
## Appends newByte at line to a chunk.
self.code.add(newByte)

View File

@ -16,7 +16,6 @@
## types inherit from this simple structure
import ../memory
import ../meta/opcode
import strformat
@ -65,6 +64,11 @@ type
message*: ptr String
proc newChunk*(): Chunk =
## The constructor for the type Chunk
result = Chunk(consts: @[], code: @[], lines: @[])
proc allocateObject*(size: int, kind: ObjectType): ptr Obj =
## Wrapper around reallocate to create a new generic JAPL object
result = cast[ptr Obj](reallocate(nil, 0, size))
@ -152,8 +156,8 @@ proc stringify(self: ptr Float): string =
# Function object methods
type
FunctionType* = enum
FUNC, SCRIPT
FunctionType* {.pure.} = enum
Func, Script
proc newFunction*(name: string = "", chunk: Chunk = newChunk(), arity: int = 0): ptr Function =
@ -394,7 +398,7 @@ proc asNan*(): ptr NotANumber =
result = allocateObj(NotANumber, ObjectType.Nan)
proc asInf*(): ptr Infinity =
proc asInf*(): ptr Infinity =
## Creates a nil object
result = allocateObj(Infinity, ObjectType.Infinity)
@ -403,4 +407,32 @@ proc asObj*(obj: ptr Obj): ptr Obj =
## Creates a generic JAPL object
result = allocateObj(Obj, ObjectType.BaseObject)
proc newIndexError*(message: string): ptr JAPLException =
result = allocateObj(JAPLException, ObjectType.Exception)
result.errName = newString("IndexError")
result.message = newString(message)
proc newReferenceError*(message: string): ptr JAPLException =
result = allocateObj(JAPLException, ObjectType.Exception)
result.errName = newString("ReferenceError")
result.message = newString(message)
proc newInterruptedError*(message: string): ptr JAPLException =
result = allocateObj(JAPLException, ObjectType.Exception)
result.errName = newString("InterruptedError")
result.message = newString(message)
proc newRecursionError*(message: string): ptr JAPLException =
result = allocateObj(JAPLException, ObjectType.Exception)
result.errName = newString("RecursionError")
result.message = newString(message)
proc newTypeError*(message: string): ptr JAPLException =
result = allocateObj(JAPLException, ObjectType.Exception)
result.errName = newString("TypeError")
result.message = newString(message)

View File

@ -24,10 +24,7 @@ import compiler
import tables
import meta/opcode
import meta/frame
import types/exceptions
import types/jobject
import types/jstring
import types/function
import memory
when DEBUG_TRACE_VM:
import util/debug
@ -41,10 +38,10 @@ proc `**`(a, b: float): float = pow(a, b)
type
KeyboardInterrupt* = object of CatchableError
InterpretResult = enum
OK,
COMPILE_ERROR,
RUNTIME_ERROR
InterpretResult {.pure.} = enum
Ok,
CompileError,
RuntimeError
VM* = ref object # The VM object
lastPop*: ptr Obj
frameCount*: int