Skip to content

Commit f881ab4

Browse files
author
Sascha Rechenberger
committed
Merge branch 'dev'
2 parents bb41117 + cd4b797 commit f881ab4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3961
-4019
lines changed

README.md

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,85 @@
11
grammata
22
========
33

4-
**grammata** is a simple script language interpreted and run as a monadic structure.
5-
In future it will support polyparadigmatic features such as functional expressions and logical knowledge bases and queries to use them.
4+
**grammata** is a simple script language interpreted and run on a monadic virtual machine.
5+
It supports polyparadigmatic features such as
6+
* imperative procedures and functions
7+
* functional expressions
8+
* logical knowledge bases and queries to use them
69

710
Syntax
811
------
912

10-
Every *program* has the stucture `program { <DECLARAION>* <STATEMENT>* }`,
11-
whereas
12-
* **`DECLARATION`** is
13-
* an uninitialized variable `var <ID>;`.
14-
* a number initialized variable `var <ID> := <NUMBER>;`.
15-
* a function initialized variable `var <ID> := func ( <PARAM>* ) { <DECLARATION>* <STATEMENT>* };`.
16-
17-
* **`STATEMENT`** is
18-
* an assignment `<ID> := <NUMBER>;`
19-
* a for loop `for( <COUNTER> ; <END> ; <STEP> ) { <STATEMENT>* }`, whereas
20-
* **`COUNTER`** is the `ID` of the counter variable.
21-
* the value of the `EXPRESSION` **`END`** must be less (if `STEP` < 0) or greater (if `STEP` >= 0) than `COUNTER` to continue the loop.
22-
* the value of the `EXPRESSION` **`STEP`** is added to the counter variable after every iteration.
23-
* a while loop `while ( <COND> ) { <STATEMENT>* };` runs till the `EXPRESSION` `COND` is less then 0.
24-
* a do while loop `do { <STATEMENT>* } while ( <COND> );` runs till the `EXPRESSION` `COND` is less then 0. The body will be executed at least once.
25-
* a one armed if `if ( <COND> ) then { <STATEMENT>* };`. The then block will be executed if the `EXPRESSION` `COND` is greater or equal to 0.
26-
* a two armed if `if ( <COND> ) then { <STATEMENT>* } else { <STATEMENT>* };`. The then block will be executed if the `EXPRESSION` `COND` is greater or equal to 0, the else block otherwise.
27-
* a return `return <TORETURN>`, which will either print the result of the program or return a value to the calling function; however, the value of the `EXPRESSION` `TORETURN` is returned.
28-
* **`ID`** is an identifier beginning with a lower or uppercase letter and consisting of lower and uppercase letters and ciphers.
29-
* **`NUMBER`** is a number literal, consisting of two blocks of ciphers seprated by a `.` (`123.456`) or one block of ciphers (`123`).
30-
* a **`PARAM`** has the form `var <ID>` separated by `,`.
31-
* an **`EXPRESSION`** is
32-
* an identifiers `ID`.
33-
* a constants `NUMBER`.
34-
* a binary operations `<EXPRESSION> ° <EXPRESSION>` whereas `°` is
35-
* an addition `+`.
36-
* a subtraction `-`.
37-
* a multiplication `*`.
38-
* a division `/`.
39-
* an integer division `div`.
40-
* a modulo operation `%` (returns integer values).
41-
* a comparisons `<`, `>`, `<=`, `>=`, `==` and `!=` which yield `0` if as `false` and `1` as `true`.
42-
* a logical and `&&`.
43-
* a logical or `||`.
44-
* a unary
45-
* `- <EXPRESSION>`.
46-
* `not <EXPRESSION>`.
47-
* a function applications `<ID>( <ARG>* )` whereas `ARG` is an `EXPRESSION`.
48-
49-
`<PARAM>*` and `<ARG>*` are separated by `,`.
50-
51-
Comments are
52-
* single line comments initialized with `##`.
53-
* multi line comments surrounded by `# ... #`.
13+
### Grammata Programs
14+
```
15+
PROGRAM ::= program {A..Z}{a..z|A..Z|0..9} { with DECL* }? begin SUBPRG+ end
16+
DECL ::= var IDENT { := EXPRESSION }? ;
17+
IDENT ::= {a..z}{ 0..9 | A..Z | a..z }*
18+
SUBPRG ::= FUNCTIONAL
19+
| IMPERATIVE
20+
| QUERY
21+
| BASE
22+
```
23+
### General arithmetical expressions
24+
```
25+
EXPRESSION ::= DISJ { || DISJ}*
26+
DISJ ::= CONJ { && CONJ}*
27+
CONJ ::= COMP {{ == | != | <= | >= | < | > } COMP}*
28+
COMP ::= SUM {{ + | - } SUM}*
29+
SUM ::= FAC {{ * | / } FAC}*
30+
FAC ::= ( EXPRESSION )
31+
| { - | ! } EXPRESSION
32+
| IDENT{(EXPRESSION { , EXPRESSION}*)}?
33+
| PARAM_VALUE
34+
```
35+
### Functional
36+
```
37+
FUNCTIONAL ::= lambda IDENT ( IDENT* ) is LAMBDA end
38+
LAMBDA ::= ARITH ARITH*
39+
ARITH ::= DISJ {|| DISJ}*
40+
DISJ ::= KONJ {&& KONJ}*
41+
KONJ ::= COMP {{ == | != | <= | >= | < | > } COMP}*
42+
COMP ::= SUM {{+ | -} SUM}*
43+
SUM ::= SIMPLE {{ * | / } SIMPLE}*
44+
SIMPLE ::= \ LOG+ . LAMBDA
45+
| if LAMBDA then LAMBDA else LAMBDA end
46+
| let DEF* in LAMBDA end
47+
| ( LAMBDA )
48+
| LOG
49+
| VALUE
50+
| IDENT[( LAMBDA [, LAMBDA]* )]
51+
DEF ::= LOG := LAMBDA ;
52+
LOG ::= $IDENT
53+
```
54+
### Imperative
55+
```
56+
IMPERATIVE ::= { func | proc } IDENT ( {IDENT { , IDENT }*}? ) { with DECL+ }? does STMT* end
57+
STMT ::= for IDENT { from EXPRESSION }? to EXPRESSION { in EXPRESSION }? do STMT* end
58+
| while EXPRESSION to STMT* end
59+
| do STMT* while EXPRESSION end
60+
| if EXPRESSION then STMT* else STMT* end
61+
| call IDENT({ EXPRESSION { , EXPRESSION }*}?) ;
62+
| IDENT := EXPRESSION ;
63+
| return EXPRESSION ;
64+
| exit ;
65+
| backtrack ;
66+
```
67+
### Logical
68+
```
69+
QUERY ::= query ( { IDENT { , IDENT}*}? ) { asks IDENT* } { for IDENT } ?- CLAUSE end
70+
CLAUSE ::= DISJ { ; DISJ}*
71+
DISJ ::= CONJ { , CONJ}*
72+
CONJ ::= - CLAUSE
73+
| ( CLAUSE )
74+
| GOAL
75+
GOAL ::= TERM :=: TERM
76+
| IDENT{( TERM { , TERM }*)}?
77+
TERM ::= VALUE
78+
| {A..Z}{a..z|A..Z|0..9}*
79+
| EXPRESSION
80+
BASE ::= base IDENT says RULE+ end
81+
RULE ::= GOAL { :- CLAUSE}? .
82+
```
5483

