Skip to content
Snippets Groups Projects

Juno Frontend

Merged Aaron Councilman requested to merge frontend into main
19 files
+ 47
353
Compare changes
  • Side-by-side
  • Inline
Files
19
+ 0
306
\documentclass[letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage[top=1in,right=1in,left=1in,bottom=1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\usepackage{xcolor}
\usepackage{comment}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=black,
filecolor=magenta,
urlcolor=cyan
}
\usepackage[inference]{semantic}
\usepackage{textcomp}
\usepackage{listings}
% Don't change, used for formatting regular expressions
\lstset{basicstyle=\ttfamily, upquote=true}
\allowdisplaybreaks
\pagestyle{fancy}
\fancyhf{}
\chead{}
\rhead{Front-End Spec}
\lhead{DRAFT: 2024-01-09}
\renewcommand{\footrulewidth}{1pt}
\rfoot{\thepage}
\title{Hercules Front-End Spec (Name TBD) \\ \large Language Specification}
\author{Aaron Councilman, Russel Arbore}
\date{January 9, 2024}
\newcommand{\nonterminal}[1]{#1\ }
\newcommand{\terminal}[1]{\text{`#1'}\ }
\newcommand{\regex}[1]{\text{\lstinline$/#1/$}\ }
\newcommand{\gramOption}[1]{[\ #1]\ }
\newcommand*{\gramRepeat}[2][]{\{\ #2\}_{#1}\ }
\newcommand*{\gramSome}[2][]{\{\ #2\}^{+}_{#1}\ }
\newcommand{\gramOr}{\mid\ }
\begin{document}
\maketitle
This document gives the formal specification of the Hercules front-end language.
Section~\ref{sec:overview} provides an informal overview of the language: its features and notable differences from other languages.
The formal description of the language is contained in Section~\ref{sec:syntax} which defines the syntax, Section~\ref{sec:types} which defines the type-system and semantic analysis rules, and Section~\ref{sec:semantics} which defines the formal semantics.
\section{Overview}\label{sec:overview}
TODO
\section{Syntax}\label{sec:syntax}
The grammar below is provided in an Extended Backus-Naur form.
In particular, concatenation is represented by whitespace between grammar symbols, terminals are represented in single quotes for literal strings as $\terminal{terminal}$ or surrounded by slashes for regular expressions as $\regex{regex}$ while nonterminals are non-quoted and italicized as $\nonterminal{nonterminal}$.
Certain other symbols have meta meanings \emph{when not quoted within a terminal}, specifically parentheses $(\cdots)$ are used for grouping, square brackets $[\cdots]$ indicate an optional component, and curly brackets $\{ \cdots \}$ indicate repetition of zero or more occurrences.
Curly brackets with a symbol in subscript indicates a list of zero or more occurrences separated by the symbol, for instance $\gramRepeat[\terminal{,}]{\nonterminal{Nt}}$ represents the empty string, a single nonterminal $\nonterminal{Nt}$, or a list of multiple $\nonterminal{Nt}$ separated by commas.
A curly bracket with a plus symbol in the superscript (with or without subscript) indicates at least repetition, as in $\{ \cdots \}^{+}$.
The vertical bar symbol $\mid$ indicates alternatives and epsilon $\varepsilon$ indicates the empty string.
The language is whitespace insensitive and resolves lexing ambiguities by maximal munch.
The language also supports C/C++ style line comments and block comments, allowing arbitrary nesting of the latter.
\begin{align*}
%
\nonterminal{Program} ::&= \gramRepeat{\nonterminal{Top}} \\
%
\nonterminal{Top} ::&= \nonterminal{Import}
\gramOr \nonterminal{TypeDecl}
\gramOr \nonterminal{ConstDecl}
\gramOr \nonterminal{FuncDecl}
\gramOr \nonterminal{Module} \\
%
\nonterminal{Module} ::&=
\gramOption{\terminal{pub}} \terminal{module} \nonterminal {Id}
\terminal{\{} \gramRepeat{\nonterminal{Top}} \terminal{\}} \\
%
\nonterminal{Import} ::&=
\terminal{use} \nonterminal{PackageId} \terminal{;} \\
%
\nonterminal{PackageId} ::&=
\nonterminal{PackageName} \gramOption{\terminal{::} \terminal{*}} \\
%
\nonterminal{PackageName} ::&= \gramSome[\terminal{::}]{\nonterminal{Id}} \\
%
\nonterminal{TypeDecl} ::&=
\gramOption{\terminal{pub}} \terminal{type} \nonterminal{Id}
\gramOption{\terminal{\textless}
\gramRepeat[\terminal{,}]{\nonterminal{TypeVar}} \terminal{\textgreater}}
\terminal{=} \nonterminal{TypeDef} \terminal{;} \\
%
\nonterminal{TypeDef} ::&= \nonterminal{Type}
\gramOr \gramOption{\terminal{pub}} \terminal{struct}
\terminal{\{} \gramRepeat{\nonterminal{ObjField}} \terminal{\}} \\
&\gramOr \gramOption{\terminal{pub}} \terminal{union}
\terminal{\{} \gramRepeat{\nonterminal{ObjField}} \terminal{\}} \\
%
\nonterminal{ObjField} ::&=
\gramOption{\terminal{pub}} \nonterminal{Id}
\gramOption{\terminal{:} \nonterminal{Type}} \terminal{;} \\
%
\nonterminal{TypeVar} ::&= \nonterminal{Id}
\gramOr \nonterminal{Id} \terminal{:} \nonterminal{Kind} \\
%
\nonterminal{Kind} ::&= \terminal{type} \gramOr \terminal{usize}
\gramOr \terminal{bool} \gramOr \terminal{number} \gramOr \terminal{integer} \\
%
\nonterminal{Type} ::&= \nonterminal{PrimType}
\gramOr \terminal{(} \gramRepeat[\terminal{,}]{\nonterminal{Type}} \terminal{)} \\
&\gramOr \nonterminal{PackageName}
\gramOption{\terminal{\textless}
\gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{\textgreater}}
\gramOr \nonterminal{Type}
\terminal{[} \gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{]} \\
%
\nonterminal{ConstDecl} ::&= \gramOption{\terminal{pub}}
\terminal{const} \nonterminal{Id} \gramOption{\terminal{:} \nonterminal{Type}}
\terminal{=} \nonterminal{Expr} \terminal{;} \\
%
\nonterminal{FuncDecl} ::&=
\gramOption{\nonterminal{FuncAttr}}
\gramOption{\terminal{pub}} \terminal{fn} \nonterminal{Id}
\gramOption{\terminal{\textless}
\gramRepeat[\terminal{,}]{\nonterminal{TypeVar}} \terminal{\textgreater}} \\
&\hspace{.5cm}
\terminal{(} \gramRepeat[\terminal{,}]{\gramOption{\terminal{inout}}
\nonterminal{VarBinding}} \terminal{)}
\gramOption{\terminal{:} \nonterminal{Type}} \nonterminal{Stmts} \\
%
\nonterminal{FuncAttr} ::&= \regex{#\\[[^\\]]*\\]} \\
%
\nonterminal{VarBinding} ::&= \nonterminal{SPattern} \gramOption{\terminal{:} \nonterminal{Type}} \\
%
\nonterminal{Stmt} ::&=
\terminal{let} \nonterminal{VarBinding}
\gramOption{\terminal{=} \nonterminal{Expr}} \terminal{;}
\gramOr \terminal{const} \nonterminal{VarBinding}
\terminal{=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \nonterminal{LExpr} \terminal{=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{+=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{-=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \nonterminal{LExpr} \terminal{*=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{/=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{\%=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \nonterminal{LExpr} \terminal{\&=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{\textbar=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{\^{}=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \nonterminal{LExpr} \terminal{\&\&=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{\textbar\textbar=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \nonterminal{LExpr} \terminal{\textless\textless=} \nonterminal{Expr} \terminal{;}
\gramOr \nonterminal{LExpr} \terminal{\textgreater\textgreater=} \nonterminal{Expr} \terminal{;} \\
&\gramOr \terminal{if} \nonterminal{Expr} \nonterminal{Stmts}
\gramOption{\terminal{else} \nonterminal{Stmts}} \\
&\gramOr \terminal{match} \nonterminal{Expr} \nonterminal{Cases} \\
&\gramOr \terminal{for}
\nonterminal{VarBinding} \terminal{=} \nonterminal{Expr}
\terminal{to} \nonterminal{Expr}
\gramOption{\terminal{by} \nonterminal{Expr}} \nonterminal{Stmts} \\
&\gramOr \terminal{while} \nonterminal{Expr} \nonterminal{Stmts} \\
&\gramOr \terminal{return} \nonterminal{Expr} \terminal{;} \\
&\gramOr \terminal{break} \terminal{;}
\gramOr \terminal{continue} \terminal{;}
\gramOr \nonterminal{Stmts} \\
&\gramOr \nonterminal{PackageName}
\gramOption{\terminal{::} \terminal{\textless}
\gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{\textgreater}}
\terminal{(} \gramRepeat[\terminal{,}]{\gramOption{\terminal{\&}} \nonterminal{Expr}} \terminal{)} \terminal{;} \\
%
\nonterminal{Stmts} ::&=
\terminal{\{} \gramRepeat{\nonterminal{Stmt}} \terminal{\}} \\
%
\nonterminal{Cases} ::&=
\terminal{\{} \gramRepeat{\nonterminal{Case}} \terminal{\}} \\
%
\nonterminal{Case} ::&=
\nonterminal{Patterns} \terminal{=\textgreater} \nonterminal{Stmt} \\
%
\nonterminal{Patterns} ::&=
\gramOption{\terminal{\textbar}}
\gramSome[\terminal{\textbar}]{\nonterminal{Pattern}} \\
%
\nonterminal{Pattern} ::&= \nonterminal{SPattern}
\gramOr \nonterminal{PackageName} \nonterminal{SPattern} \\
%
\nonterminal{SPattern} ::&= \terminal{\textunderscore}
\gramOr \nonterminal{IntLit} \gramOr \nonterminal{PackageName}
\gramOr \terminal{(} \gramRepeat[\terminal{,}]{\nonterminal{Pattern}} \terminal{)}
\gramOr \terminal{\{} \gramRepeat[\terminal{,}]{\nonterminal{Pattern}} \terminal{\}} \\
&\gramOr \terminal{\{}
\gramRepeat[\terminal{,}]{\nonterminal{Id} \terminal{=} \nonterminal{Pattern}} \terminal{\}} \\
%
\nonterminal{LExpr} ::&= \nonterminal{Id}
\gramOr \nonterminal{LExpr} \terminal{.} \nonterminal{Id}
\gramOr \nonterminal{LExpr} \regex{\\.[0-9]+}
\gramOr \nonterminal{LExpr} \terminal{[} \gramRepeat[\terminal{,}]{\nonterminal{Expr}} \terminal{]} \\
%
\nonterminal{Expr} ::&= \nonterminal{PackageName}
\gramOr \nonterminal{Expr} \terminal{.} \nonterminal{Id}
\gramOr \nonterminal{Expr} \regex{\\.[0-9]+} \\
&\gramOr \nonterminal{Expr} \terminal{[} \gramRepeat[\terminal{,}]{\nonterminal{Expr}} \terminal{]}
\gramOr \terminal{(} \gramRepeat[\terminal{,}]{\nonterminal{Expr}} \terminal{)}
\gramOr \terminal{\{} \gramRepeat[\terminal{,}]{\nonterminal{Expr}} \terminal{\}} \\
&\gramOr \terminal{\{}
\gramRepeat[\terminal{,}]{\nonterminal{Id} \terminal{=} \nonterminal{Expr}} \terminal{\}} \\
&\gramOr \nonterminal{BoolLit} \gramOr \nonterminal{IntLit} \gramOr \nonterminal{FloatLit}
\gramOr \terminal{-} \nonterminal{Expr}
\gramOr \terminal{\texttildelow} \nonterminal{Expr}
\gramOr \terminal{!} \nonterminal{Expr} \\
&\gramOr \nonterminal{Expr} \terminal{as} \nonterminal{Type} \\
&\gramOr \nonterminal{Expr} \terminal{+} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{-} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{*} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{/} \nonterminal{Expr} \\
&\gramOr \nonterminal{Expr} \terminal{\%} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\&} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textbar} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\^{}} \nonterminal{Expr} \\
&\gramOr \nonterminal{Expr} \terminal{\&\&} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textbar\textbar} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textless} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textless=} \nonterminal{Expr} \\
&\gramOr \nonterminal{Expr} \terminal{\textgreater} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textgreater=} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{==} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{!=} \nonterminal{Expr} \\
&\gramOr \nonterminal{Expr} \terminal{\textless\textless} \nonterminal{Expr}
\gramOr \nonterminal{Expr} \terminal{\textgreater\textgreater} \nonterminal{Expr} \\
&\gramOr \terminal{if} \nonterminal{Expr} \terminal{then} \nonterminal{Expr}
\terminal{else} \nonterminal{Expr} \\
&\gramOr \nonterminal{PackageName}
\gramOption{\terminal{::} \terminal{\textless}
\gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{\textgreater}}
\terminal{(} \gramRepeat[\terminal{,}]{\gramOption{\terminal{\&}} \nonterminal{Expr}} \terminal{)} \\
%
\nonterminal{TypeExpr} ::&= \nonterminal{PrimType}
\gramOr \terminal{(} \gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{)} \\
&\gramOr \nonterminal{PackageName} \gramOption{\terminal{\textless} \gramRepeat[\terminal{,}]{\nonterminal{TypeExpr}} \terminal{\textgreater}}
\gramOr \nonterminal{TypeExpr} \terminal{[} \gramRepeat[\terminal{,}]{\nonterminal{Expr}} \terminal{]} \\
&\gramOr \nonterminal{PackageName}
\gramOr \nonterminal{BoolLit} \gramOr \nonterminal{IntLit} \gramOr \nonterminal{FloatLit}
\gramOr \terminal{-} \nonterminal{TypeExpr} \\
&\gramOr \nonterminal{TypeExpr} \terminal{+} \nonterminal{TypeExpr}
\gramOr \nonterminal{TypeExpr} \terminal{-} \nonterminal{TypeExpr} \\
&\gramOr \nonterminal{TypeExpr} \terminal{*} \nonterminal{TypeExpr}
\gramOr \nonterminal{TypeExpr} \terminal{as} \nonterminal{Type} \\
%
\nonterminal{PrimType} ::&= \terminal{bool}
\gramOr \terminal{i8} \gramOr \terminal{u8}
\gramOr \terminal{i16} \gramOr \terminal{u16}
\gramOr \terminal{i32} \gramOr \terminal{u32}
\gramOr \terminal{i64} \gramOr \terminal{u64} \\
&\gramOr \terminal{usize} \gramOr \terminal{f32} \gramOr \terminal{f64} \gramOr \terminal{void} \\
%
\nonterminal{Id} ::&= \regex{[a-zA-Z][a-zA-Z0-9_]*} \\
%
\nonterminal{BoolLit} ::&= \terminal{true} \gramOr \terminal{false} \\
%
\nonterminal{IntLit} ::&= \regex{[0-9]+} \gramOr \regex{0x[0-9a-fA-F]+}
\gramOr \regex{0b[0-1]+} \gramOr \regex{0o[0-7]+} \\
%
\nonterminal{FloatLit} ::&= \regex{[0-9]+\\.[0-9]*(e[0-9]+)?} \\
\end{align*}
%
The grammar specified is ambiguous in a few places, the details of the disambiguation are provided below:
\begin{enumerate}
\item All keywords (defined below) are reserved and are not parsed as members of the $\nonterminal{Id}$ nonterminal.
\item The grammar experience dangling else ambiguity, which is resolved by attaching the else to nearest if.
\item For conditional in expressions, the expression following the `else' captures as much as possible.
\item The unary and binary operators produce significant ambiguity.
This is resolved though associativities and precedences, listed below.
In this list, the top of the list are the operators which bind most tightly.
\begin{enumerate}
\item Left associative: `.', (dot number), `[]'
\item Right associative: `-' (unary), `\texttildelow', `!', and `\&' (unary)
\item Left associative: `as' and `size'
\item Left associative: `*', `/', and `\%'
\item Left associative: `+' and `-' (binary)
\item Left associative: `\textless\textless', and `\textgreater\textgreater'
\item Non associative: `\textless', `\textless=', `\textgreater', `\textgreater='
\item Non associative: `==' and `!='
\item Left associative: `\&' (binary)
\item Left associative: `\^{}'
\item Left associative: `\textbar'
\item Left associative: `\&\&'
\item Left associative: `\textbar\textbar'
\item Non associative: `if' $\cdots$ `then' $\cdots$ `else' $\cdots$
\item Non associative: `)' (used for disambiguating dangling else)
\end{enumerate}
\end{enumerate}
%
The keywords are: `as', `bool', `break', `by', `continue', `else', `f32', `f64', `false', `fn', `for', `i8', `i16', `i32', `i64', `if', `inout', `let', `match', `module', `pub', `return', `size', `struct', `then', `to', `true', `type', `u8', `u16', `u32', `u64', `union', `use', `void', and `while'.
\section{Type System}\label{sec:types}
TODO
\section{Semantics}\label{sec:semantics}
TODO
\end{document}
Loading