add parameters to functions
This commit is contained in:
@@ -66,11 +66,18 @@ context-free syntax // Language
|
||||
L-exp "<=" L-exp
|
||||
L-exp.L-exp--L-exp-LESS-L-exp =
|
||||
L-exp "<" L-exp
|
||||
L-exp.L-exp--L-id-LPAREN-RPAREN =
|
||||
L-id "(" ")"
|
||||
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-exp L-COMMA-L-paramvalues?
|
||||
|
||||
L-COMMA-L-paramvalues.L-COMMA-L-paramvalues--COMMA-L-paramvalues =
|
||||
"," L-paramvalues
|
||||
|
||||
|
||||
L-id.LEX-id =
|
||||
LEX-id
|
||||
L-decimal.LEX-decimal =
|
||||
@@ -83,6 +90,8 @@ context-free syntax // Semantics
|
||||
|
||||
FCT.T-eval-exp =
|
||||
"eval-exp" "[:" L-exp ":]"
|
||||
FCT.T-eval-params =
|
||||
"eval-params" "[:" L-paramvalues? ":]"
|
||||
FCT-Quoted.L-id = L-id
|
||||
FCT.T-id =
|
||||
"id" "[:" L-id ":]"
|
||||
@@ -107,6 +116,10 @@ variables // Meta-variables
|
||||
L-exp? = "(:Exp" [1-9]? "?:)" {prefer}
|
||||
L-exp* = "(:Exp" [1-9]? "*:)" {prefer}
|
||||
L-exp+ = "(:Exp" [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-id = "(:Id" [1-9]? ":)" {prefer}
|
||||
L-id? = "(:Id" [1-9]? "?:)" {prefer}
|
||||
L-id* = "(:Id" [1-9]? "*:)" {prefer}
|
||||
|
||||
@@ -12,6 +12,10 @@ lexical syntax // Language
|
||||
|
||||
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
@@ -22,6 +26,10 @@ syntax // Language
|
||||
|
||||
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
@@ -35,14 +43,27 @@ context-free syntax // Language
|
||||
"{" L-statement* "}"
|
||||
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI =
|
||||
"print" "(" L-exp ")" ";"
|
||||
L-statement.L-statement--L-id-SEMI =
|
||||
L-id ";"
|
||||
L-statement.L-statement--R-int-L-id-SEMI =
|
||||
"int" L-id ";"
|
||||
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
|
||||
L-id "=" L-exp ";"
|
||||
L-statement.L-statement--R-int-L-id-EQUALS-L-exp-SEMI =
|
||||
"int" L-id "=" L-exp ";"
|
||||
L-statement.L-statement--R-return-L-exp-Q-SEMI =
|
||||
"return" L-exp? ";"
|
||||
L-statement.L-statement--R-fun-L-id-LPAREN-RPAREN-LBRACE-L-statement-S-RBRACE =
|
||||
"fun" L-id "(" ")" "{" L-statement* "}"
|
||||
L-statement.L-statement--R-fun-L-id-LPAREN-L-params-RPAREN-LBRACE-L-statement-S-RBRACE =
|
||||
"fun" L-id "(" L-params ")" "{" L-statement* "}"
|
||||
|
||||
L-params.L-params--L-id-C-COMMA-L-params-D-Q =
|
||||
L-id L-COMMA-L-params?
|
||||
|
||||
L-COMMA-L-params.L-COMMA-L-params--COMMA-L-params =
|
||||
"," L-params
|
||||
|
||||
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
@@ -57,6 +78,12 @@ context-free syntax // Semantics
|
||||
FCT.T-execute =
|
||||
"execute" "[:" L-statement* ":]"
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
FCT-SEQ.T-collect-params =
|
||||
"collect-params" "[:" L-params ":]"
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
@@ -69,6 +96,10 @@ context-free syntax // Desugaring
|
||||
|
||||
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
@@ -84,6 +115,14 @@ variables // Meta-variables
|
||||
L-statement+ = "(:Stmt" [1-9]? "+:)" {prefer}
|
||||
L-statement* = "..." [1-9]? {prefer}
|
||||
L-statement* = "..." [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}
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
@@ -95,6 +134,10 @@ variables // Meta-variables
|
||||
|
||||
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -77,12 +77,22 @@ to-funcons:
|
||||
|[ is-less (eval-exp[: (:Exp1:) :],
|
||||
eval-exp[: (:Exp2:) :]) ]|
|
||||
to-funcons:
|
||||
|[ eval-exp[: (:Id:)() :] ]| ->
|
||||
|[ eval-exp[: (:Id:)((:ParamValues?:)) :] ]| ->
|
||||
|[ handle-return apply (bound id[: (:Id:) :],
|
||||
null) ]|
|
||||
eval-params[: (:ParamValues?:) :]) ]|
|
||||
to-funcons:
|
||||
|[ eval-exp[: ((:Exp:)) :] ]| ->
|
||||
|[ eval-exp[: (:Exp:) :] ]|
|
||||
to-funcons:
|
||||
|[ eval-params[: :] ]| ->
|
||||
|[ [] ]|
|
||||
to-funcons:
|
||||
|[ eval-params[: (:Exp:) :] ]| ->
|
||||
|[ list (eval-exp[: (:Exp:) :]) ]|
|
||||
to-funcons:
|
||||
|[ eval-params[: (:Exp:),(:ParamValues:) :] ]| ->
|
||||
|[ cons (eval-exp[: (:Exp:) :],
|
||||
eval-params[: (:ParamValues:) :]) ]|
|
||||
to-funcons-lex:
|
||||
FCTDoubleQuoted(L-id(LEX-id(str))) ->
|
||||
FCTString(<double-quote> str)
|
||||
|
||||
@@ -24,13 +24,17 @@ to-funcons:
|
||||
|[ execute[: print((:Exp:)); :] ]| ->
|
||||
|[ print eval-exp[: (:Exp:) :] ]|
|
||||
to-funcons:
|
||||
|[ execute[: (:Id:); :] ]| ->
|
||||
|[ execute[: int(:Id:); :] ]| ->
|
||||
|[ assign (bound id[: (:Id:) :],
|
||||
0) ]|
|
||||
to-funcons:
|
||||
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|
||||
|[ assign (bound id[: (:Id:) :],
|
||||
eval-exp[: (:Exp:) :]) ]|
|
||||
to-funcons:
|
||||
|[ execute[: int(:Id:)=(:Exp:); :] ]| ->
|
||||
|[ assign (bound id[: (:Id:) :],
|
||||
eval-exp[: (:Exp:) :]) ]|
|
||||
to-funcons:
|
||||
|[ execute[: return(:Exp:); :] ]| ->
|
||||
|[ return eval-exp[: (:Exp:) :] ]|
|
||||
@@ -38,7 +42,7 @@ to-funcons:
|
||||
|[ execute[: return; :] ]| ->
|
||||
|[ return null ]|
|
||||
to-funcons:
|
||||
|[ execute[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
||||
|[ execute[: fun(:Id:)((:Params:)){(:Stmt*:)} :] ]| ->
|
||||
|[ null ]|
|
||||
to-funcons:
|
||||
|[ execute[: :] ]| ->
|
||||
@@ -48,21 +52,37 @@ to-funcons:
|
||||
|[ sequential (execute[: (:Stmt:) :],
|
||||
execute[: (:Stmt+:) :]) ]|
|
||||
|
||||
// # Handling parameter declarations
|
||||
|
||||
|
||||
to-funcons:
|
||||
|[ collect-params[: (:Id:) :] ]| ->
|
||||
|[ bind (id[: (:Id:) :],
|
||||
allocate-initialised-variable (integers,
|
||||
checked head given)) ]|
|
||||
to-funcons:
|
||||
|[ collect-params[: (:Id:),(:Params:) :] ]| ->
|
||||
|[ collect-params[: (:Id:) :],
|
||||
give (checked tail given,
|
||||
collect-params[: (:Params:) :]) ]|
|
||||
|
||||
// # Handling variable declarations
|
||||
|
||||
|
||||
to-funcons:
|
||||
|[ collect-declared-vars[: (:Id:)=(:Exp:); :] ]| ->
|
||||
|[ collect-declared-vars[: int(:Id:)=(:Exp:); :] ]| ->
|
||||
|[ bind (id[: (:Id:) :],
|
||||
allocate-variable (integers)) ]|
|
||||
to-funcons:
|
||||
|[ collect-declared-vars[: (:Id:); :] ]| ->
|
||||
|[ collect-declared-vars[: int(:Id:); :] ]| ->
|
||||
|[ bind (id[: (:Id:) :],
|
||||
allocate-variable (integers)) ]|
|
||||
to-funcons:
|
||||
|[ collect-declared-vars[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
||||
|[ collect-declared-vars[: fun(:Id:)((:Params:)){(:Stmt*:)} :] ]| ->
|
||||
|[ bind (id[: (:Id:) :],
|
||||
function abstraction (execute[: {(:Stmt*:)} :])) ]|
|
||||
function abstraction (scope (collateral (collect-declared-vars[: (:Stmt*:) :],
|
||||
collect-params[: (:Params:) :]),
|
||||
execute[: (:Stmt*:) :]))) ]|
|
||||
to-funcons:
|
||||
|[ collect-declared-vars[: :] ]| ->
|
||||
|[ map () ]|
|
||||
|
||||
@@ -1,28 +1,65 @@
|
||||
initialise-binding scope
|
||||
(collateral
|
||||
(bind
|
||||
("a",
|
||||
allocate-variable
|
||||
(integers)),
|
||||
bind
|
||||
("test",
|
||||
function abstraction
|
||||
(scope
|
||||
(collateral
|
||||
(map
|
||||
( )),
|
||||
return decimal-natural
|
||||
("3")))),
|
||||
( ),
|
||||
bind
|
||||
("a",
|
||||
allocate-initialised-variable
|
||||
(integers,
|
||||
checked head
|
||||
given)),
|
||||
give
|
||||
(checked tail
|
||||
given,
|
||||
bind
|
||||
("b",
|
||||
allocate-initialised-variable
|
||||
(integers,
|
||||
checked head
|
||||
given)))),
|
||||
return int-mul
|
||||
(assigned
|
||||
(bound
|
||||
("a")),
|
||||
assigned
|
||||
(bound
|
||||
("b")))))),
|
||||
bind
|
||||
("x",
|
||||
allocate-initialised-variable
|
||||
(integers,
|
||||
0)),
|
||||
("b",
|
||||
allocate-variable
|
||||
(integers)),
|
||||
map
|
||||
( ),
|
||||
map
|
||||
( )),
|
||||
sequential
|
||||
(null,
|
||||
(assign
|
||||
(bound "a",
|
||||
decimal-natural
|
||||
("20")),
|
||||
null,
|
||||
assign
|
||||
(bound "x",
|
||||
(bound "b",
|
||||
handle-return apply
|
||||
(bound "test",
|
||||
null)),
|
||||
cons
|
||||
(decimal-natural
|
||||
("6"),
|
||||
list
|
||||
(decimal-natural
|
||||
("3"))))),
|
||||
print assigned
|
||||
(bound
|
||||
("x"))))
|
||||
("a")),
|
||||
print assigned
|
||||
(bound
|
||||
("b"))))
|
||||
@@ -1,9 +1,11 @@
|
||||
int a =20;
|
||||
|
||||
|
||||
fun test() {
|
||||
return 3;
|
||||
fun test(a, b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
x = test();
|
||||
int b =test(6, 3);
|
||||
|
||||
print(x);
|
||||
|
||||
print(a);
|
||||
print(b);
|
||||
@@ -17,9 +17,11 @@ Syntax Exp:exp ::= id
|
||||
| exp '>' exp
|
||||
| exp '<=' exp
|
||||
| exp '<' exp
|
||||
| id '(' ')'
|
||||
| id '(' paramvalues? ')'
|
||||
| '(' exp ')'
|
||||
|
||||
Syntax ParamValues: paramvalues ::= exp (',' paramvalues)?
|
||||
|
||||
Semantics eval-exp[[ _:exp ]] : => values
|
||||
Rule eval-exp[[ Id ]] = assigned(bound(id[[ Id ]]))
|
||||
Rule eval-exp[[ Int ]] = int-val[[ Int ]]
|
||||
@@ -37,9 +39,13 @@ Rule eval-exp[[ Exp1 '>=' Exp2 ]] = is-greater-or-equal(eval-exp[[ Exp1 ]], eval
|
||||
Rule eval-exp[[ Exp1 '>' Exp2 ]] = is-greater(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '<=' Exp2 ]] = is-less-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Id '(' ')' ]] = handle-return apply(bound id[[ Id ]], null)
|
||||
Rule eval-exp[[ Id '(' ParamValues? ')' ]] = handle-return apply(bound id[[ Id ]], eval-params[[ ParamValues? ]])
|
||||
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
|
||||
|
||||
Semantics eval-params[[ _:paramvalues? ]] : lists(values)
|
||||
Rule eval-params[[ ]] = []
|
||||
Rule eval-params[[ Exp ]] = list(eval-exp[[ Exp ]])
|
||||
Rule eval-params[[ Exp ',' ParamValues ]] = cons(eval-exp[[ Exp ]], eval-params[[ ParamValues ]])
|
||||
|
||||
|
||||
Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
|
||||
|
||||
@@ -4,20 +4,24 @@ Language "IBAFlang"
|
||||
|
||||
Syntax Stmt: statement ::= '{' statement* '}'
|
||||
| 'print' '(' exp ')' ';'
|
||||
| id ';'
|
||||
| 'int' id ';'
|
||||
| id '=' exp ';'
|
||||
| 'int' id '=' exp ';'
|
||||
| 'return' exp? ';'
|
||||
| 'fun' id '(' ')' '{' statement* '}'
|
||||
| 'fun' id '(' params ')' '{' statement* '}'
|
||||
|
||||
Syntax Params: params ::= id (',' params)?
|
||||
|
||||
Semantics execute[[ Stmt*:statement* ]] : => null-type
|
||||
|
||||
Rule execute[[ '{' Stmt* '}' ]] = scope(collateral(collect-declared-vars[[ Stmt* ]]), execute[[ Stmt* ]])
|
||||
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
|
||||
Rule execute[[ Id ';' ]] = assign(bound id[[ Id ]], 0)
|
||||
Rule execute[[ 'int' Id ';' ]] = assign(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[[ 'return' Exp ';' ]] = return eval-exp[[ Exp ]]
|
||||
Rule execute[[ 'return' ';' ]] = return null
|
||||
Rule execute[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = null
|
||||
Rule execute[[ 'fun' Id '(' Params ')' '{' Stmt* '}' ]] = null
|
||||
|
||||
Rule execute[[ ]] = null
|
||||
Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
|
||||
@@ -25,18 +29,32 @@ Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
|
||||
|
||||
|
||||
|
||||
# Handling parameter declarations
|
||||
|
||||
Semantics collect-params[[ Params:params ]] : (=>environments)+
|
||||
Rule collect-params[[ Id ]] = bind(
|
||||
id[[ Id ]],
|
||||
allocate-initialised-variable(integers, checked head given)
|
||||
)
|
||||
Rule collect-params[[ Id ',' Params ]] = collect-params[[ Id ]], give(checked tail given, collect-params[[ Params ]])
|
||||
|
||||
# Handling variable declarations
|
||||
|
||||
Semantics collect-declared-vars[[ Stmt*:statement* ]] : (=>environments)+
|
||||
|
||||
Rule collect-declared-vars[[ Id '=' Exp ';' ]] = bind(id[[ Id ]],
|
||||
Rule collect-declared-vars[[ 'int' Id '=' Exp ';' ]] = bind(id[[ Id ]],
|
||||
allocate-variable(integers)
|
||||
)
|
||||
Rule collect-declared-vars[[ Id ';' ]] = bind(id[[ Id ]],
|
||||
Rule collect-declared-vars[[ 'int' Id ';' ]] = bind(id[[ Id ]],
|
||||
allocate-variable(integers)
|
||||
)
|
||||
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = bind(id[[ Id ]],
|
||||
function abstraction(execute[[ '{' Stmt* '}' ]])
|
||||
Rule collect-declared-vars[[ 'fun' Id '(' Params ')' '{' Stmt* '}' ]] = bind(id[[ Id ]],
|
||||
function abstraction(
|
||||
scope(
|
||||
collateral(collect-declared-vars[[ Stmt* ]], collect-params[[ Params ]]),
|
||||
execute[[ Stmt* ]]
|
||||
)
|
||||
)
|
||||
)
|
||||
Rule collect-declared-vars[[ ]] = map()
|
||||
Rule collect-declared-vars[[ Stmt ]] = map()
|
||||
|
||||
Reference in New Issue
Block a user