Commit 8c99299b authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2003-09-04 09:57:38 by cvscast] Adding a 'hacking' page to the website

Original author: cvscast
Date: 2003-09-04 09:57:38+00:00
parent 03be9b08
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="hacking">
<title>Hacking CDuce</title>
<box title="Introduction" link="intro">
<p>This page contains some information about internals of the CDuce
implementation. This can be useful for people wanting to
add features to CDuce. This is not for casual users ! </p>
<box title="Adding operators" link="operators">
Below is a copy of a mail to the <local href="mailing">CDuce users
mailing list</local>, that
describes how to add built-in operators/functions to CDuce.
Date: Tue, 19 Aug 2003 10:47:00 +0200 (MET DST)
From: Alain Frisch
Subject: Re: [cduce-users] System call from Cduce.
[ WARNING: technical content. Only for CDuce hackers ! ]
On Mon, 18 Aug 2003, Serge Leblanc wrote:
> Is it possible with Cduce to call systems functions like getenv ?
Not yet.
The current "CDuce standard library" is very small. I'm planning to add a
mechanism to make OCaml functions available to CDuce code in a future
release (CDuce-to-OCaml FFI).
For the moment, you can have a look at the file types/ in the
CDuce distribution. This is where the built-in operators are declared.
For instance, here is how to implement getenv:
1. Add to types/ the following:
let exn_not_found =
Value.CDuceExn (Value.Atom (Atoms.mk_ascii "Not_found"));;
unary_op_cst "getenv" string_latin1 string_latin1
(fun e ->
Location.protect_op "getenv";
let var = Value.get_string_latin1 e in
try Value.string_latin1 (Sys.getenv var)
with Not_found -> raise exn_not_found);;
- the first line register the name of the operator, the type
of the argument and the type of the result.
- the last argument is the runtime implementation of the function.
It takes the internal representation of a CDuce value (see
The first line ensures that we're not running in the web interface
(for security, most of the system calls should be disabled).
Then we extract the content of the argument as a Caml string
(Value.get_string_latin1), we apply the OCaml function
(Sys.getenv), and we wrap the result as a CDuce value
(Value.string_latin1). In case of failure, we raise
a CDuce exception (that can be catched by a CDuce try...with...),
which is internally the Caml exception Value.CDuceExn.
- I'm not completely sure about the type "string_latin1". Conceptually,
CDuce strings are sequences of Unicode codepoints. I don't know
what is the best way to interpret these sequences when communicating
with the OS; this probably depends on the current locale.
string_latin1 means that the codepoints must all be in the Latin1
character set (initial prefix of the Unicode character set),
and we communicate with the OS using the iso-8859-1 encoding.
- in this case, the typing of the operator is simple (input type /
output type). In general, it is possible to specify a more complex
typing behavior (actually, an arbitrary transformation from the
input type to the output type), but this requires to know some
internals of CDuce typing algorithm (which includes a bottom-up
propagation of typing constraints to localize errors precisely);
for instance, you can have a look at the typing function for the
"+" operator (which is overloaded: it denotes addition of integers
and merging of records).
2. Add to parser/ the following line:
| LIDENT "getenv"
after the line:
| LIDENT "load_file" | LIDENT "load_file_utf8"
in the production rule for operators in expressions.
I'm reluctant to include a lot of operators such as getenv for the moment
since I prefer to wait for the cleaner CDuce-to-OCaml FFI. Also,
the way to register operators is not documented and may change in the
future (especially if CDuce becomes a compiler, of course !).
But feel free to add whatever you want, and don't hesitate to ask if you
need assistance.
Hope this helps,
......@@ -21,6 +21,7 @@
<include file="examples.xml"/>
<include file="team.xml"/>
<include file="manual.xml"/>
<include file="hacking.xml"/>
<include file="mailing.xml"/>
<include file="sitemap.xml"/>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment