From: Michael Andreen Date: Sat, 11 Mar 2006 12:41:35 +0000 (+0000) Subject: parsing works X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=commitdiff_plain;h=d606bc7eda59e30fbb75555b4d4c5da91b7327b9 parsing works --- diff --git a/Abssyntax.hs b/Abssyntax.hs index d9fc344..881c172 100644 --- a/Abssyntax.hs +++ b/Abssyntax.hs @@ -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 diff --git a/Docsyntax.tex b/Docsyntax.tex index d978641..f8ad812 100644 --- a/Docsyntax.tex +++ b/Docsyntax.tex @@ -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{{$<$}{$=$}}} \\ diff --git a/Lexsyntax.x b/Lexsyntax.x index 88f0e2d..db7f77e 100644 --- a/Lexsyntax.x +++ b/Lexsyntax.x @@ -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 diff --git a/Makefile b/Makefile index 24f8a62..e065a1a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: Typechecker Interpreter Compiler +all: Testsyntax doc: Docsyntax.dvi diff --git a/Parsyntax.y b/Parsyntax.y index 6613fa3..d7b5f7d 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -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 } diff --git a/Printsyntax.hs b/Printsyntax.hs index d1b4c49..287fbae 100644 --- a/Printsyntax.hs +++ b/Printsyntax.hs @@ -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 diff --git a/Skelsyntax.hs b/Skelsyntax.hs index 9f3feab..7efec85 100644 --- a/Skelsyntax.hs +++ b/Skelsyntax.hs @@ -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 diff --git a/Testsyntax.hs b/Testsyntax.hs index 45f35b9..c5c57c5 100644 --- a/Testsyntax.hs +++ b/Testsyntax.hs @@ -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 index 0000000..fcebceb --- /dev/null +++ b/examples/func @@ -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; +} diff --git a/syntax.cf b/syntax.cf index 0347ce5..44a6cb4 100644 --- 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;