control flow

This commit is contained in:
Peter
2024-01-15 08:30:03 +01:00
parent 1b4d8d622d
commit 613fa9ab1a
16 changed files with 400 additions and 23 deletions

View File

@@ -53,6 +53,16 @@ context-free syntax // Language
"return" L-exp? ";"
L-statement.L-statement--R-fun-L-id-LPAREN-L-params-RPAREN-L-block =
"fun" L-id "(" L-params ")" L-block
L-statement.L-statement--R-if-LPAREN-L-exp-RPAREN-L-block-C-R-else-L-block-D-Q =
"if" "(" L-exp ")" L-block L-R-else-L-block?
L-statement.L-statement--R-while-LPAREN-L-exp-RPAREN-L-block =
"while" "(" L-exp ")" L-block
L-statement.L-statement--R-for-LPAREN-R-int-L-id-EQUALS-L-exp-SEMI-L-exp-RPAREN-L-block =
"for" "(" "int" L-id "=" L-exp ";" L-exp ")" L-block
L-R-else-L-block.L-R-else-L-block--R-else-L-block =
"else" L-block
L-block.L-block--LBRACE-L-statement-S-RBRACE =
"{" L-statement* "}"

File diff suppressed because one or more lines are too long

View File

@@ -99,6 +99,9 @@ to-funcons-lex:
to-funcons:
|[ id[: (:Id:) :] ]| ->
|[ \"(:Id:)\" ]|
to-funcons:
|[ int-val[: 0 :] ]| ->
|[ 0 ]|
to-funcons:
|[ int-val[: (:Dec:) :] ]| ->
|[ dec-val[: (:Dec:) :] ]|

View File

@@ -43,6 +43,30 @@ to-funcons:
to-funcons:
|[ execute[: fun(:Id:)((:Params:))(:Block:) :] ]| ->
|[ null ]|
to-funcons:
|[ execute[: if((:Exp:))(:Block:) :] ]| ->
|[ if-else (eval-exp[: (:Exp:) :],
execute-block[: (:Block:) :],
null) ]|
to-funcons:
|[ execute[: if((:Exp:))(:Block1:)else(:Block2:) :] ]| ->
|[ if-else (eval-exp[: (:Exp:) :],
execute-block[: (:Block1:) :],
execute-block[: (:Block2:) :]) ]|
to-funcons:
|[ execute[: while((:Exp:))(:Block:) :] ]| ->
|[ while (eval-exp[: (:Exp:) :],
execute-block[: (:Block:) :]) ]|
to-funcons:
|[ execute[: for(int(:Id:)=(:Exp1:);(:Exp2:))(:Block:) :] ]| ->
|[ scope (bind (id[: (:Id:) :],
allocate-initialised-variable (integers,
eval-exp[: (:Exp1:) :])),
while (eval-exp[: (:Exp2:) :],
sequential (execute-block[: (:Block:) :],
assign (bound id[: (:Id:) :],
int-add (1,
assigned bound id[: (:Id:) :]))))) ]|
to-funcons:
|[ execute[: :] ]| ->
|[ null ]|

View File

@@ -4,20 +4,28 @@ initialise-binding scope
( ),
bind
("i",
allocate-initialised-variable
(integers,
0)),
allocate-variable
(integers)),
map
( )),
sequential
(if-else
(true,
print true,
print false),
scope
(collateral
(map
( )),
print true),
scope
(collateral
(map
( )),
print false)),
assign
(bound "i",
decimal-natural
("0")),
integer-negate
(decimal-natural
("30"))),
while
(is-less
(assigned
@@ -25,6 +33,14 @@ initialise-binding scope
("i")),
decimal-natural
("10")),
scope
(collateral
(map
( ),
map
( ),
map
( )),
sequential
(assign
(bound "i",
@@ -36,4 +52,4 @@ initialise-binding scope
("1"))),
print assigned
(bound
("i"))))))
("i")))))))

View File

@@ -5,7 +5,7 @@ if (true) {
int i = 0;
int i = -30;
while (i < 10) {
i = i+1;

84
IBAF-Tests/IBAF-1/fib.fct Normal file
View File

@@ -0,0 +1,84 @@
initialise-binding scope
(collateral
(bind
("curr",
allocate-variable
(integers)),
bind
("prev1",
allocate-variable
(integers)),
bind
("prev2",
allocate-variable
(integers)),
bind
("n",
allocate-variable
(integers)),
map
( )),
sequential
(assign
(bound "curr",
0),
assign
(bound "prev1",
decimal-natural
("1")),
assign
(bound "prev2",
0),
assign
(bound "n",
decimal-natural
("7")),
while
(is-greater-or-equal
(assigned
(bound
("n")),
0),
scope
(collateral
(map
( ),
map
( ),
map
( ),
map
( ),
map
( )),
sequential
(assign
(bound "curr",
int-add
(assigned
(bound
("prev2")),
assigned
(bound
("prev1")))),
assign
(bound "prev1",
assigned
(bound
("prev2"))),
assign
(bound "prev2",
assigned
(bound
("curr"))),
print assigned
(bound
("curr")),
assign
(bound "n",
integer-subtract
(assigned
(bound
("n")),
decimal-natural
("1"))))))))

View File

@@ -0,0 +1,14 @@
int curr;
int prev1 = 1;
int prev2 = 0;
int n = 7;
while (n >= 0) {
curr = prev2 + prev1;
prev1 = prev2;
prev2 = curr;
print(curr);
n = n - 1;
}

View File

@@ -0,0 +1,90 @@
initialise-binding scope
(collateral
(bind
("fib",
function abstraction
(scope
(collateral
(map
( ),
map
( ),
map
( ),
bind
("n",
allocate-initialised-variable
(integers,
checked head
given))),
sequential
(if-else
(is-less-or-equal
(assigned
(bound
("n")),
decimal-natural
("1")),
scope
(collateral
(map
( )),
return assigned
(bound
("n"))),
null),
return int-add
(handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("1")))),
handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("2"))))))))),
map
( ),
map
( )),
sequential
(null,
scope
(bind
("n",
allocate-initialised-variable
(integers,
0)),
while
(is-less
(assigned
(bound
("n")),
decimal-natural
("8")),
sequential
(scope
(collateral
(map
( )),
print handle-return apply
(bound "fib",
list
(assigned
(bound
("n"))))),
assign
(bound "n",
int-add
(1,
assigned bound
"n")))))))

