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 # Datasource local storage ignored files
/dataSources/ /dataSources/
/dataSources.local.xml /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"> <module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>

View File

@@ -11,7 +11,19 @@ lexical syntax // Language
// # 1: General expressions // # 1: General expressions
// ## Handling expressions for function calls
// ## Ids
LEX-id = ( [a-z] | [A-Z] ) ( [a-z] | [A-Z] | [0-9] )* LEX-id = ( [a-z] | [A-Z] ) ( [a-z] | [A-Z] | [0-9] )*
// ## Integers and decimals
LEX-DASH = "-" LEX-DASH = "-"
LEX-decimal = [1-9] ( [0-9] )* LEX-decimal = [1-9] ( [0-9] )*
@@ -20,6 +32,18 @@ syntax // Language
// # 1: General expressions // # 1: General expressions
// ## Handling expressions for function calls
// ## Ids
// ## Integers and decimals
L-int-CF.L-int--R-0 = L-int-CF.L-int--R-0 =
"0" "0"
L-int-CF.L-int--C-DASH-Q-L-decimal-D = 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--L-exp-GREATER-L-exp =
L-exp ">" L-exp L-exp ">" L-exp
// ## Handling expressions for function calls
L-paramvalues.L-paramvalues--L-exp-C-COMMA-L-paramvalues-D-Q = L-paramvalues.L-paramvalues--L-exp-C-COMMA-L-paramvalues-D-Q =
L-exp L-COMMA-L-paramvalues? L-exp L-COMMA-L-paramvalues?
@@ -78,8 +106,16 @@ context-free syntax // Language
"," L-paramvalues "," L-paramvalues
// ## Ids
L-id.LEX-id = L-id.LEX-id =
LEX-id LEX-id
// ## Integers and decimals
L-decimal.LEX-decimal = L-decimal.LEX-decimal =
LEX-decimal LEX-decimal
@@ -90,11 +126,23 @@ context-free syntax // Semantics
FCT.T-eval-exp = FCT.T-eval-exp =
"eval-exp" "[:" L-exp ":]" "eval-exp" "[:" L-exp ":]"
// ## Handling expressions for function calls
FCT.T-eval-params = FCT.T-eval-params =
"eval-params" "[:" L-paramvalues? ":]" "eval-params" "[:" L-paramvalues? ":]"
// ## Ids
FCT-Quoted.L-id = L-id FCT-Quoted.L-id = L-id
FCT.T-id = FCT.T-id =
"id" "[:" L-id ":]" "id" "[:" L-id ":]"
// ## Integers and decimals
FCT.T-int-val = FCT.T-int-val =
"int-val" "[:" L-int ":]" "int-val" "[:" L-int ":]"
FCT-Quoted.L-decimal = L-decimal 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 variables // Meta-variables
// # 1: General expressions // # 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} 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}
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}
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} 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 sorts // ASTs
T-start T-start

View File

@@ -45,13 +45,5 @@ variables // Meta-variables
lexical restrictions lexical restrictions
LEX-id -/- [A-Za-z0-9] 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 sorts // ASTs
T-start T-start

View File

