Update pl0 for ebnf changes, and add more tests
parent
eef6ae7827
commit
208c88c449
|
@ -1,7 +1,7 @@
|
|||
! Copyright (C) 2007 Chris Double.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
!
|
||||
USING: kernel tools.test peg peg.pl0 ;
|
||||
USING: kernel tools.test peg peg.pl0 multiline sequences ;
|
||||
IN: peg.pl0.tests
|
||||
|
||||
{ "abc" } [
|
||||
|
@ -11,3 +11,89 @@ IN: peg.pl0.tests
|
|||
{ 55 } [
|
||||
"55abc" number parse parse-result-ast
|
||||
] unit-test
|
||||
|
||||
{ t } [
|
||||
<"
|
||||
VAR x, squ;
|
||||
|
||||
PROCEDURE square;
|
||||
BEGIN
|
||||
squ := x * x
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
x := 1;
|
||||
WHILE x <= 10 DO
|
||||
BEGIN
|
||||
CALL square;
|
||||
x := x + 1;
|
||||
END
|
||||
END.
|
||||
"> program parse parse-result-remaining empty?
|
||||
] unit-test
|
||||
|
||||
{ f } [
|
||||
<"
|
||||
CONST
|
||||
m = 7,
|
||||
n = 85;
|
||||
|
||||
VAR
|
||||
x, y, z, q, r;
|
||||
|
||||
PROCEDURE multiply;
|
||||
VAR a, b;
|
||||
|
||||
BEGIN
|
||||
a := x;
|
||||
b := y;
|
||||
z := 0;
|
||||
WHILE b > 0 DO BEGIN
|
||||
IF ODD b THEN z := z + a;
|
||||
a := 2 * a;
|
||||
b := b / 2;
|
||||
END
|
||||
END;
|
||||
|
||||
PROCEDURE divide;
|
||||
VAR w;
|
||||
BEGIN
|
||||
r := x;
|
||||
q := 0;
|
||||
w := y;
|
||||
WHILE w <= r DO w := 2 * w;
|
||||
WHILE w > y DO BEGIN
|
||||
q := 2 * q;
|
||||
w := w / 2;
|
||||
IF w <= r THEN BEGIN
|
||||
r := r - w;
|
||||
q := q + 1
|
||||
END
|
||||
END
|
||||
END;
|
||||
|
||||
PROCEDURE gcd;
|
||||
VAR f, g;
|
||||
BEGIN
|
||||
f := x;
|
||||
g := y;
|
||||
WHILE f # g DO BEGIN
|
||||
IF f < g THEN g := g - f;
|
||||
IF g < f THEN f := f - g;
|
||||
END;
|
||||
z := f
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
x := m;
|
||||
y := n;
|
||||
CALL multiply;
|
||||
x := 25;
|
||||
y := 3;
|
||||
CALL divide;
|
||||
x := 84;
|
||||
y := 36;
|
||||
CALL gcd;
|
||||
END.
|
||||
"> program parse parse-result-remaining empty?
|
||||
] unit-test
|
|
@ -1,30 +1,31 @@
|
|||
! Copyright (C) 2007 Chris Double.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel arrays strings math.parser sequences
|
||||
peg peg.ebnf peg.parsers memoize ;
|
||||
peg peg.ebnf peg.parsers memoize namespaces ;
|
||||
IN: peg.pl0
|
||||
|
||||
#! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
|
||||
MEMO: ident ( -- parser )
|
||||
CHAR: a CHAR: z range
|
||||
CHAR: A CHAR: Z range 2array choice repeat1
|
||||
[ >string ] action ;
|
||||
[
|
||||
CHAR: a CHAR: z range ,
|
||||
CHAR: A CHAR: Z range ,
|
||||
] choice* repeat1 [ >string ] action ;
|
||||
|
||||
MEMO: number ( -- parser )
|
||||
CHAR: 0 CHAR: 9 range repeat1 [ string>number ] action ;
|
||||
|
||||
<EBNF
|
||||
program = block '.' .
|
||||
block = [ 'const' ident '=' number { ',' ident '=' number } ';' ]
|
||||
[ 'var' ident { ',' ident } ';' ]
|
||||
{ 'procedure' ident ';' [ block ';' ] } statement .
|
||||
statement = [ ident ':=' expression | 'call' ident |
|
||||
'begin' statement {';' statement } 'end' |
|
||||
'if' condition 'then' statement |
|
||||
'while' condition 'do' statement ] .
|
||||
condition = 'odd' expression |
|
||||
expression ('=' | '#' | '<=' | '<' | '>=' | '>') expression .
|
||||
expression = ['+' | '-'] term {('+' | '-') term } .
|
||||
term = factor {('*' | '/') factor } .
|
||||
factor = ident | number | '(' expression ')'
|
||||
program = block "."
|
||||
block = [ "CONST" ident "=" number { "," ident "=" number } ";" ]
|
||||
[ "VAR" ident { "," ident } ";" ]
|
||||
{ "PROCEDURE" ident ";" [ block ";" ] } statement
|
||||
statement = [ ident ":=" expression | "CALL" ident |
|
||||
"BEGIN" statement {";" statement } "END" |
|
||||
"IF" condition "THEN" statement |
|
||||
"WHILE" condition "DO" statement ]
|
||||
condition = "ODD" expression |
|
||||
expression ("=" | "#" | "<=" | "<" | ">=" | ">") expression
|
||||
expression = ["+" | "-"] term {("+" | "-") term }
|
||||
term = factor {("*" | "/") factor }
|
||||
factor = ident | number | "(" expression ")"
|
||||
EBNF>
|
||||
|
|
Loading…
Reference in New Issue