Tigerインタプリタでコメントをつけれるようにしたい
#OCaml
from タイガーブック
(* ... *) に囲まれた部分をコメントとして読みとばせるようにしたい。
code:diff
diff --git a/lib/lexer.mll b/lib/lexer.mll
index 8031094..7d1ebed 100644
--- a/lib/lexer.mll
+++ b/lib/lexer.mll
@@ -14,6 +14,7 @@ let printable = symbol | letter | digit
(* 字句解析の規則 *)
rule token = parse
space+ { token lexbuf } (* 空白は読み飛ばす *)
+ | "(*" { comment lexbuf } (* コメントは読み飛ばす *)
| "let" { Parser.LET }
| "in" { Parser.IN }
| "end" { Parser.END }
@@ -43,6 +44,10 @@ rule token = parse
| eof { Parser.EOF }
| _ { failwith ("invalid character " ^ (Lexing.lexeme lexbuf)) }
+and comment = parse
+ "*)" { token lexbuf }
+ | _ { comment lexbuf }
+
and string_literal buf = parse
"\\t" { Buffer.add_string buf "\x09"; string_literal buf lexbuf }
| "\\n" { Buffer.add_string buf "\x0A"; string_literal buf lexbuf }
diff --git a/samples/fib.tiger b/samples/fib.tiger
index 8fba652..c24b140 100644
--- a/samples/fib.tiger
+++ b/samples/fib.tiger
@@ -1,3 +1,4 @@
+(* 10番目のフィボナッチ数を返す *)
let
function fib(n:int) =
if n < 2 then n