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

@@ -34,7 +34,7 @@ to-funcons:
eval-exp[: (:Exp2:) :]) ]|
to-funcons:
|[ eval-exp[: (:Exp1:)-(:Exp2:) :] ]| ->
|[ int-sub (eval-exp[: (:Exp1:) :],
|[ integer-subtract (eval-exp[: (:Exp1:) :],
eval-exp[: (:Exp2:) :]) ]|
to-funcons:
|[ eval-exp[: (:Exp1:)*(:Exp2:) :] ]| ->

View File

@@ -7,26 +7,13 @@ imports
pp/IBAF-pp
imports
cbs-gen/IBAF-Expressions
cbs-gen/IBAF-Statements
cbs-gen/IBAF-Expressions
// Language "IBAFlang"
rules
to-funcons:
|[ start[: (:Pgm:) :] ]| ->
|[ initialise-binding finalise-failing run[: (:Pgm:) :] ]|
to-funcons:
|[ run[: int(:IL:);(:Stmt:) :] ]| ->
|[ scope (collateral (declare-int-vars[: (:IL:) :]),
execute[: (:Stmt:) :]) ]|
to-funcons:
|[ declare-int-vars[: (:Id:) :] ]| ->
|[ bind (\"(:Id:)\",
allocate-initialised-variable (integers,
0)) ]|
to-funcons:
|[ declare-int-vars[: (:Id:),(:IL:) :] ]| ->
|[ declare-int-vars[: (:Id:) :],
declare-int-vars[: (:IL:) :] ]|
|[ start[: (:Stmt*:) :] ]| ->
|[ initialise-binding execute[: {(:Stmt*:)} :] ]|

View File

@@ -12,15 +12,58 @@ imports
// Language "IBAFlang"
rules
// # Statements
to-funcons:
|[ execute[: (:Typ:)(:Id:)=(:Exp:); :] ]| ->
|[ assign (bound (id[: (:Id:) :]),
eval-exp[: (:Exp:) :]) ]|
|[ execute[: {(:Stmt*:)} :] ]| ->
|[ scope (collateral collect-declared-vars[: (:Stmt*:) :],
execute[: (:Stmt*:) :]) ]|
to-funcons:
|[ execute[: print((:Exp:)); :] ]| ->
|[ print (eval-exp[: (:Exp:) :]) ]|
|[ print eval-exp[: (:Exp:) :] ]|
to-funcons:
|[ execute[: (:Stmt1:)(:Stmt2:) :] ]| ->
|[ sequential (execute[: (:Stmt1:) :],
execute[: (:Stmt2:) :]) ]|
|[ execute[: (:Typ:)(:Id:); :] ]| ->
|[ null ]|
to-funcons:
|[ execute[: (:Typ:)(:Id:)=(:Exp:); :] ]| ->
|[ assign (bound id[: (:Id:) :],
eval-exp[: (:Exp:) :]) ]|
to-funcons:
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|[ assign (bound id[: (:Id:) :],
eval-exp[: (:Exp:) :]) ]|
to-funcons:
|[ execute[: :] ]| ->
|[ null ]|
to-funcons:
|[ execute[: (:Stmt:)(:Stmt+:) :] ]| ->
|[ sequential (execute[: (:Stmt:) :],
execute[: (:Stmt+:) :]) ]|
// # Handling variable declarations
to-funcons:
|[ collect-declared-vars[: (:Typ:)(:Id:)=(:Exp:); :] ]| ->
|[ bind (\"(:Id:)\",
allocate-initialised-variable (integers,
0)) ]|
to-funcons:
|[ collect-declared-vars[: (:Typ:)(:Id:); :] ]| ->
|[ bind (\"(:Id:)\",
allocate-initialised-variable (integers,
0)) ]|
to-funcons:
|[ collect-declared-vars[: :] ]| ->
|[ map () ]|
to-funcons:
|[ collect-declared-vars[: (:Stmt:) :] ]| ->
|[ map () ]|
to-funcons:
|[ collect-declared-vars[: (:Stmt1:)(:Stmt2:)(:Stmt*:) :] ]| ->
|[ collect-declared-vars[: (:Stmt1:) :],
collect-declared-vars[: (:Stmt2:) :],
collect-declared-vars[: (:Stmt*:) :] ]|