implement scoping 🎉
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
module IBAF-Start
|
||||
|
||||
imports
|
||||
IBAF-Expressions
|
||||
IBAF-Statements
|
||||
IBAF-Expressions
|
||||
Funcons
|
||||
|
||||
// Language "IBAFlang"
|
||||
@@ -18,18 +18,8 @@ lexical syntax // Language
|
||||
syntax // Language
|
||||
|
||||
context-free syntax // Language
|
||||
L-start.L-start--L-pgm =
|
||||
L-pgm
|
||||
|
||||
L-pgm.L-pgm--R-int-L-idlist-SEMI-L-stmt =
|
||||
"int" L-idlist ";" L-stmt
|
||||
|
||||
L-idlist.L-idlist--L-id-C-COMMA-L-idlist-D-Q =
|
||||
L-id L-COMMA-L-idlist?
|
||||
|
||||
L-COMMA-L-idlist.L-COMMA-L-idlist--COMMA-L-idlist =
|
||||
"," L-idlist
|
||||
|
||||
L-start.L-start--L-statement-S =
|
||||
L-statement*
|
||||
|
||||
L-keyword.LEX-keyword =
|
||||
LEX-keyword
|
||||
@@ -37,10 +27,6 @@ context-free syntax // Language
|
||||
context-free syntax // Semantics
|
||||
FCT.T-start =
|
||||
"start" "[:" L-start ":]"
|
||||
FCT.T-run =
|
||||
"run" "[:" L-pgm ":]"
|
||||
FCT-SEQ.T-declare-int-vars =
|
||||
"declare-int-vars" "[:" L-idlist ":]"
|
||||
FCT-Quoted.LEX-keyword = LEX-keyword
|
||||
|
||||
context-free syntax // Desugaring
|
||||
@@ -50,14 +36,7 @@ variables // Meta-variables
|
||||
L-start? = "(:START" [1-9]? "?:)" {prefer}
|
||||
L-start* = "(:START" [1-9]? "*:)" {prefer}
|
||||
L-start+ = "(:START" [1-9]? "+:)" {prefer}
|
||||
L-pgm = "(:Pgm" [1-9]? ":)" {prefer}
|
||||
L-pgm? = "(:Pgm" [1-9]? "?:)" {prefer}
|
||||
L-pgm* = "(:Pgm" [1-9]? "*:)" {prefer}
|
||||
L-pgm+ = "(:Pgm" [1-9]? "+:)" {prefer}
|
||||
L-idlist = "(:IL" [1-9]? ":)" {prefer}
|
||||
L-idlist? = "(:IL" [1-9]? "?:)" {prefer}
|
||||
L-idlist* = "(:IL" [1-9]? "*:)" {prefer}
|
||||
L-idlist+ = "(:IL" [1-9]? "+:)" {prefer}
|
||||
L-statement* = "..." [1-9]? {prefer}
|
||||
|
||||
// SDF comments
|
||||
|
||||
@@ -73,9 +52,6 @@ variables // Meta-variables
|
||||
L-exp.L-exp--L-exp-AMPERSAND-AMPERSAND-L-exp =
|
||||
L-exp "&&" L-exp
|
||||
{assoc}
|
||||
L-stmt.L-stmt--L-stmt-L-stmt =
|
||||
L-stmt L-stmt
|
||||
{right}
|
||||
|
||||
sorts // ASTs
|
||||
T-start
|
||||
|
||||
@@ -8,15 +8,39 @@ imports
|
||||
|
||||
lexical syntax // Language
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
syntax // Language
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
context-free syntax // Language
|
||||
L-stmt.L-stmt--L-type-L-id-EQUALS-L-exp-SEMI =
|
||||
L-type L-id "=" L-exp ";"
|
||||
L-stmt.L-stmt--R-print-LPAREN-L-exp-RPAREN-SEMI =
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
L-statement.L-statement--LBRACE-L-statement-S-RBRACE =
|
||||
"{" L-statement* "}"
|
||||
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI =
|
||||
"print" "(" L-exp ")" ";"
|
||||
L-stmt.L-stmt--L-stmt-L-stmt =
|
||||
L-stmt L-stmt
|
||||
L-statement.L-statement--L-type-L-id-SEMI =
|
||||
L-type L-id ";"
|
||||
L-statement.L-statement--L-type-L-id-EQUALS-L-exp-SEMI =
|
||||
L-type L-id "=" L-exp ";"
|
||||
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
|
||||
L-id "=" L-exp ";"
|
||||
|
||||
L-type.L-type--R-int =
|
||||
"int"
|
||||
@@ -26,23 +50,62 @@ context-free syntax // Language
|
||||
L-id
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
context-free syntax // Semantics
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
FCT.T-execute =
|
||||
"execute" "[:" L-stmt ":]"
|
||||
"execute" "[:" L-statement* ":]"
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
FCT-SEQ.T-collect-declared-vars =
|
||||
"collect-declared-vars" "[:" L-statement* ":]"
|
||||
|
||||
context-free syntax // Desugaring
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
variables // Meta-variables
|
||||
L-stmt = "(:Stmt" [1-9]? ":)" {prefer}
|
||||
L-stmt? = "(:Stmt" [1-9]? "?:)" {prefer}
|
||||
L-stmt* = "(:Stmt" [1-9]? "*:)" {prefer}
|
||||
L-stmt+ = "(:Stmt" [1-9]? "+:)" {prefer}
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
L-statement = "(:Stmt" [1-9]? ":)" {prefer}
|
||||
L-statement? = "(:Stmt" [1-9]? "?:)" {prefer}
|
||||
L-statement* = "(:Stmt" [1-9]? "*:)" {prefer}
|
||||
L-statement+ = "(:Stmt" [1-9]? "+:)" {prefer}
|
||||
L-statement* = "..." [1-9]? {prefer}
|
||||
L-type = "(:Typ" [1-9]? ":)" {prefer}
|
||||
L-type? = "(:Typ" [1-9]? "?:)" {prefer}
|
||||
L-type* = "(:Typ" [1-9]? "*:)" {prefer}
|
||||
L-type+ = "(:Typ" [1-9]? "+:)" {prefer}
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
// SDF comments
|
||||
|
||||
// # Statements
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
sorts // ASTs
|
||||
T-start
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -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:) :] ]| ->
|
||||
|
||||
@@ -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*:)} :] ]|
|
||||
|
||||
|
||||
@@ -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*:) :] ]|
|
||||
|
||||
|
||||
Reference in New Issue
Block a user