Y-asc

yet another stupid compiler
Log | Files | Refs | README | git clone https://git.ne02ptzero.me/git/Y-asc

commit 56c89649b0b61c8234d99b2ac9149848f8fa46fc
parent 72b3b6c0114b374c008333224c863810ba8a4e6c
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 13 Jun 2018 22:29:51 +0200

FIX: Better file organisation

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>

Diffstat:
Atex/Y/1.Basics/Comments.tex | 9+++++++++
Atex/Y/1.Basics/File_Format.tex | 5+++++
Atex/Y/1.Basics/Instructions.tex | 17+++++++++++++++++
Atex/Y/1.Basics/Scope.tex | 29+++++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Arithmetic_Operators.tex | 8++++++++
Atex/Y/2.Types_Operators_and_Expressions/Compilation_time_casts.tex | 29+++++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Constants.tex | 14++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Data_types_and_Sizes.tex | 23+++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Declarations.tex | 26++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Inline_Constant_Declarations.tex | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Precedence_and_Order_of_Evaluation.tex | 25+++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Relational_and_Logical_Operators.tex | 30++++++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Types_Conversions.tex | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Atex/Y/2.Types_Operators_and_Expressions/Variable_name.tex | 16++++++++++++++++
Atex/Y/3.Control_Flow/Break_and_Continue.tex | 0
Atex/Y/3.Control_Flow/Else_If.tex | 0
Atex/Y/3.Control_Flow/If_Else.tex | 0
Atex/Y/3.Control_Flow/Loops_Do-While.tex | 0
Atex/Y/3.Control_Flow/Loops_For.tex | 0
Atex/Y/3.Control_Flow/Loops_While.tex | 0
Atex/Y/4.Functions_and_Program_Structure/Basics.tex | 0
Atex/Y/4.Functions_and_Program_Structure/External_Variables.tex | 0
Atex/Y/4.Functions_and_Program_Structure/Header_Files.tex | 0
Atex/Y/4.Functions_and_Program_Structure/Namespaces.tex | 0
Atex/Y/4.Functions_and_Program_Structure/Static_Variables.tex | 0
Atex/Y/5.Preprocessor/Conditions.tex | 0
Atex/Y/5.Preprocessor/File_Inclusion.tex | 0
Atex/Y/5.Preprocessor/Macro_Substitution.tex | 0
Atex/Y/6.Pointers_and_Arrays/Address_Arithmetic.tex | 0
Atex/Y/6.Pointers_and_Arrays/Command-line_arguments.tex | 0
Atex/Y/6.Pointers_and_Arrays/Pointer_and_Arrays.tex | 0
Atex/Y/6.Pointers_and_Arrays/Pointers_and_Addresses.tex | 0
Atex/Y/6.Pointers_and_Arrays/Pointers_and_Function_Arguments.tex | 0
Atex/Y/6.Pointers_and_Arrays/The_heap_argument.tex | 0
Atex/Y/7.Structures/Array_of_Structures.tex | 0
Atex/Y/7.Structures/Basics.tex | 0
Atex/Y/7.Structures/Packing.tex | 0
Atex/Y/7.Structures/Pointer_to_Structures.tex | 0
Atex/Y/7.Structures/Structures_and_Functions.tex | 0
Atex/Y/8.Standard_Library/Input_and_Output.tex | 0
Atex/Y/8.Standard_Library/Memory_Control.tex | 0
Atex/Y/8.Standard_Library/System_Calls.tex | 0
Atex/Y/9.Format/Comments.tex | 0
Atex/Y/9.Format/Inline_definitions.tex | 0
Atex/Y/9.Format/Unicode.tex | 0
Mtex/yasc.tex | 407++++---------------------------------------------------------------------------
46 files changed, 430 insertions(+), 388 deletions(-)

