mirror of https://github.com/janet-lang/janet
Update examples, add method like semantics to calling keywords.
This commit is contained in:
parent
be85196de8
commit
d64a57297d
|
@ -18,3 +18,11 @@
|
||||||
(ret 0) # return $0
|
(ret 0) # return $0
|
||||||
]
|
]
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
# Test it
|
||||||
|
|
||||||
|
(defn testn
|
||||||
|
[n]
|
||||||
|
(print "fibasm(" n ") = " (fibasm n)))
|
||||||
|
|
||||||
|
(for i 0 10 (testn i))
|
||||||
|
|
|
@ -35,7 +35,13 @@
|
||||||
:bright-white 97
|
:bright-white 97
|
||||||
:bg-bright-white 107})
|
:bg-bright-white 107})
|
||||||
|
|
||||||
(loop [[name color] :in (pairs colormap)]
|
(defn color
|
||||||
(defglobal (string/slice name 1)
|
"Take a string made by concatenating xs and colorize it for an ANSI terminal."
|
||||||
(fn color-wrapper [& pieces]
|
[c & xs]
|
||||||
(string "\e[" color "m" ;pieces "\e[0m"))))
|
(def code (get colormap c))
|
||||||
|
(if (not code) (error (string "color " c " unknown")))
|
||||||
|
(string "\e[" code "m" ;xs "\e[0m"))
|
||||||
|
|
||||||
|
# Print all colors
|
||||||
|
|
||||||
|
(loop [c :keys colormap] (print (color c c)))
|
||||||
|
|
|
@ -556,6 +556,12 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) {
|
||||||
if (fiber->stacktop > fiber->maxstack) {
|
if (fiber->stacktop > fiber->maxstack) {
|
||||||
vm_throw("stack overflow");
|
vm_throw("stack overflow");
|
||||||
}
|
}
|
||||||
|
if (janet_checktype(callee, JANET_KEYWORD)) {
|
||||||
|
vm_commit();
|
||||||
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
||||||
|
if (argc < 1) janet_panicf("method call takes at least 1 argument, got %d", argc);
|
||||||
|
callee = janet_get(fiber->data[fiber->stackstart], callee);
|
||||||
|
}
|
||||||
if (janet_checktype(callee, JANET_FUNCTION)) {
|
if (janet_checktype(callee, JANET_FUNCTION)) {
|
||||||
func = janet_unwrap_function(callee);
|
func = janet_unwrap_function(callee);
|
||||||
janet_stack_frame(stack)->pc = pc;
|
janet_stack_frame(stack)->pc = pc;
|
||||||
|
@ -587,6 +593,12 @@ static JanetSignal run_vm(JanetFiber *fiber, Janet in) {
|
||||||
VM_OP(JOP_TAILCALL)
|
VM_OP(JOP_TAILCALL)
|
||||||
{
|
{
|
||||||
Janet callee = stack[D];
|
Janet callee = stack[D];
|
||||||
|
if (janet_checktype(callee, JANET_KEYWORD)) {
|
||||||
|
vm_commit();
|
||||||
|
int32_t argc = fiber->stacktop - fiber->stackstart;
|
||||||
|
if (argc < 1) janet_panicf("method call takes at least 1 argument, got %d", argc);
|
||||||
|
callee = janet_get(fiber->data[fiber->stackstart], callee);
|
||||||
|
}
|
||||||
if (janet_checktype(callee, JANET_FUNCTION)) {
|
if (janet_checktype(callee, JANET_FUNCTION)) {
|
||||||
func = janet_unwrap_function(callee);
|
func = janet_unwrap_function(callee);
|
||||||
if (janet_fiber_funcframe_tail(fiber, func)) {
|
if (janet_fiber_funcframe_tail(fiber, func)) {
|
||||||
|
|
Loading…
Reference in New Issue