Y-asc

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

commit 57e7c9864d8780f8485ff3e9306511db355753e9
parent 25b364fe6ece29ae5324f0b7437cb25783add2e9
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 28 Jun 2017 16:31:52 +0200

Add(Tex): Moved some sections, add anoter

Diffstat:
Mtex/yasc.tex | 487++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 335 insertions(+), 152 deletions(-)

diff --git a/tex/yasc.tex b/tex/yasc.tex @@ -54,6 +54,35 @@ \part{The Y Language} \section{Getting Started} \section{Types, Operators and Expressions} + \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 @@ -69,134 +98,6 @@ 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{Namespaces} \label{namespaces} - Namespaces are used to limit scope of functions. They must be declared - via the keyword \texttt{def}, with brackets to delimit the scope. - Namespaces names are made up of letters and digits; the first character - must be a letter, in uppercase: Lowercase names are reserved to variables. - - Example of a namespace declaration: - \begin{lstlisting} - def Namespace_name { - [...] - }; - \end{lstlisting} - Functions can be declared in a namespace: - \begin{lstlisting} - def Namespace_name { - function_name(ubyte[] : str) : bool { - [...] - return true; - } - }; - \end{lstlisting} - Any functions declared in a namespace is meant to be used with this - namespace. In order to use a specific namespace, one must declare it: - \begin{lstlisting} - use Namespace_name; - \end{lstlisting} - The keyword \texttt{use} used with the namespace name will allow - the call of all functions in this namespace. In order to use one - function of this namespace, one can use an accessor on the namespace name: - \begin{lstlisting} - use Namespace_name.function_name; - \end{lstlisting} - With this declaration, any other function of this namespace used in - code will throw an error. - - With proper usage of the keyword \texttt{use}, one can use a - namespace's function in the following way: - \begin{lstlisting} - Namespace_name.function_name("Hello!"); - \end{lstlisting} - - Since namespaces are a limited scope, they can be two functions with - the same name, in two different namespaces but they must always be used - with a proper accessor; this way, the compiler / interpreter will - never be confused as what function should be called. - However, take the following example: - - \begin{lstlisting} - def Namespace_one { - hello(void) : void { - print("Hello from one"); - } - }; - - def Namespace_two { - use Namespace_one; - - hello(void) : void { - print("Hello from two"); - hello(); - } - }; - \end{lstlisting} - In the \texttt{Namespace\_two} function \texttt{hello}, we call a - function named \texttt{hello}: In this specific case, the function - called will be the one of the current namespace, thus resulting in - an infinite loop. If one must use a same named function in another - namespace, a proper accessor must be used: - \begin{lstlisting} - def Namespace_one { - hello(void) : void { - print("Hello from one"); - } - }; - - def Namespace_two { - use Namespace_one; - - hello(void) : void { - print("Hello from two"); - Namespace_two.hello(); - } - }; - - \end{lstlisting} - The same logic can be used for globals functions named the same as - namespace's function: - \begin{lstlisting} - hello(void) : void { - print("Hello from one"); - } - - def Namespace_two { - hello(void) : void { - print("Hello from two"); - hello(); - } - }; - \end{lstlisting} - Here, in the \texttt{Namespace\_two.hello} function, the function - called will be itself. However, if one call the \texttt{hello} function - from a global scope, such as \texttt{main}, the global scope will be used: - \begin{lstlisting} - hello(void) : void { - print("Hello from one"); - } - - def Namespace_two { - hello(void) : void { - print("Hello from two"); - } - }; - - use Namespace_two; - - main(u32 : ac, ubyte[]* : av) : u32 { - hello(); - return 0; - } - \end{lstlisting} - Will return: - \begin{lstlisting} - > Hello from one - \end{lstlisting} - If one must use the \texttt{Namespace\_two} \texttt{hello} function, - a proper accessor must be used, as shown above. - - \subsection{Data types and Sizes} One can use the following standard data types: @@ -232,34 +133,134 @@ Name & Range & Content\\ The \texttt{\_\_const} keyword must always be between a variable type and its name. - \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. + + \subsection{Inline Constant Declarations} + One can use inline constant declarations in order to simplify code \begin{lstlisting} - u32 count, j; - bool __const ret = true; + print("Hello!"); \end{lstlisting} - Variables can be distributed among declarations in any fashion; the - list above could well be written as + 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} - u32 count; - u32 j; + print_int(u8 : num) : void { + print("%d\n", num); + } + + [...] + + main(u32 : ac, ubyte[]* : av) : u32 { + print_int(23); + } \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. + 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 integent, 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 constat 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} - 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{Relational and Logical Operators} The relational operators are @@ -289,7 +290,8 @@ $\begin{array}{ll} 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} + + \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: @@ -340,7 +342,59 @@ Name & Argument & Returns & Role\\ \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); + } + + [...] + + 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} \subsection{If-Else} \subsection{Else-If} @@ -352,6 +406,135 @@ Name & Argument & Returns & Role\\ \subsection{Basics} \subsection{External Variables} \subsection{Scope Rules} + \subsection{Namespaces} \label{namespaces} + Namespaces are used to limit scope of functions. They must be declared + via the keyword \texttt{def}, with brackets to delimit the scope. + Namespaces names are made up of letters and digits; the first character + must be a letter, in uppercase: Lowercase names are reserved to variables. + + Example of a namespace declaration: + \begin{lstlisting} + def Namespace_name { + [...] + }; + \end{lstlisting} + Functions can be declared in a namespace: + \begin{lstlisting} + def Namespace_name { + function_name(ubyte[] : str) : bool { + [...] + return true; + } + }; + \end{lstlisting} + Any functions declared in a namespace is meant to be used with this + namespace. In order to use a specific namespace, one must declare it: + \begin{lstlisting} + use Namespace_name; + \end{lstlisting} + The keyword \texttt{use} used with the namespace name will allow + the call of all functions in this namespace. In order to use one + function of this namespace, one can use an accessor on the namespace name: + \begin{lstlisting} + use Namespace_name.function_name; + \end{lstlisting} + With this declaration, any other function of this namespace used in + code will throw an error. + + With proper usage of the keyword \texttt{use}, one can use a + namespace's function in the following way: + \begin{lstlisting} + Namespace_name.function_name("Hello!"); + \end{lstlisting} + + Since namespaces are a limited scope, they can be two functions with + the same name, in two different namespaces but they must always be used + with a proper accessor; this way, the compiler / interpreter will + never be confused as what function should be called. + However, take the following example: + + \begin{lstlisting} + def Namespace_one { + hello(void) : void { + print("Hello from one"); + } + }; + + def Namespace_two { + use Namespace_one; + + hello(void) : void { + print("Hello from two"); + hello(); + } + }; + \end{lstlisting} + In the \texttt{Namespace\_two} function \texttt{hello}, we call a + function named \texttt{hello}: In this specific case, the function + called will be the one of the current namespace, thus resulting in + an infinite loop. If one must use a same named function in another + namespace, a proper accessor must be used: + \begin{lstlisting} + def Namespace_one { + hello(void) : void { + print("Hello from one"); + } + }; + + def Namespace_two { + use Namespace_one; + + hello(void) : void { + print("Hello from two"); + Namespace_two.hello(); + } + }; + + \end{lstlisting} + The same logic can be used for globals functions named the same as + namespace's function: + \begin{lstlisting} + hello(void) : void { + print("Hello from one"); + } + + def Namespace_two { + hello(void) : void { + print("Hello from two"); + hello(); + } + }; + \end{lstlisting} + Here, in the \texttt{Namespace\_two.hello} function, the function + called will be itself. However, if one call the \texttt{hello} function + from a global scope, such as \texttt{main}, the global scope will be used: + \begin{lstlisting} + hello(void) : void { + print("Hello from one"); + } + + def Namespace_two { + hello(void) : void { + print("Hello from two"); + } + }; + + use Namespace_two; + + main(u32 : ac, ubyte[]* : av) : u32 { + hello(); + return 0; + } + \end{lstlisting} + Will return: + \begin{lstlisting} + > Hello from one + \end{lstlisting} + If one must use the \texttt{Namespace\_two} \texttt{hello} function, + a proper accessor must be used, as shown above. + + + \subsection{Header Files} \subsection{Static Variables} \subsection{Recursion}