Functions v0.1

This commit is contained in:
Peter Smit
2023-12-06 15:16:07 +01:00
parent ae30900155
commit f594027de5
2 changed files with 12 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ Syntax Stmt: statement ::= '{' statement* '}'
| type id ';'
| type id '=' exp ';'
| id '=' exp ';'
| 'return' exp? ';'
| 'fun' id '(' ')' '{' statement* '}'
Syntax Typ:type ::= 'int'
| 'bool'
@@ -20,6 +22,9 @@ 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+ ]])
@@ -47,6 +52,11 @@ Rule collect-declared-vars[[ Typ Id ';' ]] =
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* ]]