diff --git a/src/Examples.thy b/src/Examples.thy index a1e99da..7f8f56e 100644 --- a/src/Examples.thy +++ b/src/Examples.thy @@ -109,4 +109,8 @@ lemma hexadecimal_execution: lemma incomplete_rejected: "prepare ''(if x y)'' = None" by eval +lemma comment_tokens: + "lex (''(+ x; ignore ) tokens'' @ newline @ ''y) ; end'') [] [] = [''('', ''+'', ''x'', ''y'', '')'']" + by eval + end diff --git a/src/Frontend.thy b/src/Frontend.thy index 85b2075..a997121 100644 --- a/src/Frontend.thy +++ b/src/Frontend.thy @@ -6,12 +6,18 @@ fun finish :: "string \ string list \ string list" where "finish [] out = out" | "finish word out = rev word # out" -fun lex :: "string \ string \ string list \ string list" where - "lex [] word out = rev (finish word out)" -| "lex (c # cs) word out = (if c = CHR '' '' \ c = CHR 0x09 \ c = CHR 0x0A \ c = CHR 0x0D - then lex cs [] (finish word out) - else if c = CHR ''('' \ c = CHR '')'' then lex cs [] ([c] # finish word out) - else lex cs (c # word) out)" +fun scan :: "string \ string \ string list \ bool \ string list" where + "scan [] word out comment = rev (finish word out)" +| "scan (c # cs) word out comment = (if comment then + scan cs [] out (c \ CHR 0x0A \ c \ CHR 0x0D) + else if c = CHR '';'' then scan cs [] (finish word out) True + else if c = CHR '' '' \ c = CHR 0x09 \ c = CHR 0x0A \ c = CHR 0x0D + then scan cs [] (finish word out) False + else if c = CHR ''('' \ c = CHR '')'' then scan cs [] ([c] # finish word out) False + else scan cs (c # word) out False)" + +definition lex :: "string \ string \ string list \ string list" where + "lex s word out = scan s word out False" fun digit :: "char \ nat" where "digit c = (let n = nat_of_char c in diff --git a/tests/cli.py b/tests/cli.py index 1e3f962..f9936fb 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -26,6 +26,9 @@ def main(): assert int(invoke('eval',source,'0','0','0')) == 81 source.write_text('(let 1x 3 1x)') invoke(source,binary,ok=False) + for text in ['(+ x; ignore ) tokens\ny) ; end', '(+ x; comment\r\ny)', '(+ x y);end']: + source.write_text(text) + assert int(invoke('eval',source,'3','4','0')) == 7, text for text in ['0x','0x10000000000000000','0xfg','0xffextra','-0x1']: source.write_text(text) binary.write_bytes(b'preserve')