5584
Examples
5685
--------

examples/FailureWhereNonShouldBe.gr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
program FailureWhereNoneShouldBe
2+
with
3+
begin
4+
lambda fak(n) is
5+
let
6+
$fak := \$n . if $n <= 1
7+
then 1
8+
else $n * ($fak ($n - 1))
9+
end;
10+
in $fak $n
11+
end
12+
end
13+
14+
base numbers says
15+
p(fak(5)).
16+
p(5).
17+
end
18+
19+
query blub() asks numbers for X ?- p(X) end
20+
21+
func main()
22+
with
23+
var p := blub();
24+
does
25+
return $p;
26+
end
27+
end
28+
end

examples/ack.gr

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
program Ackermann
2+
with
3+
var x := nums();
4+
var y := oneOf(t(1,2,3));
5+
begin
6+
7+
base elems says
8+
elem(X,c(X,Xs)).
9+
elem(X,c(Y,Xs)) :- elem(X,Xs).
10+
end
11+
12+
query oneOf(c) for X ?-
13+
c :=: t(A,B,C),(A :=: X; B :=: X; C :=: X)
14+
end
15+
16+
query nums() asks elems for X ?-
17+
elem(X,c(1,c(2,c(3,c(4,nil)))))
18+
end
19+
20+
lambda main() is
21+
let
22+
$ack := \$n $m.if $n == 0
23+
then $m + 1
24+
else if $m == 0
25+
then $ack ($n-1) 1
26+
else $ack ($n-1) ($ack $n ($m-1))
27+
end
28+
end
29+
;
30+
in res($x,$y,$ack $y $x)
31+
end
32+
end
33+
end

examples/call.gr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
program Call
2+
with
3+
var x := 0;
4+
begin
5+
proc setX(s)
6+
does
7+
x := $s;
8+
exit;
9+
end
10+
11+
func main() does
12+
call setX(6);
13+
return $x;
14+
end
15+
end

examples/constructs.gr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
program Constructs
2+
begin
3+
4+
query nums() for X ?-
5+
X :=: 0;
6+
X :=: 1;
7+
X :=: 2;
8+
X :=: 3;
9+
X :=: 5;
10+
X :=: 8;
11+
X :=: 13
12+
end
13+
14+
func main()
15+
with
16+
var x := nums();
17+
var y := $x;
18+
does
19+
if $x < 2 then
20+
while $x < 3 do
21+
x := $x+1;
22+
end
23+
else
24+
do
25+
x := $x - 2;
26+
while $x >= 0
27+
end
28+
end
29+
return pair($y,$x * $x);
30+
end
31+
end

examples/curry.gr

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/doWhile.gr

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/examples.gr

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/fak.gr

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/fib.gr

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/flip.gr

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/ggt.gr

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)