| TBool
deriving (Eq,Ord,Show)
-data Stms =
- Program [Stm]
+data Program =
+ Program [FuncStm]
deriving (Eq,Ord,Show)
data Stm =
| SWhile Exp Stm
| SIf Exp Stm Stm
| SPrint Exp
+ | SReturn Exp
| SNoop
deriving (Eq,Ord,Show)
| ENot Exp
| EReadI
| EReadB
+ | EFunc Ident [Exp]
| BiOpExp Exp Op Exp
| EPost Ident Op
deriving (Eq,Ord,Show)
+data Decl =
+ Decl Type Ident
+ deriving (Eq,Ord,Show)
+
+data Func =
+ Func Type Ident [Decl] [Stm]
+ deriving (Eq,Ord,Show)
+
+data FuncStm =
+ S Stm
+ | F Func
+ deriving (Eq,Ord,Show)
+
data Op =
Lt
| ELt
\begin{tabular}{lll}
{\reserved{bool}} &{\reserved{else}} &{\reserved{false}} \\
{\reserved{if}} &{\reserved{int}} &{\reserved{print}} \\
-{\reserved{readBool}} &{\reserved{readInt}} &{\reserved{true}} \\
-{\reserved{while}} & & \\
+{\reserved{readBool}} &{\reserved{readInt}} &{\reserved{return}} \\
+{\reserved{true}} &{\reserved{while}} & \\
\end{tabular}\\
The symbols used in syntax are the following: \\
{\symb{;}} &{\symb{\{}} &{\symb{\}}} \\
{\symb{{$=$}}} &{\symb{(}} &{\symb{)}} \\
{\symb{{$+$}{$+$}}} &{\symb{{$-$}{$-$}}} &{\symb{{$-$}}} \\
-{\symb{!}} &{\symb{{$<$}}} &{\symb{{$<$}{$=$}}} \\
-{\symb{{$>$}}} &{\symb{{$>$}{$=$}}} &{\symb{{$=$}{$=$}}} \\
-{\symb{!{$=$}}} &{\symb{{$+$}}} &{\symb{*}} \\
-{\symb{/}} & & \\
+{\symb{!}} &{\symb{,}} &{\symb{{$<$}}} \\
+{\symb{{$<$}{$=$}}} &{\symb{{$>$}}} &{\symb{{$>$}{$=$}}} \\
+{\symb{{$=$}{$=$}}} &{\symb{!{$=$}}} &{\symb{{$+$}}} \\
+{\symb{*}} &{\symb{/}} & \\
\end{tabular}\\
\subsection*{Comments}
\end{tabular}\\
\begin{tabular}{lll}
-{\nonterminal{Stms}} & {\arrow} &{\nonterminal{ListStm}} \\
+{\nonterminal{Program}} & {\arrow} &{\nonterminal{ListFuncStm}} \\
\end{tabular}\\
\begin{tabular}{lll}
& {\delimit} &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} {\terminal{else}} {\nonterminal{Stm}} \\
& {\delimit} &{\terminal{if}} {\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} {\nonterminal{Stm}} \\
& {\delimit} &{\terminal{print}} {\nonterminal{Exp}} {\terminal{;}} \\
+ & {\delimit} &{\terminal{return}} {\nonterminal{Exp}} {\terminal{;}} \\
\end{tabular}\\
\begin{tabular}{lll}
& {\delimit} &{\terminal{!}} {\nonterminal{Exp3}} \\
& {\delimit} &{\terminal{readInt}} \\
& {\delimit} &{\terminal{readBool}} \\
+ & {\delimit} &{\nonterminal{Ident}} {\terminal{(}} {\nonterminal{ListExp}} {\terminal{)}} \\
& {\delimit} &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}} \\
\end{tabular}\\
& {\delimit} &{\nonterminal{Stm}} {\nonterminal{ListStm}} \\
\end{tabular}\\
+\begin{tabular}{lll}
+{\nonterminal{ListExp}} & {\arrow} &{\emptyP} \\
+ & {\delimit} &{\nonterminal{Exp}} \\
+ & {\delimit} &{\nonterminal{Exp}} {\terminal{,}} {\nonterminal{ListExp}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{Decl}} & {\arrow} &{\nonterminal{Type}} {\nonterminal{Ident}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{ListDecl}} & {\arrow} &{\emptyP} \\
+ & {\delimit} &{\nonterminal{Decl}} \\
+ & {\delimit} &{\nonterminal{Decl}} {\terminal{,}} {\nonterminal{ListDecl}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{Func}} & {\arrow} &{\nonterminal{Type}} {\nonterminal{Ident}} {\terminal{(}} {\nonterminal{ListDecl}} {\terminal{)}} {\terminal{\{}} {\nonterminal{ListStm}} {\terminal{\}}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{ListFunc}} & {\arrow} &{\emptyP} \\
+ & {\delimit} &{\nonterminal{Func}} {\nonterminal{ListFunc}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{FuncStm}} & {\arrow} &{\nonterminal{Stm}} \\
+ & {\delimit} &{\nonterminal{Func}} \\
+\end{tabular}\\
+
+\begin{tabular}{lll}
+{\nonterminal{ListFuncStm}} & {\arrow} &{\emptyP} \\
+ & {\delimit} &{\nonterminal{FuncStm}} {\nonterminal{ListFuncStm}} \\
+\end{tabular}\\
+
\begin{tabular}{lll}
{\nonterminal{Op0}} & {\arrow} &{\terminal{{$<$}}} \\
& {\delimit} &{\terminal{{$<$}{$=$}}} \\
$u = [\0-\255] -- universal: any character
@rsyms = -- symbols and non-identifier-like reserved words
- \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
+ \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \, | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
:-
"//" [.]* ; -- Toss single line comments
| s > a = treeFind right
| s == a = t
-resWords = b "print" (b "false" (b "else" (b "bool" N N) N) (b "int" (b "if" N N) N)) (b "true" (b "readInt" (b "readBool" N N) N) (b "while" N N))
+resWords = b "print" (b "false" (b "else" (b "bool" N N) N) (b "int" (b "if" N N) N)) (b "return" (b "readInt" (b "readBool" N N) N) (b "while" (b "true" N N) N))
where b s = B s (TS s)
unescapeInitTail :: String -> String
-all: Typechecker Interpreter Compiler
+all: Testsyntax
doc: Docsyntax.dvi
import ErrM
}
-%name pStms Stms
-%name pExp Exp
+%name pProgram Program
-- no lexer declaration
%monad { Err } { thenM } { returnM }
'--' { PT _ (TS "--") }
'-' { PT _ (TS "-") }
'!' { PT _ (TS "!") }
+ ',' { PT _ (TS ",") }
'<' { PT _ (TS "<") }
'<=' { PT _ (TS "<=") }
'>' { PT _ (TS ">") }
'print' { PT _ (TS "print") }
'readBool' { PT _ (TS "readBool") }
'readInt' { PT _ (TS "readInt") }
+ 'return' { PT _ (TS "return") }
'true' { PT _ (TS "true") }
'while' { PT _ (TS "while") }
| 'bool' { TBool }
-Stms :: { Stms }
-Stms : ListStm { Program (reverse $1) }
+Program :: { Program }
+Program : ListFuncStm { Program (reverse $1) }
Stm :: { Stm }
| 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
| 'if' '(' Exp ')' Stm { if_ $3 $5 }
| 'print' Exp ';' { SPrint $2 }
+ | 'return' Exp ';' { SReturn $2 }
Exp :: { Exp }
| '!' Exp3 { ENot $2 }
| 'readInt' { EReadI }
| 'readBool' { EReadB }
+ | Ident '(' ListExp ')' { EFunc $1 $3 }
| '(' Exp ')' { $2 }
| ListStm Stm { flip (:) $1 $2 }
+ListExp :: { [Exp] }
+ListExp : {- empty -} { [] }
+ | Exp { (:[]) $1 }
+ | Exp ',' ListExp { (:) $1 $3 }
+
+
+Decl :: { Decl }
+Decl : Type Ident { Decl $1 $2 }
+
+
+ListDecl :: { [Decl] }
+ListDecl : {- empty -} { [] }
+ | Decl { (:[]) $1 }
+ | Decl ',' ListDecl { (:) $1 $3 }
+
+
+Func :: { Func }
+Func : Type Ident '(' ListDecl ')' '{' ListStm '}' { Func $1 $2 $4 (reverse $7) }
+
+
+ListFunc :: { [Func] }
+ListFunc : {- empty -} { [] }
+ | ListFunc Func { flip (:) $1 $2 }
+
+
+FuncStm :: { FuncStm }
+FuncStm : Stm { S $1 }
+ | Func { F $1 }
+
+
+ListFuncStm :: { [FuncStm] }
+ListFuncStm : {- empty -} { [] }
+ | ListFuncStm FuncStm { flip (:) $1 $2 }
+
+
Op0 :: { Op }
Op0 : '<' { Lt }
| '<=' { ELt }
TBool -> prPrec i 0 (concatD [doc (showString "bool")])
-instance Print Stms where
+instance Print Program where
prt i e = case e of
- Program stms -> prPrec i 0 (concatD [prt 0 stms])
+ Program funcstms -> prPrec i 0 (concatD [prt 0 funcstms])
instance Print Stm where
SWhile exp stm -> prPrec i 0 (concatD [doc (showString "while") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm])
SIf exp stm0 stm -> prPrec i 0 (concatD [doc (showString "if") , doc (showString "(") , prt 0 exp , doc (showString ")") , prt 0 stm0 , doc (showString "else") , prt 0 stm])
SPrint exp -> prPrec i 0 (concatD [doc (showString "print") , prt 0 exp , doc (showString ";")])
+ SReturn exp -> prPrec i 0 (concatD [doc (showString "return") , prt 0 exp , doc (showString ";")])
SNoop -> prPrec i 0 (concatD [])
prtList es = case es of
ENot exp -> prPrec i 3 (concatD [doc (showString "!") , prt 3 exp])
EReadI -> prPrec i 3 (concatD [doc (showString "readInt")])
EReadB -> prPrec i 3 (concatD [doc (showString "readBool")])
+ EFunc id exps -> prPrec i 3 (concatD [prt 0 id , doc (showString "(") , prt 0 exps , doc (showString ")")])
BiOpExp exp0 op exp -> prPrec i 0 (concatD [prt 0 exp0 , prt 0 op , prt 0 exp])
EPost id op -> prPrec i 0 (concatD [prt 0 id , prt 1 op])
+ prtList es = case es of
+ [] -> (concatD [])
+ [x] -> (concatD [prt 0 x])
+ x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
+
+instance Print Decl where
+ prt i e = case e of
+ Decl type' id -> prPrec i 0 (concatD [prt 0 type' , prt 0 id])
+
+ prtList es = case es of
+ [] -> (concatD [])
+ [x] -> (concatD [prt 0 x])
+ x:xs -> (concatD [prt 0 x , doc (showString ",") , prt 0 xs])
+
+instance Print Func where
+ prt i e = case e of
+ Func type' id decls stms -> prPrec i 0 (concatD [prt 0 type' , prt 0 id , doc (showString "(") , prt 0 decls , doc (showString ")") , doc (showString "{") , prt 0 stms , doc (showString "}")])
+
+ prtList es = case es of
+ [] -> (concatD [])
+ x:xs -> (concatD [prt 0 x , prt 0 xs])
+
+instance Print FuncStm where
+ prt i e = case e of
+ S stm -> prPrec i 0 (concatD [prt 0 stm])
+ F func -> prPrec i 0 (concatD [prt 0 func])
+
+ prtList es = case es of
+ [] -> (concatD [])
+ x:xs -> (concatD [prt 0 x , prt 0 xs])
instance Print Op where
prt i e = case e of
TBool -> failure x
-transStms :: Stms -> Result
-transStms x = case x of
- Program stms -> failure x
+transProgram :: Program -> Result
+transProgram x = case x of
+ Program funcstms -> failure x
transStm :: Stm -> Result
SWhile exp stm -> failure x
SIf exp stm0 stm -> failure x
SPrint exp -> failure x
+ SReturn exp -> failure x
SNoop -> failure x
ENot exp -> failure x
EReadI -> failure x
EReadB -> failure x
+ EFunc id exps -> failure x
BiOpExp exp0 op exp -> failure x
EPost id op -> failure x
+transDecl :: Decl -> Result
+transDecl x = case x of
+ Decl type' id -> failure x
+
+
+transFunc :: Func -> Result
+transFunc x = case x of
+ Func type' id decls stms -> failure x
+
+
+transFuncStm :: FuncStm -> Result
+transFuncStm x = case x of
+ S stm -> failure x
+ F func -> failure x
+
+
transOp :: Op -> Result
transOp x = case x of
Lt -> failure x
main :: IO ()
main = do args <- getArgs
case args of
- [] -> hGetContents stdin >>= run 2 pStms
- "-s":fs -> mapM_ (runFile 0 pStms) fs
- fs -> mapM_ (runFile 2 pStms) fs
+ [] -> hGetContents stdin >>= run 2 pProgram
+ "-s":fs -> mapM_ (runFile 0 pProgram) fs
+ fs -> mapM_ (runFile 2 pProgram) fs
--- /dev/null
+int a = fac(3);
+int b = fib(2);
+int c = func(a,b);
+
+if (boolfunc(b,c)) print true;
+
+int fac(int n){
+ n++;
+ int sum = 1;
+ while (n-- > 1) sum = sum * n;
+ return n;
+}
TInt. Type ::= "int" ;
TBool. Type ::= "bool" ;
-Program. Stms ::= [Stm] ;
+Program. Program ::= [FuncStm] ;
SExp. Stm ::= Exp ";" ;
-- SFor. Stm ::= "for" "(" Stm Exp ";" Exp ")" Stm ;
SPrint. Stm ::= "print" Exp ";" ;
+SReturn. Stm ::= "return" Exp ";" ;
+
EAss. Exp ::= Ident "=" Exp;
EReadI. Exp3 ::= "readInt" ;
EReadB. Exp3 ::= "readBool" ;
+EFunc. Exp3 ::= Ident "(" [Exp] ")" ;
+
+
coercions Exp 3 ;
terminator Stm "" ;
+separator Exp "," ;
+
+Decl. Decl ::= Type Ident ;
+
+separator Decl "," ;
+
+Func. Func ::= Type Ident "(" [Decl] ")" "{" [Stm] "}" ;
+
+separator Func "" ;
+
+S. FuncStm ::= Stm ;
+F. FuncStm ::= Func ;
+
+separator FuncStm "" ;
+
+-- E1. Exps ::= Exp ;
+-- E2. Exps ::= Exp "," Exps ;
Lt. Op0 ::= "<" ;
ELt. Op0 ::= "<=" ;
comment "/*" "*/" ;
comment "//" ;
-entrypoints Stms, Exp ;
+entrypoints Program;