Skip to main content

Getting Started

·309 words·2 mins
Documentation - This article is part of a series.
Part 3: This Article

Writing And Executing A Sage Program
#

Install Sage: First, head to the installation page to get Sage on your machine!

Once you have Sage installed, you can start writing Sage code!

Begin by creating a new folder for your Sage project:

$ mkdir hello-world
$ cd hello-world

Create a Sage program with the .sg file extension:

$ echo 'println("Hello world!");' > main.sg

Then, compile and run your program!

$ # Compile and interpret the VM code
$ sage main.sg
$ # Compile and translate the VM code to C
$ sage main.sg -tc
$ gcc out.c -O3 -o main # Compile the generated C code

CLI Interface
#

For more information about Sage’s CLI, you can run sage -h:

$ sage -h

   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ    β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ   `-.        _.-'
  β–ˆβ–ˆβ–ˆβ–‘β–‘   β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ  β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ   \ `,    .'/.'
 β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ     \`.`. :.-'.-= .-'/
  β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘       `-.:/o  .'-'/ .'
  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ         o\o / ._/.'
 β–‘β–‘β–‘β–‘β–‘β–‘   β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘  β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–‘            \| /o|\`.
                    β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ                    |'o `.`.'.
                   β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ                           `--'
                    β–‘β–‘β–‘β–‘β–‘β–‘            

Usage: sage [OPTIONS] <INPUT>

Arguments:
  <INPUT>  The input file to compiler

Options:
  -o, --output <OUTPUT>
          The file to write the output of the compiler to [default: out]
  -s <SOURCE_TYPE>
          The source language to compile [default: sage] [possible values: sage, low-ir, core-asm, std-asm, core-vm, std-vm]
  -t <TARGET_TYPE>
          The target language to compile to [default: run] [possible values: run, core-asm, std-asm, core-vm, std-vm, c]
  -c, --call-stack-size <CALL_STACK_SIZE>
          The number of cells allocated for the call stack [default: 8192]
  -l, --log-level <LOG_LEVEL>
          The log level to use [default: off] [possible values: error, warn, info, debug, trace, off]
  -d, --debug <DEBUG>
          The symbol to debug (if any exists). This will also enable debug logging
  -h, --help
          Print help (see more with '--help')
  -V, --version
          Print version

Now that you have Sage on your machine, let’s dive into the language itself and write some neat programs!

Documentation - This article is part of a series.
Part 3: This Article