]> ruin.nu Git - proglang.git/commitdiff
parsing works
authorMichael Andreen <harv@ruin.nu>
Sat, 11 Mar 2006 12:41:35 +0000 (12:41 +0000)
committerMichael Andreen <harv@ruin.nu>
Sat, 11 Mar 2006 12:41:35 +0000 (12:41 +0000)
Abssyntax.hs
Docsyntax.tex
Lexsyntax.x
Makefile
Parsyntax.y
Printsyntax.hs
Skelsyntax.hs
Testsyntax.hs
examples/func [new file with mode: 0644]
syntax.cf

index d9fc3445b15970a66b0ac32ce0d1ca796c5648a8..881c1723bcc4fa5021eb17ef6d9503edfc6ffd3c 100644 (file)
@@ -9,8 +9,8 @@ data Type =
  | TBool
   deriving (Eq,Ord,Show)
 
-data Stms =
-   Program [Stm]
+data Program =
+   Program [FuncStm]
   deriving (Eq,Ord,Show)
 
 data Stm =
@@ -21,6 +21,7 @@ data Stm =
  | SWhile Exp Stm
  | SIf Exp Stm Stm
  | SPrint Exp
+ | SReturn Exp
  | SNoop
   deriving (Eq,Ord,Show)
 
@@ -33,10 +34,24 @@ data Exp =
  | 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
index d978641a5ecfd0855d40eb5228723db4ecda8e1a..f8ad812f68132a4a546510dfa5330591f4c81986 100644 (file)
@@ -42,8 +42,8 @@ The reserved words used in syntax are the following: \\
 \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: \\
@@ -52,10 +52,10 @@ 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}
@@ -78,7 +78,7 @@ All other symbols are terminals.\\
 \end{tabular}\\
 
 \begin{tabular}{lll}
-{\nonterminal{Stms}} & {\arrow}  &{\nonterminal{ListStm}}  \\
+{\nonterminal{Program}} & {\arrow}  &{\nonterminal{ListFuncStm}}  \\
 \end{tabular}\\
 
 \begin{tabular}{lll}
@@ -90,6 +90,7 @@ All other symbols are terminals.\\
  & {\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}
@@ -118,6 +119,7 @@ All other symbols are terminals.\\
  & {\delimit}  &{\terminal{!}} {\nonterminal{Exp3}}  \\
  & {\delimit}  &{\terminal{readInt}}  \\
  & {\delimit}  &{\terminal{readBool}}  \\
+ & {\delimit}  &{\nonterminal{Ident}} {\terminal{(}} {\nonterminal{ListExp}} {\terminal{)}}  \\
  & {\delimit}  &{\terminal{(}} {\nonterminal{Exp}} {\terminal{)}}  \\
 \end{tabular}\\
 
@@ -126,6 +128,41 @@ All other symbols are terminals.\\
  & {\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{{$<$}{$=$}}}  \\
index 88f0e2d714f9a304daae1337c4ff2897ccece5a8..db7f77e8d80c8e2cae337ee32e606375b8c6b1ac 100644 (file)
@@ -16,7 +16,7 @@ $i = [$l $d _ ']          -- identifier character
 $u = [\0-\255]          -- universal: any character
 
 @rsyms =    -- symbols and non-identifier-like reserved words
-   \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
+   \; | \{ | \} | \= | \( | \) | \+ \+ | \- \- | \- | \! | \, | \< | \< \= | \> | \> \= | \= \= | \! \= | \+ | \* | \/
 
 :-
 "//" [.]* ; -- Toss single line comments
@@ -79,7 +79,7 @@ eitherResIdent tv s = treeFind resWords
                               | 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
index 24f8a623b51038738dda35bb8bebd26fcb382b52..e065a1ac77d78adfbdfd71a0bcabd710d5c8e346 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: Typechecker Interpreter Compiler
+all: Testsyntax
 
 doc: Docsyntax.dvi
 
index 6613fa351325648a0f3cd8bff19ae301e6fbc25b..d7b5f7deeee2f694a080b55ae195af1ff5c40e40 100644 (file)
@@ -6,8 +6,7 @@ import Lexsyntax
 import ErrM
 }
 
-%name pStms Stms
-%name pExp Exp
+%name pProgram Program
 
 -- no lexer declaration
 %monad { Err } { thenM } { returnM }
