From 29d164f53e029e8133e05b599241eb6c14d7fe47 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 11 May 2016 21:24:55 +0200 Subject: [PATCH] nieuwe presentatie jow --- deliverables/p3/.chktexrc | 1 + deliverables/p3/Makefile | 18 +++++ deliverables/p3/clean.sty | 70 ++++++++++++++++++++ deliverables/p3/p3.tex | 91 ++++++++++++++++++++++++++ deliverables/p3/pre.tex | 24 +++++++ examples/Markus/multiple_recursion.spl | 4 ++ gen.dcl | 3 +- gen.icl | 6 +- parse.icl | 7 +- sem.icl | 2 +- spl.icl | 18 +++-- 11 files changed, 229 insertions(+), 15 deletions(-) create mode 100644 deliverables/p3/.chktexrc create mode 100644 deliverables/p3/Makefile create mode 100644 deliverables/p3/clean.sty create mode 100644 deliverables/p3/p3.tex create mode 100644 deliverables/p3/pre.tex diff --git a/deliverables/p3/.chktexrc b/deliverables/p3/.chktexrc new file mode 100644 index 0000000..d11a15c --- /dev/null +++ b/deliverables/p3/.chktexrc @@ -0,0 +1 @@ +VerbEnvir { CleanCode CI } diff --git a/deliverables/p3/Makefile b/deliverables/p3/Makefile new file mode 100644 index 0000000..85af192 --- /dev/null +++ b/deliverables/p3/Makefile @@ -0,0 +1,18 @@ +LATEX:=pdflatex +LATEXFLAGS:=-shell-escape +DOCUMENT:=p3 + +.PHONY: all clean +.SECONDARY: $(DOCUMENT).fmt + +all: $(DOCUMENT).pdf + +%.pdf: %.tex %.fmt + $(LATEX) $(LATEXFLAGS) $(basename $@) + $(LATEX) $(LATEXFLAGS) $(basename $@) + +%.fmt: pre.tex + $(LATEX) $(LATEXFLAGS) -ini -jobname="$(basename $@)" "&$(LATEX) $<\dump" + +clean: + $(RM) -v $(addprefix $(DOCUMENT).,aux fmt log nav out snm toc vrb pdf) diff --git a/deliverables/p3/clean.sty b/deliverables/p3/clean.sty new file mode 100644 index 0000000..046c239 --- /dev/null +++ b/deliverables/p3/clean.sty @@ -0,0 +1,70 @@ +\usepackage{listings} + +\lstdefinelanguage{Clean}{% + alsoletter={ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_`1234567890}, + alsoletter={~!@\#$\%^\&*-+=?<>:|\\.}, + morekeywords={generic,implementation,definition,dynamic,module,import,from,where,in,of,case,let,infix,infixr,infixl,class,instance,with,if,derive}, + sensitive=true, + morecomment=[l]{//}, + morecomment=[n]{/*}{*/}, + morestring=[b]", + morestring=[b]', + emptylines=1, + basicstyle=\small, + identifierstyle=\small\ttfamily, + commentstyle=\itshape, + keywordstyle=\bfseries, + stringstyle=\ttfamily, + numbers=none, + showstringspaces=false, + basewidth=0.45em, + columns=[c]fixed, + keepspaces=true, + breaklines=false, + tabsize=4, + texcl=true, + escapeinside={(\#}{\#)}, + literate=% + % Basic Clean constructs + {\\}{{$\lambda\:$}}1 + {A.}{{$\forall\;\,$}}1 + {E.}{{$\exists\;\,$}}1 + {>}{{$>$}}1 + {<}{{$<$}}1 + {<=}{{$\leq$}}1 + {>=}{{$\geq$}}1 + {<>}{{$\neq$}}1 + {->}{{$\rightarrow$}}2 + {<-}{{$\leftarrow$}}1 + {=}{{$=$}}1 + {~}{{$\sim$}}1 + {\#}{{$\sharp$}}1 + {\{|}{{$\{\!|\!$}}1 + {|\}}{{$\!|\!\}$}}1 + {:=}{{$:=$}}2 + {==}{{$==$}}2 + {++}{{$+\!\!+$}}2 + {+++}{{$+\!\!\!\!+\!\!\!\!+$}}2 + {:==}{{$:==$}}3 + {\{|*|\}}{{$\{\!|\!\!\star\!\!|\!\}$}}3 + % + % Basic iTask constructs + {>||>}{{$\triangleright\triangleright$}}2 + {>>=}{{\texttt{>>=}}}3 + {>>|}{{\texttt{>>|}}}3 + {?>>}{{\texttt{?>>}}}3 + {!>>}{{\texttt{!>>}}}3 + {-||-}{{\texttt{-||-}}}4 + {.||.}{{\texttt{.||.}}}4 + {.&&.}{{\texttt{.\&\&.}}}4 +} + +\newcommand{\CleanInline}[1]{\lstinline[language=Clean]¦#1¦} +\newcommand{\CI}[1]{\CleanInline{#1}} + +\lstdefinestyle{numbers}{numbers=left, stepnumber=1, numberstyle=\tiny, numbersep=5pt} + +\lstnewenvironment{CleanCode}{\lstset{language=Clean,identifierstyle=\ttfamily}}{} +\lstnewenvironment{CleanCodeN}{\lstset{language=Clean,style=numbers}}{} +\lstnewenvironment{CleanCodeB}{\lstset{language=Clean,frame=single}}{} +\lstnewenvironment{CleanCodeNB}{\lstset{language=Clean,style=numbers,frame=single}}{} diff --git a/deliverables/p3/p3.tex b/deliverables/p3/p3.tex new file mode 100644 index 0000000..ece80bb --- /dev/null +++ b/deliverables/p3/p3.tex @@ -0,0 +1,91 @@ +%&p3 +\begin{document} +\frame{\titlepage} + +\begin{frame} + \frametitle{\textsc{SPLC}} + \begin{block}{Features} + \begin{itemize} + \item Implementation language: + Clean ({\tiny\url{http://clean.cs.ru.nl}}) + \item Type checking and type inference +% \item No multiple recursion\ldots\pause\ yet\ldots\pause% + \item Higher order functions + \end{itemize} + \end{block} +\end{frame} + +\begin{frame} + \frametitle{Progress since previous presentation} + \begin{itemize}[<+->] + \item Completely redid typechecking and inference + \item Extended semantic analysis with + \begin{itemize}[<+->] + \item Complain when unknown functions are used + \item Complain when there is no main + \item Complain when \CI{Void} is used as a non terminal type + \item Complain when duplicate functions are used + \item Complain when the main is not of the correct form + \end{itemize} + \item Higher order functions + \item Code generation for expressions + \item A lot needs to be done\ldots + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Type checking} + \begin{block}{Two pass} + \begin{itemize} + \item Constraint collecting + \item Constraint satisfaction + \end{itemize} + \end{block} + \pause% + \begin{block}{\CI{RWST}} + \begin{CleanCode} +:: Scheme = Forall [TVar] Type +:: Gamma :== Map String Scheme +:: Typing a :== StateT (Gamma, [TVar]) (Either SemError) a +:: Substitution :== `Map'.Map TVar Type +:: Constraints :== [(Type, Type)] +:: SemError + \end{CleanCode} + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Code generation} + \begin{block}{} + \begin{CleanCode} +:: SSMProgram :== [Instr] +:: Instr = Instr String [Arg] String + | Lab Label +:: Label :== String +:: Arg = L Label | Lit Int | Raw String +:: GenError = Error String +:: GenMap :== Map String LoadPlace +:: LoadPlace = LDA Int | LDC Int | LDH Int | LDL Int + | LDR Int | LDS Int + | FUNC Label + \end{CleanCode} + \end{block} + \pause% + \begin{block}{\CI{RWST} we meet again} + \begin{CleanCode} +:: Gen a :== RWST () SSMProgram (GenMap, [Label]) (Either GenError) a + \end{CleanCode} + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Future} + \begin{block}{Higher order functions} + Read up on how to implement higher order functions + \end{block} + \pause% + \begin{block}{Hand in hand with code generation} + To get a fully working compiler and report before the deadline + \end{block} +\end{frame} +\end{document} diff --git a/deliverables/p3/pre.tex b/deliverables/p3/pre.tex new file mode 100644 index 0000000..f9b0161 --- /dev/null +++ b/deliverables/p3/pre.tex @@ -0,0 +1,24 @@ +\documentclass{beamer} + +\usepackage{xcolor} +\usepackage{listings} +\usepackage{clean} + +\title[cc1516: Semantic analysis]{SPLC} +\subtitle{\texttt{~::= `,' `and' }} +\author[P. Jager, M. Lubbers]{Pim Jager\inst{1}\and Mart Lubbers\inst{1}} +\institute[Radboud University]{% + \inst{1}% + Computer Science: Software Science\\ + Radboud University +} +\subject{Code generation} +\date{\today} + +\lstset{% + basicstyle=\ttfamily\footnotesize, + breaklines +} + +\usetheme{Warsaw} +\usecolortheme{beaver} diff --git a/examples/Markus/multiple_recursion.spl b/examples/Markus/multiple_recursion.spl index bfe6701..1810a03 100644 --- a/examples/Markus/multiple_recursion.spl +++ b/examples/Markus/multiple_recursion.spl @@ -8,3 +8,7 @@ flip(n, l) :: Int -> [Int] -> [Int] { flop(n, l) { return flip(n, 1:l); } + +main() { + return 0; +} diff --git a/gen.dcl b/gen.dcl index 0ed553c..8c6f3a3 100644 --- a/gen.dcl +++ b/gen.dcl @@ -1,5 +1,6 @@ definition module gen import AST +import Data.Either -gen :: AST -> String \ No newline at end of file +gen :: AST -> Either String String diff --git a/gen.icl b/gen.icl index 269ef14..819da19 100644 --- a/gen.icl +++ b/gen.icl @@ -45,10 +45,10 @@ FALSE :== 0 labelStream :: [Label] labelStream = ["lbl_" +++ toString i\\i<-[1..]] -gen :: AST -> String +gen :: AST -> Either String String gen (AST fds) = case evalRWST prog "" ('Map'.newMap, labelStream) of - Left (Error e) = e - Right (_, p) = toString p + Left (Error e) = Left e + Right (_, p) = Right $ toString p where prog = tell [Instr "bra" [L "main"] ""] >>| mapM_ g fds //gen _ = prog diff --git a/parse.icl b/parse.icl index 766f1ce..004c1f4 100644 --- a/parse.icl +++ b/parse.icl @@ -75,9 +75,10 @@ parseFunType :: Parser Token Type parseFunType = satTok DoubleColonToken *> parseFT where parseFT :: Parser Token Type - parseFT = (liftM2 (->>) - ((parseBBraces parseFT <|> parseType) <* satTok ArrowToken) - parseFT) <|> parseType + parseFT = (liftM2 (->>) (parseSF <* satTok ArrowToken) (parseFT)) <|> + parseSF + parseSF :: Parser Token Type + parseSF = parseBBraces parseFT <|> parseType parseVarDecl :: Parser Token VarDecl parseVarDecl = liftM4 VarDecl diff --git a/sem.icl b/sem.icl index 68b804d..2341eef 100644 --- a/sem.icl +++ b/sem.icl @@ -26,7 +26,6 @@ from Text import class Text(concat), instance Text String import AST - :: Scheme = Forall [TVar] Type :: Gamma :== 'Map'.Map String Scheme //map from Variables! to types :: Typing a :== StateT (Gamma, [TVar]) (Either SemError) a @@ -173,6 +172,7 @@ generalize :: Type -> Typing Scheme generalize t = gamma >>= \g-> pure $ Forall (difference (ftv t) (ftv g)) t lookup :: String -> Typing Type +lookup "isEmpty" = ListType <$> fresh lookup k = gamma >>= \g-> case 'Map'.member k g of False = liftT (Left $ UndeclaredVariableError zero k) True = instantiate $ 'Map'.find k g diff --git a/spl.icl b/spl.icl index e88e943..b5738ee 100644 --- a/spl.icl +++ b/spl.icl @@ -54,7 +54,8 @@ Start w <<< " --version Show the version\n" <<< " --[no-]lex Lexer output(default: disabled)\n" <<< " --[no-]parse Parser output(default: disabled)\n" - <<< " --[no-]sem Semantic analysis output(default: enabled)\n" + <<< " --[no-]sem Semantic analysis output(default: disabled)\n" + <<< " --[no-]code Code generation output(default: enabled)\n" = snd $ fclose stdin w # (contents, stdin, w) = readFileOrStdin stdin args.fp w = case contents of @@ -74,9 +75,12 @@ Start w (Right ast) # stdin = if (not args.sem) stdin (stdin <<< "//SEM G\n" <<< toString ast <<< "//SEMA\n") - # stdin = if (not args.gen) stdin (stdin - <<< "//CODE GEN\n" <<< gen ast <<< "\n//CODE GEN\n") - = snd $ fclose (stdin <<< "\n") w + = case gen ast of + (Left e) = snd $ fclose (stdin <<< e) w + (Right asm) + # stdin = if (not args.gen) stdin (stdin + <<< ";CODE GEN\n" <<< asm <<< "\n;CODE GEN\n") + = snd $ fclose (stdin <<< "\n") w where printConstraints :: Constraints -> String printConstraints [] = "" @@ -97,7 +101,7 @@ parseArgs w version=False, lex=False, parse=False, - sem=True, + sem=False, gen=True, fp=Nothing, help=False}, w) @@ -112,8 +116,8 @@ where pa ["--no-parse":r] o = pa r {o & parse=False} pa ["--sem":r] o = pa r {o & sem=True} pa ["--no-sem":r] o = pa r {o & sem=False} - pa ["--gen":r] o = pa r {o & gen=True} - pa ["--no-gen":r] o = pa r {o & gen=False} + pa ["--code":r] o = pa r {o & gen=True} + pa ["--no-code":r] o = pa r {o & gen=False} pa [x:r] o = pa r {o & fp=Just x} readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World) -- 2.20.1