use aexp from IMP-1 instead of example exp

This commit is contained in:
Peter
2023-11-06 08:19:52 +01:00
parent d6c745207b
commit 70b3e9c722
7 changed files with 146 additions and 105 deletions

View File

@@ -18,7 +18,7 @@ language:
sdf: sdf:
enabled: true enabled: true
pretty-print: IBAF pretty-print: IBAF
sdf2table: java sdf2table: c
placeholder: placeholder:
prefix: "$" prefix: "$"
stratego: stratego:

View File

@@ -7,66 +7,97 @@ imports
// Language "IBAFlang" // Language "IBAFlang"
lexical syntax // Language lexical syntax // Language
LEX-int = ( [0-9] )+
LEX-id = ( [a-z] ) ( [a-z] | [0-9] )* // #1 Arithmetic expressions
LEX-DASH = "-"
LEX-decimal = ( [0-9] )+
LEX-id = ( [A-Z] | [a-z] )+
syntax // Language syntax // Language
// #1 Arithmetic expressions
context-free syntax // Language context-free syntax // Language
L-start.L-start--L-exp = L-start.L-start--L-aexp =
L-exp L-aexp
L-exp.L-exp--L-int =
L-int // #1 Arithmetic expressions
L-exp.L-exp--L-id =
L-aexp.L-aexp--L-num =
L-num
L-aexp.L-aexp--L-id =
L-id L-id
L-exp.L-exp--R-lambda-L-id-DOT-L-exp = L-aexp.L-aexp--L-aexp-PLUS-L-aexp =
"lambda" L-id "." L-exp L-aexp "+" L-aexp
L-exp.L-exp--L-exp-LPAREN-L-exp-RPAREN = L-aexp.L-aexp--L-aexp-SLASH-L-aexp =
L-exp "(" L-exp ")" L-aexp "/" L-aexp
L-exp.L-exp--R-let-L-id-EQUALS-L-exp-R-in-L-exp = L-aexp.L-aexp--LPAREN-L-aexp-RPAREN =
"let" L-id "=" L-exp "in" L-exp "(" L-aexp ")"
L-exp.L-exp--LPAREN-L-exp-RPAREN =
"(" L-exp ")"
L-int.LEX-int = L-decimal.LEX-decimal =
LEX-int LEX-decimal
L-id.LEX-id = L-id.LEX-id =
LEX-id LEX-id
context-free syntax // Semantics context-free syntax // Semantics
FCT.T-start = FCT.T-start =
"start" "[:" L-start ":]" "start" "[:" L-start ":]"
FCT.T-eval =
"eval" "[:" L-exp ":]" // #1 Arithmetic expressions
FCT-Quoted.L-int = L-int
FCT.T-int =
"int" "[:" L-int ":]" FCT.T-eval-arith =
"eval-arith" "[:" L-aexp ":]"
FCT-Quoted.L-decimal = L-decimal
FCT.T-int-val =
"int-val" "[:" L-num ":]"
FCT-Quoted.L-id = L-id FCT-Quoted.L-id = L-id
FCT.T-id = FCT.T-id =
"id" "[:" L-id ":]" "id" "[:" L-id ":]"
context-free syntax // Desugaring context-free syntax // Desugaring
// #1 Arithmetic expressions
variables // Meta-variables variables // Meta-variables
L-start = "(:START" [1-9]? ":)" {prefer} L-start = "(:START" [1-9]? ":)" {prefer}
L-start? = "(:START" [1-9]? "?:)" {prefer} L-start? = "(:START" [1-9]? "?:)" {prefer}
L-start* = "(:START" [1-9]? "*:)" {prefer} L-start* = "(:START" [1-9]? "*:)" {prefer}
L-start+ = "(:START" [1-9]? "+:)" {prefer} L-start+ = "(:START" [1-9]? "+:)" {prefer}
L-exp = "(:E" [1-9]? ":)" {prefer}
L-exp? = "(:E" [1-9]? "?:)" {prefer} // #1 Arithmetic expressions
L-exp* = "(:E" [1-9]? "*:)" {prefer}
L-exp+ = "(:E" [1-9]? "+:)" {prefer}
L-int = "(:N" [1-9]? ":)" {prefer} L-aexp = "(:AExp" [1-9]? ":)" {prefer}
L-int? = "(:N" [1-9]? "?:)" {prefer} L-aexp? = "(:AExp" [1-9]? "?:)" {prefer}
L-int* = "(:N" [1-9]? "*:)" {prefer} L-aexp* = "(:AExp" [1-9]? "*:)" {prefer}
L-int+ = "(:N" [1-9]? "+:)" {prefer} L-aexp+ = "(:AExp" [1-9]? "+:)" {prefer}
L-id = "(:X" [1-9]? ":)" {prefer} L-num = "(:N" [1-9]? ":)" {prefer}
L-id? = "(:X" [1-9]? "?:)" {prefer} L-num? = "(:N" [1-9]? "?:)" {prefer}
L-id* = "(:X" [1-9]? "*:)" {prefer} L-num* = "(:N" [1-9]? "*:)" {prefer}
L-id+ = "(:X" [1-9]? "+:)" {prefer} L-num+ = "(:N" [1-9]? "+:)" {prefer}
L-decimal = "(:D" [1-9]? ":)" {prefer}
L-decimal? = "(:D" [1-9]? "?:)" {prefer}
L-decimal* = "(:D" [1-9]? "*:)" {prefer}
L-decimal+ = "(:D" [1-9]? "+:)" {prefer}
L-id = "(:I" [1-9]? ":)" {prefer}
L-id? = "(:I" [1-9]? "?:)" {prefer}
L-id* = "(:I" [1-9]? "*:)" {prefer}
L-id+ = "(:I" [1-9]? "+:)" {prefer}
// SDF comments // SDF comments
// #1 Arithmetic expressions
sorts // ASTs sorts // ASTs
T-start T-start

