Commit 49960418 authored by Pietro Abate's avatar Pietro Abate

[r2003-10-10 22:00:14 by cvscast] Logical operators + regexp example

Original author: cvscast
Date: 2003-10-10 22:00:15+00:00
parent 79a7451f
......@@ -3,6 +3,7 @@
* -o option for compilation
* .cd suffixes are now optional in command line
* Bugfixes
* Add logical operators &&, ||, and not
0.2.0
* Code upgraded to Ocaml 3.07+beta2
......
......@@ -43,6 +43,7 @@ and pexpr =
| Abstraction of abstr
(* Data constructors *)
| Const of Types.Const.t
| Integer of Intervals.V.t
| Char of Chars.V.t
| Pair of pexpr * pexpr
......
......@@ -51,7 +51,7 @@ let tuple_queue =
let char = mknoloc (Internal (Types.char Chars.any))
let string_regexp = Star (Elem char)
let cst_nil = Atom (U.mk "nil")
let cst_nil = Const (Types.Atom (Atoms.V.mk_ascii "nil"))
let pat_nil = mknoloc (Internal (Sequence.nil_type))
let seq_of_string s =
......@@ -171,6 +171,7 @@ EXTEND
| "transform" | "fun" | "in"
| "let" | "type" | "debug" | "include"
| "and" | "validate" | "schema" | "namespace" | "ref" | "using"
| "not"
]
-> a
]
......@@ -207,6 +208,11 @@ EXTEND
exp loc (Match (e1, [pat_nil,e2]))
| "ref"; p = pat; e = expr ->
exp loc (Ref (e,p))
| "not"; e = expr ->
let p1 = mknoloc (Internal true_type)
and p2 = mknoloc (Internal false_type) in
exp loc (Match (e, [p1,Const (Types.Atom (false_atom));
p2,Const (Types.Atom (true_atom))]))
]
|
[ e1 = expr; ":="; e2 = expr ->
......@@ -224,11 +230,19 @@ EXTEND
|
[ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr ->
exp loc (Op (op,[e1;e2]))
| e1 = expr; "||"; e2 = expr ->
let p1 = mknoloc (Internal true_type)
and p2 = mknoloc (Internal false_type) in
exp loc (Match (e1, [p1,Const (Types.Atom (true_atom)); p2,e2]))
| e = expr; "\\"; l = [IDENT | keyword ] ->
exp loc (RemoveField (e, label l))
]
|
[ e1 = expr; op = ["*"]; e2 = expr -> exp loc (Op (op,[e1;e2]))
| e1 = expr; "&&"; e2 = expr ->
let p1 = mknoloc (Internal true_type)
and p2 = mknoloc (Internal false_type) in
exp loc (Match (e1, [p1,e2;p2,Const (Types.Atom (false_atom))]))
| e = expr; op = "/"; p = pat ->
let tag = mk loc (Internal (Types.atom (Atoms.any))) in
......
......@@ -74,7 +74,7 @@ let rec token = lexer
return lexbuf ("INT", L.utf8_lexeme lexbuf)
| [ "<>=.,:;+-*/@&{}[]()|?`!" ]
| "->" | "::" | ";;" | "--" | ":=" | "\\" | "++"
| "{|" | "|}" | "<=" | ">=" | "<<" | ">>"
| "{|" | "|}" | "<=" | ">=" | "<<" | ">>" | "||" | "&&"
| ["?+*"] "?" | "#" ->
return lexbuf ("", L.utf8_lexeme lexbuf)
| "#" ncname ->
......
......@@ -19,13 +19,13 @@ let compare = 1
module CompUnit = struct
include Pool.Make(Utf8)
module Tbl = Inttbl
let pervasives = mk (U.mk "Pervasives")
let close_serialize_ref = ref (fun () -> assert false)
let depend = Inttbl.create ()
let serialize t cu =
if cu != pervasives then Inttbl.add depend cu ();
serialize t cu
......
......@@ -146,6 +146,7 @@ let rec const env loc = function
| Atom t -> Types.Atom (parse_atom env loc t)
| Integer i -> Types.Integer i
| Char c -> Types.Char c
| Const c -> c
| _ -> raise_loc_generic loc "This should be a scalar or structured constant"
(* I. Transform the abstract syntax of types and patterns into
......@@ -821,7 +822,7 @@ let rec expr env loc = function
Typed.fun_fv = fv
} in
exp loc fv e
| (Integer _ | Char _ | Atom _) as c ->
| (Integer _ | Char _ | Atom _ | Const _) as c ->
exp loc Fv.empty (Typed.Cst (const env loc c))
| Pair (e1,e2) ->
let (fv1,e1) = expr env loc e1 and (fv2,e2) = expr env loc e2 in
......
......@@ -37,6 +37,24 @@ A detailed explanation of the code can be found <a href="tutorial_overloading.ht
</sample>
</box>
<box title="Datatypes + first-class functions" link="data">
<p>
The program below shows how to simulate ML data types in CDuce.
It implements a (naive backtracking) regular expression recognizer.
The examples also demonstrate the use of first-class functions
(used as continuations).
</p>
<p>
Exercise for the reader: show that the algorithm may not terminate for
some special regular expressions.
</p>
<sample highlight="false">
<include-verbatim file="regexp.cd"/>
</sample>
</box>
<box title="The script that generates this site" link="site">
<p>
The script below is one of the longest CDuce application ever written
......
......@@ -368,6 +368,14 @@ match %%e1%% with `true -> %%e2%% | `false -> %%e3%%
<p>
Note that the else-clause is mandatory.
</p>
<p>
The infix operators <code>||</code> and <code>&amp;&amp;</code>
denote respectively the logical or and the logical and. The prefix
operator <code>not</code> denotes the logical negation.
</p>
</box>
<box title="Upward coercions" link="upward">
......
......@@ -55,6 +55,8 @@ _" character, starting by a capitalized letter or underscore.</li>
<br/> <code>+,*,-,div,mod</code> : Integer,Integer -> Integer
<br/> <code>=, &lt;&lt;, &lt;=, &gt;&gt;, &gt;= </code> :
<i>t</i>,<i>t</i> -> Bool = <code>`true | `false</code> (any non functional type <i>t</i>)
<br/> <code>||, &amp;&amp;</code> : Bool,Bool -> Bool
<br/> <code>not</code>: Bool -> Bool
</li>
<li>Prefix:
<br/><code>load_xml,load_html</code> : String -> Any,
......
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