Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CodeMirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Moritz Aurel Pascal Schubotz
CodeMirror
Commits
5b3b2f93
Commit
5b3b2f93
authored
12 years ago
by
Jan Keromnes
Committed by
Marijn Haverbeke
12 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[ocaml] new mode
parent
204f30d9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mode/ocaml/index.html
+130
-0
130 additions, 0 deletions
mode/ocaml/index.html
mode/ocaml/ocaml.js
+85
-0
85 additions, 0 deletions
mode/ocaml/ocaml.js
with
215 additions
and
0 deletions
mode/ocaml/index.html
0 → 100644
+
130
−
0
View file @
5b3b2f93
<!doctype html>
<meta
charset=
utf-8
>
<title>
CodeMirror: OCaml mode
</title>
<link
rel=
stylesheet
href=
../../lib/codemirror.css
>
<link
rel=
stylesheet
href=
../../doc/docs.css
>
<style
type=
text/css
>
.CodeMirror
{
border-top
:
1px
solid
black
;
border-bottom
:
1px
solid
black
;}
</style>
<script
src=
../../lib/codemirror.js
></script>
<script
src=
ocaml.js
></script>
<h1>
CodeMirror: OCaml mode
</h1>
<textarea
id=
code
>
(* Summing a list of integers *)
let rec sum xs =
match xs with
| [] -
>
0
| x :: xs' -
>
x + sum xs'
(* Quicksort *)
let rec qsort = function
| [] -
>
[]
| pivot :: rest -
>
let is_less x = x
<
pivot in
let left, right = List.partition is_less rest in
qsort left @ [pivot] @ qsort right
(* Fibonacci Sequence *)
let rec fib_aux n a b =
match n with
| 0 -
>
a
| _ -
>
fib_aux (n - 1) (a + b) a
let fib n = fib_aux n 0 1
(* Birthday paradox *)
let year_size = 365.
let rec birthday_paradox prob people =
let prob' = (year_size -. float people) /. year_size *. prob in
if prob'
<
0.5 then
Printf.printf "answer = %d\n" (people+1)
else
birthday_paradox prob' (people+1) ;;
birthday_paradox 1.0 1
(* Church numerals *)
let zero f x = x
let succ n f x = f (n f x)
let one = succ zero
let two = succ (succ zero)
let add n1 n2 f x = n1 f (n2 f x)
let to_string n = n (fun k -
>
"S" ^ k) "0"
let _ = to_string (add (succ two) two)
(* Elementary functions *)
let square x = x * x;;
let rec fact x =
if x
<
= 1 then 1 else x * fact (x - 1);;
(* Automatic memory management *)
let l = 1 :: 2 :: 3 :: [];;
[1; 2; 3];;
5 :: l;;
(* Polymorphism: sorting lists *)
let rec sort = function
| [] -
>
[]
| x :: l -
>
insert x (sort l)
and insert elem = function
| [] -
>
[elem]
| x :: l -
>
if elem
<
x then elem :: x :: l else x :: insert elem l;;
(* Imperative features *)
let add_polynom p1 p2 =
let n1 = Array.length p1
and n2 = Array.length p2 in
let result = Array.create (max n1 n2) 0 in
for i = 0 to n1 - 1 do result.(i)
<
- p1.(i) done;
for i = 0 to n2 - 1 do result.(i)
<
- result.(i) + p2.(i) done;
result;;
add_polynom [| 1; 2 |] [| 1; 2; 3 |];;
(* We may redefine fact using a reference cell and a for loop *)
let fact n =
let result = ref 1 in
for i = 2 to n do
result := i * !result
done;
!result;;
fact 5;;
(* Triangle (graphics) *)
let () =
ignore( Glut.init Sys.argv );
Glut.initDisplayMode ~double_buffer:true ();
ignore (Glut.createWindow ~title:"OpenGL Demo");
let angle t = 10. *. t *. t in
let render () =
GlClear.clear [ `color ];
GlMat.load_identity ();
GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. ();
GlDraw.begins `triangles;
List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.];
GlDraw.ends ();
Glut.swapBuffers () in
GlMat.mode `modelview;
Glut.displayFunc ~cb:render;
Glut.idleFunc ~cb:(Some Glut.postRedisplay);
Glut.mainLoop ()
(* A Hundred Lines of Caml - http://caml.inria.fr/about/taste.en.html *)
(* OCaml page on Wikipedia - http://en.wikipedia.org/wiki/OCaml *)
</textarea>
<script>
var
editor
=
CodeMirror
.
fromTextArea
(
document
.
getElementById
(
'
code
'
),
{
mode
:
'
ocaml
'
,
lineNumbers
:
true
,
matchBrackets
:
true
});
</script>
<p><strong>
MIME types defined:
</strong>
<code>
text/x-ocaml
</code>
.
</p>
This diff is collapsed.
Click to expand it.
mode/ocaml/ocaml.js
0 → 100644
+
85
−
0
View file @
5b3b2f93
CodeMirror
.
defineMode
(
'
ocaml
'
,
function
(
config
)
{
var
atoms
=
[
'
true
'
,
'
false
'
],
keywords
=
[
'
let
'
,
'
rec
'
,
'
in
'
,
'
of
'
,
'
and
'
,
'
succ
'
,
'
if
'
,
'
then
'
,
'
else
'
,
'
for
'
,
'
to
'
,
'
while
'
,
'
do
'
,
'
done
'
,
'
fun
'
,
'
function
'
,
'
val
'
,
'
type
'
,
'
mutable
'
,
'
match
'
,
'
with
'
,
'
try
'
,
'
raise
'
,
'
begin
'
,
'
end
'
],
commands
=
[
'
open
'
,
'
print_string
'
,
'
print_endline
'
,
'
trace
'
,
'
ignore
'
,
'
exit
'
];
function
tokenBase
(
stream
,
state
)
{
var
sol
=
stream
.
sol
();
var
ch
=
stream
.
next
();
if
(
ch
===
'
"
'
)
{
state
.
tokenize
=
tokenString
;
return
state
.
tokenize
(
stream
,
state
);
}
if
(
ch
===
'
(
'
)
{
if
(
stream
.
eat
(
'
*
'
))
{
state
.
commentLevel
++
;
state
.
tokenize
=
tokenComment
;
return
state
.
tokenize
(
stream
,
state
);
}
}
if
(
ch
===
'
~
'
)
{
stream
.
eatWhile
(
/
\w
/
);
return
'
variable-2
'
;
}
if
(
ch
===
'
`
'
)
{
stream
.
eatWhile
(
/
\w
/
);
return
'
quote
'
;
}
if
(
/
\d
/
.
test
(
ch
))
{
stream
.
eatWhile
(
/
[\d]
/
);
if
(
stream
.
eat
(
'
.
'
))
{
stream
.
eatWhile
(
/
[\d]
/
);
}
return
'
number
'
;
}
if
(
/
[
+
\-
*&%=<>!?|
]
/
.
test
(
ch
))
{
return
'
operator
'
;
}
stream
.
eatWhile
(
/
\w
/
);
var
cur
=
stream
.
current
();
if
(
atoms
.
indexOf
(
cur
)
!==
-
1
)
return
'
atom
'
;
if
(
commands
.
indexOf
(
cur
)
!==
-
1
)
return
'
builtin
'
;
if
(
keywords
.
indexOf
(
cur
)
!==
-
1
)
return
'
keyword
'
;
return
'
word
'
;
}
function
tokenString
(
stream
,
state
)
{
var
next
,
end
=
false
,
escaped
=
false
;
while
((
next
=
stream
.
next
())
!=
null
)
{
if
(
next
===
'
"
'
&&
!
escaped
)
{
end
=
true
;
break
;
}
escaped
=
!
escaped
&&
next
===
'
\\
'
;
}
if
(
end
&&
!
escaped
)
{
state
.
tokenize
=
tokenBase
;
}
return
'
string
'
;
};
function
tokenComment
(
stream
,
state
)
{
var
prev
,
next
;
while
(
state
.
commentLevel
>
0
&&
(
next
=
stream
.
next
())
!=
null
)
{
if
(
prev
===
'
(
'
&&
next
===
'
*
'
)
state
.
commentLevel
++
;
if
(
prev
===
'
*
'
&&
next
===
'
)
'
)
state
.
commentLevel
--
;
prev
=
next
;
}
if
(
state
.
commentLevel
<=
0
)
{
state
.
tokenize
=
tokenBase
;
}
return
'
comment
'
;
}
return
{
startState
:
function
()
{
return
{
tokenize
:
tokenBase
,
commentLevel
:
0
}},
token
:
function
(
stream
,
state
)
{
if
(
stream
.
eatSpace
())
return
null
;
return
state
.
tokenize
(
stream
,
state
);
}
};
});
CodeMirror
.
defineMIME
(
'
text/x-ocaml
'
,
'
ocaml
'
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment