voor de initialise-variable verandering

This commit is contained in:
Peter
2024-03-10 09:53:47 +01:00
parent 613fa9ab1a
commit cb7f6f8e5d
18 changed files with 163 additions and 183 deletions

View File

@@ -38,14 +38,22 @@ context-free syntax // Language
L-id L-id
L-exp.L-exp--L-int = L-exp.L-exp--L-int =
L-int L-int
L-exp.L-exp--R-true =
"true"
L-exp.L-exp--R-false =
"false"
L-exp.L-exp--L-exp-PLUS-L-exp = L-exp.L-exp--L-exp-PLUS-L-exp =
L-exp "+" L-exp L-exp "+" L-exp
L-exp.L-exp--L-exp-DASH-L-exp = L-exp.L-exp--L-exp-DASH-L-exp =
L-exp "-" L-exp L-exp "-" L-exp
L-exp.L-exp--L-exp-LESS-EQUALS-L-exp =
L-exp "<=" L-exp
L-exp.L-exp--L-exp-LESS-L-exp =
L-exp "<" L-exp
L-exp.L-exp--L-id-LPAREN-L-paramvalues-Q-RPAREN =
L-id "(" L-paramvalues? ")"
L-exp.L-exp--LPAREN-L-exp-RPAREN =
"(" L-exp ")"
L-exp.L-exp--R-true =
"true"
L-exp.L-exp--R-false =
"false"
L-exp.L-exp--L-exp-STAR-L-exp = L-exp.L-exp--L-exp-STAR-L-exp =
L-exp "*" L-exp L-exp "*" L-exp
L-exp.L-exp--L-exp-SLASH-L-exp = L-exp.L-exp--L-exp-SLASH-L-exp =
@@ -62,14 +70,6 @@ context-free syntax // Language
L-exp ">=" L-exp L-exp ">=" L-exp
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
L-exp.L-exp--L-exp-LESS-EQUALS-L-exp =
L-exp "<=" L-exp
L-exp.L-exp--L-exp-LESS-L-exp =
L-exp "<" L-exp
L-exp.L-exp--L-id-LPAREN-L-paramvalues-Q-RPAREN =
L-id "(" L-paramvalues? ")"
L-exp.L-exp--LPAREN-L-exp-RPAREN =
"(" L-exp ")"
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?

View File

@@ -39,6 +39,9 @@ context-free syntax // Language
// # Statements // # Statements
L-block.L-block--LBRACE-L-statement-S-RBRACE =
"{" L-statement* "}"
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-LPAREN-L-exp-RPAREN-SEMI =
@@ -64,9 +67,6 @@ context-free syntax // Language
"else" L-block "else" L-block
L-block.L-block--LBRACE-L-statement-S-RBRACE =
"{" L-statement* "}"
L-params.L-params--L-id-C-COMMA-L-params-D-Q = L-params.L-params--L-id-C-COMMA-L-params-D-Q =
L-id L-COMMA-L-params? L-id L-COMMA-L-params?
@@ -124,15 +124,15 @@ variables // Meta-variables
// # Statements // # 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-block = "(:Block" [1-9]? ":)" {prefer} L-block = "(:Block" [1-9]? ":)" {prefer}
L-block? = "(:Block" [1-9]? "?:)" {prefer} L-block? = "(:Block" [1-9]? "?:)" {prefer}
L-block* = "(:Block" [1-9]? "*:)" {prefer} L-block* = "(:Block" [1-9]? "*:)" {prefer}
L-block+ = "(:Block" [1-9]? "+:)" {prefer} L-block+ = "(:Block" [1-9]? "+:)" {prefer}
L-statement* = "..." [1-9]? {prefer} L-statement* = "..." [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-params = "(:Params" [1-9]? ":)" {prefer} L-params = "(:Params" [1-9]? ":)" {prefer}
L-params? = "(:Params" [1-9]? "?:)" {prefer} L-params? = "(:Params" [1-9]? "?:)" {prefer}
L-params* = "(:Params" [1-9]? "*:)" {prefer} L-params* = "(:Params" [1-9]? "*:)" {prefer}

File diff suppressed because one or more lines are too long

View File

@@ -54,7 +54,7 @@ to-funcons:
eval-exp[: (:Exp2:) :]) ]| eval-exp[: (:Exp2:) :]) ]|
to-funcons: to-funcons:
|[ eval-exp[: (:Exp1:)||(:Exp2:) :] ]| -> |[ eval-exp[: (:Exp1:)||(:Exp2:) :] ]| ->
|[ and (eval-exp[: (:Exp1:) :], |[ or (eval-exp[: (:Exp1:) :],
eval-exp[: (:Exp2:) :]) ]| eval-exp[: (:Exp2:) :]) ]|
to-funcons: to-funcons:
|[ eval-exp[: (:Exp1:)==(:Exp2:) :] ]| -> |[ eval-exp[: (:Exp1:)==(:Exp2:) :] ]| ->
@@ -85,7 +85,7 @@ to-funcons:
|[ eval-exp[: (:Exp:) :] ]| |[ eval-exp[: (:Exp:) :] ]|
to-funcons: to-funcons:
|[ eval-params[: :] ]| -> |[ eval-params[: :] ]| ->
|[ [] ]| |[ list () ]|
to-funcons: to-funcons:
|[ eval-params[: (:Exp:) :] ]| -> |[ eval-params[: (:Exp:) :] ]| ->
|[ list (eval-exp[: (:Exp:) :]) ]| |[ list (eval-exp[: (:Exp:) :]) ]|

