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

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 () ]|