Astra Language
AstraBook: Official User Guide for the Astra Programming Language
================================================================================
1. THE BASICS: SYNTAX AND VARIABLES
================================================================================
Astra is designed to make code flow from left to right, similar to natural writing.
This is achieved through specific syntactical changes.
Key Concepts:
* New Assignment Operator: Astra uses "variable : value" instead of the equals sign to allow
for natural left-to-right flow (e.g., a : a + b).
* Tagged Keywords: Control flow and structural keywords are enclosed in curly braces,
such as <while>, <fun>, and <if>, making mistakes easier to find.
* Comments: Single-line comments begin with //.
Data Types and Operators:
* Standard data types include int, float, char, string, and bool.
* Standard arithmetic operators supported are +, -, *, /, and %.
Variable Declaration:
* Variables can be declared only (e.g., int i) or initialized
(e.g., int i(0), char c("a"), bool l(true), string s("abcde")).
================================================================================
2. PROGRAM STRUCTURE AND FUNCTIONS
Astra programs are organized into modules and functions.
Modules:
* A program is defined within a <module> block.
<module> Euclidean // module itself
...
</module>
Function Declaration:
* Function declarations follow the left-to-right flow with the syntax:
<fun> <return_type> <function_name>(<input_parameters>).
* For example, "<fun> int Euclid(int a, int b)" declares a function
named Euclid returning an integer.
* Functions are called using the format: f(a, b).
The "main" Function:
* The entry point is declared as: <fun> int main(string args[]).
Example: Euclidean Program
The code "Euclid(in -> a, b) -> out" demonstrates Astra's seamless I/O syntax.
This single line takes input, assigns it to variables, passes them to the function,
and sends the return value to the output.
<module> Euclidean // module itself
<fun> int Euclid(int a, int b) // function with type int
<while> b!= 0
<if> a > b
a : a - b
<else>
b : b - a
</else>
</if>
</while>
<return> a </return>
</fun>
<fun> int main(string args[]) // main function
int a, b
//standard I/O
// in -> a, b
// Euclid(a , b) -> out
//short I/O
Euclid(in -> a , b) -> out
// combined input and output and function call
// seamless input to output syntax
</fun>
</module>
================================================================================
3. CONTROL FLOW AND ARRAYS
Conditionals and Loops:
* Conditionals use <if> and <else> blocks.
The `Euclidean` example shows a simple `<if>`/`<else>` block.
```Astra
<if> a > b
a : a - b
<else>
b : b - a
</else>
</if>
* Loops use the <while> keyword.
<while> b!= 0
...
</while>
Arrays:
* Declaration: int a[n] declares an array with n elements.
* Initialization: int a[4]([1, 2, 3, 4]).
* Access: Uses 0-based indexing, such as a[i].
For Loops:
* The syntax is <for>int i(0)++ <n.
* "int i(0)" initializes the iterator, "++" defines the step,
and "< n" is the continuation condition.
<for> int k(0)++ < n // fill with true
primes[k] : true
</for>
================================================================================
4. ADVANCED EXAMPLE: SIEVE OF ERATOSTHENES
The "primes" module demonstrates complex array manipulation, nested <while> loops,
and formatted output like "'\n' -> out". It uses logic to calculate prime numbers and print
them to the console in a formatted list.
<module> primes
<fun> int Eratosthenes(int n) // subroutine declaration
bool primes [ n ] // variable declaration
int l(0), i(0), index_square(3)
int first, last, factor
<for> int k(0)++ < n // fill with true
primes[k] : true
</for>
<while> index_square < n
<if> primes[i]
first : 0 + index_square
last : 0 + n
factor : i + i + 3
primes[first] : false
<while> last - first > factor
first : first + factor
primes[first] : false
</while>
</if>
i : i + 1
index_square : 2 * i * (i + 3) + 3
</while>
' 2' : out // print out
<for> i(0)++ < n // print out
<if> primes[i]
<if> 2 * i + 3 > n
break
</if>
' ' 2 * i + 3 -> out
l : l + 1
<if> l % 10 == 0
'\n' -> out
</if>
</if> // if
</for> // print out
'\n number : ' l -> out
</fun> // erato fun
<fun> int main // main function
Eratosthenes(1000) // subroutine call
</fun>
</module>
================================================================================
5. OBJECT-ORIENTED PROGRAMMING: CLASSES
* Declaration: Classes are declared using <class> ClassName ... </class>.
* Members: Classes can contain data members and member functions.
* Object Creation: "GCD N" creates an instance of the class GCD.
* Access: Members are accessed with the dot operator, such as N.a or N.Euclid.
<module> Euclidean
<class> GCD //class declaration
int a, int b //data members
<fun> int Euclid(int a, int b) //member function
<while> b!=0
<if> a > b
a : a - b
<else>
b : b - a
</else>
</if>
</while>
<return> a </return>
</fun>
</class>
<fun> int main(string args[]) //main function
GCD N //object N of class GCD
N.Euclid(in -> N.a, N.b) -> out
//input, output in one line
</fun>
</module>
The syntax "N.Euclid(in -> N.a, N.b) -> out" reads input directly into object members,
executes a function, and prints the result.
================================================================================
6. INTERMODULAR
Every module can connect to others through an <intermodular> block,
which appears before the module itself.
* <link> filename.Astra is used to connect to other modules or standard I/O.
* Functions declared in this block can be used by other modules.
<intermodular>
<link> in2out.ast </link> //using standard input and output
<link> module2.ast </link> //connecting to another module
<fun> int Euclid(int a, int b) </fun> //function that can be used by another modules
</intermodular>
<module> Euclidean
<fun> int Euclid(int a, int b)
...
</fun>
...
</module>
Notice that source code extension of Astra modules is `.ast`
================================================================================
7. FILE SYSTEM
* File Declaration: file f(name, type, options).
* Types: bin, txt, or hex.
* Options: r (read), w (write).
* Usage: Files use .open and .close methods,
with arrows for data transfer (s -> f for writing, f -> s for reading).
file f("Readme.md", txt, wr+) //creating text file
string s //srtring for reading and writing
f.open
<while> s
//string for writing exists like reading "in : s" from standard input
s -> f //writing string to file
</while>
f.close
f.open
<while> not f.eof //reading
f -> s //reading string from file
</while>
f.close
================================================================================
8. INTEGRATION WITH HTML
Astra code can be embedded directly within HTML using the <Astra> ... </Astra> block.
This can be placed in either the header or the body of a webpage.
<html>
...
<Astra>
<module> Euclidean
<fun> int Euclid(int a, int b)
...
</fun>
...
</module>
</Astra>
...
</html>
================================================================================
9. ORIGINS
Astra has roots in several languages:
* Tagged keywords are derived from HTML and SGML.
* Variable declarations (int i) are influenced by C.
* The assignment operator (a : b) is influenced by Pascal.
---
Comments
Post a Comment