Files
IBAFLang/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs
2024-01-15 08:30:03 +01:00

87 lines
3.3 KiB
Plaintext

Language "IBAFlang"
# Statements
Syntax Stmt: statement ::= block
| 'print' '(' exp ')' ';'
| 'int' id ';'
| id '=' exp ';'
| '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* '}'
Syntax Params: params ::= id (',' params)?
Semantics execute[[ Stmt*:statement* ]] : => null-type
Rule execute[[ Block ]] = execute-block[[ Block ]]
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
Rule execute[[ 'int' Id ';' ]] = assign(bound id[[ Id ]], 0)
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
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+ ]])
Semantics execute-block[[ _:block ]] : => null-type
Rule execute-block[[ '{' Stmt* '}' ]] = scope(collateral(collect-declared-vars[[ Stmt* ]]), execute[[ Stmt* ]])
# Handling parameter declarations
Semantics collect-params[[ Params:params ]] : (=>environments)+
Rule collect-params[[ Id ]] = bind(
id[[ Id ]],
allocate-initialised-variable(integers, checked head given)
)
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)+
Rule collect-declared-vars[[ 'int' Id '=' Exp ';' ]] = bind(id[[ Id ]], allocate-variable(integers))
Rule collect-declared-vars[[ 'int' Id ';' ]] = bind(id[[ Id ]], allocate-variable(integers))
Rule collect-declared-vars[[ 'fun' Id '(' Params ')' '{' Stmt* '}' ]] = bind(id[[ Id ]],
function abstraction(
scope(
collateral(collect-declared-vars[[ Stmt* ]], collect-params[[ Params ]]),
execute[[ Stmt* ]]
)
)
)
Rule collect-declared-vars[[ ]] = map()
Rule collect-declared-vars[[ Stmt ]] = map()
Rule collect-declared-vars[[ Stmt1 Stmt2 Stmt* ]] = collect-declared-vars[[ Stmt1 ]], collect-declared-vars[[ Stmt2 ]], collect-declared-vars[[ Stmt* ]]