File diff suppressed because one or more lines are too long

View File

@@ -13,41 +13,42 @@ imports
rules rules
to-funcons: to-funcons:
|[ start[: (:E:) :] ]| -> |[ start[: (:AExp:) :] ]| ->
|[ initialise-binding finalise-failing eval[: (:E:) :] ]| |[ initialise-binding finalise-failing eval-arith[: (:AExp:) :] ]|
// #1 Arithmetic expressions
to-funcons: to-funcons:
|[ eval[: (:N:) :] ]| -> |[ eval-arith[: (:N:) :] ]| ->
|[ int[: (:N:) :] ]| |[ int-val[: (:N:) :] ]|
to-funcons: to-funcons:
|[ eval[: (:X:) :] ]| -> |[ eval-arith[: (:I:) :] ]| ->
|[ bound id[: (:X:) :] ]| |[ assigned (bound (id[: (:I:) :])) ]|
to-funcons: to-funcons:
|[ eval[: lambda(:X:).(:E:) :] ]| -> |[ eval-arith[: (:AExp1:)+(:AExp2:) :] ]| ->
|[ function closure scope (bind (id[: (:X:) :], |[ integer-add (eval-arith[: (:AExp1:) :],
given), eval-arith[: (:AExp2:) :]) ]|
eval[: (:E:) :]) ]|
to-funcons: to-funcons:
|[ eval[: (:E1:)((:E2:)) :] ]| -> |[ eval-arith[: (:AExp1:)/(:AExp2:) :] ]| ->
|[ apply (eval[: (:E1:) :], |[ checked integer-divide (eval-arith[: (:AExp1:) :],
eval[: (:E2:) :]) ]| eval-arith[: (:AExp2:) :]) ]|
to-funcons: to-funcons:
|[ eval[: let(:X:)=(:E1:)in(:E2:) :] ]| -> |[ eval-arith[: ((:AExp:)) :] ]| ->
|[ scope (bind (id[: (:X:) :], |[ eval-arith[: (:AExp:) :] ]|
eval[: (:E1:) :]),
eval[: (:E2:) :]) ]|
to-funcons:
|[ eval[: ((:E:)) :] ]| ->
|[ eval[: (:E:) :] ]|
to-funcons-lex: to-funcons-lex:
FCTDoubleQuoted(L-int(LEX-int(str))) -> FCTDoubleQuoted(L-decimal(LEX-decimal(str))) ->
FCTString(<double-quote> str) FCTString(<double-quote> str)
to-funcons: to-funcons:
|[ int[: (:N:) :] ]| -> |[ int-val[: (:D:) :] ]| ->
|[ decimal \"(:N:)\" ]| |[ decimal-natural (\"(:D:)\") ]|
to-funcons:
|[ int-val[: -(:D:) :] ]| ->
|[ integer-negate (int-val[: (:D:) :]) ]|
to-funcons-lex: to-funcons-lex:
FCTDoubleQuoted(L-id(LEX-id(str))) -> FCTDoubleQuoted(L-id(LEX-id(str))) ->
FCTString(<double-quote> str) FCTString(<double-quote> str)
to-funcons: to-funcons:
|[ id[: (:X:) :] ]| -> |[ id[: (:I:) :] ]| ->
|[ \"(:X:)\" ]| |[ \"(:I:)\" ]|

View File

@@ -1,5 +1,8 @@
module ibaf module ibaf
imports
generate
imports imports
libstratego-lib libstratego-lib
@@ -16,6 +19,3 @@ rules // Debugging
with with
filename := <guarantee-extension(|"aterm")> path filename := <guarantee-extension(|"aterm")> path
; result := node ; result := node
imports
generate

View File

@@ -1,47 +1,55 @@
Language "IBAFlang" Language "IBAFlang"
Syntax START:start ::= exp Syntax START:start ::= aexp
Semantics start[[ _:start ]] : =>values Semantics start[[ _:start ]] : =>values
Rule start[[ E ]] = Rule start[[ AExp ]] =
initialise-binding finalise-failing eval[[ E ]] initialise-binding
finalise-failing
Syntax E:exp ::= int eval-arith[[ AExp ]]
| id
| 'lambda' id '.' exp
| exp '(' exp ')' #1 Arithmetic expressions
| 'let' id '=' exp 'in' exp
| '(' exp ')'
Syntax
Semantics eval[[ _:exp ]] : => values AExp : aexp ::= num
| id
Rule eval[[ N ]] = int[[ N ]] | aexp '+' aexp
| aexp '/' aexp
Rule eval[[ X ]] = bound id[[ X ]] | '(' aexp ')'
Rule eval[[ 'lambda' X '.' E ]] = Semantics
function closure eval-arith[[ _:aexp ]] : =>integers
scope( bind( id[[ X ]], given ), Rule
eval[[ E ]] ) eval-arith[[ N ]] = int-val[[ N ]]
Rule
Rule eval[[ E1 '(' E2 ')' ]] = eval-arith[[ I ]] = assigned(bound(id[[ I ]]))
apply( eval[[ E1 ]], eval[[ E2 ]] ) Rule
eval-arith[[ AExp1 '+' AExp2 ]] =
Rule eval[[ 'let' X '=' E1 'in' E2 ]] = integer-add(eval-arith[[ AExp1 ]], eval-arith[[ AExp2 ]])
scope( bind( id[[ X ]], eval[[ E1 ]] ), Rule
eval[[ E2 ]] ) eval-arith[[ AExp1 '/' AExp2 ]] =
checked integer-divide(eval-arith[[ AExp1 ]], eval-arith[[ AExp2 ]])
Rule eval[[ '(' E ')' ]] = eval[[ E ]] Rule
eval-arith[[ '(' AExp ')' ]] = eval-arith[[ AExp ]]
Lexis N:int ::= ('0'-'9')+ Syntax
N : num ::= '-'?_decimal
Semantics int[[ N:int ]] : => integers Lexis
= decimal \"N\" D : decimal ::= ('0'-'9')+
Semantics
int-val[[ _:num ]] : integers
Lexis X:id ::= ('a'-'z') ('a'-'z'|'0'-'9')* Rule
int-val[[ D ]] = decimal-natural(\"D\")
Semantics id[[ X:id ]] : => identifiers Rule
= \"X\" int-val[[ '-' D ]] = integer-negate(int-val[[ D ]])
Lexis
I : id ::= ('A'-'Z'|'a'-'z')+
Semantics
id[[ _:id ]] : ids
Rule
id[[ I ]] = \"I\"