Code Monkey home page Code Monkey logo

torth's Introduction

Torth

Stack based programming language inspired by Porth that uses Reverse Polish notation.

  • Compiled
  • Statically typed
  • Native (Linux x86_64)
  • Stack-based (just like Porth)
  • Turing complete
  • Self-hosted

Documentation

Usage

$ cat hello.torth
include "std"
function main :
  "Hello, World!\n" puts
end
$ ./torth -r hello.torth
Hello, World!
$ ./torth --help
Usage: ./torth [-h] [--out FILE] [-r] [-s] [-v] code_file

Torth compiler

Positional arguments:
  code_file             Input file

Options:
  -h                    Show this help message and exit
  --out FILE            Output file
  -r                    Run program after compilation
  -s                    Save files generated during compilation
  -v                    Output compilation steps

Examples

More examples are found from the examples-folder.

include "std"
function main :
    "Hello, World!\n" puts
end
// === FIZZBUZZ ===
// Print integers 1 to N with some exceptions:
// => Print “Fizz” if an integer is divisible by 3
// => Print “Buzz” if an integer is divisible by 5
// => Print “FizzBuzz” if an integer is divisible by both 3 and 5

// The standard library implements common functions, for example:
// puts => Output string
// %    => Modulo-operator
include "std"

// Returns the sum of numbers not divisible by 3 or 5
function FizzBuzz limit:int -> int :
    // Push the initial values to stack
    0   // sum
    1   // index

    // Save two topmost values from the stack to variables
    // => TAKE keyword also removes the values from the stack
    // => PEEK keyword would instead save the values without removing them from the stack
    take index sum in

    // Loop while index <= limit
    WHILE index limit <= DO

        // Newlines inside IF condition are just for readability
        // => The whole program could be in one line
        IF
            index 3 % 0 ==
            index 5 % 0 ==
            &&
        DO
            "FizzBuzz\n" puts
        ELIF index 3 % 0 == DO
            "Fizz\n" puts
        ELIF index 5 % 0 == DO
            "Buzz\n" puts
        ELSE
            // Print the current index
            index putu "\n" puts

            // Add index to sum
            sum index + sum =
        ENDIF

        // Increment loop's index
        index 1 + index =
    DONE
    sum // Return sum
end

// Program execution starts from MAIN function (case-insensitive)
function main :
    // Call FizzBuzz with one integer parameter
    // FizzBuzz(limit=30)
    30 FizzBuzz

    // Save the return value to variable called sum and print it with text
    take sum in
    "Sum of all numbers not divisible by 3 or 5: " puts
    sum putu "\n" puts
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.