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

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/IBAFlang.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/IBAFlang.iml" filepath="$PROJECT_DIR$/.idea/IBAFlang.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -1,8 +1,8 @@
module IBAF-Start module IBAF-Start
imports imports
IBAF-Expressions
IBAF-Statements IBAF-Statements
IBAF-Expressions
Funcons Funcons
// Language "IBAFlang" // Language "IBAFlang"
@@ -18,18 +18,8 @@ lexical syntax // Language
syntax // Language syntax // Language
context-free syntax // Language context-free syntax // Language
L-start.L-start--L-pgm = L-start.L-start--L-statement-S =
L-pgm L-statement*
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-keyword.LEX-keyword = L-keyword.LEX-keyword =
LEX-keyword LEX-keyword
@@ -37,10 +27,6 @@ context-free syntax // Language
context-free syntax // Semantics context-free syntax // Semantics
FCT.T-start = FCT.T-start =
"start" "[:" L-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 FCT-Quoted.LEX-keyword = LEX-keyword
context-free syntax // Desugaring 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-start* = "(:START" [1-9]? "*:)" {prefer}
L-start+ = "(:START" [1-9]? "+:)" {prefer} L-start+ = "(:START" [1-9]? "+:)" {prefer}
L-pgm = "(:Pgm" [1-9]? ":)" {prefer} L-statement* = "..." [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}
// SDF comments // SDF comments
@@ -73,9 +52,6 @@ variables // Meta-variables
L-exp.L-exp--L-exp-AMPERSAND-AMPERSAND-L-exp = L-exp.L-exp--L-exp-AMPERSAND-AMPERSAND-L-exp =
L-exp "&&" L-exp L-exp "&&" L-exp
{assoc} {assoc}
L-stmt.L-stmt--L-stmt-L-stmt =
L-stmt L-stmt
{right}
sorts // ASTs sorts // ASTs
T-start T-start

View File

@@ -8,15 +8,39 @@ imports
lexical syntax // Language lexical syntax // Language
// # Statements
// # Handling variable declarations
syntax // Language syntax // Language
// # Statements
// # Handling variable declarations
context-free syntax // Language context-free syntax // Language
L-stmt.L-stmt--L-type-L-id-EQUALS-L-exp-SEMI =
L-type L-id "=" L-exp ";" // # Statements
L-stmt.L-stmt--R-print-LPAREN-L-exp-RPAREN-SEMI =
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 ")" ";" "print" "(" L-exp ")" ";"
L-stmt.L-stmt--L-stmt-L-stmt = L-statement.L-statement--L-type-L-id-SEMI =
L-stmt L-stmt 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 = L-type.L-type--R-int =
"int" "int"
@@ -26,23 +50,62 @@ context-free syntax // Language
L-id L-id
// # Handling variable declarations
context-free syntax // Semantics context-free syntax // Semantics
// # Statements
FCT.T-execute = 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 context-free syntax // Desugaring
// # Statements
// # Handling variable declarations
variables // Meta-variables variables // Meta-variables
L-stmt = "(:Stmt" [1-9]? ":)" {prefer}
L-stmt? = "(:Stmt" [1-9]? "?:)" {prefer} // # Statements
L-stmt* = "(:Stmt" [1-9]? "*:)" {prefer}
L-stmt+ = "(:Stmt" [1-9]? "+:)" {prefer}
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}
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 // SDF comments
// # Statements
// # Handling variable declarations
sorts // ASTs sorts // ASTs
T-start T-start

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -7,26 +7,13 @@ imports
pp/IBAF-pp pp/IBAF-pp
imports imports
cbs-gen/IBAF-Expressions
cbs-gen/IBAF-Statements cbs-gen/IBAF-Statements
cbs-gen/IBAF-Expressions
// Language "IBAFlang" // Language "IBAFlang"
rules rules
to-funcons: to-funcons:
|[ start[: (:Pgm:) :] ]| -> |[ start[: (:Stmt*:) :] ]| ->
|[ initialise-binding finalise-failing run[: (:Pgm:) :] ]| |[ initialise-binding execute[: {(:Stmt*:)} :] ]|
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:) :] ]|

View File

@@ -12,15 +12,58 @@ imports
// Language "IBAFlang" // Language "IBAFlang"
rules rules
// # Statements
to-funcons: to-funcons:
|[ execute[: (:Typ:)(:Id:)=(:Exp:); :] ]| -> |[ execute[: {(:Stmt*:)} :] ]| ->
|[ assign (bound (id[: (:Id:) :]), |[ scope (collateral collect-declared-vars[: (:Stmt*:) :],
eval-exp[: (:Exp:) :]) ]| execute[: (:Stmt*:) :]) ]|
to-funcons: to-funcons:
|[ execute[: print((:Exp:)); :] ]| -> |[ execute[: print((:Exp:)); :] ]| ->
|[ print (eval-exp[: (:Exp:) :]) ]| |[ print eval-exp[: (:Exp:) :] ]|
to-funcons: to-funcons:
|[ execute[: (:Stmt1:)(:Stmt2:) :] ]| -> |[ execute[: (:Typ:)(:Id:); :] ]| ->
|[ sequential (execute[: (:Stmt1:) :], |[ null ]|
execute[: (:Stmt2:) :]) ]| 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*:) :] ]|

