From bd80caf4405f3e9dfc67a650d2a126f9ed0c33dd Mon Sep 17 00:00:00 2001 From: Dmitry Boulytchev Date: Mon, 17 Feb 2020 01:51:52 +0300 Subject: [PATCH] Continue spec; --- doc/spec/.gitignore | 1 + doc/spec/03.01.lexical_structure.tex | 8 ++- doc/spec/03.03.scope_expressions.tex | 2 +- doc/spec/03.concrete_syntax.tex | 3 +- doc/spec/04.extensions.tex | 89 ++++++++++++++++++++++++++++ doc/spec/05.driver_options.tex | 6 +- doc/spec/06.standard_library.tex | 2 +- doc/spec/Makefile | 5 +- doc/spec/spec.tex | 84 +++++++++++++++++++++++--- src/Driver.ml | 2 +- 10 files changed, 181 insertions(+), 21 deletions(-) create mode 100644 doc/spec/.gitignore diff --git a/doc/spec/.gitignore b/doc/spec/.gitignore new file mode 100644 index 000000000..c78b14e25 --- /dev/null +++ b/doc/spec/.gitignore @@ -0,0 +1 @@ +commitdate.tex diff --git a/doc/spec/03.01.lexical_structure.tex b/doc/spec/03.01.lexical_structure.tex index 91ee9e6a7..550e2fe70 100644 --- a/doc/spec/03.01.lexical_structure.tex +++ b/doc/spec/03.01.lexical_structure.tex @@ -14,6 +14,7 @@ The following characters are treated as whitespaces: \begin{itemize} \item blank character "\texttt{ }"; \item newline character "\texttt{\textbackslash n}"; +\item carriage return character "\texttt{\textbackslash r}"; \item tabulation character "\texttt{\textbackslash t}". \end{itemize} @@ -76,9 +77,10 @@ The following identifiers are reserved for keywords: \begin{lstlisting} after array at before boxed case do elif else - esac false fi for fun if import infix infixl - infixr length local od of public repeat return sexp - skip string string then true unboxed until when while + esac eta false fi for fun if import infix + infixl infixr lazy length local od of public repeat + return sexp skip string string then true unboxed until + when while \end{lstlisting} \subsection{Infix Operators} diff --git a/doc/spec/03.03.scope_expressions.tex b/doc/spec/03.03.scope_expressions.tex index b9bf10f5e..e898d9b12 100644 --- a/doc/spec/03.03.scope_expressions.tex +++ b/doc/spec/03.03.scope_expressions.tex @@ -1,7 +1,7 @@ \begin{figure}[t] \[ \begin{array}{rcl} - \defterm{scopeExpression} & : & \nonterm{definition}^\star\s\nonterm{expression}\\ + \defterm{scopeExpression} & : & \nonterm{definition}^\star\s[\s\nonterm{expression}\s]\\ \defterm{definition} & : & \nonterm{variableDefinition}\alt\nonterm{functionDefinition}\alt\nonterm{infixDefinition}\\ \defterm{variableDefinition} & : & (\s\term{local}\alt\term{public}\s)\s\nonterm{variableDefinitionSequence}\s\term{;}\\ \defterm{variableDefinitionSequence} & : & \nonterm{variableDefinitionSequenceItem}\s(\s\term{,}\s\nonterm{variableDefinitionSequenceItem}\s)^\star\\ diff --git a/doc/spec/03.concrete_syntax.tex b/doc/spec/03.concrete_syntax.tex index d18c6d7d9..806e54fd7 100644 --- a/doc/spec/03.concrete_syntax.tex +++ b/doc/spec/03.concrete_syntax.tex @@ -1,5 +1,4 @@ -\chapter{Concrete Syntax and\\ -Informal Semantics} +\chapter{Concrete Syntax and Semantics} \label{sec:concrete_syntax} In this chapter we describe the concrete syntax of the language as it is recognized by the parser. In the diff --git a/doc/spec/04.extensions.tex b/doc/spec/04.extensions.tex index f91572a63..9b80e6b9b 100644 --- a/doc/spec/04.extensions.tex +++ b/doc/spec/04.extensions.tex @@ -1,10 +1,99 @@ \chapter{Extensions} \label{sec:extensions} +There are some extensions for the core language defined in the previous chapters. These +extensions add some syntactic sugar, which makes writing programs in \lama a more +pleasant task. + \section{Custom Infix Operators} +\label{sec:custom_infix} + +Besides the set of builtin infix operators (see Fig.~\ref{builtin_infixes}) users may define +custom infix operators. These operators may be declared at any scope level; when defined +at the top level they can be exported as well. However, there are some restrictions regarding the +redefinition of builtin infix operators: + +\begin{itemize} +\item redefinitions of builtin infix operators can not be exported; +\item assignment operator "\lstinline|:=|" can not be redefined. +\end{itemize} + +The syntax for infix operator definition is shown on Fig.~\ref{custom_infix_construct}; a custom infix definition must specify exactly two arguments. +An associativity and precedence level has to be assigned to each custom infix operator. A precedence level is assigned by specifying at which +position, relative to other known infix operators, the operator being defined is inserted. Three kinds of specifications are allowed: at given level, +immediately before or immediatly after. For example, "\lstinline|at +|" means that the operator is assigned exactly the same +level of precedence as "\lstinline|+|"; "\lstinline|after +|" creates a new precedence level immediately \emph{after} that for +"\lstinline|+|" (but \emph{before} that for "\lstinline|*|"), and "\lstinline|before *|" has exactly the same effect (provided +there were no insertions of precedence levels betwee those for "\lstinline|+|" and "\lstinline|*|"). + +When begin inserted at existing precedence level, an infix operator inherits the associativity from that level; hence, only "\lstinline|infix|" +keyword can be used for such definitions. When a new level is created, an associativity for this level has to be additionally specified +by using corresponding keyword ("\lstinline|infix|" for non-associative levels, "\lstinline|infixr|"~--- for levels with right +associativity, and "\lstinline|infixl|"~--- for levels with left associativity). + +When public infix operators are exported, their relative precedence levels and associativity are exported as well; since not all +custom infix definitions may be made public some levels may disappear from the export. For example, let us have the following definitions: + +\begin{lstlisting} + infixl ** before * (x, y) {...} + public infixr *** before ** (x, y) {...} +\end{lstlisting} + +Here in the top scope for the compilation unit we have two additional precedence levels: one for the "\lstinline|**|" and another for the "\lstinline|***|". +However, as "\lstinline|**|" is not exported its precedence level will be forgotten during the import. Thus, only the precedence level for +"\lstinline|***|" will be created during the import as if is was defined at the level "\lstinline|before *|". + +Respectively, multiple imports of units with custom infix operators will modify the precedence level in the order of their import. For example, +if there are two units "\lstinline|A|" and "\lstinline|B|" with declarations "\lstinline|infixl ++ before +|" and "\lstinline|infixl +++ before +|" +correspondingly, then importing "\lstinline|B|" after "\lstinline|B|" will result in "\lstinline|++|" having a \emph{lower} precedence, then +"\lstinline|+++|". + +\begin{figure}[t] + \[ + \begin{array}{rcl} + \defterm{infixDefinition} & : & \nonterm{infixHead}\s\term{(}\s\nonterm{functionArguments}\s\term{)}\s\nonterm{functionBody}\\ + \defterm{infixHead} & : & [\s\term{public}\s]\s\nonterm{infixity}\s\token{INFIX}\s\nonterm{level}\\ + \defterm{infixity} & : & \term{infix}\alt\term{infixl}\alt\term{infixr}\\ + \defterm{level} & : & [\s\term{at}\alt\term{before}\alt\term{after}\s]\s\token{INFIX} + \end{array} + \] + \caption{The Synax for Infix Operator Definition} + \label{custom_infix_construct} +\end{figure} \section{Lazy Values and Eta-extension} +An expression + +\begin{lstlisting} + lazy$\;e$ +\end{lstlisting} + +where $e$~--- a $\nonterm{basicExpression}$~--- is converted into + +\begin{lstlisting} + makeLazy (fun () {$e$}) +\end{lstlisting} + +where "\lstinline|makeLazy|"~--- a function from standard unit "\lstinline|Lazy|" (see Section~\ref{sec:std:lazy}). An import for +"\lstinline|Lazy|" is added implicitly. + +An expression + +\begin{lstlisting} + eta$\;e$ +\end{lstlisting} + +where $e$~--- a $\nonterm{basicExpression}$~--- is converted into + +\begin{lstlisting} + fun ($x$) {$e$ ($x$)}) +\end{lstlisting} + +where "$x$"~--- a fresh variable which does not occur free in "$e$". + \section{Dot Notation} \section{Mutable Closures} + +\section{Patterns in Function Arguments} diff --git a/doc/spec/05.driver_options.tex b/doc/spec/05.driver_options.tex index 033fdb904..5d0a94ce8 100644 --- a/doc/spec/05.driver_options.tex +++ b/doc/spec/05.driver_options.tex @@ -39,11 +39,11 @@ Additionally, the following options can be given to the driver: \item "\texttt{-dp}"~--- forces the driver to dump the AST of compiled unit. The dump is written in the file with the same basename as the source one, with the extension replaced with "\texttt{.ast}". \item "\texttt{-ds}"~--- forces the driver to sump stack machine code. The option is only in effect in stack interpreter on - native mode. The sump is written in the file "\texttt{.sm}". + native mode. The dump is written in the file "\texttt{.sm}". \item "\texttt{-v}"~--- makes the dirver to print the version of the compiler. -\item "\texttt{-h}"~--- makes the driver to print the helpon options. +\item "\texttt{-h}"~--- makes the driver to print the help on the options. \end{itemize} -Apart from the paths specified by the "\texttt{-I}" option the driver uses the environment variable "\texttt{LAMA\_STD}" +Apart from the paths specified by the "\texttt{-I}" option the driver uses the environment variable "\texttt{LAMA}" to locate the runtime and standard libraries (see~\ref{sec:stdlib}). Thus, the units from standard libraries are accessible without any "\texttt{-I}" option given. diff --git a/doc/spec/06.standard_library.tex b/doc/spec/06.standard_library.tex index 917095d8c..1d56d54aa 100644 --- a/doc/spec/06.standard_library.tex +++ b/doc/spec/06.standard_library.tex @@ -1,7 +1,7 @@ \chapter{Standard Library} \label{sec:stdlib} -The standard library is comprised of the runtime for the language and the set of pre-shipped units written in \lama itself. +The standard library is comprised of the runtime for the language and a set of pre-shipped units written in \lama itself. \section{Unit \texttt{Std}} \label{sec:std} diff --git a/doc/spec/Makefile b/doc/spec/Makefile index 58a09a4b4..830c3ea7f 100644 --- a/doc/spec/Makefile +++ b/doc/spec/Makefile @@ -1,7 +1,10 @@ -all: +all: metagen pdflatex spec.tex bibtex spec pdflatex spec.tex +metagen: + git rev-parse --verify HEAD | LC_TIME=en_US.UTF-8 git show --no-patch --no-notes --pretty='%cd' --date=format:"%B, %e, %Y" > commitdate.tex + clean: rm -Rf *~ *.log *.aux *.toc *.bbl *.blg *.out diff --git a/doc/spec/spec.tex b/doc/spec/spec.tex index 12c98296c..ded3dfac2 100644 --- a/doc/spec/spec.tex +++ b/doc/spec/spec.tex @@ -28,10 +28,7 @@ \usepackage{xspace} \usepackage{bm} \usepackage{enumitem} - -\makeatletter - -\makeatother +\usepackage{mathpazo} \definecolor{shadecolor}{gray}{1.00} \definecolor{darkgray}{gray}{0.30} @@ -74,7 +71,7 @@ \renewcommand{\emptyset}{\varnothing} \newcommand{\dom}[1]{\mathtt{dom}\;{#1}} \newcommand{\primi}[2]{\mathbf{#1}\;{#2}} -\newcommand{\lama}{$\lambda\mbox{\textsc{Algol}}$\xspace} +\newcommand{\lama}{$\lambda\kern -.1667em\lower -.5ex\hbox{$a$}\kern -.1000em\lower .2ex\hbox{$\mathcal M$}\kern -.1000em\lower -.5ex\hbox{$a$}$\xspace} %\newcommand{\sial}{S\textit{\lower -.5ex\hbox{I}\kern -.1667em\lower .5ex\hbox {A}}\kern -.125emL\@\xspace} \definecolor{light-gray}{gray}{0.90} \newcommand{\graybox}[1]{\colorbox{light-gray}{#1}} @@ -90,7 +87,7 @@ \lstdefinelanguage{alm}{ keywords={skip,if,then,else,elif,fi,while,do,od,repeat,until,for,fun,local,public,return,import,length, -string,case,of,esac,when,boxed,unboxed,string,sexp,array,infix,infixl,infixr,at,before,after,true,false}, +string,case,of,esac,when,boxed,unboxed,string,sexp,array,infix,infixl,infixr,at,before,after,true,false,eta,lazy}, sensitive=true, basicstyle=\small, %commentstyle=\scriptsize\rmfamily, @@ -118,13 +115,82 @@ language=alm \sloppy -\title{\lama Language Definition} +%\title{\lama Language Definition} -\author{Dmitry Boulytchev} +%\author{Dmitry Boulytchev} \begin{document} -\maketitle +\begin{titlepage} % Suppresses displaying the page number on the title page and the subsequent page counts as page 1 + \newcommand{\HRule}{\rule{\linewidth}{0.5mm}} % Defines a new command for horizontal lines, change thickness here + + \center % Centre everything on the page + + %------------------------------------------------ + % Headings + %------------------------------------------------ + + \includegraphics[scale=0.5]{jetbrains.png}\\[3cm] +% \textsc{\LARGE JetBrains Research}\\[2.5cm] % Main heading such as the name of your university/college + +% \textsc{\Large Major Heading}\\[0.5cm] % Major heading such as course name + +% \textsc{\large Minor Heading}\\[0.5cm] % Minor heading such as course title + + %------------------------------------------------ + % Title + %------------------------------------------------ + + \HRule\\[0.4cm] + + {\huge\bfseries \lama Language Specification}\\[0.4cm] % Title of your document + + \HRule\\[1.5cm] + + %------------------------------------------------ + % Author(s) + %------------------------------------------------ + + \begin{minipage}{0.4\textwidth} + \begin{flushleft} + \large + \textit{Author}\\ + Dmitry \textsc{Boulytchev} % Your name + \end{flushleft} + \end{minipage} + %~ + %\begin{minipage}{0.4\textwidth} + % \begin{flushright} + % \large + % \textit{Supervisor}\\ + % Dr. Caroline \textsc{Becker} % Supervisor's name + % \end{flushright} + %\end{minipage} + + % If you don't want a supervisor, uncomment the two lines below and comment the code above + %{\large\textit{Author}}\\ + %John \textsc{Smith} % Your name + + %------------------------------------------------ + % Date + %------------------------------------------------ + + \vfill\vfill\vfill % Position the date 3/4 down the remaining page + + {\large\input{commitdate}} % Date, change the \today to a set date if you want to be precise + + %------------------------------------------------ + % Logo + %------------------------------------------------ + + %\vfill\vfill + %\includegraphics[width=0.2\textwidth]{placeholder.jpg}\\[1cm] % Include a department/university logo - this will require the graphicx package + + %---------------------------------------------------------------------------------------- + + \vfill % Push the date up 1/4 of the remaining page + +\end{titlepage} \tableofcontents diff --git a/src/Driver.ml b/src/Driver.ml index 7f7e2de7c..2530155e3 100644 --- a/src/Driver.ml +++ b/src/Driver.ml @@ -26,7 +26,7 @@ let parse cmd = inherit Util.Lexers.lident kws s inherit Util.Lexers.uident kws s inherit Util.Lexers.skip [ - Matcher.Skip.whitespaces " \t\n"; + Matcher.Skip.whitespaces " \t\n\r"; Matcher.Skip.lineComment "--"; Matcher.Skip.nestedComment "(*" "*)" ] s