Skip to content

3 Hello World

Spydr06 edited this page Oct 24, 2022 · 2 revisions

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.

Let's go through this line by line:

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.

Running the program

(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