diff --git a/tex/Y/1.Basics/Comments.tex b/tex/Y/1.Basics/Comments.tex @@ -0,0 +1,9 @@ +\subsection{Comments} + +Comments can be written in the following format + +\begin{lstlisting} + /* Comment here */ +\end{lstlisting} + +Those are only for the developer, as it will not be compiled. diff --git a/tex/Y/1.Basics/File_Format.tex b/tex/Y/1.Basics/File_Format.tex @@ -0,0 +1,5 @@ +\subsection{File format} + +All files in the Y programming language will have the \texttt{.y} +extension. Though this extension serve no purpose, we advise the +reader to stick to this rule, as is it much better for organizing files. diff --git a/tex/Y/1.Basics/Instructions.tex b/tex/Y/1.Basics/Instructions.tex @@ -0,0 +1,17 @@ +\subsection{Instructions} + +In the Y language, one must declare an instruction with a \texttt{;} at the end +of it + +\begin{lstlisting} + instruction; +\end{lstlisting} + +One can chain instruction on the same line, regardless of line breaks + +\begin{lstlisting} + instruction1; instruction2; +\end{lstlisting} + +It is worth noting that the Y language does not care about line returns, +in whatever standards (\textbackslash n, \textbackslash r\textbackslash n, ..) diff --git a/tex/Y/1.Basics/Scope.tex b/tex/Y/1.Basics/Scope.tex @@ -0,0 +1,29 @@ +\subsection{Scope} + +As many programming languages, Y use a couple of brackets \texttt{\{\}} +to determine scope. Scope is to be determined on special instructions, +like conditions, functions and namespaces. + +\begin{lstlisting} + if (condition) { + /* If's scope */ + } + + [...] + + function(ubyte[] : str) : bool { + /* Function's scope */ + } +\end{lstlisting} + +One can use brackets to determine an internal scope, that serve no +other purpose that limiting variable use + +\begin{lstlisting} + { + u32 num = 2; + + print("Num is equal to %u\n", num); + } + /* Cannot use num here */ +\end{lstlisting} diff --git a/tex/Y/2.Types_Operators_and_Expressions/Arithmetic_Operators.tex b/tex/Y/2.Types_Operators_and_Expressions/Arithmetic_Operators.tex @@ -0,0 +1,8 @@ +\subsection{Arithmetic Operators} + +The binary arithmetic operators are \texttt{+}, \texttt{-}, +\texttt{*}, \texttt{/} and the modulus operator \texttt{\%}. +The binary operator \texttt{+} and \texttt{-} operators have the +same precedence, which is lower than the precedence of \texttt{*}, +\texttt{/}, and \texttt{\%}. Arithmetic operators associate left +to right. diff --git a/tex/Y/2.Types_Operators_and_Expressions/Compilation_time_casts.tex b/tex/Y/2.Types_Operators_and_Expressions/Compilation_time_casts.tex @@ -0,0 +1,29 @@ +\subsection{Compilation time casts} \label{comp_cast} + +In order to respect the 'non implicit cast' rule on constant expressions, +one must use standards function for it: +\begin{lstlisting} + print_int(u8 : num) : void { + print("%d\n", num); + } + + [...] + + main(u32 : ac, ubyte[]* : av) : u32 { + print_int(u32_to_u8(23)); + } +\end{lstlisting} +Alternatively, in order to gain on performance, one can specify +the desired type for the compiler: +\begin{lstlisting} + print_int(__u8(23)); +\end{lstlisting} +The following keywords can be used +\\\\ +$\begin{array}{l} +\_\_u8\\ +\_\_ubyte\\ +\_\_u16\\ +\_\_u32\\ +\end{array}$ +\\\\ diff --git a/tex/Y/2.Types_Operators_and_Expressions/Constants.tex b/tex/Y/2.Types_Operators_and_Expressions/Constants.tex @@ -0,0 +1,14 @@ +\subsection{Constants} \label{const} + +One can declare a specific variable to never be changed, in any scope, +via the keyword \texttt{\_\_const}: +\begin{lstlisting} + ubyte __const char = 'A'; +\end{lstlisting} +This \texttt{char} variable can never change value; any attempt to it +will throw an error. +Constant variables must have a initialization value, otherwise, its +utility is quite limited. + +The \texttt{\_\_const} keyword must always be between a variable +type and its name. diff --git a/tex/Y/2.Types_Operators_and_Expressions/Data_types_and_Sizes.tex b/tex/Y/2.Types_Operators_and_Expressions/Data_types_and_Sizes.tex @@ -0,0 +1,23 @@ +\subsection{Data types and Sizes} + +One can use the following standard data types: + +$\begin{array}{|c|c|c|} +\hline +Name & Range & Content\\ +\hline +\text{ubyte} & \text{0 - 255} & \text{One byte, can be used for an ASCII-character}\\ +\hline +\text{u8} & \text{0 - 255} & \text{Unsigned integer on 8 bits, same as ubyte}\\ +\hline +\text{s8} & \text{-128 - 127} & \text{Signed integer on 8 bits}\\ +\hline +\text{u16} & \text{0 - 65,535} & \text{Unsigned integer on 16 bits}\\ +\hline +\text{s16} & \text{-32,768 - 32,767} & \text{Signed integer on 16 bits}\\ +\hline +\text{u32} & \text{0 - 4,294,967,295} & \text{Unsigned integer on 32 bits}\\ +\hline +\text{s32} & \text{-2,147,483,648 - 2,147,483,647} & \text{Signed integer on 32 bits}\\ +\hline +\end{array}$ diff --git a/tex/Y/2.Types_Operators_and_Expressions/Declarations.tex b/tex/Y/2.Types_Operators_and_Expressions/Declarations.tex @@ -0,0 +1,26 @@ +\subsection{Declarations} + +All variables must be declared before use, although certain +declarations can be made implicity by content. A declaration +specifies a type, an optionnal flag and a name. + +\begin{lstlisting} + u32 count, j; + bool __const ret = true; +\end{lstlisting} + +Variables can be distributed among declarations in any fashion; the +list above could well be written as + +\begin{lstlisting} + u32 count; + u32 j; +\end{lstlisting} + +A variable may also be initialized in its declaration. If the name +is followed by an equals signs and an expression, the expression +serves as an initializer. Constant variables must be initialized, +see the \ref{const} section for more information. + +The flag between the type and the name is implemented by the language, +and should always be in this position. diff --git a/tex/Y/2.Types_Operators_and_Expressions/Inline_Constant_Declarations.tex b/tex/Y/2.Types_Operators_and_Expressions/Inline_Constant_Declarations.tex @@ -0,0 +1,128 @@ +\subsection{Inline Constant Declarations} + +One can use inline constant declarations in order to simplify code + +\begin{lstlisting} + print("Hello!"); +\end{lstlisting} +Here, the \texttt{Hello!} is a constant inline string. One can also +use an integer constant like \texttt{1234}, which is by default an +unsigned integer on 32 bits; therefore, the following example is wrong +\begin{lstlisting} + print_int(u8 : num) : void { + print("%d\n", num); + } + + [...] + + main(u32 : ac, ubyte[]* : av) : u32 { + print_int(23); + } +\end{lstlisting} +Instead, one must use +\begin{lstlisting} + print_int(u32_to_u8(23)); +\end{lstlisting} +Which is valid by the type conversions standard of the Y language, see +\ref{Type_conv} section for more information. + +In order to gain some performance at run-time, by avoiding a function call, +one can use constants cast +\begin{lstlisting} + print_int(__u8(23)); +\end{lstlisting} +It will change the internal type of the variable at compilation time, +acting like a cast. See the \ref{comp_cast} section for more information. + +The value of an integer can be specified in octal or hexadecimal +instead of decimal. A leading \texttt{0} (zero) on an integer +constant means octal; a leading \texttt{0x} or \texttt{0X} means +hexadecimal. For example, decimal 31 can be written as +\texttt{037} in octal and \texttt{0x1f} or \texttt{0x1F} in hex. + +A character constant is an integer, written as one character within +single quotes, such as \texttt{'x'}. The value of a character constant +is the numeric value of the characters in the machine's character set. +For example, in the ASCII character set the character constant \texttt{'0'} +has the value \texttt{48}, which is unrelated to the numeric value \texttt{0}. + +Certain characters can be represented in character and string constants +by escape sequences like \texttt{\textbackslash n} (newline); these sequences look +like two characters, but represent only one. In addition, an arbitrary +byte-sized (ubyte) pattern can be specified by +\begin{lstlisting} + '\ooo' +\end{lstlisting} +Where \texttt{ooo} is one to three octal digits (0..7) or by +\begin{lstlisting} + '\xhh' +\end{lstlisting} +where \texttt{hh} is one or more hexadecimal digits (0..9, a..f, A..F). +The complete set of escape sequences is +\\\\ +$\begin{array}{|l|l|} +\hline +Sequence & Value\\ +\hline +\backslash a & \text{alert (bell) character} \\ +\hline +\backslash b & \text{backspace} \\ +\hline +\backslash f & \text{formfeed} \\ +\hline +\backslash n & \text{newline} \\ +\hline +\backslash r & \text{carriage return} \\ +\hline +\backslash t & \text{horizontal tab} \\ +\hline +\backslash v & \text{vertical tab} \\ +\hline +\backslash \backslash & \text{backslash} \\ +\hline +\backslash ? & \text{question mark} \\ +\hline +\backslash ' & \text{single quote} \\ +\hline +\backslash " & \text{double quote} \\ +\hline +\backslash ooo & \text{octal number} \\ +\hline +\backslash xhh & \text{hexadecimal number} \\ +\hline +\end{array}$ +\\\\ +The character constant \texttt{'\textbackslash 0'} represents the character +with value zero, the null character. + +A \texttt{constant expression} is an expression that involves only constants. +Such expressions may be evaluated at during compilation rather than run-time, +and accordingly may be used in any place that a constant occur, as in +\begin{lstlisting} + ubyte[100 + 20] str; +\end{lstlisting} +will be compiled as +\begin{lstlisting} + ubyte[120] str; +\end{lstlisting} + +A string constant, or string literal, is sequence of zero or more +characters surrounded by doubles quotes, as in +\begin{lstlisting} + "I am a string" +\end{lstlisting} +or +\begin{lstlisting} + "" /* The empty string */ +\end{lstlisting} +The quotes are not part of the string, but serve only to delimit it. The +same escape sequences used in character constants apply in strings; +\textbackslash " represents the double-quote character. String constants +can be concatenated at compile time: +\begin{lstlisting} + "hello, " "world" +\end{lstlisting} +is equivalent to +\begin{lstlisting} + "hello, world" +\end{lstlisting} diff --git a/tex/Y/2.Types_Operators_and_Expressions/Precedence_and_Order_of_Evaluation.tex b/tex/Y/2.Types_Operators_and_Expressions/Precedence_and_Order_of_Evaluation.tex @@ -0,0 +1,25 @@ +\subsection{Precedence and Order of Evaluation} + +You can find the order of evaluation of all Y keywords below + +$\begin{array}{|l|l|} +\hline +Operators & Associativity\\ +\hline +\text{() [] .} & \text{Left to right}\\ +\hline +\text{! *(variable)} & \text{Right to left}\\ +\hline +\text{* / \%} & \text{Left to right}\\ +\hline +\text{+ -} & \text{Left to right}\\ +\hline +< <= > >= & \text{Left to right}\\ +\hline +\text{\&\&} & \text{Left to right}\\ +\hline +|| & \text{Left to right}\\ +\hline +\text{\_\_keyword()} & \text{Left to right}\\ +\hline +\end{array}$ diff --git a/tex/Y/2.Types_Operators_and_Expressions/Relational_and_Logical_Operators.tex b/tex/Y/2.Types_Operators_and_Expressions/Relational_and_Logical_Operators.tex @@ -0,0 +1,30 @@ +\subsection{Relational and Logical Operators} + +The relational operators are + +$\begin{array}{llll} +> & >= & < & <= +\end{array}$ + +They all have the same precedence. Just below them in precedence are +the equality operators: + +$\begin{array}{ll} +== & != +\end{array}$ + +Relational operators have lower precedence than arithmetic operators, +so an expression like \texttt{i < lim - 1} is taken as \texttt{i < (lim - 1)} +, as would be expected. + +The logical operators are + +$\begin{array}{ll} +\&\& & || +\end{array}$ + +Expressions connected by \texttt{\&\&} or \texttt{||} are evaluated +left to right, and evaluation stops as soon as the truth or falsehood +of the result is known. By definition, the numeric value of a relational +or logical expression is 1 if the relation is true, and 0 if the relation +is false. diff --git a/tex/Y/2.Types_Operators_and_Expressions/Types_Conversions.tex b/tex/Y/2.Types_Operators_and_Expressions/Types_Conversions.tex @@ -0,0 +1,52 @@ +\subsection{Type Conversions} \label{Type_conv} + +In the Y language, there is no such thing as 'implicit type conversion': +All conversions must be explicit, using the standard functions. +Therefore, the following example is wrong: +\begin{lstlisting} + u32 i = 10; + u8 j; + + i = j; +\end{lstlisting} +In place of that, one must use +\begin{lstlisting} + u32 i = 10; + u8 j; + + j = u32_to_u8(i); +\end{lstlisting} +All standards type-conversions function are to be safe, with no +possibilty of underflow or overflow. You can find all the standards +function below: +\\\\ +\hspace*{-60px} +$\begin{array}{|l|l|l|l|} +\hline +Name & Argument & Returns & Role\\ +\hline +\text{u8\_to\_u16} & \text{u8} & \text{u16} & \text{Convert an unsigned integer on 8 bits to an unsigned integer on 16 bits}\\ +\hline +\text{u8\_to\_u32} & \text{u8} & \text{u32} & \text{Convert an unsigned integer on 8 bits to an unsigned integer on 32 bits}\\ +\hline +\text{u16\_to\_u8} & \text{u16} & \text{u8} & \text{Convert an unsigned integer on 16 bits to an unsigned integer on 8 bits}\\ +\hline +\text{u16\_to\_u32} & \text{u16} & \text{u32} & \text{Convert an unsigned integer on 16 bits to an unsigned integer on 32 bits}\\ +\hline +\text{u32\_to\_u8} & \text{u32} & \text{u8} & \text{Convert an unsigned integer on 32 bits to an unsigned integer on 8 bits}\\ +\hline +\text{u32\_to\_u16} & \text{u32} & \text{u16} & \text{Convert an unsigned integer on 32 bits to an unsigned integer on 16 bits}\\ +\hline +\text{s8\_to\_s16} & \text{s8} & \text{s16} & \text{Convert a signed integer on 8 bits to a signed integer on 16 bits}\\ +\hline +\text{s8\_to\_s32} & \text{s8} & \text{s32} & \text{Convert a signed integer on 8 bits to a signed integer on 32 bits}\\ +\hline +\text{s16\_to\_s8} & \text{s16} & \text{s8} & \text{Convert a signed integer on 16 bits to a signed integer on 8 bits}\\ +\hline +\text{s16\_to\_s32} & \text{s16} & \text{s32} & \text{Convert a signed integer on 16 bits to a signed integer on 32 bits}\\ +\hline +\text{s32\_to\_s8} & \text{s32} & \text{s8} & \text{Convert a signed integer on 32 bits to a signed integer on 8 bits}\\ +\hline +\text{s32\_to\_s16} & \text{s32} & \text{s16} & \text{Convert a signed integer on 32 bits to a signed integer on 16 bits}\\ +\hline +\end{array}$ diff --git a/tex/Y/2.Types_Operators_and_Expressions/Variable_name.tex b/tex/Y/2.Types_Operators_and_Expressions/Variable_name.tex @@ -0,0 +1,16 @@ +\subsection{Variable names} \label{varnames} + +Variable names are made up of letters and digits; the first character +must be a letter. A letter is, in this context, a character inside +the English dictionnary, through the letters \texttt{a} to \texttt{z}. +All other symbols from other languages / Unicode are not supported, +and will throw an error upon reading. The underscore \texttt{\_} +count as a letter. All variable must be in lowercase, as uppercase +names are reserved to namespaces. +Keywords like \texttt{if}, \texttt{else}, \texttt{struct}, etc., are +reserved: They cannot be used as variables names. + +A variable name cannot be used twice in a scope: If such a thing +would happen, an error will be thrown. However, different variables +can have the same name in differents namespaces. See the \ref{namespaces} +section for more information. diff --git a/tex/Y/3.Control_Flow/Break_and_Continue.tex b/tex/Y/3.Control_Flow/Break_and_Continue.tex diff --git a/tex/Y/3.Control_Flow/Else_If.tex b/tex/Y/3.Control_Flow/Else_If.tex diff --git a/tex/Y/3.Control_Flow/If_Else.tex b/tex/Y/3.Control_Flow/If_Else.tex diff --git a/tex/Y/3.Control_Flow/Loops_Do-While.tex b/tex/Y/3.Control_Flow/Loops_Do-While.tex diff --git a/tex/Y/3.Control_Flow/Loops_For.tex b/tex/Y/3.Control_Flow/Loops_For.tex diff --git a/tex/Y/3.Control_Flow/Loops_While.tex b/tex/Y/3.Control_Flow/Loops_While.tex diff --git a/tex/Y/4.Functions_and_Program_Structure/Basics.tex b/tex/Y/4.Functions_and_Program_Structure/Basics.tex diff --git a/tex/Y/4.Functions_and_Program_Structure/External_Variables.tex b/tex/Y/4.Functions_and_Program_Structure/External_Variables.tex diff --git a/tex/Y/4.Functions_and_Program_Structure/Header_Files.tex b/tex/Y/4.Functions_and_Program_Structure/Header_Files.tex diff --git a/tex/Y/4.Functions_and_Program_Structure/Namespaces.tex b/tex/Y/4.Functions_and_Program_Structure/Namespaces.tex diff --git a/tex/Y/4.Functions_and_Program_Structure/Static_Variables.tex b/tex/Y/4.Functions_and_Program_Structure/Static_Variables.tex diff --git a/tex/Y/5.Preprocessor/Conditions.tex b/tex/Y/5.Preprocessor/Conditions.tex diff --git a/tex/Y/5.Preprocessor/File_Inclusion.tex b/tex/Y/5.Preprocessor/File_Inclusion.tex diff --git a/tex/Y/5.Preprocessor/Macro_Substitution.tex b/tex/Y/5.Preprocessor/Macro_Substitution.tex diff --git a/tex/Y/6.Pointers_and_Arrays/Address_Arithmetic.tex b/tex/Y/6.Pointers_and_Arrays/Address_Arithmetic.tex diff --git a/tex/Y/6.Pointers_and_Arrays/Command-line_arguments.tex b/tex/Y/6.Pointers_and_Arrays/Command-line_arguments.tex diff --git a/tex/Y/6.Pointers_and_Arrays/Pointer_and_Arrays.tex b/tex/Y/6.Pointers_and_Arrays/Pointer_and_Arrays.tex diff --git a/tex/Y/6.Pointers_and_Arrays/Pointers_and_Addresses.tex b/tex/Y/6.Pointers_and_Arrays/Pointers_and_Addresses.tex diff --git a/tex/Y/6.Pointers_and_Arrays/Pointers_and_Function_Arguments.tex b/tex/Y/6.Pointers_and_Arrays/Pointers_and_Function_Arguments.tex diff --git a/tex/Y/6.Pointers_and_Arrays/The_heap_argument.tex b/tex/Y/6.Pointers_and_Arrays/The_heap_argument.tex diff --git a/tex/Y/7.Structures/Array_of_Structures.tex b/tex/Y/7.Structures/Array_of_Structures.tex diff --git a/tex/Y/7.Structures/Basics.tex b/tex/Y/7.Structures/Basics.tex diff --git a/tex/Y/7.Structures/Packing.tex b/tex/Y/7.Structures/Packing.tex diff --git a/tex/Y/7.Structures/Pointer_to_Structures.tex b/tex/Y/7.Structures/Pointer_to_Structures.tex diff --git a/tex/Y/7.Structures/Structures_and_Functions.tex b/tex/Y/7.Structures/Structures_and_Functions.tex diff --git a/tex/Y/8.Standard_Library/Input_and_Output.tex b/tex/Y/8.Standard_Library/Input_and_Output.tex diff --git a/tex/Y/8.Standard_Library/Memory_Control.tex b/tex/Y/8.Standard_Library/Memory_Control.tex diff --git a/tex/Y/8.Standard_Library/System_Calls.tex b/tex/Y/8.Standard_Library/System_Calls.tex diff --git a/tex/Y/9.Format/Comments.tex b/tex/Y/9.Format/Comments.tex diff --git a/tex/Y/9.Format/Inline_definitions.tex b/tex/Y/9.Format/Inline_definitions.tex diff --git a/tex/Y/9.Format/Unicode.tex b/tex/Y/9.Format/Unicode.tex diff --git a/tex/yasc.tex b/tex/yasc.tex @@ -11,6 +11,9 @@ \geometry{a4paper} \lstset{basicstyle=\footnotesize\ttfamily,breaklines=true} \lstset{framextopmargin=50pt} +\makeatletter +\def\input@path{{tex/}} +\makeatother \renewcommand\partheadstartvskip{\clearpage\null\vfil} \renewcommand\partheadmidvskip{\par\nobreak\vskip 20pt\thispagestyle{empty}} @@ -53,54 +56,11 @@ \part{Introduction} \part{The Y Language} \section{The basics} - \subsection{File format} - All files in the Y programming language will have the \texttt{.y} - extension. Though this extension serve no purpose, we advise the - reader to stick to this rule, as is it much better for organizing files. - \subsection{Instructions} - In the Y language, one must declare an instruction with a \texttt{;} - at the end of it - \begin{lstlisting} - instruction; - \end{lstlisting} - One can chain instruction on the same line, regardless of line breaks - \begin{lstlisting} - instruction1; instruction2; - \end{lstlisting} - It is worth noting that the Y language does not care about line returns, - in whatever standards (\textbackslash n, \textbackslash r\textbackslash n, ..) - \subsection{Scope} - As many programming languages, Y use a couple of brackets \texttt{\{\}} - to determine scope. Scope is to be determined on special instructions, - like conditions, functions and namespaces. - \begin{lstlisting} - if (condition) { - /* If's scope */ - } - - [...] - - function(ubyte[] : str) : bool { - /* Function's scope */ - } - \end{lstlisting} - One can use brackets to determine an internal scope, that serve no - other purpose that limiting variable use - \begin{lstlisting} - { - u32 num = 2; - - print("Num is equal to %u\n", num); - } - /* Cannot use num here */ - \end{lstlisting} - - \subsection{Comments} - Comments can be written in the following format - \begin{lstlisting} - /* Comment here */ - \end{lstlisting} - Those are only for the developer, as it will not be compiled. + \input{Y/1.Basics/File_Format.tex} + \input{Y/1.Basics/Instructions.tex} + \input{Y/1.Basics/Scope.tex} + \input{Y/1.Basics/Comments.tex} + \newpage \section{Types, Operators and Expressions} Variables and constants are the basic data objects manipulated in a @@ -111,347 +71,18 @@ of values it can have and what operations can be performed on it. These building blocks are the topic of this chapter. - \subsection{Declarations} - All variables must be declared before use, although certain - declarations can be made implicity by content. A declaration - specifies a type, an optionnal flag and a name. - \begin{lstlisting} - u32 count, j; - bool __const ret = true; - \end{lstlisting} - Variables can be distributed among declarations in any fashion; the - list above could well be written as - \begin{lstlisting} - u32 count; - u32 j; - \end{lstlisting} - A variable may also be initialized in its declaration. If the name - is followed by an equals signs and an expression, the expression - serves as an initializer. Constant variables must be initialized, - see the \ref{const} section for more information. - - The flag between the type and the name is implemented by the language, - and should always be in this position. - \subsection{Arithmetic Operators} - The binary arithmetic operators are \texttt{+}, \texttt{-}, - \texttt{*}, \texttt{/} and the modulus operator \texttt{\%}. - The binary operator \texttt{+} and \texttt{-} operators have the - same precedence, which is lower than the precedence of \texttt{*}, - \texttt{/}, and \texttt{\%}. Arithmetic operators associate left - to right. - - \subsection{Variable names} \label{varnames} - Variable names are made up of letters and digits; the first character - must be a letter. A letter is, in this context, a character inside - the English dictionnary, through the letters \texttt{a} to \texttt{z}. - All other symbols from other languages / Unicode are not supported, - and will throw an error upon reading. The underscore \texttt{\_} - count as a letter. All variable must be in lowercase, as uppercase - names are reserved to namespaces. - Keywords like \texttt{if}, \texttt{else}, \texttt{struct}, etc., are - reserved: They cannot be used as variables names. - - A variable name cannot be used twice in a scope: If such a thing - would happen, an error will be thrown. However, different variables - can have the same name in differents namespaces. See the \ref{namespaces} - section for more information. - \subsection{Data types and Sizes} - One can use the following standard data types: - -$\begin{array}{|c|c|c|} -\hline -Name & Range & Content\\ -\hline -\text{ubyte} & \text{0 - 255} & \text{One byte, can be used for an ASCII-character}\\ -\hline -\text{u8} & \text{0 - 255} & \text{Unsigned integer on 8 bits, same as ubyte}\\ -\hline -\text{s8} & \text{-128 - 127} & \text{Signed integer on 8 bits}\\ -\hline -\text{u16} & \text{0 - 65,535} & \text{Unsigned integer on 16 bits}\\ -\hline -\text{s16} & \text{-32,768 - 32,767} & \text{Signed integer on 16 bits}\\ -\hline -\text{u32} & \text{0 - 4,294,967,295} & \text{Unsigned integer on 32 bits}\\ -\hline -\text{s32} & \text{-2,147,483,648 - 2,147,483,647} & \text{Signed integer on 32 bits}\\ -\hline -\end{array}$ - \subsection{Constants} \label{const} - One can declare a specific variable to never be changed, in any scope, - via the keyword \texttt{\_\_const}: - \begin{lstlisting} - ubyte __const char = 'A'; - \end{lstlisting} - This \texttt{char} variable can never change value; any attempt to it - will throw an error. - Constant variables must have a initialization value, otherwise, its - utility is quite limited. - - The \texttt{\_\_const} keyword must always be between a variable - type and its name. - - \subsection{Inline Constant Declarations} - One can use inline constant declarations in order to simplify code - \begin{lstlisting} - print("Hello!"); - \end{lstlisting} - Here, the \texttt{Hello!} is a constant inline string. One can also - use an integer constant like \texttt{1234}, which is by default an - unsigned integer on 32 bits; therefore, the following example is wrong - \begin{lstlisting} - print_int(u8 : num) : void { - print("%d\n", num); - } - - [...] - - main(u32 : ac, ubyte[]* : av) : u32 { - print_int(23); - } - \end{lstlisting} - Instead, one must use - \begin{lstlisting} - print_int(u32_to_u8(23)); - \end{lstlisting} - Which is valid by the type conversions standard of the Y language, see - \ref{Type_conv} section for more information. - - In order to gain some performance at run-time, by avoiding a function call, - one can use constants cast - \begin{lstlisting} - print_int(__u8(23)); - \end{lstlisting} - It will change the internal type of the variable at compilation time, - acting like a cast. See the \ref{comp_cast} section for more information. - - The value of an integer can be specified in octal or hexadecimal - instead of decimal. A leading \texttt{0} (zero) on an integer - constant means octal; a leading \texttt{0x} or \texttt{0X} means - hexadecimal. For example, decimal 31 can be written as - \texttt{037} in octal and \texttt{0x1f} or \texttt{0x1F} in hex. - - A character constant is an integer, written as one character within - single quotes, such as \texttt{'x'}. The value of a character constant - is the numeric value of the characters in the machine's character set. - For example, in the ASCII character set the character constant \texttt{'0'} - has the value \texttt{48}, which is unrelated to the numeric value \texttt{0}. - - Certain characters can be represented in character and string constants - by escape sequences like \texttt{\textbackslash n} (newline); these sequences look - like two characters, but represent only one. In addition, an arbitrary - byte-sized (ubyte) pattern can be specified by - \begin{lstlisting} - '\ooo' - \end{lstlisting} - Where \texttt{ooo} is one to three octal digits (0..7) or by - \begin{lstlisting} - '\xhh' - \end{lstlisting} - where \texttt{hh} is one or more hexadecimal digits (0..9, a..f, A..F). - The complete set of escape sequences is -\\\\ -$\begin{array}{|l|l|} -\hline -Sequence & Value\\ -\hline -\backslash a & \text{alert (bell) character} \\ -\hline -\backslash b & \text{backspace} \\ -\hline -\backslash f & \text{formfeed} \\ -\hline -\backslash n & \text{newline} \\ -\hline -\backslash r & \text{carriage return} \\ -\hline -\backslash t & \text{horizontal tab} \\ -\hline -\backslash v & \text{vertical tab} \\ -\hline -\backslash \backslash & \text{backslash} \\ -\hline -\backslash ? & \text{question mark} \\ -\hline -\backslash ' & \text{single quote} \\ -\hline -\backslash " & \text{double quote} \\ -\hline -\backslash ooo & \text{octal number} \\ -\hline -\backslash xhh & \text{hexadecimal number} \\ -\hline -\end{array}$ -\\\\ - The character constant \texttt{'\textbackslash 0'} represents the character - with value zero, the null character. - - A \texttt{constant expression} is an expression that involves only constants. - Such expressions may be evaluated at during compilation rather than run-time, - and accordingly may be used in any place that a constant occur, as in - \begin{lstlisting} - ubyte[100 + 20] str; - \end{lstlisting} - will be compiled as - \begin{lstlisting} - ubyte[120] str; - \end{lstlisting} - - A string constant, or string literal, is sequence of zero or more - characters surrounded by doubles quotes, as in - \begin{lstlisting} - "I am a string" - \end{lstlisting} - or - \begin{lstlisting} - "" /* The empty string */ - \end{lstlisting} - The quotes are not part of the string, but serve only to delimit it. The - same escape sequences used in character constants apply in strings; - \textbackslash " represents the double-quote character. String constants - can be concatenated at compile time: - \begin{lstlisting} - "hello, " "world" - \end{lstlisting} - is equivalent to - \begin{lstlisting} - "hello, world" - \end{lstlisting} - - \subsection{Relational and Logical Operators} - The relational operators are - -$\begin{array}{llll} -> & >= & < & <= -\end{array}$ - - They all have the same precedence. Just below them in precedence are - the equality operators: - -$\begin{array}{ll} -== & != -\end{array}$ - - Relational operators have lower precedence than arithmetic operators, - so an expression like \texttt{i < lim - 1} is taken as \texttt{i < (lim - 1)} - , as would be expected. - - The logical operators are - -$\begin{array}{ll} -\&\& & || -\end{array}$ - - Expressions connected by \texttt{\&\&} or \texttt{||} are evaluated - left to right, and evaluation stops as soon as the truth or falsehood - of the result is known. By definition, the numeric value of a relational - or logical expression is 1 if the relation is true, and 0 if the relation - is false. - - \subsection{Type Conversions} \label{Type_conv} - In the Y language, there is no such thing as 'implicit type conversion': - All conversions must be explicit, using the standard functions. - Therefore, the following example is wrong: - \begin{lstlisting} - u32 i = 10; - u8 j; - - i = j; - \end{lstlisting} - In place of that, one must use - \begin{lstlisting} - u32 i = 10; - u8 j; - - j = u32_to_u8(i); - \end{lstlisting} - All standards type-conversions function are to be safe, with no - possibilty of underflow or overflow. You can find all the standards - function below: -\\\\ -\hspace*{-60px} -$\begin{array}{|l|l|l|l|} -\hline -Name & Argument & Returns & Role\\ -\hline -\text{u8\_to\_u16} & \text{u8} & \text{u16} & \text{Convert an unsigned integer on 8 bits to an unsigned integer on 16 bits}\\ -\hline -\text{u8\_to\_u32} & \text{u8} & \text{u32} & \text{Convert an unsigned integer on 8 bits to an unsigned integer on 32 bits}\\ -\hline -\text{u16\_to\_u8} & \text{u16} & \text{u8} & \text{Convert an unsigned integer on 16 bits to an unsigned integer on 8 bits}\\ -\hline -\text{u16\_to\_u32} & \text{u16} & \text{u32} & \text{Convert an unsigned integer on 16 bits to an unsigned integer on 32 bits}\\ -\hline -\text{u32\_to\_u8} & \text{u32} & \text{u8} & \text{Convert an unsigned integer on 32 bits to an unsigned integer on 8 bits}\\ -\hline -\text{u32\_to\_u16} & \text{u32} & \text{u16} & \text{Convert an unsigned integer on 32 bits to an unsigned integer on 16 bits}\\ -\hline -\text{s8\_to\_s16} & \text{s8} & \text{s16} & \text{Convert a signed integer on 8 bits to a signed integer on 16 bits}\\ -\hline -\text{s8\_to\_s32} & \text{s8} & \text{s32} & \text{Convert a signed integer on 8 bits to a signed integer on 32 bits}\\ -\hline -\text{s16\_to\_s8} & \text{s16} & \text{s8} & \text{Convert a signed integer on 16 bits to a signed integer on 8 bits}\\ -\hline -\text{s16\_to\_s32} & \text{s16} & \text{s32} & \text{Convert a signed integer on 16 bits to a signed integer on 32 bits}\\ -\hline -\text{s32\_to\_s8} & \text{s32} & \text{s8} & \text{Convert a signed integer on 32 bits to a signed integer on 8 bits}\\ -\hline -\text{s32\_to\_s16} & \text{s32} & \text{s16} & \text{Convert a signed integer on 32 bits to a signed integer on 16 bits}\\ -\hline -\end{array}$ - - \subsection{Compilation time casts} \label{comp_cast} - In order to respect the 'non implicit cast' rule on constant expressions, - one must use standards function for it: - \begin{lstlisting} - print_int(u8 : num) : void { - print("%d\n", num); - } + \input{Y/2.Types_Operators_and_Expressions/Declarations.tex} + \input{Y/2.Types_Operators_and_Expressions/Arithmetic_Operators.tex} + \input{Y/2.Types_Operators_and_Expressions/Variable_name.tex} + \input{Y/2.Types_Operators_and_Expressions/Data_types_and_Sizes.tex} + \input{Y/2.Types_Operators_and_Expressions/Constants.tex} + \input{Y/2.Types_Operators_and_Expressions/Inline_Constant_Declarations.tex} + \input{Y/2.Types_Operators_and_Expressions/Relational_and_Logical_Operators.tex} + \input{Y/2.Types_Operators_and_Expressions/Types_Conversions.tex} + \input{Y/2.Types_Operators_and_Expressions/Compilation_time_casts.tex} + \input{Y/2.Types_Operators_and_Expressions/Precedence_and_Order_of_Evaluation.tex} + \newpage - [...] - - main(u32 : ac, ubyte[]* : av) : u32 { - print_int(u32_to_u8(23)); - } - \end{lstlisting} - Alternatively, in order to gain on performance, one can specify - the desired type for the compiler: - \begin{lstlisting} - print_int(__u8(23)); - \end{lstlisting} - The following keywords can be used -\\\\ -$\begin{array}{l} -\_\_u8\\ -\_\_ubyte\\ -\_\_u16\\ -\_\_u32\\ -\end{array}$ -\\\\ - \subsection{Precedence and Order of Evaluation} - You can find the order of evaluation of all Y keywords below -\\\\ -$\begin{array}{|l|l|} -\hline -Operators & Associativity\\ -\hline -\text{() [] .} & \text{Left to right}\\ -\hline -\text{! *(variable)} & \text{Right to left}\\ -\hline -\text{* / \%} & \text{Left to right}\\ -\hline -\text{+ -} & \text{Left to right}\\ -\hline -< <= > >= & \text{Left to right}\\ -\hline -\text{\&\&} & \text{Left to right}\\ -\hline -|| & \text{Left to right}\\ -\hline -\text{\_\_keyword()} & \text{Left to right}\\ -\hline -\end{array}$ \section{Control Flow} The control-flow of a language specify the order in which computations are performed. We have already met the most common control-flow constructions