mirror of https://github.com/japl-lang/japl.git
Made print a variadic function and added clock() for benchmarking
This commit is contained in:
parent
1e2227ea38
commit
875440ba8c
|
@ -6,4 +6,4 @@ fun fib(n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fib(20); //TODO: Benchmark this
|
print(fib(15)); //TODO: Benchmark this
|
||||||
|
|
|
@ -25,7 +25,7 @@ import stdlib
|
||||||
|
|
||||||
proc repl() =
|
proc repl() =
|
||||||
var bytecodeVM = initVM()
|
var bytecodeVM = initVM()
|
||||||
stdlibInit(bytecodeVM)
|
bytecodeVM.stdlibInit()
|
||||||
echo JAPL_VERSION_STRING
|
echo JAPL_VERSION_STRING
|
||||||
echo &"[Nim {NimVersion} on {hostOs} ({hostCPU})]"
|
echo &"[Nim {NimVersion} on {hostOs} ({hostCPU})]"
|
||||||
when DEBUG_TRACE_VM:
|
when DEBUG_TRACE_VM:
|
||||||
|
@ -75,6 +75,7 @@ proc main(file: string = "") =
|
||||||
except IOError:
|
except IOError:
|
||||||
echo &"Error: '{file}' could not be read, probably you don't have the permission to read it"
|
echo &"Error: '{file}' could not be read, probably you don't have the permission to read it"
|
||||||
var bytecodeVM = initVM()
|
var bytecodeVM = initVM()
|
||||||
|
bytecodeVM.stdlibInit()
|
||||||
when DEBUG_TRACE_VM:
|
when DEBUG_TRACE_VM:
|
||||||
echo "Debugger enabled, expect verbose output\n"
|
echo "Debugger enabled, expect verbose output\n"
|
||||||
echo "==== VM Constants ====\n"
|
echo "==== VM Constants ====\n"
|
||||||
|
|
|
@ -1,15 +1,53 @@
|
||||||
|
# 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 implements stdlib functions for JAPL
|
# Implementations of builtin functions and modules
|
||||||
|
|
||||||
import vm
|
import vm
|
||||||
import types/native
|
import types/native
|
||||||
import types/baseObject
|
import types/baseObject
|
||||||
import types/japlNil
|
import types/japlNil
|
||||||
|
import types/numbers
|
||||||
import types/methods
|
import types/methods
|
||||||
|
import times
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc natPrint(args: seq[ptr Obj]): tuple[ok: bool, result: ptr Obj] =
|
proc natPrint(args: seq[ptr Obj]): tuple[ok: bool, result: ptr Obj] =
|
||||||
echo args[0].stringify()
|
## Native function print
|
||||||
|
## Prints an object representation
|
||||||
|
## to stdout. If more than one argument
|
||||||
|
## is passed, they will be printed separated
|
||||||
|
## by a space
|
||||||
|
var res = ""
|
||||||
|
for arg in args:
|
||||||
|
res = res & arg.stringify() & " "
|
||||||
|
echo res
|
||||||
return (ok: true, result: asNil())
|
return (ok: true, result: asNil())
|
||||||
|
|
||||||
|
|
||||||
|
proc natClock(args: seq[ptr Obj]): tuple[ok: bool, result: ptr Obj] =
|
||||||
|
## Native function clock
|
||||||
|
## Returns the current unix
|
||||||
|
## time (also known as epoch)
|
||||||
|
## with subsecond precision
|
||||||
|
|
||||||
|
# TODO: Move this to a separate module once we have imports
|
||||||
|
|
||||||
|
result = (ok: true, result: getTime().toUnixFloat().asFloat())
|
||||||
|
|
||||||
|
|
||||||
template stdlibInit*(vm: VM) =
|
template stdlibInit*(vm: VM) =
|
||||||
vm.defineGlobal("print", newNative("print", natPrint, 1))
|
vm.defineGlobal("print", newNative("print", natPrint, -1))
|
||||||
|
vm.defineGlobal("clock", newNative("clock", natClock, 0))
|
||||||
|
|
|
@ -231,8 +231,9 @@ proc call(self: var VM, function: ptr Function, argCount: int): bool =
|
||||||
self.frameCount += 1
|
self.frameCount += 1
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
|
||||||
proc call(self: var VM, native: ptr Native, argCount: int): bool =
|
proc call(self: var VM, native: ptr Native, argCount: int): bool =
|
||||||
if argCount != native.arity:
|
if argCount != native.arity and native.arity != -1:
|
||||||
self.error(newTypeError(&"function '{stringify(native.name)}' takes {native.arity} argument(s), got {argCount}"))
|
self.error(newTypeError(&"function '{stringify(native.name)}' takes {native.arity} argument(s), got {argCount}"))
|
||||||
return false
|
return false
|
||||||
let slot = self.stack.high() - argCount + 1
|
let slot = self.stack.high() - argCount + 1
|
||||||
|
@ -242,14 +243,15 @@ proc call(self: var VM, native: ptr Native, argCount: int): bool =
|
||||||
let nativeResult = native.nimproc(args)
|
let nativeResult = native.nimproc(args)
|
||||||
if not nativeResult.ok:
|
if not nativeResult.ok:
|
||||||
self.error(cast[ptr JaplException](nativeResult.result))
|
self.error(cast[ptr JaplException](nativeResult.result))
|
||||||
# assumes that all native procs behave well, and if not ok, they
|
# assumes that all native procs behave well, and if not ok, they
|
||||||
# only return japl exceptions
|
# only return japl exceptions
|
||||||
for i in countup(slot - 1, self.stack.high()):
|
for i in countup(slot - 1, self.stack.high()):
|
||||||
discard self.pop() # TODO once stack is a custom datatype,
|
discard self.pop() # TODO once stack is a custom datatype,
|
||||||
# just reduce its length
|
# just reduce its length
|
||||||
self.push(nativeResult.result)
|
self.push(nativeResult.result)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
|
||||||
proc callObject(self: var VM, callee: ptr Obj, argCount: uint8): bool =
|
proc callObject(self: var VM, callee: ptr Obj, argCount: uint8): bool =
|
||||||
## Wrapper around call() to do type checking
|
## Wrapper around call() to do type checking
|
||||||
if callee.isCallable():
|
if callee.isCallable():
|
||||||
|
|
Loading…
Reference in New Issue