Collect all declared variables at start of program execution

This commit is contained in:
Peter
2024-04-16 08:12:52 +02:00
parent cb7f6f8e5d
commit f5f5626521
21 changed files with 299 additions and 284 deletions

2
.idea/.gitignore generated vendored
View File

@@ -6,3 +6,5 @@
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

4
.idea/IBAFlang.iml generated
View File

@@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@@ -11,7 +11,19 @@ lexical syntax // Language
// # 1: General expressions
// ## Handling expressions for function calls
// ## Ids
LEX-id = ( [a-z] | [A-Z] ) ( [a-z] | [A-Z] | [0-9] )*
// ## Integers and decimals
LEX-DASH = "-"
LEX-decimal = [1-9] ( [0-9] )*
@@ -20,6 +32,18 @@ syntax // Language
// # 1: General expressions
// ## Handling expressions for function calls
// ## Ids
// ## Integers and decimals
L-int-CF.L-int--R-0 =
"0"
L-int-CF.L-int--C-DASH-Q-L-decimal-D =
@@ -71,6 +95,10 @@ context-free syntax // Language
L-exp.L-exp--L-exp-GREATER-L-exp =
L-exp ">" L-exp
// ## Handling expressions for function calls
L-paramvalues.L-paramvalues--L-exp-C-COMMA-L-paramvalues-D-Q =
L-exp L-COMMA-L-paramvalues?
@@ -78,8 +106,16 @@ context-free syntax // Language
"," L-paramvalues
// ## Ids
L-id.LEX-id =
LEX-id
// ## Integers and decimals
L-decimal.LEX-decimal =
LEX-decimal
@@ -90,11 +126,23 @@ context-free syntax // Semantics
FCT.T-eval-exp =
"eval-exp" "[:" L-exp ":]"
// ## Handling expressions for function calls
FCT.T-eval-params =
"eval-params" "[:" L-paramvalues? ":]"
// ## Ids
FCT-Quoted.L-id = L-id
FCT.T-id =
"id" "[:" L-id ":]"
// ## Integers and decimals
FCT.T-int-val =
"int-val" "[:" L-int ":]"
FCT-Quoted.L-decimal = L-decimal
@@ -107,6 +155,18 @@ context-free syntax // Desugaring
// ## Handling expressions for function calls
// ## Ids
// ## Integers and decimals
variables // Meta-variables
// # 1: General expressions
@@ -116,14 +176,26 @@ variables // Meta-variables
L-exp? = "(:Exp" [1-9]? "?:)" {prefer}
L-exp* = "(:Exp" [1-9]? "*:)" {prefer}
L-exp+ = "(:Exp" [1-9]? "+:)" {prefer}
// ## Handling expressions for function calls
L-paramvalues = "(:ParamValues" [1-9]? ":)" {prefer}
L-paramvalues? = "(:ParamValues" [1-9]? "?:)" {prefer}
L-paramvalues* = "(:ParamValues" [1-9]? "*:)" {prefer}
L-paramvalues+ = "(:ParamValues" [1-9]? "+:)" {prefer}
// ## Ids
L-id = "(:Id" [1-9]? ":)" {prefer}
L-id? = "(:Id" [1-9]? "?:)" {prefer}
L-id* = "(:Id" [1-9]? "*:)" {prefer}
L-id+ = "(:Id" [1-9]? "+:)" {prefer}
// ## Integers and decimals
L-int = "(:Int" [1-9]? ":)" {prefer}
L-int? = "(:Int" [1-9]? "?:)" {prefer}
L-int* = "(:Int" [1-9]? "*:)" {prefer}
@@ -139,5 +211,17 @@ variables // Meta-variables
// ## Handling expressions for function calls
// ## Ids
// ## Integers and decimals
sorts // ASTs
T-start

View File

@@ -45,13 +45,5 @@ variables // Meta-variables
lexical restrictions
LEX-id -/- [A-Za-z0-9]
context-free syntax
L-exp.L-exp--L-exp-PLUS-L-exp =
L-exp "+" L-exp
{assoc}
L-exp.L-exp--L-exp-AMPERSAND-AMPERSAND-L-exp =
L-exp "&&" L-exp
{assoc}
sorts // ASTs
T-start

View File