View File

@@ -0,0 +1,10 @@
fun fib(n) {
if (n <= 1) {
return n;
}
return fib(n-1) + fib(n-2);
}
for (int n = 0; n < 8) {
print(fib(n));
}

View File

@@ -0,0 +1,95 @@
initialise-binding scope
(collateral
(bind
("fib",
function abstraction
(scope
(collateral
(map
( ),
map
( ),
map
( ),
bind
("n",
allocate-initialised-variable
(integers,
checked head
given))),
sequential
(if-else
(is-less-or-equal
(assigned
(bound
("n")),
decimal-natural
("1")),
scope
(collateral
(map
( )),
return assigned
(bound
("n"))),
null),
return int-add
(handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("1")))),
handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("2"))))))))),
bind
("n",
allocate-variable
(integers)),
map
( )),
sequential
(null,
assign
(bound "n",
0),
while
(is-less
(assigned
(bound
("n")),
decimal-natural
("8")),
scope
(collateral
(map
( ),
map
( ),
map
( )),
sequential
(print handle-return apply
(bound "fib",
list
(assigned
(bound
("n")))),
assign
(bound "n",
int-add
(assigned
(bound
("n")),
decimal-natural
("1"))))))))

View File

@@ -0,0 +1,13 @@
fun fib(n) {
if (n <= 1) {
return n;
}
return fib(n-1) + fib(n-2);
}
int n = 0;
while (n < 8) {
print(fib(n));
n = n + 1;
}

View File

@@ -6,6 +6,5 @@ fun test(a, b) {
int b =test(6, 3);
print(a);
print(b);

View File

@@ -57,7 +57,7 @@ Rule id[[ Id ]] = \"Id\"
Syntax Int:int ::= '0' | ('-'?_decimal)
Semantics int-val[[ _:int ]] : ints
Rule int-val[[ '0' ]] = decimal-natural(\"0\")
Rule int-val[[ '0' ]] = 0
Rule int-val[[ Dec ]] = dec-val[[ Dec ]]
Rule int-val[[ '-' Dec ]] = integer-negate(dec-val[[ Dec ]])

View File

@@ -0,0 +1,6 @@
//Funcon increment(Id:ids) : => values
// ~> assign(bound(\"Id\"), int-add(assigned(bound(\"Id\")), 1))
Funcon
print-line(S:strings) : => null-type
~> print(S, "\n")

View File

@@ -9,6 +9,9 @@ Syntax Stmt: statement ::= block
| 'int' id '=' exp ';'
| 'return' exp? ';'
| 'fun' id '(' params ')' block
| 'if' '(' exp ')' block ('else' block)?
| 'while' '(' exp ')' block
| 'for' '(' 'int' id '=' exp ';' exp ')' block
Syntax Block: block ::= '{' statement* '}'
@@ -24,6 +27,14 @@ Rule execute[[ 'int' Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp
Rule execute[[ 'return' Exp ';' ]] = return eval-exp[[ Exp ]]
Rule execute[[ 'return' ';' ]] = return null
Rule execute[[ 'fun' Id '(' Params ')' Block ]] = null
Rule execute[[ 'if' '(' Exp ')' Block ]] = if-else(eval-exp[[ Exp ]], execute-block[[ Block ]], null)
Rule execute[[ 'if' '(' Exp ')' Block1 'else' Block2 ]] = if-else(eval-exp[[ Exp ]], execute-block[[ Block1 ]], execute-block[[ Block2 ]])
Rule execute[[ 'while' '(' Exp ')' Block ]] = while(eval-exp[[ Exp ]], execute-block[[ Block ]])
Rule execute[[ 'for' '(' 'int' Id '=' Exp1 ';' Exp2 ')' Block ]] = scope(
bind(id[[ Id ]], allocate-initialised-variable(integers, eval-exp[[ Exp1 ]])),
// while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], increment Id))
while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], assign(bound id[[ Id ]], int-add(1,assigned bound id[[ Id ]]))))
)
Rule execute[[ ]] = null
Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
@@ -42,6 +53,8 @@ Rule collect-params[[ Id ]] = bind(
)
Rule collect-params[[ Id ',' Params ]] = collect-params[[ Id ]], give(checked tail given, collect-params[[ Params ]])
# Handling variable declarations
Semantics collect-declared-vars[[ Stmt*:statement* ]] : (=>environments)+