Files
IBAFLang/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs
2023-12-06 15:16:07 +01:00

75 lines
2.0 KiB
Plaintext

Language "IBAFlang"
# Statements
Syntax Stmt: statement ::= '{' statement* '}'
| 'print' '(' exp ')' ';'
| type id ';'
| type id '=' exp ';'
| id '=' exp ';'
| 'return' exp? ';'
| 'fun' id '(' ')' '{' statement* '}'
Syntax Typ:type ::= 'int'
| 'bool'
| id
Semantics execute[[ Stmt*:statement* ]] : => null-type
Rule execute[[ '{' Stmt* '}' ]] = scope(collateral collect-declared-vars[[ Stmt* ]], execute[[ Stmt* ]])
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
Rule execute[[ Typ Id ';' ]] = null
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
Rule execute[[ 'return' Exp ';' ]] = return exal-exp[[ Exp ]]
Rule execute[[ 'return' ';' ]] = return null
Rule execute[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = null
Rule execute[[ ]] = null
Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
# Handling variable declarations
Semantics collect-declared-vars[[ Stmt*:statement* ]] : (=>environments)+
Rule collect-declared-vars[[ Typ Id '=' Exp ';' ]] =
bind(
\"Id\",
allocate-initialised-variable(
integers, //TODO: use type
0 //TODO: use correct default value for type
)
)
Rule collect-declared-vars[[ Typ Id ';' ]] =
bind(
\"Id\",
allocate-initialised-variable(
integers, //TODO: use type
0 //TODO: use correct default value for type
)
)
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] =
bind(
\"Id\",
handle-return function abstraction 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* ]]