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

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