| LIBFTH(3) | FreeBSD Library Functions Manual | LIBFTH(3) |
% cat hello.c
#include <fth.h>
int
main(int argc, char *argv[])
{
fth_init();
fth_printf("%s, World%c\n", "Hello", '!');
return (EXIT_SUCCESS);
}
Compile and link it with:% cc -I/usr/local/include/fth -I/usr/local/include -c hello.c % cc hello.o -o hello -L/usr/local/lib -lfth -lmA test run looks like this:
% ./hello ⇒ Hello, World!
% cat libhello.c
#include <fth.h>
/*
* hello-prim ( str1 -- str2 )
* intro hello-prim ⇒ "Hello, World!"
*/
static void
ficl_hello_primitive(ficlVm *vm)
{
FTH intro, world, result;
FTH_STACK_CHECK(vm, 1, 1);
intro = fth_pop_ficl_cell(vm);
world = fth_variable_ref("world");
result = fth_make_string_format("%S, %S!", intro, world);
fth_push_ficl_cell(vm, result);
}
/*
* hello-proc ( str1 -- str2 )
* intro hello-proc ⇒ "Hello, World!"
*/
static FTH
fth_hello_procedure(FTH intro)
{
FTH world, result;
world = fth_variable_ref("world");
result = fth_make_string_format("%S, %S!", intro, world);
return (result);
}
void
Init_libhello(void)
{
fth_define_variable("intro",
fth_make_string("Hello"), NULL);
fth_define_constant("world",
fth_make_string("World"), NULL);
FTH_PRI1("hello-prim",
ficl_hello_primitive, NULL);
FTH_PROC("hello-proc",
fth_hello_procedure, 1, 0, 0, NULL);
}
Compile and link it with:% cc -fPIC -I/usr/local/include/fth -I/usr/local/include -c libhello.c % cc -shared -o libhello.so libhello.o -L/usr/local/lib -lfth -lmInstalling isn't necessarily required for testing. Start fth and load the new library with
The new words hello-prim and hello-proc as well as the variable intro and the constant world are available.dl-load ./libhello.so Init_libhello
% fth fth> dl-load ./libhello.so Init_libhello fth> intro hello-prim ⇒ "Hello, World!" fth> intro hello-proc ⇒ "Hello, World!" fth> "Goodbye" to intro fth> intro hello-prim ⇒ "Goodbye, World!" fth> "Bye" hello-proc ⇒ "Bye, World!" fth> bye %If the new library is finished, one can install it with
After installing you can load your new library with% fth -ve "install libhello.so"
dl-load libhello Init_libhello
Assocs:
Lists:
File test functions:
Property functions:
Initialization functions:
Evaluation functions:
Loading source files:
Rest:
Random numbers:
Complex numbers:
Big and rational numbers
Object type related functions:
Instance related functions:
Object set functions:
Object functions:
Cycle through objects:
Stack access functions:
If proc is not a Proc object, return FTH_FALSE, If proc doesn't leave a return value on stack, return FTH_FALSE, if proc leaves a singel value on stack, return it, if proc leaves more than one values on stack, return them as Array object.
If proc is not a Proc object, return FTH_FALSE, If proc doesn't leave a return value on stack, return FTH_FALSE, if proc leaves a singel value on stack, return it, if proc leaves more than one values on stack, return them as Array object.
/*
* ARGS: any object
*/
fth_throw_error(FTH_BAD_ARITY, proc);
⇒ #<bad-arity in test-proc>
/*
* ARGS: nil or #()
*/
fth_throw_error(FTH_BAD_ARITY, FTH_NIL);
⇒ #<bad-arity: proc has bad arity>
/*
* ARGS: #( string )
*/
fth_throw_error(FTH_BAD_ARITY,
FTH_LIST_1(fth_make_string("test-proc"));
⇒ #<bad-arity in test-proc>
/*
* ARGS: #( fmt arg1 arg2 arg3 )
*/
fth_throw_error(FTH_BAD_ARITY,
FTH_LIST_4(fth_make_string("%s: %s args require, got %s"),
proc,
FTH_TWO,
FTH_THREE));
⇒ #<bad-arity in test-proc: 2 args required, got 3>
libfth has no extra floating point stack; floats are of type ficlFloat.
| November 28, 2012 | FreeBSD 9.1 |