@@ -24,6 +23,7 @@ import ErrM
  '--' { PT _ (TS "--") }
  '-' { PT _ (TS "-") }
  '!' { PT _ (TS "!") }
+ ',' { PT _ (TS ",") }
  '<' { PT _ (TS "<") }
  '<=' { PT _ (TS "<=") }
  '>' { PT _ (TS ">") }
@@ -41,6 +41,7 @@ import ErrM
  'print' { PT _ (TS "print") }
  'readBool' { PT _ (TS "readBool") }
  'readInt' { PT _ (TS "readInt") }
+ 'return' { PT _ (TS "return") }
  'true' { PT _ (TS "true") }
  'while' { PT _ (TS "while") }
 
@@ -64,8 +65,8 @@ Type : 'int' { TInt }
   | 'bool' { TBool }
 
 
-Stms :: { Stms }
-Stms : ListStm { Program (reverse $1) } 
+Program :: { Program }
+Program : ListFuncStm { Program (reverse $1) } 
 
 
 Stm :: { Stm }
@@ -77,6 +78,7 @@ Stm : Exp ';' { SExp $1 }
   | '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 }
@@ -105,6 +107,7 @@ Exp3 : Ident '++' { postIncr_ $1 }
   | '!' Exp3 { ENot $2 }
   | 'readInt' { EReadI }
   | 'readBool' { EReadB }
+  | Ident '(' ListExp ')' { EFunc $1 $3 }
   | '(' Exp ')' { $2 }
 
 
@@ -113,6 +116,41 @@ ListStm : {- empty -} { [] }
   | 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 }
index d1b4c4962d5b0a37e4d40bfec23c249040213763..287fbaefe878444e73a106d2d33887fed4c3856a 100644 (file)
@@ -93,9 +93,9 @@ instance Print Type where
    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
@@ -107,6 +107,7 @@ 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
@@ -123,9 +124,40 @@ instance Print Exp where
    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
index 9f3feab046e4624d3c594fad502219d14a04f1a6..7efec8512f8ca8ea0c89e36a68268106dfd05fe4 100644 (file)
@@ -26,9 +26,9 @@ transType x = case x 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
@@ -40,6 +40,7 @@ transStm x = case x of
   SWhile exp stm  -> failure x
   SIf exp stm0 stm  -> failure x
   SPrint exp  -> failure x
+  SReturn exp  -> failure x
   SNoop  -> failure x
 
 
@@ -53,10 +54,27 @@ transExp x = case x of
   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
index 45f35b972d017946d22cb22f57d36d3a3b3fb690..c5c57c5d975e21feabe8def16433630fe1d11c5a 100644 (file)
@@ -48,9 +48,9 @@ showTree v tree
 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
 
 
 
diff --git a/examples/func b/examples/func
new file mode 100644 (file)
index 0000000..fcebceb
--- /dev/null
@@ -0,0 +1,12 @@
+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;
+}
index 0347ce56e2982362825fb40f7ec2582f778e9690..44a6cb4284db6eb0d08e95ec53423cccab7f8eb8 100644 (file)
--- a/syntax.cf
+++ b/syntax.cf
@@ -8,7 +8,7 @@ False.    Bool ::= "false" ;
 TInt.   Type  ::= "int" ;
 TBool.  Type  ::= "bool" ;
 
-Program.  Stms ::= [Stm] ;
+Program.  Program ::= [FuncStm] ;
 
 
 SExp.      Stm      ::= Exp ";" ;
@@ -25,6 +25,8 @@ define if e s = SIf e s SNoop ;
 -- SFor.     Stm      ::= "for" "(" Stm Exp ";" Exp ")" Stm ;
 SPrint.   Stm      ::= "print" Exp ";" ;
 
+SReturn. Stm ::= "return" Exp ";" ;
+
 
 
 EAss.     Exp     ::= Ident "=" Exp;
@@ -54,6 +56,9 @@ ENot.     Exp3    ::= "!" Exp3 ;
 EReadI.   Exp3    ::= "readInt" ;
 EReadB.   Exp3    ::= "readBool" ;
 
+EFunc.    Exp3    ::= Ident "(" [Exp] ")" ;
+
+
 
 coercions Exp 3 ;
 
@@ -63,6 +68,23 @@ 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 ::= "<=" ;
@@ -94,4 +116,4 @@ internal SNoop. Stm ::= ;
 comment "/*" "*/" ;
 comment "//" ;
 
-entrypoints Stms, Exp ;
+entrypoints Program;