-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAPSIL.tex
414 lines (227 loc) · 16.9 KB
/
APSIL.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
%\documentclass[11pt]{article}
%\usepackage{booktabs}
%\usepackage{fullpage}
%\usepackage{palatino}
%\usepackage{tabularx}
%\usepackage{listings}
%\usepackage{verbatim}
%\usepackage[bookmarks]{hyperref}
%
%
%\title{APSIL \\ Language Specification \\
%Version 1.0}
%\author{Dr. K. Muralikrishnan \\ \texttt{[email protected]} \\ {NIT Calicut} }
%
%
%\hypersetup{
%colorlinks=false,
%urlcolor=cyan,
%pdfborder= 0 0 0
%}
%
%
%\begin{document}
\chapter{APSIL}
%\newcommand{\kw}[1]{\texttt{#1}}
%.......................Title Page.................................>%
%\maketitle
%\pagebreak
%......................Table of Contents............................%
\thispagestyle{plain}
%\tableofcontents
%\pagebreak
%...............................Introduction..........................%
\section{Introduction}
%\paragraph{}
\textit{APSIL} or \textit{Application Programmer's Simple Integer Language} is a simple and strongly typed programming language. The features and constructs of this language are minimal and mainly intended for testing an experimental operating system. The compiler of APSIL runs on ESIM (\textit{Extended Simple Integer Machine}) architecture.
%\paragraph{}
This document briefly describes the programming constructs, syntax and semantics APSIL. The structure of APSIL is similar in some aspects to programming languages like C and Java.
A typical APSIL program is orgnaized in the following way.
\begin{verbatim}
Global Declarations
...
Function Definitions
...
Main Function
\end{verbatim}
%-----------------------------Lexical Elements-------------------------%
\section{Lexical Elements}
%*********%
\subsection{Comments and White Spaces}
APSIL allows only line comments. Line comments start with the character sequence \textbf{//} and stop at the end of the line.
White spaces in the program including tabs, newline and horizontal spaces are ignored.
%*********%
\subsection{Keywords}
The following are the reserved words in APSIL and it cannot be used as identifiers.
\begin{tabular}{c c c c c c }
\kw{read} & \kw{write} & \kw{if} & \kw{then} & \kw{else} & \kw{endif} \\
\kw{while} & \kw{do} & \kw{endwhile} & \kw{break} & \kw{continue} & \kw{integer} \\
\kw{string} & \kw{main} & \kw{return} & \kw{decl} & \kw{enddecl} & \kw{Create} \\
\kw{Open} & \kw{Write} & \kw{Seek} & \kw{Read} & \kw{Close} & \kw{Delete} \\
\kw{Fork} & \kw{Exec} & \kw{Exit}
\end{tabular}
%*********%
\subsection{Operators and Delimiters}
The following are the operators and delimiters in APSIL \\
\begin{tabular}{c c c c c c c c c c c c c}
( & ) & \{ & \} & [ & ] &
/ & * & + & - & \% \\
\textgreater & \textless & \textgreater = & \textless = & != & ==
& ; & = & \&\& & $\Vert$ & ! \\
\end{tabular}
%*********%
\subsection{Indentifiers}
Identifiers are names of variables and user-defined functions. Identifiers should start with an alphabet, and may contain both alphabets and digits. Special characters are not allowed in identifiers.
\begin{verbatim}
identifier -> (alphabet)(alphabet | digit)*
\end{verbatim}
%*********%
\subsection{Literals}
There are integer literals and string literals in APSIL. An integer literal is a sequence of digits representing an integer.
Negative integers are represented with a negative sign preceding the sequence of digits. Any sequence of characters enclosed within double quotes (") are considered as string literals. However APSIL restricts string literals to size of atmost 16 characters including the '\textbackslash 0' character which is implicitly appended at the end of a string value.
\\
\\
Examples of literals are \texttt{
19, -35, "Hello World"}
%-----------------------------Data Types-------------------------%
\section{Data Types}
\subsection{Primitive Types}
There are two primitive datatypes in APSIL.
\begin{enumerate}
\item \textbf{Integer} : An integer value can range from -32767 to +32768. An integer type variable is declared using the keyword \kw {integer}
\item \textbf{String}
A string type represents the set of string values. A string value can be atmost 16 characters long. String type variables is declared using the keyword \kw{string}.
\end{enumerate}
\subsection{Arrays}
Arrays are sequence of elements of a single type. Arrays can be of \textbf{integer} or \textbf{string} data types.
APSIL allows the use of single-dimensional arrays only, i.e. linear arrays. Array elements are accessed by the array name followed an index value within square brackets ( e.g. arr[10] ).
%-----------------------------Declarations and Scope-------------------------%%
\section{Declarations and Scope}
Declarations should be made for variables and functions defined in the APSIL program.
\subsection{Global Variables}
Global variables are declared in the first section of the program within a \textbf{decl} ... \textbf{enddecl} block. Global variables can be accessed from any function in the program. Global variables can be of integer, string, integer array or string array datatypes. Global variables are declared with its datatype followed by the variable name. If the variable refers to an array the size of the array must be given in square brackets. The general form of declarations is as follows
\textit{type variable\_name;}
\textit{type variable\_name[size];}
\subsection{Function Declaration}
For every function except the \textbf{main()} function defined in a APSIL program, there must be a declaration. All functions have global scope and is declared in the first section within \textbf{decl} ... \textbf{enddecl} block, along with the global variables.
A function declaration should specify the name of the function, the name and type of each of its arguments and the return type of the function. A function can have integer/string arguments. Parameters may be passed by value or reference. Arrays cannot be passed as arguments. If a global variable name appears as an argument, then within the scope of the function, the new declaration will be valid and global variable declaration is suppressed. Different functions may have arguments of the same name. For arguments that are passed by reference, the argument name is preceded by an ampersand(\&) in the function declaration. The return type of a function must be either integer or string. The general form of declarations is as follows \\
\textit{type function\_name (type1 argument1,argument2,...; type2 argument1,argument2,...;...);}
\\
\\
Examples for global declarations
\begin{lstlisting}
decl
integer x,y,a[10],b[20];
integer f1(integer a1,a2; string b1; integer &c1), f2();
string t, q[10], f3(integer x);
integer swap(integer &x, &y);
enddecl
\end{lstlisting}
\subsection{Local Variables}
Local variables can be declared anywhere inside a function definition except in the body of \textbf{if} and \textbf{while}. Local variables will have a function scope, i.e. it can only be accessed in the function in which it is declared. Arguments of a function are treated as local variables. Local variables can be integer or string. Arrays cannot be declared locally. All globally declared variables are visible inside a function, unless suppressed by a re-declaration. The general form of declarations is as follows \\
\textit{type variable\_name;}
%------------------__Function Definition----------------------_%
\section{Function Definition and Main Function}
Every APSIL program must have a \textbf{main()} function and zero or more user-defined functions. Every function other than the \textbf{main()} function must be declared within the \textbf{decl ... enddecl} block. The general form of a function definition is given below
\\
\\
\textit{
type function\_name(ArgumentList) \\ \{ \\
Function Body \\
\}
}
\\
\\
The function body must contain a return statement and the return value must be of the return type of the function. The arguments and return type of each function definition should match exactly with the corresponding declaration. Every declared function must have a definition. The signature of the function in the delcaration should match the definition of the function which includes the return type, and the names, passing mehtod and datatypes of the arguments. The language supports recursion and static scope rules apply.
\subsection{main()}
The \textbf{main()} function must be a zero argument function of type integer. Program execution begins from the body of the \textbf{main()} function. The \textbf{main()} function need not be declared. The \textbf{main()} function definition follows all user-defined function definitions. The definition part of \textbf{main()} should be given in the same format as any other function.
%----------------------------_Expressions-------------------------_%
\section{Expressions}
An expression specifies the computation of a value by applying operators and functions to operands. Function call in APSIL are treated as expressions, and the value of the expression is its return value. APSIL supports arithmetic and logical expressions
\subsection{Arithmetic Expressions}
Any integer value, variable, function returning an integer or 2 or more arithmetic expressions connected by arithmetic operators termed as arithmetic expressions. APSIL provides five arithmetic operators, viz., +, -, *, / (Integer Division) and \% (Modulo operator) through which arithmetic expressions may be combined. Expression syntax and semantics are similar to standard practice in programming languages and normal rules of precedence, associativity and paranthesization hold. APSIL is strongly typed, and hence the types of the oprands must match the operation.
\subsection{Logical Expressions}
Logical expressions may be formed by combining arithmetic expressions using relational operators. The relational operators supported by APSIL are \begin{verbatim} <, >, <=, >=, ==, !=
\end{verbatim}
Standard meanings apply to these operators. The operators take two arithmetic expressions as operands and the result will be a boolean value, either of 1(true) or 0(false). Only realational operator that can be applied to two strings is == (to check equality). This also considered as a Logical expression. Logical expressions themselves may be combined using logical operators, \&\& (logical and) , $\Vert$ (logical or) and ! (not).
\subsection{Function Call}
All functions except the \textbf{main()} function can be invoked from any other function including itself. The general form of a function call is \\
\textit{function\_name(value1,value1...);}
\\
Function calls are treated as expressions. The function takes in the values of its arguments and returns a value of type equal to the return type of the function. This value is treated as the evaluated result of the function call.
%-----------------________Statements---------------------------_%
\section{Statements}
Statements control the execution of the program. All statements in APSIL are terminated with a semicolon ;
\subsection{Assignment Statement}
The APSIL assignment statement assigns the value of an expression to a variable, or an indexed array of the same type or a string value to a string variable. \textbf{=} is known as the assignment operator. Initialization during declaration is not allowed in APSIL. The general syntax is as follows \\
\textit{ variable\_name = string\_value / array\_variable / expression }
\subsection{If Statement}
\textbf{If} statements specify the conditional execution of two branches according to the value of a boolean expression. If the expression evaluates to true, the \textbf{if} branch is executed, otherwise, if present, the \textbf{else} branch is executed. The \textbf{else} part is optional. The general syntax is as follows \\
\textit{
\textbf{if} (logical expression) \textbf{then} \\
\indent \indent statements; \\
\indent \textbf{else} \\
\indent \indent statements; \\
\indent \textbf{endif;} \\
}
\subsection{While Statement}
\textbf{While} statement iteratively executes a set of statements based on a condition which is a logical expression. The statements are iteratively executed as long as the logical expression evaluates to true.
\textit{
\textbf{while} (logical expression) \textbf{do} \\
\indent \indent statements; \\
\indent \textbf{endwhile;} \\
}
\subsection{Break statement}
\textbf{Break} statement is a statement which is used in a while loop block. This statement stops the execution of the loop in which it is used and passes the control of execution to the next statement after the loop. This statement cannot be used anywhere else other than while loop. The syntax is as follows\\
\textit{\textbf{break} ;}
\subsection{Continue statement}
\textbf{Continue} statement is a statement which is also used only in a while loop block. This statement skips the current iteration of the loop and passes the control to the next iteration after checking the loop condition. The syntax is as follows\\
\textit{\textbf{continue} ;}
\subsection{Return statement}
\textbf{Return} statement in a function passes the control from the callee to the caller function and returns a value to the caller function. All functions including the \textbf{main()} must have exactly one \textbf{return} statement and it should be the last statement in the funtion body. The return type of the function should match the type of the expression. The return type of main is integer. The syntax is as follows\\
\textit{\textbf{return} expression;}
\subsection{Read/Write statements}
The standard input and output statements in APSIL are \textbf{read} and \textbf{write} respectively. The read statement reads an integer value from the standard input device into an integer variable or an indexed array variable or a string value into a string variable. The write statement outputs a string literal or the value of string variable or an arithmetic expression into the standard output.\\
\textit{\textbf{read} variable\_name;}
\textit{\textbf{write} expression / string;}
\section{System Calls}
System Calls allow the programs written in APSIL to interact with the operating system running on the ESIM architecture. 10 System Calls are supported by APSIL.
\subsection{Create}
Creates a file with the specified filename in the filesystem. The return value of this system call is 0 in case of success and the appropriate error code in case of failure.
\\
\textit{integer \textbf{Create}(string filename);}
\subsection{Open}
Returns a file descriptor of the file in the filesystem with the specified filename. The file descriptor is an integer value. If the \textbf{Open} fails, an appropriate error code is returned.
.\\
\textit{integer \textbf{Open}(string fileName);}
\subsection{Read}
Reads the specified number of words(16 bytes) from a file which has the specified file descriptor into a string array. The return value of this system call is the number of words successfully read. If the \textbf{Read} fails, an appropriate error code is returned.\\
\textit{integer \textbf{Read}(integer fileDescriptor, string buffer[], integer numWords);}
\subsection{Write}
Writes the specified number of words from the string buffer to a file in the filesystem with the specified file descriptor. The return value of this system call is the number of words successfully written. If the \textbf{Write} fails, an appropriate error code is returned.\\
\textit{integer \textbf{Write}(integer fileDescriptor, string buffer[], integer numWords);}
\subsection{Seek}
\textbf{Seek} is used to change the read/write head position in a file. It moves the head to the specified number of words from the beginning of the file. The return value of this system call is 0 in case of success and the appropriate error code in case of failure.\\
\textit{integer \textbf{Seek}(integer fileDescriptor, integer numWords);}
\subsection{Close}
This system call is used to close an open file. The return value of this system call is 0 in case of success and the appropriate error code in case of failure.\\
\textit{integer \textbf{Close}(integer fileDescriptor);}
\subsection{Delete}
This system call is used to delete the file from the file system whose name is specified in the
argument. The return value of this system call is 0 in case of success and the appropriate error code in case
of failure.\\
\textit{integer \textbf{Delete}(string fileName);}
\subsection{Fork}
This system call is used to create a copy of the current process in the system. The return value of this system call is the PID of the child process for the parent, and 0 for the child.
\textit{integer \textbf{Fork}();}
\subsection{Exec}
This system call is used to load the program, whose name is specified in the argument, in the
memory space of the current process and start its execution. The return value of this system call is 1 in case of failure.
\textit{integer \textbf{Exec}(string fileName);}
\subsection{Exit}
This system call is used to terminate the execution of the process which invoked it and remove it from the memory.
\textit{void \textbf{Exit}();}
\subsection{Halt}
This system call is used to halt the machine.
\textit{void \textbf{Halt}();}
%\end{document}