-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
This directory contains the following directories: | ||
|
||
- **Docker**: A directory containing two Docker files for STklos (one | ||
for building STklos with the sources coming from the git tree and | ||
another one to built it from a stable release). | ||
|
||
- **Unicode**: A directory containing some files to build the UT8 tables | ||
needed by STklos | ||
|
||
- **completions**: A directory which contains code for *bash(1)* and *zsh(1)* | ||
completions. | ||
|
||
It also contains two files, which are not complete examples (it is why | ||
they are not in the `examples` directory). They can be used as | ||
templates, for some advanced work in C with STklos. | ||
- **simple-module.c** : A simple module which adds some primitives | ||
written in C to the interpreter. This module must be compiled and | ||
dynamically loaded with the interpreter (documentation can be | ||
found in the source file). | ||
- **simple-stklos.c**: A simple main program which uses the STklos | ||
library) | ||
|
||
**IMPORTANT**: this program must be updated to be run | ||
on recent versions of STklos. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* simple-module.c -- A simple C module for STklos | ||
* | ||
* Copyright © 2000-2023 Erick Gallesio <[email protected]> | ||
* Copyright © 2000-2024 Erick Gallesio <[email protected]> | ||
* | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
|
@@ -23,8 +23,10 @@ | |
* Creation date: 22-Jul-2000 12:10 (eg) | ||
*/ | ||
|
||
/* This module defines two simple primitives called "test" and "add". | ||
* - "test" takes one parameter, displays it and returns it. | ||
/* This module defines three simple primitives called "hello", "test" and "add". | ||
* - "hello" takes no parameter and displays a simple message | ||
* - "test" takes one parameter, displays it and returns it (reversed if it | ||
* is a pair, unmodified otherwise. | ||
* - "add" returns the sum of its two (small) integer parameters | ||
* | ||
* New primitives are defined with the DEFINE_PRIMITIVE macro | ||
|
@@ -54,13 +56,22 @@ | |
#include <stklos.h> | ||
|
||
|
||
DEFINE_PRIMITIVE("hello", hello, subr0, (void)) | ||
{ | ||
SCM msg = STk_Cstring2string("Hello from STklos.\n"); // convert C str -> SCM str | ||
STk_display(msg, STk_current_output_port()); // call SCM display | ||
return STk_void; // return #void | ||
} | ||
|
||
|
||
DEFINE_PRIMITIVE("test", tst, subr1, (SCM l)) | ||
{ | ||
SCM oport = STk_current_output_port(); | ||
STk_fprintf(oport, "Parameter is "); | ||
STk_display(l, oport); | ||
STk_newline(oport); | ||
return l; | ||
STk_putc('\n', oport); | ||
|
||
return CONSP(l) ? STk_cons(CDR(l), CAR(l)): l; // Reverse CONSes. Keep others | ||
} | ||
|
||
DEFINE_PRIMITIVE("add", add, subr2, (SCM a, SCM b)) | ||
|
@@ -69,20 +80,25 @@ DEFINE_PRIMITIVE("add", add, subr2, (SCM a, SCM b)) | |
if (!INTP(a)) STk_error("First parameter ~S is not an integer", a); | ||
if (!INTP(b)) STk_error("Second parameter ~S is not an integer", b); | ||
|
||
sum = INT_VAL(a) + INT_VAL(b); | ||
return MAKE_INT(sum); | ||
sum = INT_VAL(a) + INT_VAL(b); // INT_VAL returns a C int | ||
return MAKE_INT(sum); // MAKE_INT returns a SCM int | ||
} | ||
|
||
|
||
/* | ||
* The name of the module is just used to display error message. Put | ||
* something meaningful in it. The statements beween MODULE_ENTRY_START | ||
* and MODULE_ENTRY_END are executed when the module is loaded. | ||
* | ||
* ADD_PRIMITIVE_IN_MODULE adds a primitive in the given module. | ||
*/ | ||
MODULE_ENTRY_START("simple-module") | ||
{ | ||
SCM mod = STk_current_module(); | ||
STk_puts("Loading extension simple-module\n", STk_current_output_port()); | ||
ADD_PRIMITIVE(tst); | ||
ADD_PRIMITIVE(add); | ||
|
||
ADD_PRIMITIVE_IN_MODULE(hello, mod); | ||
ADD_PRIMITIVE_IN_MODULE(tst, mod); | ||
ADD_PRIMITIVE_IN_MODULE(add, mod); | ||
} | ||
MODULE_ENTRY_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters