Implement basic functions (no parameters yet)
This commit is contained in:
@@ -66,6 +66,8 @@ context-free syntax // Language
|
|||||||
L-exp "<=" L-exp
|
L-exp "<=" L-exp
|
||||||
L-exp.L-exp--L-exp-LESS-L-exp =
|
L-exp.L-exp--L-exp-LESS-L-exp =
|
||||||
L-exp "<" L-exp
|
L-exp "<" L-exp
|
||||||
|
L-exp.L-exp--L-id-LPAREN-RPAREN =
|
||||||
|
L-id "(" ")"
|
||||||
L-exp.L-exp--LPAREN-L-exp-RPAREN =
|
L-exp.L-exp--LPAREN-L-exp-RPAREN =
|
||||||
"(" L-exp ")"
|
"(" L-exp ")"
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ context-free syntax // Language
|
|||||||
L-type L-id "=" L-exp ";"
|
L-type L-id "=" L-exp ";"
|
||||||
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
|
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
|
||||||
L-id "=" L-exp ";"
|
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-type.L-type--R-int =
|
L-type.L-type--R-int =
|
||||||
"int"
|
"int"
|
||||||
@@ -88,6 +92,7 @@ variables // Meta-variables
|
|||||||
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-statement* = "..." [1-9]? {prefer}
|
L-statement* = "..." [1-9]? {prefer}
|
||||||
|
L-statement* = "..." [1-9]? {prefer}
|
||||||
L-type = "(:Typ" [1-9]? ":)" {prefer}
|
L-type = "(:Typ" [1-9]? ":)" {prefer}
|
||||||
L-type? = "(:Typ" [1-9]? "?:)" {prefer}
|
L-type? = "(:Typ" [1-9]? "?:)" {prefer}
|
||||||
L-type* = "(:Typ" [1-9]? "*:)" {prefer}
|
L-type* = "(:Typ" [1-9]? "*:)" {prefer}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -76,6 +76,10 @@ to-funcons:
|
|||||||
|[ eval-exp[: (:Exp1:)<(:Exp2:) :] ]| ->
|
|[ eval-exp[: (:Exp1:)<(:Exp2:) :] ]| ->
|
||||||
|[ is-less (eval-exp[: (:Exp1:) :],
|
|[ is-less (eval-exp[: (:Exp1:) :],
|
||||||
eval-exp[: (:Exp2:) :]) ]|
|
eval-exp[: (:Exp2:) :]) ]|
|
||||||
|
to-funcons:
|
||||||
|
|[ eval-exp[: (:Id:)() :] ]| ->
|
||||||
|
|[ handle-return apply (bound id[: (:Id:) :],
|
||||||
|
null) ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ eval-exp[: ((:Exp:)) :] ]| ->
|
|[ eval-exp[: ((:Exp:)) :] ]| ->
|
||||||
|[ eval-exp[: (:Exp:) :] ]|
|
|[ eval-exp[: (:Exp:) :] ]|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ rules
|
|||||||
|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ execute[: {(:Stmt*:)} :] ]| ->
|
|[ execute[: {(:Stmt*:)} :] ]| ->
|
||||||
|[ scope (collateral collect-declared-vars[: (:Stmt*:) :],
|
|[ scope (collateral (collect-declared-vars[: (:Stmt*:) :]),
|
||||||
execute[: (:Stmt*:) :]) ]|
|
execute[: (:Stmt*:) :]) ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ execute[: print((:Exp:)); :] ]| ->
|
|[ execute[: print((:Exp:)); :] ]| ->
|
||||||
@@ -34,6 +34,15 @@ to-funcons:
|
|||||||
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|
||||||
|[ assign (bound id[: (:Id:) :],
|
|[ assign (bound id[: (:Id:) :],
|
||||||
eval-exp[: (:Exp:) :]) ]|
|
eval-exp[: (:Exp:) :]) ]|
|
||||||
|
to-funcons:
|
||||||
|
|[ execute[: return(:Exp:); :] ]| ->
|
||||||
|
|[ return eval-exp[: (:Exp:) :] ]|
|
||||||
|
to-funcons:
|
||||||
|
|[ execute[: return; :] ]| ->
|
||||||
|
|[ return null ]|
|
||||||
|
to-funcons:
|
||||||
|
|[ execute[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
||||||
|
|[ null ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ execute[: :] ]| ->
|
|[ execute[: :] ]| ->
|
||||||
|[ null ]|
|
|[ null ]|
|
||||||
@@ -55,6 +64,10 @@ to-funcons:
|
|||||||
|[ bind (\"(:Id:)\",
|
|[ bind (\"(:Id:)\",
|
||||||
allocate-initialised-variable (integers,
|
allocate-initialised-variable (integers,
|
||||||
0)) ]|
|
0)) ]|
|
||||||
|
to-funcons:
|
||||||
|
|[ collect-declared-vars[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
||||||
|
|[ bind (\"(:Id:)\",
|
||||||
|
function abstraction (execute[: {(:Stmt*:)} :])) ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ collect-declared-vars[: :] ]| ->
|
|[ collect-declared-vars[: :] ]| ->
|
||||||
|[ map () ]|
|
|[ map () ]|
|
||||||
|
|||||||
39
IBAF-Tests/IBAF-1/basicControlFlow.fct
Normal file
39
IBAF-Tests/IBAF-1/basicControlFlow.fct
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
initialise-binding scope
|
||||||
|
(collateral
|
||||||
|
(map
|
||||||
|
( ),
|
||||||
|
bind
|
||||||
|
("i",
|
||||||
|
allocate-initialised-variable
|
||||||
|
(integers,
|
||||||
|
0)),
|
||||||
|
map
|
||||||
|
( )),
|
||||||
|
sequential
|
||||||
|
(if-else
|
||||||
|
(true,
|
||||||
|
print true,
|
||||||
|
print false),
|
||||||
|
assign
|
||||||
|
(bound "i",
|
||||||
|
decimal-natural
|
||||||
|
("0")),
|
||||||
|
while
|
||||||
|
(is-less
|
||||||
|
(assigned
|
||||||
|
(bound
|
||||||
|
("i")),
|
||||||
|
decimal-natural
|
||||||
|
("10")),
|
||||||
|
sequential
|
||||||
|
(assign
|
||||||
|
(bound "i",
|
||||||
|
int-add
|
||||||
|
(assigned
|
||||||
|
(bound
|
||||||
|
("i")),
|
||||||
|
decimal-natural
|
||||||
|
("1"))),
|
||||||
|
print assigned
|
||||||
|
(bound
|
||||||
|
("i"))))))
|
||||||
13
IBAF-Tests/IBAF-1/basicControlFlow.ibaf
Normal file
13
IBAF-Tests/IBAF-1/basicControlFlow.ibaf
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
if (true) {
|
||||||
|
print(true);
|
||||||
|
}else{print(false);}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (i < 10) {
|
||||||
|
i = i+1;
|
||||||
|
print(i);
|
||||||
|
}
|
||||||
28
IBAF-Tests/IBAF-1/fun.fct
Normal file
28
IBAF-Tests/IBAF-1/fun.fct
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
initialise-binding scope
|
||||||
|
(collateral
|
||||||
|
(bind
|
||||||
|
("test",
|
||||||
|
function abstraction
|
||||||
|
(scope
|
||||||
|
(collateral
|
||||||
|
(map
|
||||||
|
( )),
|
||||||
|
return decimal-natural
|
||||||
|
("3")))),
|
||||||
|
bind
|
||||||
|
("x",
|
||||||
|
allocate-initialised-variable
|
||||||
|
(integers,
|
||||||
|
0)),
|
||||||
|
map
|
||||||
|
( )),
|
||||||
|
sequential
|
||||||
|
(null,
|
||||||
|
assign
|
||||||
|
(bound "x",
|
||||||
|
handle-return apply
|
||||||
|
(bound "test",
|
||||||
|
null)),
|
||||||
|
print assigned
|
||||||
|
(bound
|
||||||
|
("x"))))
|
||||||
9
IBAF-Tests/IBAF-1/fun.ibaf
Normal file
9
IBAF-Tests/IBAF-1/fun.ibaf
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
int x = test();
|
||||||
|
|
||||||
|
print(x);
|
||||||
@@ -37,7 +37,7 @@ 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-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-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||||
Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||||
Rule eval-exp[[ Id '(' ')' ]] = enact id[[ Id ]]
|
Rule eval-exp[[ Id '(' ')' ]] = handle-return apply(bound id[[ Id ]], null)
|
||||||
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
|
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
31
IBAF-cbs/IBAF/IBAF-Start/IBAF-Functions.cbs
Normal file
31
IBAF-cbs/IBAF/IBAF-Start/IBAF-Functions.cbs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//Syntax
|
||||||
|
//MD:
|
||||||
|
// method-declaration ::=
|
||||||
|
// 'public' type identifier '(' formal-list? ')' '{'
|
||||||
|
// var-declaration*
|
||||||
|
// statement*
|
||||||
|
// 'return' expression ';'
|
||||||
|
// '}'
|
||||||
|
//
|
||||||
|
//Type
|
||||||
|
// methods
|
||||||
|
// ~> functions(tuples(references(objects), minijava-values*), minijava-values)
|
||||||
|
//
|
||||||
|
//Semantics
|
||||||
|
// declare-methods[[MD*:method-declaration*]] : => envs
|
||||||
|
//Rule
|
||||||
|
// declare-methods[['public' T ID '(' FL? ')' '{' VD* S* 'return' E ';' '}']] =
|
||||||
|
// { id[[ID]] |->
|
||||||
|
// function closure scope (
|
||||||
|
// collateral ( // variables not allowed to shadow visible fields
|
||||||
|
// match ( given,
|
||||||
|
// tuple (
|
||||||
|
// pattern abstraction { "this" |-> allocate-initialised-variable ( pointers(objects), given ) },
|
||||||
|
// bind-formals[[FL?]]
|
||||||
|
// )
|
||||||
|
// ),
|
||||||
|
// object-single-inheritance-feature-map checked dereference first tuple-elements given,
|
||||||
|
// declare-variables[[VD*]] ),
|
||||||
|
//
|
||||||
|
// sequential ( execute[[S*]], evaluate[[E]] ) ) }
|
||||||
|
|
||||||
@@ -17,12 +17,12 @@ Syntax Typ:type ::= 'int'
|
|||||||
|
|
||||||
Semantics execute[[ Stmt*:statement* ]] : => null-type
|
Semantics execute[[ Stmt*:statement* ]] : => null-type
|
||||||
|
|
||||||
Rule execute[[ '{' Stmt* '}' ]] = scope(collateral collect-declared-vars[[ Stmt* ]], execute[[ Stmt* ]])
|
Rule execute[[ '{' Stmt* '}' ]] = scope(collateral(collect-declared-vars[[ Stmt* ]]), execute[[ Stmt* ]])
|
||||||
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
|
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print eval-exp[[ Exp ]]
|
||||||
Rule execute[[ Typ Id ';' ]] = null
|
Rule execute[[ Typ Id ';' ]] = null
|
||||||
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
|
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
|
||||||
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
|
Rule execute[[ Id '=' Exp ';' ]] = assign(bound id[[ Id ]], eval-exp[[ Exp ]])
|
||||||
Rule execute[[ 'return' Exp ';' ]] = return exal-exp[[ Exp ]]
|
Rule execute[[ 'return' Exp ';' ]] = return eval-exp[[ Exp ]]
|
||||||
Rule execute[[ 'return' ';' ]] = return null
|
Rule execute[[ 'return' ';' ]] = return null
|
||||||
Rule execute[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = null
|
Rule execute[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = null
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ Rule collect-declared-vars[[ Typ Id ';' ]] =
|
|||||||
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] =
|
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] =
|
||||||
bind(
|
bind(
|
||||||
\"Id\",
|
\"Id\",
|
||||||
handle-return function abstraction execute[[ '{' Stmt* '}' ]]
|
function abstraction(execute[[ '{' Stmt* '}' ]])
|
||||||
)
|
)
|
||||||
Rule collect-declared-vars[[ ]] = map()
|
Rule collect-declared-vars[[ ]] = map()
|
||||||
Rule collect-declared-vars[[ Stmt ]] = map()
|
Rule collect-declared-vars[[ Stmt ]] = map()
|
||||||
|
|||||||
Reference in New Issue
Block a user