@@ -44,8 +44,8 @@ context-free syntax // Language
L-statement.L-statement--L-block =
L-block
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI =
"print" "(" L-exp ")" ";"
L-statement.L-statement--R-print-L-exp-SEMI =
"print" L-exp ";"
L-statement.L-statement--R-int-L-id-SEMI =
"int" L-id ";"
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
@@ -60,8 +60,6 @@ context-free syntax // Language
"if" "(" L-exp ")" L-block L-R-else-L-block?
L-statement.L-statement--R-while-LPAREN-L-exp-RPAREN-L-block =
"while" "(" L-exp ")" L-block
L-statement.L-statement--R-for-LPAREN-R-int-L-id-EQUALS-L-exp-SEMI-L-exp-RPAREN-L-block =
"for" "(" "int" L-id "=" L-exp ";" L-exp ")" L-block
L-R-else-L-block.L-R-else-L-block--R-else-L-block =
"else" L-block

File diff suppressed because one or more lines are too long

View File

@@ -18,7 +18,7 @@ rules
to-funcons:
|[ eval-exp[: (:Id:) :] ]| ->
|[ assigned (bound (id[: (:Id:) :])) ]|
|[ assigned bound id[: (:Id:) :] ]|
to-funcons:
|[ eval-exp[: (:Int:) :] ]| ->
|[ int-val[: (:Int:) :] ]|
@@ -83,6 +83,10 @@ to-funcons:
to-funcons:
|[ eval-exp[: ((:Exp:)) :] ]| ->
|[ eval-exp[: (:Exp:) :] ]|
// ## Handling expressions for function calls
to-funcons:
|[ eval-params[: :] ]| ->
|[ list () ]|
@@ -93,12 +97,20 @@ to-funcons:
|[ eval-params[: (:Exp:),(:ParamValues:) :] ]| ->
|[ cons (eval-exp[: (:Exp:) :],
eval-params[: (:ParamValues:) :]) ]|
// ## Ids
to-funcons-lex:
FCTDoubleQuoted(L-id(LEX-id(str))) ->
FCTString(<double-quote> str)
to-funcons:
|[ id[: (:Id:) :] ]| ->
|[ \"(:Id:)\" ]|
// ## Integers and decimals
to-funcons:
|[ int-val[: 0 :] ]| ->
|[ 0 ]|

View File

@@ -15,5 +15,6 @@ imports
rules
to-funcons:
|[ start[: (:Stmt*:) :] ]| ->
|[ initialise-binding execute-block[: {(:Stmt*:)} :] ]|
|[ initialise-binding scope (collateral (collect-declared-vars[: (:Stmt*:) :]),
execute[: (:Stmt*:) :]) ]|

View File

@@ -20,11 +20,12 @@ to-funcons:
|[ execute[: (:Block:) :] ]| ->
|[ execute-block[: (:Block:) :] ]|
to-funcons:
|[ execute[: print((:Exp:)); :] ]| ->
|[ print eval-exp[: (:Exp:) :] ]|
|[ execute[: print(:Exp:); :] ]| ->
|[ print (to-string eval-exp[: (:Exp:) :],
"\n") ]|
to-funcons:
|[ execute[: int(:Id:); :] ]| ->
|[ assign (bound id[: (:Id:) :],
|[ initialise-variable (bound id[: (:Id:) :],
0) ]|
to-funcons:
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
@@ -32,7 +33,7 @@ to-funcons:
eval-exp[: (:Exp:) :]) ]|
to-funcons:
|[ execute[: int(:Id:)=(:Exp:); :] ]| ->
|[ assign (bound id[: (:Id:) :],
|[ initialise-variable (bound id[: (:Id:) :],
eval-exp[: (:Exp:) :]) ]|
to-funcons:
|[ execute[: return(:Exp:); :] ]| ->
@@ -57,16 +58,6 @@ to-funcons:
|[ execute[: while((:Exp:))(:Block:) :] ]| ->
|[ while (eval-exp[: (:Exp:) :],
execute-block[: (:Block:) :]) ]|
to-funcons:
|[ execute[: for(int(:Id:)=(:Exp1:);(:Exp2:))(:Block:) :] ]| ->
|[ scope (bind (id[: (:Id:) :],
allocate-initialised-variable (integers,
eval-exp[: (:Exp1:) :])),
while (eval-exp[: (:Exp2:) :],
sequential (execute-block[: (:Block:) :],
assign (bound id[: (:Id:) :],
int-add (1,
assigned bound id[: (:Id:) :]))))) ]|
to-funcons:
|[ execute[: :] ]| ->
|[ null ]|
@@ -76,8 +67,7 @@ to-funcons:
execute[: (:Stmt+:) :]) ]|
to-funcons:
|[ execute-block[: {(:Stmt*:)} :] ]| ->
|[ scope (collateral (collect-declared-vars[: (:Stmt*:) :]),
execute[: (:Stmt*:) :]) ]|
|[ execute[: (:Stmt*:) :] ]|
// # Handling parameter declarations
@@ -110,6 +100,19 @@ to-funcons:
function abstraction (scope (collateral (collect-declared-vars[: (:Stmt*:) :],
collect-params[: (:Params:) :]),
execute[: (:Stmt*:) :]))) ]|
to-funcons:
|[ collect-declared-vars[: {(:Stmt*:)} :] ]| ->
|[ collect-declared-vars[: (:Stmt*:) :] ]|
to-funcons:
|[ collect-declared-vars[: if((:Exp:))(:Block:) :] ]| ->
|[ collect-declared-vars[: (:Block:) :] ]|
to-funcons:
|[ collect-declared-vars[: if((:Exp:))(:Block1:)else(:Block2:) :] ]| ->
|[ collect-declared-vars[: (:Block1:) :],
collect-declared-vars[: (:Block2:) :] ]|
to-funcons:
|[ collect-declared-vars[: while((:Exp:))(:Block:) :] ]| ->
|[ collect-declared-vars[: (:Block:) :] ]|
to-funcons:
|[ collect-declared-vars[: :] ]| ->
|[ map () ]|

