all will be int
This commit is contained in:
@@ -35,10 +35,8 @@ context-free syntax // Language
|
|||||||
"{" L-statement* "}"
|
"{" L-statement* "}"
|
||||||
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI =
|
L-statement.L-statement--R-print-LPAREN-L-exp-RPAREN-SEMI =
|
||||||
"print" "(" L-exp ")" ";"
|
"print" "(" L-exp ")" ";"
|
||||||
L-statement.L-statement--L-type-L-id-SEMI =
|
L-statement.L-statement--L-id-SEMI =
|
||||||
L-type L-id ";"
|
L-id ";"
|
||||||
L-statement.L-statement--L-type-L-id-EQUALS-L-exp-SEMI =
|
|
||||||
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 =
|
L-statement.L-statement--R-return-L-exp-Q-SEMI =
|
||||||
@@ -46,13 +44,6 @@ context-free syntax // Language
|
|||||||
L-statement.L-statement--R-fun-L-id-LPAREN-RPAREN-LBRACE-L-statement-S-RBRACE =
|
L-statement.L-statement--R-fun-L-id-LPAREN-RPAREN-LBRACE-L-statement-S-RBRACE =
|
||||||
"fun" L-id "(" ")" "{" L-statement* "}"
|
"fun" L-id "(" ")" "{" L-statement* "}"
|
||||||
|
|
||||||
L-type.L-type--R-int =
|
|
||||||
"int"
|
|
||||||
L-type.L-type--R-bool =
|
|
||||||
"bool"
|
|
||||||
L-type.L-type--L-id =
|
|
||||||
L-id
|
|
||||||
|
|
||||||
|
|
||||||
// # Handling variable declarations
|
// # Handling variable declarations
|
||||||
|
|
||||||
@@ -93,10 +84,6 @@ variables // Meta-variables
|
|||||||
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-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}
|
|
||||||
|
|
||||||
// # Handling variable declarations
|
// # Handling variable declarations
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -24,12 +24,9 @@ to-funcons:
|
|||||||
|[ execute[: print((:Exp:)); :] ]| ->
|
|[ execute[: print((:Exp:)); :] ]| ->
|
||||||
|[ print eval-exp[: (:Exp:) :] ]|
|
|[ print eval-exp[: (:Exp:) :] ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ execute[: (:Typ:)(:Id:); :] ]| ->
|
|[ execute[: (:Id:); :] ]| ->
|
||||||
|[ null ]|
|
|
||||||
to-funcons:
|
|
||||||
|[ execute[: (:Typ:)(:Id:)=(:Exp:); :] ]| ->
|
|
||||||
|[ assign (bound id[: (:Id:) :],
|
|[ assign (bound id[: (:Id:) :],
|
||||||
eval-exp[: (:Exp:) :]) ]|
|
0) ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|
||||||
|[ assign (bound id[: (:Id:) :],
|
|[ assign (bound id[: (:Id:) :],
|
||||||
@@ -55,18 +52,16 @@ to-funcons:
|
|||||||
|
|
||||||
|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ collect-declared-vars[: (:Typ:)(:Id:)=(:Exp:); :] ]| ->
|
|[ collect-declared-vars[: (:Id:)=(:Exp:); :] ]| ->
|
||||||
|[ bind (\"(:Id:)\",
|
|[ bind (id[: (:Id:) :],
|
||||||
allocate-initialised-variable (integers,
|
allocate-variable (integers)) ]|
|
||||||
0)) ]|
|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ collect-declared-vars[: (:Typ:)(:Id:); :] ]| ->
|
|[ collect-declared-vars[: (:Id:); :] ]| ->
|
||||||
|[ bind (\"(:Id:)\",
|
|[ bind (id[: (:Id:) :],
|
||||||
allocate-initialised-variable (integers,
|
allocate-variable (integers)) ]|
|
||||||
0)) ]|
|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ collect-declared-vars[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
|[ collect-declared-vars[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|
||||||
|[ bind (\"(:Id:)\",
|
|[ bind (id[: (:Id:) :],
|
||||||
function abstraction (execute[: {(:Stmt*:)} :])) ]|
|
function abstraction (execute[: {(:Stmt*:)} :])) ]|
|
||||||
to-funcons:
|
to-funcons:
|
||||||
|[ collect-declared-vars[: :] ]| ->
|
|[ collect-declared-vars[: :] ]| ->
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ fun test() {
|
|||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x = test();
|
x = test();
|
||||||
|
|
||||||
print(x);
|
print(x);
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
|
|
||||||
int z =5;
|
z =5;
|
||||||
int x =5*3;
|
x =5*3;
|
||||||
print(x);
|
print(x);
|
||||||
|
|
||||||
x = 2;
|
x = 2;
|
||||||
print(x*3);
|
print(x*3);
|
||||||
|
|
||||||
{
|
{
|
||||||
int y =4;
|
y =4;
|
||||||
print (y);
|
print (y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
|
|||||||
|
|
||||||
Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
|
Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
|
||||||
|
|
||||||
Semantics id[[ _:id ]] : ids
|
Semantics id[[ _:id ]] : identifiers
|
||||||
Rule id[[ Id ]] = \"Id\"
|
Rule id[[ Id ]] = \"Id\"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,23 +4,16 @@ Language "IBAFlang"
|
|||||||
|
|
||||||
Syntax Stmt: statement ::= '{' statement* '}'
|
Syntax Stmt: statement ::= '{' statement* '}'
|
||||||
| 'print' '(' exp ')' ';'
|
| 'print' '(' exp ')' ';'
|
||||||
| type id ';'
|
| id ';'
|
||||||
| type id '=' exp ';'
|
|
||||||
| id '=' exp ';'
|
| id '=' exp ';'
|
||||||
| 'return' exp? ';'
|
| 'return' exp? ';'
|
||||||
| 'fun' id '(' ')' '{' statement* '}'
|
| 'fun' id '(' ')' '{' statement* '}'
|
||||||
|
|
||||||
Syntax Typ:type ::= 'int'
|
|
||||||
| 'bool'
|
|
||||||
| id
|
|
||||||
|
|
||||||
|
|
||||||
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[[ Id ';' ]] = assign(bound id[[ Id ]], 0)
|
||||||
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 eval-exp[[ Exp ]]
|
Rule execute[[ 'return' Exp ';' ]] = return eval-exp[[ Exp ]]
|
||||||
Rule execute[[ 'return' ';' ]] = return null
|
Rule execute[[ 'return' ';' ]] = return null
|
||||||
@@ -36,25 +29,13 @@ Rule execute[[ Stmt Stmt+ ]] = sequential(execute[[ Stmt ]], execute[[ Stmt+ ]])
|
|||||||
|
|
||||||
Semantics collect-declared-vars[[ Stmt*:statement* ]] : (=>environments)+
|
Semantics collect-declared-vars[[ Stmt*:statement* ]] : (=>environments)+
|
||||||
|
|
||||||
Rule collect-declared-vars[[ Typ Id '=' Exp ';' ]] =
|
Rule collect-declared-vars[[ Id '=' Exp ';' ]] = bind(id[[ Id ]],
|
||||||
bind(
|
allocate-variable(integers)
|
||||||
\"Id\",
|
|
||||||
allocate-initialised-variable(
|
|
||||||
integers, //TODO: use type
|
|
||||||
0 //TODO: use correct default value for type
|
|
||||||
)
|
)
|
||||||
|
Rule collect-declared-vars[[ Id ';' ]] = bind(id[[ Id ]],
|
||||||
|
allocate-variable(integers)
|
||||||
)
|
)
|
||||||
Rule collect-declared-vars[[ Typ Id ';' ]] =
|
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] = bind(id[[ Id ]],
|
||||||
bind(
|
|
||||||
\"Id\",
|
|
||||||
allocate-initialised-variable(
|
|
||||||
integers, //TODO: use type
|
|
||||||
0 //TODO: use correct default value for type
|
|
||||||
)
|
|
||||||
)
|
|
||||||
Rule collect-declared-vars[[ 'fun' Id '(' ')' '{' Stmt* '}' ]] =
|
|
||||||
bind(
|
|
||||||
\"Id\",
|
|
||||||
function abstraction(execute[[ '{' Stmt* '}' ]])
|
function abstraction(execute[[ '{' Stmt* '}' ]])
|
||||||
)
|
)
|
||||||
Rule collect-declared-vars[[ ]] = map()
|
Rule collect-declared-vars[[ ]] = map()
|
||||||
|
|||||||
Reference in New Issue
Block a user