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

@@ -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)+