Implement basic functions (no parameters yet)

This commit is contained in:
Peter
2023-12-10 16:29:29 +01:00
parent f594027de5
commit 165be5cfdb
12 changed files with 150 additions and 6 deletions

View File

@@ -66,6 +66,8 @@ 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--LPAREN-L-exp-RPAREN =
"(" L-exp ")"

View File

@@ -41,6 +41,10 @@ context-free syntax // Language
L-type L-id "=" L-exp ";"
L-statement.L-statement--L-id-EQUALS-L-exp-SEMI =
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 =
"int"
@@ -88,6 +92,7 @@ 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-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

View File

@@ -76,6 +76,10 @@ to-funcons:
|[ eval-exp[: (:Exp1:)<(:Exp2:) :] ]| ->
|[ is-less (eval-exp[: (:Exp1:) :],
eval-exp[: (:Exp2:) :]) ]|
to-funcons:
|[ eval-exp[: (:Id:)() :] ]| ->
|[ handle-return apply (bound id[: (:Id:) :],
null) ]|
to-funcons:
|[ eval-exp[: ((:Exp:)) :] ]| ->
|[ eval-exp[: (:Exp:) :] ]|

View File

@@ -18,7 +18,7 @@ rules
to-funcons:
|[ execute[: {(:Stmt*:)} :] ]| ->
|[ scope (collateral collect-declared-vars[: (:Stmt*:) :],
|[ scope (collateral (collect-declared-vars[: (:Stmt*:) :]),
execute[: (:Stmt*:) :]) ]|
to-funcons:
|[ execute[: print((:Exp:)); :] ]| ->
@@ -34,6 +34,15 @@ to-funcons:
|[ execute[: (:Id:)=(:Exp:); :] ]| ->
|[ assign (bound id[: (:Id:) :],
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:
|[ execute[: :] ]| ->
|[ null ]|
@@ -55,6 +64,10 @@ to-funcons:
|[ bind (\"(:Id:)\",
allocate-initialised-variable (integers,
0)) ]|
to-funcons:
|[ collect-declared-vars[: fun(:Id:)(){(:Stmt*:)} :] ]| ->
|[ bind (\"(:Id:)\",
function abstraction (execute[: {(:Stmt*:)} :])) ]|
to-funcons:
|[ collect-declared-vars[: :] ]| ->
|[ map () ]|