View File

@@ -1,7 +1,9 @@
if (true) { if (true) {
print(true); print(true);
}else{print(false);} }else{
print(false);
}

View File

@@ -70,7 +70,7 @@ initialise-binding scope
(bound (bound
("n")), ("n")),
decimal-natural decimal-natural
("8")), ("5")),
sequential sequential
(scope (scope
(collateral (collateral

View File

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

View File

@@ -69,7 +69,7 @@ initialise-binding scope
(bound (bound
("n")), ("n")),
decimal-natural decimal-natural
("8")), ("5")),
scope scope
(collateral (collateral
(map (map

View File

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

View File

@@ -0,0 +1,64 @@
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",
allocate-variable
(integers)),
bind
("y",
allocate-variable
(integers)),
map
( ),
map
( ),
map
( )),
sequential
(assign
(bound "x",
decimal-natural
("10")),
assign
(bound "y",
int-mul
(assigned
(bound
("x")),
assigned
(bound
("x")))),
print assigned
(bound
("x")),
print assigned
(bound
("y")))),
print assigned
(bound
("y"))))

View File

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

View File

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

View File

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

View File

@@ -1,50 +0,0 @@
L-start--L-pgm(
L-pgm--R-int-L-idlist-SEMI-L-stmt(
L-idlist--L-id-C-COMMA-L-idlist-D-Q(
LEX-id("x")
, Some(L-COMMA-L-idlist--COMMA-L-idlist(L-idlist--L-id-C-COMMA-L-idlist-D-Q(LEX-id("y"), None())))
)
, L-stmt--L-stmt-L-stmt(
L-stmt--L-id-EQUALS-L-exp-SEMI(
LEX-id("x")
, L-exp--L-aexp(
L-aexp--L-aexp-PERCENT-L-aexp(
L-aexp--L-int(L-int--C-DASH-Q-L-decimal-D(L-DASH-Q-L-decimal--DASH-Q-L-decimal(None(), LEX-decimal("10"))))
, L-aexp--L-int(L-int--C-DASH-Q-L-decimal-D(L-DASH-Q-L-decimal--DASH-Q-L-decimal(None(), LEX-decimal("3"))))
)
)
)
, L-stmt--L-stmt-L-stmt(
L-stmt--L-id-EQUALS-L-exp-SEMI(
LEX-id("y")
, L-exp--L-aexp(
L-aexp--L-aexp-PLUS-L-aexp(
L-aexp--L-id(LEX-id("x"))
, L-aexp--L-int(L-int--C-DASH-Q-L-decimal-D(L-DASH-Q-L-decimal--DASH-Q-L-decimal(None(), LEX-decimal("10"))))
)
)
)
, L-stmt--L-stmt-L-stmt(
L-stmt--R-print-LPAREN-L-exp-RPAREN-SEMI(
amb(
[ amb(
[L-exp--L-id(LEX-id("x")), L-exp--L-bexp(L-bexp--L-id(LEX-id("x")))]
)
, L-exp--L-aexp(L-aexp--L-id(LEX-id("x")))
]
)
)
, L-stmt--R-print-LPAREN-L-exp-RPAREN-SEMI(
amb(
[ amb(
[L-exp--L-id(LEX-id("y")), L-exp--L-bexp(L-bexp--L-id(LEX-id("y")))]
)
, L-exp--L-aexp(L-aexp--L-id(LEX-id("y")))
]
)
)
)
)
)
)
)

View File

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

View File

@@ -3,10 +3,14 @@ Language "IBAFlang"
# 1: General expressions # 1: General expressions
Syntax Exp:exp ::= id Syntax Exp:exp ::= id
| int | int
| 'true'
| 'false'
| exp '+' exp | exp '+' exp
| exp '-' exp | exp '-' exp
| exp '<=' exp
| exp '<' exp
| id '(' paramvalues? ')'
| '(' exp ')'
| 'true'
| 'false'
| exp '*' exp | exp '*' exp
| exp '/' exp | exp '/' exp
| exp '%' exp | exp '%' exp
@@ -15,10 +19,6 @@ Syntax Exp:exp ::= id
| exp '==' exp | exp '==' exp
| exp '>=' exp | exp '>=' exp
| exp '>' exp | exp '>' exp
| exp '<=' exp
| exp '<' exp
| id '(' paramvalues? ')'
| '(' exp ')'
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)? Syntax ParamValues: paramvalues ::= exp (',' paramvalues)?
@@ -33,7 +33,7 @@ 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 ]])
Rule eval-exp[[ Exp1 '&&' Exp2 ]] = and(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]]) Rule eval-exp[[ Exp1 '&&' Exp2 ]] = and(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '||' Exp2 ]] = and(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]]) Rule eval-exp[[ Exp1 '||' Exp2 ]] = or(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
Rule eval-exp[[ Exp1 '==' Exp2 ]] = is-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) Rule eval-exp[[ Exp1 '==' Exp2 ]] = is-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
Rule eval-exp[[ Exp1 '>=' Exp2 ]] = is-greater-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) Rule eval-exp[[ Exp1 '>=' Exp2 ]] = is-greater-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
Rule eval-exp[[ Exp1 '>' Exp2 ]] = is-greater(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] ) Rule eval-exp[[ Exp1 '>' Exp2 ]] = is-greater(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
@@ -43,7 +43,7 @@ Rule eval-exp[[ Id '(' ParamValues? ')' ]] = handle-return apply(bound id[[ Id ]
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]] Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
Semantics eval-params[[ _:paramvalues? ]] : lists(values) Semantics eval-params[[ _:paramvalues? ]] : lists(values)
Rule eval-params[[ ]] = [] 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 ]])

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,6 +2,8 @@ Language "IBAFlang"
# Statements # Statements
Syntax Block: block ::= '{' statement* '}'
Syntax Stmt: statement ::= block Syntax Stmt: statement ::= block
| 'print' '(' exp ')' ';' | 'print' '(' exp ')' ';'
| 'int' id ';' | 'int' id ';'
@@ -13,7 +15,6 @@ Syntax Stmt: statement ::= block
| 'while' '(' exp ')' block | 'while' '(' exp ')' block
| 'for' '(' 'int' id '=' exp ';' exp ')' block | 'for' '(' 'int' id '=' exp ';' exp ')' block
Syntax Block: block ::= '{' statement* '}'
Syntax Params: params ::= id (',' params)? Syntax Params: params ::= id (',' params)?
@@ -32,7 +33,6 @@ Rule execute[[ 'if' '(' Exp ')' Block1 'else' Block2 ]] = if-else(eval-exp[[ Exp
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( Rule execute[[ 'for' '(' 'int' Id '=' Exp1 ';' Exp2 ')' Block ]] = scope(
bind(id[[ Id ]], allocate-initialised-variable(integers, eval-exp[[ Exp1 ]])), bind(id[[ Id ]], allocate-initialised-variable(integers, eval-exp[[ Exp1 ]])),
// while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], increment Id))
while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], assign(bound id[[ Id ]], int-add(1, assigned bound id[[ Id ]])))) while(eval-exp[[ Exp2 ]], sequential(execute-block[[ Block ]], assign(bound id[[ Id ]], int-add(1, assigned bound id[[ Id ]]))))
) )
@@ -44,6 +44,8 @@ Rule execute-block[[ '{' Stmt* '}' ]] = scope(collateral(collect-declared-vars[[
# Handling parameter declarations # Handling parameter declarations
Semantics collect-params[[ Params:params ]] : (=>environments)+ Semantics collect-params[[ Params:params ]] : (=>environments)+
@@ -69,6 +71,7 @@ Rule collect-declared-vars[[ 'fun' Id '(' Params ')' '{' Stmt* '}' ]] = bind(id[
) )
) )
) )
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* ]]