implement scoping 🎉

This commit is contained in:
Peter
2023-11-18 11:18:25 +01:00
parent c3ee83292f
commit ae30900155
19 changed files with 338 additions and 181 deletions

View File

@@ -25,7 +25,7 @@ Rule eval-exp[[ Int ]] = int-val[[ Int ]]
Rule eval-exp[[ 'true' ]] = true
Rule eval-exp[[ 'false' ]] = false
Rule eval-exp[[ Exp1 '+' Exp2 ]] = int-add(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '-' Exp2 ]] = int-sub(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '-' Exp2 ]] = integer-subtract(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]]) // For some reason the funcon interpreter doesn't recognise the alias `int-sub` :/
Rule eval-exp[[ Exp1 '*' Exp2 ]] = int-mul(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '/' Exp2 ]] = checked int-div(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '%' Exp2 ]] = checked int-mod(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])

View File

@@ -1,30 +1,9 @@
Language "IBAFlang"
Syntax START:start ::= pgm
Syntax START:start ::= statement*
Semantics start[[ _:start ]] : =>null-type
Rule start[[ Stmt* ]] = initialise-binding execute[[ '{' Stmt* '}' ]]
Rule start[[ Pgm ]] =
initialise-binding
finalise-failing
run [[ Pgm ]]
Syntax Pgm : pgm ::= 'int' idlist ';' stmt
Semantics run[[ _:pgm ]] : =>null-type
Rule run[[ 'int' IL ';' Stmt ]] =
scope( collateral(declare-int-vars[[ IL ]]),
execute[[ Stmt ]] )
Syntax IL : idlist ::= id (',' idlist)?
Semantics declare-int-vars[[ _: idlist ]] : (=>environments)+
Rule declare-int-vars[[ Id ]] = bind(\"Id\", allocate-initialised-variable(integers, 0))
Rule declare-int-vars[[ Id ',' IL ]] = declare-int-vars[[ Id ]], declare-int-vars[[ IL ]]
@@ -43,5 +22,4 @@ Syntax SDF
context-free syntax
``exp ::= exp '+' exp`` {assoc}
``exp ::= exp '&&' exp`` {assoc}
``stmt ::= stmt stmt`` {right}
*/

View File

@@ -1,41 +1,64 @@
Language "IBAFlang"
# Statements
Syntax Stmt: statement ::= '{' statement* '}'
| 'print' '(' exp ')' ';'
| type id ';'
| type id '=' exp ';'
| id '=' exp ';'
Syntax Stmt:stmt ::= type id '=' exp ';'
| 'print' '(' exp ')' ';'
// | '{' stmt '}'
| stmt stmt
Syntax Typ:type ::= 'int'
| 'bool'
| id
Semantics execute[[ _:stmt ]] : => null
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound(id[[ Id ]]), eval-exp[[ Exp ]])
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print(eval-exp[[ Exp ]])
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[[ ]] = 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[[ ]] = 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* ]]
//Rule execute[[ '{' Stmt '}' ]] = scope(
// collateral(collect-declared-vars[[ Stmt ]]),
// execute[[ Stmt ]]
//)
Rule execute[[ Stmt1 Stmt2 ]] = sequential(execute[[ Stmt1 ]], execute[[ Stmt2 ]])
//Semantics collect-declared-vars[[ _:stmt ]] : (=>environments)*
//
//
//Rule collect-declared-vars[[ Typ Id '=' Exp ';' ]] =
// bind(
// \"Id\",
// allocate-initialised-variable(
// integers, //TODO: use type
// eval-exp[[ Exp ]]
// )
// )
//Rule collect-declared-vars[[ Stmt ]] = null
//Rule collect-declared-vars[[ Stmt1 Stmt2 ]] = (collect-declared-vars[[ Stmt1 ]], collect-declared-vars[[ Stmt2 ]]))