mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-03 16:22:52 -06:00
cleaned up Calculator example
This commit is contained in:
@@ -6,7 +6,7 @@ abstract Calculator = {
|
|||||||
|
|
||||||
fun
|
fun
|
||||||
PEmpty : Prog ;
|
PEmpty : Prog ;
|
||||||
PDecl : Exp -> (Var -> Prog) -> Prog ;
|
PInit : Exp -> (Var -> Prog) -> Prog ;
|
||||||
PAss : Var -> Exp -> Prog -> Prog ;
|
PAss : Var -> Exp -> Prog -> Prog ;
|
||||||
|
|
||||||
EPlus, EMinus, ETimes, EDiv : Exp -> Exp -> Exp ;
|
EPlus, EMinus, ETimes, EDiv : Exp -> Exp -> Exp ;
|
||||||
@@ -18,8 +18,8 @@ abstract Calculator = {
|
|||||||
|
|
||||||
def
|
def
|
||||||
ex1 =
|
ex1 =
|
||||||
PDecl (EPlus (EInt 2) (EInt 3)) (\x ->
|
PInit (EPlus (EInt 2) (EInt 3)) (\x ->
|
||||||
PDecl (EPlus (EVar x) (EInt 1)) (\y ->
|
PInit (EPlus (EVar x) (EInt 1)) (\y ->
|
||||||
PAss x (EPlus (EVar x) (ETimes (EInt 9) (EVar y))) PEmpty)) ;
|
PAss x (EPlus (EVar x) (ETimes (EInt 9) (EVar y))) PEmpty)) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ concrete CalculatorC of Calculator = open Prelude, Formal in {
|
|||||||
|
|
||||||
lin
|
lin
|
||||||
PEmpty = ss [] ;
|
PEmpty = ss [] ;
|
||||||
PDecl exp prog = ss ("int" ++ prog.$0 ++ "=" ++ top exp ++ ";" ++ prog.s) ;
|
PInit exp prog = ss ("int" ++ prog.$0 ++ "=" ++ top exp ++ ";" ++ prog.s) ;
|
||||||
PAss vr exp prog = ss (vr.s ++ "=" ++ top exp ++ ";" ++ prog.s) ;
|
PAss vr exp prog = ss (vr.s ++ "=" ++ top exp ++ ";" ++ prog.s) ;
|
||||||
|
|
||||||
EPlus = infixl 0 "+" ;
|
EPlus = infixl 0 "+" ;
|
||||||
|
|||||||
27
examples/tutorial/calculator/CalculatorE.gf
Normal file
27
examples/tutorial/calculator/CalculatorE.gf
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
--# -path=.:prelude
|
||||||
|
|
||||||
|
concrete CalculatorE of Calculator = open Prelude in {
|
||||||
|
|
||||||
|
flags lexer=codevar ; unlexer=unwords ;
|
||||||
|
|
||||||
|
lincat
|
||||||
|
Prog, Exp, Var = SS ;
|
||||||
|
|
||||||
|
lin
|
||||||
|
PEmpty = ss [] ;
|
||||||
|
PInit exp prog = ss ("initialize" ++ prog.$0 ++ "as" ++ exp.s ++ PAUSE ++ prog.s) ;
|
||||||
|
PAss vr exp prog = ss ("redefine" ++ vr.s ++ "as" ++ exp.s ++ PAUSE ++ prog.s) ;
|
||||||
|
|
||||||
|
EPlus = infix "plus" ;
|
||||||
|
EMinus = infix "minus" ;
|
||||||
|
ETimes = infix "times" ;
|
||||||
|
EDiv = infix ["divided by"] ;
|
||||||
|
|
||||||
|
EInt i = i ;
|
||||||
|
EVar x = x ;
|
||||||
|
|
||||||
|
oper
|
||||||
|
infix : Str -> SS -> SS -> SS = \op,x,y ->
|
||||||
|
ss (x.s ++ op ++ y.s ++ PAUSE) ;
|
||||||
|
PAUSE = "PAUSE" ;
|
||||||
|
}
|
||||||
@@ -9,13 +9,13 @@ concrete CalculatorJ of Calculator = open Prelude in {
|
|||||||
|
|
||||||
lin
|
lin
|
||||||
PEmpty = ss [] ;
|
PEmpty = ss [] ;
|
||||||
PDecl exp prog = ss (exp.s ++ ";" ++ prog.s) ;
|
PInit exp prog = ss (exp.s ++ ";" ++ "istore" ++ prog.$0 ++ ";" ++ prog.s) ;
|
||||||
PAss vr exp prog = ss (exp.s ++ ";" ++ "istore" ++ vr.s ++ ";" ++ prog.s) ;
|
PAss vr exp prog = ss (exp.s ++ ";" ++ "istore" ++ vr.s ++ ";" ++ prog.s) ;
|
||||||
|
|
||||||
EPlus = postfix "iadd" ;
|
EPlus = postfix "iadd" ;
|
||||||
EMinus = postfix "isub" ;
|
EMinus = postfix "isub" ;
|
||||||
ETimes = postfix "imul" ;
|
ETimes = postfix "imul" ;
|
||||||
EDiv = postfix "imul" ;
|
EDiv = postfix "idiv" ;
|
||||||
|
|
||||||
EInt = prefixSS "iconst" ;
|
EInt = prefixSS "iconst" ;
|
||||||
EVar = prefixSS "iload" ;
|
EVar = prefixSS "iload" ;
|
||||||
|
|||||||
Reference in New Issue
Block a user