View File

@@ -0,0 +1,99 @@
initialise-binding scope
(collateral
(bind
("z",
allocate-initialised-variable
(integers,
0)),
bind
("x",
allocate-initialised-variable
(integers,
0)),
map
( ),
map
( ),
map
( ),
map
( ),
map
( ),
map
( ),
map
( )),
sequential
(assign
(bound
("z"),
decimal-natural
("5")),
assign
(bound
("x"),
int-mul
(decimal-natural
("5"),
decimal-natural
("3"))),
print
(assigned
(bound
("x"))),
assign
(bound
("x"),
decimal-natural
("2")),
print
(int-mul
(assigned
(bound
("x")),
decimal-natural
("3"))),
scope
(collateral
(bind
("y",
allocate-initialised-variable
(integers,
0)),
map
( ),
map
( )),
sequential
(assign
(bound
("y"),
decimal-natural
("4")),
print
(assigned
(bound
("y"))))),
assign
(bound
("z"),
int-mul
(assigned
(bound
("z")),
assigned
(bound
("x")))),
print
(assigned
(bound
("z"))),
print
(int-add
(assigned
(bound
("z")),
assigned
(bound
("x"))))))

View File

@@ -0,0 +1,16 @@
int z =5;
int x =5*3;
print(x);
x = 2;
print(x*3);
{
int y =4;
print (y);
}
z = z * x;
print(z);
print(z+x);

View File

@@ -1,64 +0,0 @@
initialise-binding finalise-failing scope
(collateral
(bind
("x",
allocate-initialised-variable
(integers,
0)),
bind
("y",
allocate-initialised-variable
(integers,
0))),
sequential
(assign
(bound
("x"),
decimal-natural
("10")),
assign
(bound
("y"),
checked int-mod
(assigned
(bound
("x")),
int-val [: 0 :])),
print
(assigned
(bound
("x"))),
print
(assigned
(bound
("y"))),
print
(is-equal
(assigned
(bound
("x")),
decimal-natural
("1"))),
print
(is-equal
(assigned
(bound
("x")),
assigned
(bound
("y")))),
print
(is-less
(assigned
(bound
("x")),
assigned
(bound
("y")))),
print
(int-add
(assigned
(bound
("x")),
decimal-natural
("2")))))

View File

@@ -1,4 +1,4 @@
int x, y; int x,y;
int x = 10; int x = 10;
int y = x % 3; int y = x % 3;

View File

@@ -1 +0,0 @@
int x , y ; x = 10 % 3 ; y = x + 10 ; print ( x ) ; print ( y ) ;

View File

@@ -25,7 +25,7 @@ Rule eval-exp[[ Int ]] = int-val[[ Int ]]
Rule eval-exp[[ 'true' ]] = true Rule eval-exp[[ 'true' ]] = true
Rule eval-exp[[ 'false' ]] = false Rule eval-exp[[ 'false' ]] = false
Rule eval-exp[[ Exp1 '+' Exp2 ]] = int-add(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]]) 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 ]] = 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-div(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '%' Exp2 ]] = checked int-mod(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" Language "IBAFlang"
Syntax START:start ::= pgm Syntax START:start ::= statement*
Semantics start[[ _:start ]] : =>null-type 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 context-free syntax
``exp ::= exp '+' exp`` {assoc} ``exp ::= exp '+' exp`` {assoc}
``exp ::= exp '&&' exp`` {assoc} ``exp ::= exp '&&' exp`` {assoc}
``stmt ::= stmt stmt`` {right}
*/ */

View File

@@ -1,41 +1,64 @@
Language "IBAFlang" Language "IBAFlang"
# Statements
Syntax Stmt:stmt ::= type id '=' exp ';' Syntax Stmt: statement ::= '{' statement* '}'
| 'print' '(' exp ')' ';' | 'print' '(' exp ')' ';'
// | '{' stmt '}' | type id ';'
| stmt stmt | type id '=' exp ';'
| id '=' exp ';'
Syntax Typ:type ::= 'int' Syntax Typ:type ::= 'int'
| 'bool' | 'bool'
| id | id
Semantics execute[[ _:stmt ]] : => null
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound(id[[ Id ]]), eval-exp[[ Exp ]]) Semantics execute[[ Stmt*:statement* ]] : => null-type
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print(eval-exp[[ Exp ]])
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 ]]))