From f594027de5eedcd757cdc2b647b6bbe70a666115 Mon Sep 17 00:00:00 2001 From: Peter Smit Date: Wed, 6 Dec 2023 15:16:07 +0100 Subject: [PATCH] Functions v0.1 --- IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs | 2 ++ IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs b/IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs index c8c5ac1..42bf953 100644 --- a/IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs +++ b/IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs @@ -17,6 +17,7 @@ Syntax Exp:exp ::= id | exp '>' exp | exp '<=' exp | exp '<' exp + | id '(' ')' | '(' exp ')' Semantics eval-exp[[ _:exp ]] : => values @@ -36,6 +37,7 @@ Rule eval-exp[[ Exp1 '>=' Exp2 ]] = is-greater-or-equal(eval-exp[[ Exp1 ]], eval Rule eval-exp[[ Exp1 '>' Exp2 ]] = is-greater(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) Rule eval-exp[[ Exp1 '<=' Exp2 ]] = is-less-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) +Rule eval-exp[[ Id '(' ')' ]] = enact id[[ Id ]] Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]] diff --git a/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs b/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs index 3541d7f..ea785b0 100644 --- a/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs +++ b/IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs @@ -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* ]]