Constructors capitalized

This commit is contained in:
Dmitry Boulytchev 2019-03-07 19:06:04 +03:00
parent 3bf36ae719
commit 4879a02753
18 changed files with 147 additions and 145 deletions

View file

@ -15,8 +15,7 @@
32
51
93
96
99
67
111
110
115
@ -25,8 +24,7 @@
49
44
32
96
99
67
111
110
115
@ -35,8 +33,7 @@
50
44
32
96
110
78
105
108
41

View file

@ -1,21 +1,21 @@
fun append (x, y) {
case x of
`nil -> return y
| `cons (h, t) -> return `cons (h, append (t, y))
Nil -> return y
| Cons (h, t) -> return Cons (h, append (t, y))
esac
}
fun printList (x) {
case x of
`nil -> skip
| `cons (h, t) -> write (h); printList (t)
Nil -> skip
| Cons (h, t) -> write (h); printList (t)
esac
}
n := read ();
x := `cons (1, `cons (2, `nil));
y := `cons (3, `cons (4, `nil));
x := Cons (1, Cons (2, Nil));
y := Cons (3, Cons (4, Nil));
printList (x);
printList (y);

View file

@ -1,17 +1,17 @@
fun insert (t, x) {
case t of
`leaf -> return `node (x, `leaf, `leaf)
| `node (y, l, r) -> if x > y
then return `node (y, insert (l, x), r)
else return `node (y, l, insert (r, x))
Leaf -> return Node (x, Leaf, Leaf)
| Node (y, l, r) -> if x > y
then return Node (y, insert (l, x), r)
else return Node (y, l, insert (r, x))
fi
esac
}
fun find (t, x) {
case t of
`leaf -> return 0
| `node (y, l, r) -> if x == y then return 1
Leaf -> return 0
| Node (y, l, r) -> if x == y then return 1
elif x > y then return find (l, x)
else return find (r, x)
fi
@ -20,7 +20,7 @@ fun find (t, x) {
n := read ();
t := insert (insert (insert (insert (`leaf, 5), 4), 6), 3);
t := insert (insert (insert (insert (Leaf, 5), 4), 6), 3);
write (find (t, 5));
write (find (t, 4));

View file

@ -1,15 +1,15 @@
fun f (x) {
case x of
`a -> write (1)
| `b -> write (2)
| `c -> write (3)
A -> write (1)
| B -> write (2)
| C -> write (3)
| _ -> write (4)
esac
}
x := read ();
f (`a);
f (`b);
f (`c);
f (`d)
f (A);
f (B);
f (C);
f (D)

View file

@ -1,11 +1,11 @@
fun f (a) {
case a of
`a (x, y, z) -> write (x + y + z)
| `b (x, y, z) -> write (x + y + z)
A (x, y, z) -> write (x + y + z)
| B (x, y, z) -> write (x + y + z)
esac
}
x := read ();
f (`a (100, 200, 300));
f (`b (500, 600, 700))
f (A (100, 200, 300));
f (B (500, 600, 700))

View file

@ -1,17 +1,17 @@
fun f (x) {
case x of
`nil -> write (0)
| `cons (_, `nil) -> write (1)
| `cons (_, `cons (_, `nil)) -> write (2)
| `cons (_, `cons (_, `cons (_, `nil))) -> write (3)
| _ -> write (4)
Nil -> write (0)
| Cons (_, Nil) -> write (1)
| Cons (_, Cons (_, Nil)) -> write (2)
| Cons (_, Cons (_, Cons (_, Nil))) -> write (3)
| _ -> write (4)
esac
}
x := read ();
y := `nil;
y := Nil;
for i := 0, i < 10, i := i + 1 do
f (y);
y := `cons (i, y)
y := Cons (i, y)
od

View file

@ -1,12 +1,12 @@
fun sum (x) {
case x of
`nil -> return 0
| `cons (x, tl) -> return x + sum (tl)
Nil -> return 0
| Cons (x, tl) -> return x + sum (tl)
esac
}
x := read ();
write (sum (`nil));
write (sum (`cons (100, `nil)));
write (sum (`cons (100, `cons (200, `nil))))
write (sum (Nil));
write (sum (Cons (100, Nil)));
write (sum (Cons (100, Cons (200, Nil))))

View file

@ -1,10 +1,10 @@
fun zip (x) {
case x of `pair (x, y) ->
case x of Pair (x, y) ->
case x of
`nil -> return `nil
| `cons (x, xs) -> case y of
`nil -> return `nil
| `cons (y, ys) -> return `cons (`pair (x, y), zip (`pair (xs, ys)))
Nil -> return Nil
| Cons (x, xs) -> case y of
Nil -> return Nil
| Cons (y, ys) -> return Cons (Pair (x, y), zip (Pair (xs, ys)))
esac
esac
esac
@ -12,26 +12,26 @@ fun zip (x) {
fun unzip (x) {
case x of
`nil -> return `pair (`nil, `nil)
| `cons (`pair (x, y), tl) ->
Nil -> return Pair (Nil, Nil)
| Cons (Pair (x, y), tl) ->
case unzip (tl) of
`pair (xs, ys) -> return `pair (`cons (x, xs), `cons (y, ys))
Pair (xs, ys) -> return Pair (Cons (x, xs), Cons (y, ys))
esac
esac
}
fun printList (l) {
case l of
`nil -> skip
| `cons (x, xs) -> write (x); printList (xs)
Nil -> skip
| Cons (x, xs) -> write (x); printList (xs)
esac
}
z := read ();
x := `cons (1, `cons (2, `cons (3, `nil)));
y := `cons (100, `cons (200, `cons (300, `nil)));
x := Cons (1, Cons (2, Cons (3, Nil)));
y := Cons (100, Cons (200, Cons (300, Nil)));
case unzip (zip (`pair (x, y))) of
`pair (x, y) -> printList (x); printList (y)
case unzip (zip (Pair (x, y))) of
Pair (x, y) -> printList (x); printList (y)
esac

View file

@ -9,4 +9,4 @@ printString (1.string);
printString ("abc".string);
printString ([].string);
printString ([1, 2, 3].string);
printString (`cons (1, `cons (2, `nil)).string)
printString (Cons (1, Cons (2, Nil)).string)

View file

@ -13,26 +13,26 @@ case 3 of
a@_ -> write (a)
esac;
case `a (1, 2, 3) of
`a -> write (1)
| a@`a (_, _, _) -> case a of
`a (x, y, z) -> write (x); write (y); write (z)
case A (1, 2, 3) of
A -> write (1)
| a@A (_, _, _) -> case a of
A (x, y, z) -> write (x); write (y); write (z)
esac
esac;
case `a (1, 2, 3, 4, 5) of
`a -> write (0)
| `a (_) -> write (1)
| `a (_, _) -> write (2)
| `a (_, _, _) -> write (3)
| `a (_, _, _, _) -> write (4)
| `a (_, _, _, _, _) -> write (5)
case A (1, 2, 3, 4, 5) of
A -> write (0)
| A (_) -> write (1)
| A (_, _) -> write (2)
| A (_, _, _) -> write (3)
| A (_, _, _, _) -> write (4)
| A (_, _, _, _, _) -> write (5)
esac;
write (`a (1, 2, 3, 4, 5).length);
write (A (1, 2, 3, 4, 5).length);
write (`a (1, 2, 3, 4, 5)[0]);
write (`a (1, 2, 3, 4, 5)[1]);
write (`a (1, 2, 3, 4, 5)[2]);
write (`a (1, 2, 3, 4, 5)[3]);
write (`a (1, 2, 3, 4, 5)[4])
write (A (1, 2, 3, 4, 5)[0]);
write (A (1, 2, 3, 4, 5)[1]);
write (A (1, 2, 3, 4, 5)[2]);
write (A (1, 2, 3, 4, 5)[3]);
write (A (1, 2, 3, 4, 5)[4])

View file

@ -1,6 +1,6 @@
fun collect_ints_acc (v, tail) local i {
case v of
a@#unboxed -> return `cons (a, tail)
a@#unboxed -> return Cons (a, tail)
| #string -> return tail
| _ ->
for i := 0, i < v.length, i := i + 1 do
@ -11,13 +11,13 @@ fun collect_ints_acc (v, tail) local i {
}
fun collect_ints (v) {
return collect_ints_acc (v, `nil)
return collect_ints_acc (v, Nil)
}
fun print_list (l) {
case l of
`nil -> skip
| `cons (n, t) -> write (n); print_list (t)
Nil -> skip
| Cons (n, t) -> write (n); print_list (t)
esac
}
@ -41,11 +41,11 @@ case 1 of
| a@0 -> write (a)
esac;
case `a (1, 2, 3) of
`a (1, 3, 5) -> write (0)
| `a (3, 4, 5) -> write (0)
| `a (1, 2, 3) -> write (1)
| `a (6, 7, 8) -> write (0)
case A (1, 2, 3) of
A (1, 3, 5) -> write (0)
| A (3, 4, 5) -> write (0)
| A (1, 2, 3) -> write (1)
| A (6, 7, 8) -> write (0)
esac;
case "abc" of
@ -69,4 +69,4 @@ case [1, 2, 3] of
| [a, b, c] -> write (a); write (b); write (c)
esac;
print_list (collect_ints ([1, 2, 3, [4, 5, 6, `cons (1, 2, 3)]]))
print_list (collect_ints ([1, 2, 3, [4, 5, 6, Cons (1, 2, 3)]]))

View file

@ -1,12 +1,12 @@
`Empty
`Node (0, `Empty, `Empty)
`Node (0, `Empty, `Node (1, `Empty, `Empty))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Empty)))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Empty))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Empty)))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Empty))))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Node (6, `Empty, `Empty)))))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Node (6, `Empty, `Node (7, `Empty, `Empty))))))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Node (6, `Empty, `Node (7, `Empty, `Node (8, `Empty, `Empty)))))))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Node (6, `Empty, `Node (7, `Empty, `Node (8, `Empty, `Node (9, `Empty, `Empty))))))))))
`Node (0, `Empty, `Node (1, `Empty, `Node (2, `Empty, `Node (3, `Empty, `Node (4, `Empty, `Node (5, `Empty, `Node (6, `Empty, `Node (7, `Empty, `Node (8, `Empty, `Node (9, `Empty, `Node (10, `Empty, `Empty)))))))))))
Empty
Node (0, Empty, Empty)
Node (0, Empty, Node (1, Empty, Empty))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Empty)))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Empty))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Empty)))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Empty))))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Node (6, Empty, Empty)))))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Node (6, Empty, Node (7, Empty, Empty))))))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Node (6, Empty, Node (7, Empty, Node (8, Empty, Empty)))))))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Node (6, Empty, Node (7, Empty, Node (8, Empty, Node (9, Empty, Empty))))))))))
Node (0, Empty, Node (1, Empty, Node (2, Empty, Node (3, Empty, Node (4, Empty, Node (5, Empty, Node (6, Empty, Node (7, Empty, Node (8, Empty, Node (9, Empty, Node (10, Empty, Empty)))))))))))

View file

@ -6,4 +6,4 @@
2
3
100
`cons (3, `cons (2, `cons (1, `cons (6, `cons (5, `cons (4, `cons (3, `cons (2, `cons (1, `nil)))))))))
Cons (3, Cons (2, Cons (1, Cons (6, Cons (5, Cons (4, Cons (3, Cons (2, Cons (1, Nil)))))))))

View file

@ -1,15 +1,15 @@
fun insert (tree, value) {
case tree of
`Empty -> return `Node (value, `Empty, `Empty)
| `Node (x, left, right) ->
Empty -> return Node (value, Empty, Empty)
| Node (x, left, right) ->
if x > value
then return `Node (x, insert (left, value), right)
else return `Node (x, left, insert (right, value))
then return Node (x, insert (left, value), right)
else return Node (x, left, insert (right, value))
fi
esac
}
tree := `Empty;
tree := Empty;
for i := 0, i <= 10, i := i+1 do
printf ("%s\n", tree.string);

View file

@ -1,6 +1,6 @@
fun collect_ints_acc (v, tail) local i {
case v of
a@#unboxed -> return `cons (a, tail)
a@#unboxed -> return Cons (a, tail)
| #string -> return tail
| _ ->
for i := 0, i < v.length, i := i + 1 do
@ -11,7 +11,7 @@ fun collect_ints_acc (v, tail) local i {
}
fun collect_ints (v) {
return collect_ints_acc (v, `nil)
return collect_ints_acc (v, Nil)
}
case 1 of
@ -32,11 +32,11 @@ case 1 of
| a@0 -> write (a)
esac;
case `a (1, 2, 3) of
`a (1, 3, 5) -> write (0)
| `a (3, 4, 5) -> write (0)
| `a (1, 2, 3) -> write (1)
| `a (6, 7, 8) -> write (0)
case A (1, 2, 3) of
A (1, 3, 5) -> write (0)
| A (3, 4, 5) -> write (0)
| A (1, 2, 3) -> write (1)
| A (6, 7, 8) -> write (0)
esac;
case "abc" of
@ -60,4 +60,4 @@ case [1, 2, 3] of
| [a, b, c] -> write (a); write (b); write (c)
esac;
printf ("%s\n", collect_ints ([1, 2, 3, [4, 5, 6, `cons (1, 2, 3)]]).string)
printf ("%s\n", collect_ints ([1, 2, 3, [4, 5, 6, Cons (1, 2, 3)]]).string)

View file

@ -135,7 +135,7 @@ static void printValue (void *p) {
break;
case SEXP_TAG:
printStringBuf ("`%s", de_hash (TO_SEXP(p)->tag));
printStringBuf ("%s", de_hash (TO_SEXP(p)->tag));
if (LEN(a->tag)) {
printStringBuf (" (");
for (i = 0; i < LEN(a->tag); i++) {

View file

@ -1,23 +1,27 @@
open Ostap
let parse infile =
let s = Util.read infile in
let s = Util.read infile in
let kws = [
"skip";
"if"; "then"; "else"; "elif"; "fi";
"while"; "do"; "od";
"repeat"; "until";
"for";
"fun"; "local"; "return";
"length";
"string";
"case"; "of"; "esac"; "when";
"boxed"; "unboxed"; "string"; "sexp"; "array"]
in
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.decimal s
inherit Util.Lexers.string s
inherit Util.Lexers.char s
inherit Util.Lexers.ident ["skip";
"if"; "then"; "else"; "elif"; "fi";
"while"; "do"; "od";
"repeat"; "until";
"for";
"fun"; "local"; "return";
"length";
"string";
"case"; "of"; "esac"; "when";
"boxed"; "unboxed"; "string"; "sexp"; "array"] s
inherit Util.Lexers.lident kws s
inherit Util.Lexers.uident kws s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n";
Matcher.Skip.lineComment "--";

View file

@ -46,7 +46,7 @@ module Value =
| Array a -> let n = Array.length a in
append "["; Array.iteri (fun i a -> (if i > 0 then append ", "); inner a) a; append "]"
| Sexp (t, a) -> let n = List.length a in
append "`"; append t; (if n > 0 then (append " ("; List.iteri (fun i a -> (if i > 0 then append ", "); inner a) a; append ")"))
append t; (if n > 0 then (append " ("; List.iteri (fun i a -> (if i > 0 then append ", "); inner a) a; append ")"))
in
inner v;
Bytes.of_string @@ Buffer.contents buf
@ -132,7 +132,7 @@ module Builtin =
)
| ".length" -> (st, i, o, Some (Value.of_int (match List.hd args with Value.Sexp (_, a) -> List.length a | Value.Array a -> Array.length a | Value.String s -> Bytes.length s)))
| ".array" -> (st, i, o, Some (Value.of_array @@ Array.of_list args))
| ".stringval" -> let [a] = args in (st, i, o, Some (Value.of_string @@ Value.string_val a))
| ".stringval" -> let [a] = args in (st, i, o, Some (Value.of_string @@ Value.string_val a))
end
@ -240,7 +240,8 @@ module Expr =
(* Expression parser. You can use the following terminals:
IDENT --- a non-empty identifier a-zA-Z[a-zA-Z0-9_]* as a string
LIDENT --- a non-empty identifier a-z[a-zA-Z0-9_]* as a string
UIDENT --- a non-empty identifier A-Z[a-zA-Z0-9_]* as a string
DECIMAL --- a decimal constant [0-9]+ as a string
*)
ostap (
@ -268,12 +269,12 @@ module Expr =
primary: b:base is:(-"[" i:parse -"]" {`Elem i} | -"." (%"length" {`Len} | %"string" {`Str})) *
{List.fold_left (fun b -> function `Elem i -> Elem (b, i) | `Len -> Length b | `Str -> StringVal b) b is};
base:
n:DECIMAL {Const n}
| s:STRING {String (String.sub s 1 (String.length s - 2))}
| c:CHAR {Const (Char.code c)}
| "[" es:!(Util.list0)[parse] "]" {Array es}
| "`" t:IDENT args:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match args with None -> [] | Some args -> args)}
| x:IDENT s:("(" args:!(Util.list0)[parse] ")" {Call (x, args)} | empty {Var x}) {s}
n:DECIMAL {Const n}
| s:STRING {String (String.sub s 1 (String.length s - 2))}
| c:CHAR {Const (Char.code c)}
| "[" es:!(Util.list0)[parse] "]" {Array es}
| t:UIDENT args:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match args with None -> [] | Some args -> args)}
| x:LIDENT s:("(" args:!(Util.list0)[parse] ")" {Call (x, args)} | empty {Var x}) {s}
| -"(" parse -")"
)
@ -306,17 +307,17 @@ module Stmt =
ostap (
parse:
%"_" {Wildcard}
| "`" t:IDENT ps:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match ps with None -> [] | Some ps -> ps)}
| "[" ps:(!(Util.list0)[parse]) "]" {Array ps}
| x:IDENT y:(-"@" parse)? {match y with None -> Named (x, Wildcard) | Some y -> Named (x, y)}
| c:DECIMAL {Const c}
| s:STRING {String (String.sub s 1 (String.length s - 2))}
| c:CHAR {Const (Char.code c)}
| "#" %"boxed" {Boxed}
| "#" %"unboxed" {UnBoxed}
| "#" %"string" {StringTag}
| "#" %"sexp" {SexpTag}
| "#" %"array" {ArrayTag}
| t:UIDENT ps:(-"(" !(Util.list)[parse] -")")? {Sexp (t, match ps with None -> [] | Some ps -> ps)}
| "[" ps:(!(Util.list0)[parse]) "]" {Array ps}
| x:LIDENT y:(-"@" parse)? {match y with None -> Named (x, Wildcard) | Some y -> Named (x, y)}
| c:DECIMAL {Const c}
| s:STRING {String (String.sub s 1 (String.length s - 2))}
| c:CHAR {Const (Char.code c)}
| "#" %"boxed" {Boxed}
| "#" %"unboxed" {UnBoxed}
| "#" %"string" {StringTag}
| "#" %"sexp" {SexpTag}
| "#" %"array" {ArrayTag}
)
let vars p = transform(t) (fun f -> object inherit [string list, _] @t[foldl] f method c_Named s _ name p = name :: f s p end) [] p
@ -437,7 +438,7 @@ module Stmt =
| %"repeat" s:parse %"until" e:!(Expr.parse) {Repeat (s, e)}
| %"return" e:!(Expr.parse)? {Return e}
| %"case" e:!(Expr.parse) %"of" bs:!(Util.listBy)[ostap ("|")][ostap (!(Pattern.parse) -"->" parse)] %"esac" {Case (e, bs)}
| x:IDENT
| x:LIDENT
s:(is:(-"[" !(Expr.parse) -"]")* ":=" e :!(Expr.parse) {Assign (x, is, e)} |
"(" args:!(Util.list0)[Expr.parse] ")" {Call (x, args)}
) {s}
@ -453,8 +454,8 @@ module Definition =
type t = string * (string list * string list * Stmt.t)
ostap (
arg : IDENT;
parse: %"fun" name:IDENT "(" args:!(Util.list0 arg) ")"
arg : LIDENT;
parse: %"fun" name:LIDENT "(" args:!(Util.list0 arg) ")"
locs:(%"local" !(Util.list arg))?
"{" body:!(Stmt.parse) "}" {
(name, (args, (match locs with None -> [] | Some l -> l), body))