View File

@@ -1,90 +0,0 @@
initialise-binding scope
(collateral
(bind
("fib",
function abstraction
(scope
(collateral
(map
( ),
map
( ),
map
( ),
bind
("n",
allocate-initialised-variable
(integers,
checked head
given))),
sequential
(if-else
(is-less-or-equal
(assigned
(bound
("n")),
decimal-natural
("1")),
scope
(collateral
(map
( )),
return assigned
(bound
("n"))),
null),
return int-add
(handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("1")))),
handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
decimal-natural
("2"))))))))),
map
( ),
map
( )),
sequential
(null,
scope
(bind
("n",
allocate-initialised-variable
(integers,
0)),
while
(is-less
(assigned
(bound
("n")),
decimal-natural
("5")),
sequential
(scope
(collateral
(map
( )),
print handle-return apply
(bound "fib",
list
(assigned
(bound
("n"))))),
assign
(bound "n",
int-add
(1,
assigned bound
"n")))))))

View File

@@ -1,10 +0,0 @@
fun fib(n) {
if (n <= 1) {
return n;
}
return fib(n-1) + fib(n-2);
}
for (int n = 0; n < 5) {
print(fib(n));
}

View File

@@ -20,36 +20,32 @@ initialise-binding scope
sequential
(if-else
(is-less-or-equal
(assigned
(bound
("n")),
(assigned bound
"n",
decimal-natural
("1")),
scope
(collateral
(map
( )),
return assigned
(bound
("n"))),
return assigned bound
"n"),
null),
return int-add
(handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
(assigned bound
"n",
decimal-natural
("1")))),
handle-return apply
(bound "fib",
list
(integer-subtract
(assigned
(bound
("n")),
(assigned bound
"n",
decimal-natural
("2"))))))))),
bind
@@ -60,16 +56,15 @@ initialise-binding scope
( )),
sequential
(null,
assign
initialise-variable
(bound "n",
0),
while
(is-less
(assigned
(bound
("n")),
(assigned bound
"n",
decimal-natural
("5")),
("8")),
scope
(collateral
(map
@@ -79,17 +74,18 @@ initialise-binding scope
map
( )),
sequential
(print handle-return apply
(print
(to-string handle-return apply
(bound "fib",
list
(assigned
(bound
("n")))),
(assigned bound
"n")),
"
"),
assign
(bound "n",
int-add
(assigned
(bound
("n")),
(assigned bound
"n",
decimal-natural
("1"))))))))

View File

@@ -8,7 +8,7 @@ fun fib(n) {
int n = 0;
while (n < 5) {
while (n < 8) {
print(fib(n));
n = n + 1;
}

View File

@@ -6,59 +6,64 @@ initialise-binding scope
(integers)),
map
( ),
map
( ),
map
( ),
map
( )),
sequential
(assign
(bound "x",
int-mul
(decimal-natural
("10"),
decimal-natural
("10"))),
print assigned
(bound
("x")),
scope
(collateral
(bind
("x",
allocate-variable
(integers)),
bind
("y",
allocate-variable
(integers)),
map
( ),
map
( )),
sequential
(initialise-variable
(bound "x",
int-mul
(decimal-natural
("10"),
decimal-natural
("10"))),
print
(to-string assigned bound
"x",
"
"),
initialise-variable
(bound "y",
decimal-natural
("5")),
if-else
(true,
scope
(collateral
(bind
("y",
allocate-variable
(integers)),
map
( ),
map
( )),
sequential
(assign
(bound "x",
decimal-natural
("10")),
assign
(initialise-variable
(bound "y",
int-mul
(assigned
(bound
("x")),
assigned
(bound
("x")))),
print assigned
(bound
("x")),
print assigned
(bound
("y")))),
print assigned
(bound
("y"))))
(assigned bound
"x",
assigned bound
"x")),
print
(to-string assigned bound
"x",
"
"),
print
(to-string assigned bound
"y",
"
"))),
null),
print
(to-string assigned bound
"y",
"
")))

View File

@@ -1,13 +1,15 @@
int x =10*10;
print(x);
//y = 100;
//print y;
{
int x = 10;
int x =10*10;
print x;
int y = 5;
if(true){
// int x = 10;
int y = x*x;
print(x);
print(y);
print x;
print y;
}
print(y);
//int x = 1;
print y;

View File

@@ -1,29 +1,4 @@
initialise-binding scope
(collateral
(bind
("x",
allocate-variable
(integers)),
map
( ),
map
( ),
map
( ),
map
( )),
sequential
(assign
(bound "x",
int-mul
(decimal-natural
("10"),
decimal-natural
("10"))),
print assigned
(bound
("x")),
scope
(collateral
(bind
("x",
@@ -37,28 +12,48 @@ initialise-binding scope
( ),
map
( ),
bind
("y",
allocate-variable
(integers)),
map
( ),
map
( )),
sequential
(assign
(initialise-variable
(bound "x",
int-mul
(decimal-natural
("10"),
decimal-natural
("10")),
assign
("10"))),
sequential
(initialise-variable
(bound "y",
int-mul
(assigned
(bound
("x")),
assigned
(bound
("x")))),
print assigned
(bound
("x")),
print assigned
(bound
("y")))),
print assigned
(bound
("x"))))
(assigned bound
"x",
assigned bound
"x")),
print
(to-string assigned bound
"x",
"
"),
print
(to-string assigned bound
"y",
"
")),
if-else
(false,
initialise-variable
(bound "y",
0),
null),
print
(to-string assigned bound
"y",
"
")))

View File

@@ -1,11 +1,13 @@
int x =10*10;
print(x);
{
int x = 10;
int y = x*x;
print(x);
print(y);
}
print(x);
if(false){
int y =0;
}
print(y);

View File

@@ -20,10 +20,8 @@ Syntax Exp:exp ::= id
| exp '>=' exp
| exp '>' exp
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)?
Semantics eval-exp[[ _:exp ]] : =>values
Rule eval-exp[[ Id ]] = assigned(bound(id[[ Id ]]))
Rule eval-exp[[ Id ]] = assigned bound id[[ Id ]]
Rule eval-exp[[ Int ]] = int-val[[ Int ]]
Rule eval-exp[[ 'true' ]] = true
Rule eval-exp[[ 'false' ]] = false
@@ -42,18 +40,28 @@ Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]]
Rule eval-exp[[ Id '(' ParamValues? ')' ]] = handle-return apply(bound id[[ Id ]], eval-params[[ ParamValues? ]])
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
## Handling expressions for function calls
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)?
Semantics eval-params[[ _:paramvalues? ]] : lists(values)
Rule eval-params[[ ]] = list()
Rule eval-params[[ Exp ]] = list(eval-exp[[ Exp ]])
Rule eval-params[[ Exp ',' ParamValues ]] = cons(eval-exp[[ Exp ]], eval-params[[ ParamValues ]])
## Ids
Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
Semantics id[[ _:id ]] : identifiers
Rule id[[ Id ]] = \"Id\"
## Integers and decimals
Syntax Int:int ::= '0' | ('-'?_decimal)
Semantics int-val[[ _:int ]] : ints
@@ -71,4 +79,24 @@ Rule dec-val[[ Dec ]] = decimal-natural(\"Dec\")
//Semantics eval-exp[[ _:exp ]] : => values
//
//Rule eval-exp[[ Id ]] = assigned bound id[[ Id ]]
//
//Rule eval-exp[[ Int ]] = int-val[[ Int ]]
//
//Rule eval-exp[[ Exp1 '+' Exp2 ]] =
// int-add(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
//
//Rule eval-exp[[ Exp1 '/' Exp2 ]] =
// checked int-div(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
//
//Rule eval-exp[[ Id '(' ParamValues? ')' ]] =
// handle-return apply(bound id[[ Id ]], eval-params[[ ParamValues? ]])
//
//Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]

View File

@@ -1,5 +1,5 @@
Funcon increment(Id:ids) : => values
~> assign(bound(\"Id\"), int-add(assigned(bound(\"Id\")), 1))
~> assign(bound(Id), int-add(assigned(bound(Id)), 1))
Funcon
print-line(S:strings) : => null-type

View File

@@ -2,7 +2,7 @@ Language "IBAFlang"
Syntax START:start ::= statement*
Semantics start[[ _:start ]] : =>null-type
Rule start[[ Stmt* ]] = initialise-binding execute-block[[ '{' Stmt* '}' ]]
Rule start[[ Stmt* ]] = initialise-binding scope(collateral(collect-declared-vars[[ Stmt* ]]), execute[[ Stmt* ]])
@@ -16,10 +16,3 @@ lexical syntax
lexical restrictions
``id`` -/- [A-Za-z0-9]
*/
Syntax SDF
/*
context-free syntax
``exp ::= exp '+' exp`` {assoc}
``exp ::= exp '&&' exp`` {assoc}
*/

View File

@@ -5,7 +5,7 @@ Language "IBAFlang"
Syntax Block: block ::= '{' statement* '}'
Syntax Stmt: statement ::= block
| 'print' '(' exp ')' ';'
| 'print' exp ';'
| 'int' id ';'
| id '=' exp ';'
| 'int' id '=' exp ';'
@@ -13,7 +13,6 @@ Syntax Stmt: statement ::= block
| 'fun' id '(' params ')' block
| 'if' '(' exp ')' block ('else' block)?
| 'while' '(' exp ')' block
| 'for' '(' 'int' id '=' exp ';' exp ')' block
Syntax Params: params ::= id (',' params)?
@@ -21,26 +20,22 @@ Syntax Params: params ::= id (',' params)?
Semantics execute[[ Stmt*:statement* ]] : =>null-type
Rule execute[[ Block ]] = execute-block[[ Block ]]
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
Rule execute[[ 'int' Id ';' ]] = assign(bound id[[ Id ]], 0)
Rule execute[[ 'print' Exp ';' ]] = print(to-string eval-exp[[ Exp ]], "\n")
Rule execute[[ 'int' Id ';' ]] = initialise-variable(bound id[[ Id ]], 0)
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
Rule execute[[ 'int' Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
Rule execute[[ 'int' Id '=' Exp ';' ]] = initialise-variable(bound id[[ Id ]], eval-exp[[ Exp ]])
Rule execute[[ 'return' Exp ';' ]] = return eval-exp[[ Exp ]]
Rule execute[[ 'return' ';' ]] = return null
Rule execute[[ 'fun' Id '(' Params ')' Block ]] = null
Rule execute[[ 'if' '(' Exp ')' Block ]] = if-else(eval-exp[[ Exp ]], execute-block[[ Block ]], null)
Rule execute[[ 'if' '(' Exp ')' Block1 'else' Block2 ]] = if-else(eval-exp[[ Exp ]], execute-block[[ Block1 ]], execute-block[[ Block2 ]])
Rule execute[[ 'while' '(' Exp ')' Block ]] = while(eval-exp[[ Exp ]], execute-block[[ Block ]])
Rule execute[[ 'for' '(' 'int' Id '=' Exp1 ';' Exp2 ')' Block ]] = scope(
bind(id[[ Id ]], allocate-initialised-variable(integers, eval-exp[[ Exp1 ]])),
while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], assign(bound id[[ Id ]], int-add(1, assigned bound id[[ Id ]]))))
)
Rule execute[[ ]] = null
Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
Semantics execute-block[[ _:block ]] : =>null-type
Rule execute-block[[ '{' Stmt* '}' ]] = scope(collateral(collect-declared-vars[[ Stmt* ]]), execute[[ Stmt* ]])
Rule execute-block[[ '{' Stmt* '}' ]] = execute[[ Stmt* ]]
@@ -72,6 +67,11 @@ Rule collect-declared-vars[[ 'fun' Id '(' Params ')' '{' Stmt* '}' ]] = bind(id[
)
)
Rule collect-declared-vars[[ '{' Stmt* '}' ]] = collect-declared-vars[[ Stmt* ]]
Rule collect-declared-vars[[ 'if' '(' Exp ')' Block ]] = collect-declared-vars[[ Block ]]
Rule collect-declared-vars[[ 'if' '(' Exp ')' Block1 'else' Block2 ]] = collect-declared-vars[[ Block1 ]], collect-declared-vars[[ Block2 ]]
Rule collect-declared-vars[[ 'while' '(' Exp ')' Block ]] = collect-declared-vars[[ Block ]]
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* ]]