-
Notifications
You must be signed in to change notification settings - Fork 2
3 Hello World
This is the traditional "Hello, World!" program implemented in CSpydr:
import "io.csp";
fn main(): i32
{
std::io::puts("Hello, World");
<- 0;
}
As you can see, the syntax is quite like Rust, while the semantics work like C.
import "io.csp";
This is a preprocessor directive, importing the file "io.csp" from the standard library.
fn main(): i32
Here we define a function called "main" using the fn
keyword. It takes no arguments and returns a signed 32-bit integer. This is the return value of the program.
{
Here, the code execution begins by opening a new block statement. It groups together multiple statements for execution.
std::io::puts("Hello, World");
This is a call to the function "puts", defined in the "std::io" namespace implemented in the standard library. It passes the string "Hello, World" to the function, which it will print out to the terminal. We end the statement using the ;
(semicolon) symbol.
<- 0;
Finally, we return 0 from the main function to end the program successfully. Every value other than 0 indicates an error.
}
At the end, we still need to close the block statement.
(This assumes, you have CSpydr correctly installed; see 2 - Installation for help.)
user@host:/some/directory $ cspc run helloworld.csp <-- your file
Compiling helloworld.csp
Optimizing (2/2) evaluate constant expressions
Generating assembly for x86_64-linux-gnu
Linking a.out
Executing a.out
Hello, World! <-- your program output
[./a.out terminated with exit code 0]
user@host:/some/directory $ _
The Hello World example and others can be found in the examples/
directory of the CSpydr GitHub repository
Next: 4 - Basic Concepts
2.1 - Compatibility
2.2 - Obtaining CSpydr
2.3 - Dependencies
2.4 - Building
2.5 - Installation
2.6 - Usage
4.1 - Types and Values
4.2 - Type Modifiers
4.3 - Truthiness
4.4 - Naming Conventions
5.1 - Lexical Conventions
5.2 - Variables, Members and Arguments
5.3 - Statements
5.3.1 - Blocks
5.3.2 - Loops
5.3.3 - Control Flow
5.3.4 - Expression Statements
5.3.5 - Variable Declarations
5.4 - Expressions
5.4.1 - Literals
5.4.2 - Operator Precedence
5.4.3 - Binary Operators
5.4.4 - Unary Operators
5.4.5 - Postfix Operators