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

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 ]]))
Semantics eval-exp[[ _:exp ]] : =>values
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,34 +13,29 @@ 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)?
Semantics execute[[ Stmt*:statement* ]] : => null-type
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* ]])
Semantics execute-block[[ _:block ]] : =>null-type
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* ]]