@@ -44,8 +44,8 @@ context-free syntax // Language
L-statement.L-statement--L-block = L-statement.L-statement--L-block =
L-block L-block
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI = L-statement.L-statement--R-print-L-exp-SEMI =
"print" "(" L-exp ")" ";" "print" L-exp ";"
L-statement.L-statement--R-int-L-id-SEMI = L-statement.L-statement--R-int-L-id-SEMI =
"int" L-id ";" "int" L-id ";"
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI = 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? "if" "(" L-exp ")" L-block L-R-else-L-block?
L-statement.L-statement--R-while-LPAREN-L-exp-RPAREN-L-block = L-statement.L-statement--R-while-LPAREN-L-exp-RPAREN-L-block =
"while" "(" L-exp ")" 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 = L-R-else-L-block.L-R-else-L-block--R-else-L-block =
"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: to-funcons:
|[ eval-exp[: (:Id:) :] ]| -> |[ eval-exp[: (:Id:) :] ]| ->
|[ assigned (bound (id[: (:Id:) :])) ]| |[ assigned bound id[: (:Id:) :] ]|
to-funcons: to-funcons:
|[ eval-exp[: (:Int:) :] ]| -> |[ eval-exp[: (:Int:) :] ]| ->
|[ int-val[: (:Int:) :] ]| |[ int-val[: (:Int:) :] ]|
@@ -83,6 +83,10 @@ to-funcons:
to-funcons: to-funcons:
|[ eval-exp[: ((:Exp:)) :] ]| -> |[ eval-exp[: ((:Exp:)) :] ]| ->
|[ eval-exp[: (:Exp:) :] ]| |[ eval-exp[: (:Exp:) :] ]|
// ## Handling expressions for function calls
to-funcons: to-funcons:
|[ eval-params[: :] ]| -> |[ eval-params[: :] ]| ->
|[ list () ]| |[ list () ]|
@@ -93,12 +97,20 @@ to-funcons:
|[ eval-params[: (:Exp:),(:ParamValues:) :] ]| -> |[ eval-params[: (:Exp:),(:ParamValues:) :] ]| ->
|[ cons (eval-exp[: (:Exp:) :], |[ cons (eval-exp[: (:Exp:) :],
eval-params[: (:ParamValues:) :]) ]| eval-params[: (:ParamValues:) :]) ]|
// ## Ids
to-funcons-lex: to-funcons-lex:
FCTDoubleQuoted(L-id(LEX-id(str))) -> FCTDoubleQuoted(L-id(LEX-id(str))) ->
FCTString(<double-quote> str) FCTString(<double-quote> str)
to-funcons: to-funcons:
|[ id[: (:Id:) :] ]| -> |[ id[: (:Id:) :] ]| ->
|[ \"(:Id:)\" ]| |[ \"(:Id:)\" ]|
// ## Integers and decimals
to-funcons: to-funcons:
|[ int-val[: 0 :] ]| -> |[ int-val[: 0 :] ]| ->
|[ 0 ]| |[ 0 ]|

View File

@@ -15,5 +15,6 @@ imports
rules rules
to-funcons: to-funcons:
|[ start[: (:Stmt*:) :] ]| -> |[ 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:) :] ]| ->
|[ execute-block[: (:Block:) :] ]| |[ execute-block[: (:Block:) :] ]|
to-funcons: to-funcons:
|[ execute[: print((:Exp:)); :] ]| -> |[ execute[: print(:Exp:); :] ]| ->
|[ print eval-exp[: (:Exp:) :] ]| |[ print (to-string eval-exp[: (:Exp:) :],
"\n") ]|
to-funcons: to-funcons:
|[ execute[: int(:Id:); :] ]| -> |[ execute[: int(:Id:); :] ]| ->
|[ assign (bound id[: (:Id:) :], |[ initialise-variable (bound id[: (:Id:) :],
0) ]| 0) ]|
to-funcons: to-funcons:
|[ execute[: (:Id:)=(:Exp:); :] ]| -> |[ execute[: (:Id:)=(:Exp:); :] ]| ->
@@ -32,7 +33,7 @@ to-funcons:
eval-exp[: (:Exp:) :]) ]| eval-exp[: (:Exp:) :]) ]|
to-funcons: to-funcons:
|[ execute[: int(:Id:)=(:Exp:); :] ]| -> |[ execute[: int(:Id:)=(:Exp:); :] ]| ->
|[ assign (bound id[: (:Id:) :], |[ initialise-variable (bound id[: (:Id:) :],
eval-exp[: (:Exp:) :]) ]| eval-exp[: (:Exp:) :]) ]|
to-funcons: to-funcons:
|[ execute[: return(:Exp:); :] ]| -> |[ execute[: return(:Exp:); :] ]| ->
@@ -57,16 +58,6 @@ to-funcons:
|[ execute[: while((:Exp:))(:Block:) :] ]| -> |[ execute[: while((:Exp:))(:Block:) :] ]| ->
|[ while (eval-exp[: (:Exp:) :], |[ while (eval-exp[: (:Exp:) :],
execute-block[: (:Block:) :]) ]| 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: to-funcons:
|[ execute[: :] ]| -> |[ execute[: :] ]| ->
|[ null ]| |[ null ]|
@@ -76,8 +67,7 @@ to-funcons:
execute[: (:Stmt+:) :]) ]| execute[: (:Stmt+:) :]) ]|
to-funcons: to-funcons:
|[ execute-block[: {(:Stmt*:)} :] ]| -> |[ execute-block[: {(:Stmt*:)} :] ]| ->
|[ scope (collateral (collect-declared-vars[: (:Stmt*:) :]), |[ execute[: (:Stmt*:) :] ]|
execute[: (:Stmt*:) :]) ]|
// # Handling parameter declarations // # Handling parameter declarations
@@ -110,6 +100,19 @@ to-funcons:
function abstraction (scope (collateral (collect-declared-vars[: (:Stmt*:) :], function abstraction (scope (collateral (collect-declared-vars[: (:Stmt*:) :],
collect-params[: (:Params:) :]), collect-params[: (:Params:) :]),
execute[: (:Stmt*:) :]))) ]| 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: to-funcons:
|[ collect-declared-vars[: :] ]| -> |[ collect-declared-vars[: :] ]| ->
|[ map () ]| |[ 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 sequential
(if-else (if-else
(is-less-or-equal (is-less-or-equal
(assigned (assigned bound
(bound "n",
("n")),
decimal-natural decimal-natural
("1")), ("1")),
scope scope
(collateral (collateral
(map (map
( )), ( )),
return assigned return assigned bound
(bound "n"),
("n"))),
null), null),
return int-add return int-add
(handle-return apply (handle-return apply
(bound "fib", (bound "fib",
list list
(integer-subtract (integer-subtract
(assigned (assigned bound
(bound "n",
("n")),
decimal-natural decimal-natural
("1")))), ("1")))),
handle-return apply handle-return apply
(bound "fib", (bound "fib",
list list
(integer-subtract (integer-subtract
(assigned (assigned bound
(bound "n",
("n")),
decimal-natural decimal-natural
("2"))))))))), ("2"))))))))),
bind bind
@@ -60,16 +56,15 @@ initialise-binding scope
( )), ( )),
sequential sequential
(null, (null,
assign initialise-variable
(bound "n", (bound "n",
0), 0),
while while
(is-less (is-less
(assigned (assigned bound
(bound "n",
("n")),
decimal-natural decimal-natural
("5")), ("8")),
scope scope
(collateral (collateral
(map (map
@@ -79,17 +74,18 @@ initialise-binding scope
map map
( )), ( )),
sequential sequential
(print handle-return apply (print
(bound "fib", (to-string handle-return apply
list (bound "fib",
(assigned list
(bound (assigned bound
("n")))), "n")),
"
"),
assign assign
(bound "n", (bound "n",
int-add int-add
(assigned (assigned bound
(bound "n",
("n")),
decimal-natural decimal-natural
("1")))))))) ("1"))))))))

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,13 @@
int x =10*10; int x =10*10;
print(x);
{ {
int x = 10;
int y = x*x; int y = x*x;
print(x); print(x);
print(y); 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
| exp '>' exp | exp '>' exp
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)? Semantics eval-exp[[ _:exp ]] : =>values
Rule eval-exp[[ Id ]] = assigned bound id[[ Id ]]
Semantics eval-exp[[ _:exp ]] : => values
Rule eval-exp[[ Id ]] = assigned(bound(id[[ Id ]]))
Rule eval-exp[[ Int ]] = int-val[[ Int ]] 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
@@ -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[[ Id '(' ParamValues? ')' ]] = handle-return apply(bound id[[ Id ]], eval-params[[ ParamValues? ]])
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]] Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
## Handling expressions for function calls
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)?
Semantics eval-params[[ _:paramvalues? ]] : lists(values) Semantics eval-params[[ _:paramvalues? ]] : lists(values)
Rule eval-params[[ ]] = list() Rule eval-params[[ ]] = list()
Rule eval-params[[ Exp ]] = list(eval-exp[[ Exp ]]) Rule eval-params[[ Exp ]] = list(eval-exp[[ Exp ]])
Rule eval-params[[ Exp ',' ParamValues ]] = cons(eval-exp[[ Exp ]], eval-params[[ ParamValues ]]) 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')* Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
Semantics id[[ _:id ]] : identifiers Semantics id[[ _:id ]] : identifiers
Rule id[[ Id ]] = \"Id\" Rule id[[ Id ]] = \"Id\"
## Integers and decimals
Syntax Int:int ::= '0' | ('-'?_decimal) Syntax Int:int ::= '0' | ('-'?_decimal)
Semantics int-val[[ _:int ]] : ints 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 Funcon increment(Id:ids) : => values
~> assign(bound(\"Id\"), int-add(assigned(bound(\"Id\")), 1)) ~> assign(bound(Id), int-add(assigned(bound(Id)), 1))
Funcon Funcon
print-line(S:strings) : => null-type print-line(S:strings) : => null-type

View File

@@ -2,7 +2,7 @@ Language "IBAFlang"
Syntax START:start ::= statement* Syntax START:start ::= statement*
Semantics start[[ _:start ]] : =>null-type 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 lexical restrictions
``id`` -/- [A-Za-z0-9] ``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 Block: block ::= '{' statement* '}'
Syntax Stmt: statement ::= block Syntax Stmt: statement ::= block
| 'print' '(' exp ')' ';' | 'print' exp ';'
| 'int' id ';' | 'int' id ';'
| id '=' exp ';' | id '=' exp ';'
| 'int' id '=' exp ';' | 'int' id '=' exp ';'
@@ -13,34 +13,29 @@ Syntax Stmt: statement ::= block
| 'fun' id '(' params ')' block | 'fun' id '(' params ')' block
| 'if' '(' exp ')' block ('else' block)? | 'if' '(' exp ')' block ('else' block)?
| 'while' '(' exp ')' block | 'while' '(' exp ')' block
| 'for' '(' 'int' id '=' exp ';' exp ')' block
Syntax Params: params ::= id (',' params)? Syntax Params: params ::= id (',' params)?
Semantics execute[[ Stmt*:statement* ]] : => null-type Semantics execute[[ Stmt*:statement* ]] : =>null-type
Rule execute[[ Block ]] = execute-block[[ Block ]] Rule execute[[ Block ]] = execute-block[[ Block ]]
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]] Rule execute[[ 'print' Exp ';' ]] = print(to-string eval-exp[[ Exp ]], "\n")
Rule execute[[ 'int' Id ';' ]] = assign(bound id[[ Id ]], 0) Rule execute[[ 'int' Id ';' ]] = initialise-variable(bound id[[ Id ]], 0)
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]]) 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' Exp ';' ]] = return eval-exp[[ Exp ]]
Rule execute[[ 'return' ';' ]] = return null Rule execute[[ 'return' ';' ]] = return null
Rule execute[[ 'fun' Id '(' Params ')' Block ]] = 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 ')' 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[[ '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[[ '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[[ ]] = null
Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]]) Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
Semantics execute-block[[ _:block ]] : => null-type 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[[ ]] = map()
Rule collect-declared-vars[[ Stmt ]] = 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 collect-declared-vars[[ Stmt1 Stmt2 Stmt* ]] = collect-declared-vars[[ Stmt1 ]], collect-declared-vars[[ Stmt2 ]], collect-declared-vars[[ Stmt* ]]