Skip to main content

The Talon programming language

Project description

The Talon Programming Language

Talon is a toy programming language with Python and JavaScript flavors.

fun factorial(n) {
  if (n == 1) {
    ret 1
  } else {
    ret n * factorial(n - 1)
  }
}

this f = factorial(4)

print(f) // -> 24
this name = getstr('What is your name? ')
this length = len(name)

printf('Your name is %0; it has %1 characters.', [name, length])

You can view more demos in the examples folder.

Running and Compiling Code

First you must install Talon.

pip install talonlang

Then you can run Talon programs:

tal [-c] <input.tal[c]> [-o <output.talc>]
  • The -c switches on compiling. If no -o flag is provided (along with a filename), the output file will be named according to the input file. Eg. input.tal to input.talc. (Note: you may compile already compiled code, but that is redundant and pointless.)
  • The -o flag changes the name of the output file. It can only be used if -c is present.
  • If the -c flag is not included, the interpreter will either
    • compile and run the provided code if the file ends in .tal,
    • or interpret the compiled code if the file extension is .talc.

Basic Syntax

A basic rundown of Talon's syntax.

Comments

Single line:

// I'm a comment
print('Hello') // Another comment

Multi-line:

/*
  This is a multi-line comment.
  It can cover multiple lines.
*/

Variables

Variables are initialized with the this keyword:

this foo = bar

Like in JavaScript, values are not required when first initializing a variable:

this x

However, re-initializing a variable results in an error:

this name = 'Talon'
this name // ERROR

Assignment can be accomplished via a variety of operators:

this n
n = 1
n += 2    // n = n + 2
n -= 5    // n = n - 5
n *= 7    // n = n * 7
n /= 1.5  // n = n / 1.5
n %= 3    // n = n % 3
n ^= 20   // n = n ^ 20

Values

  • Numbers
    • Integer: 42, 1, -67
    • Float: 3.1415, 98.6, -1.168
  • Strings
    • Single-quoted: 'Hello!'
    • Double-quoted: "Good day!"
  • Booleans
    • Truthy: true, on, yes
    • Falsey: false, off, no
  • Lists
    • Creating
      • Single dimensional: [1, 2, 3, 4]
      • Multi-dimensional: [[1, 2], [3, 4]]
    • Accessing: list[index], list[index][index2]
    • Slicing: list[:-1], list[1:], list[2:-2]
  • Ranges
    • Inclusive: 1 to 10
    • Exclusive: 1 upto 11

Operators

  • Binary
    • Addition: a + b
    • Subtraction: a - b
    • Multiplication: a * b
    • Division: a / b
    • Modulus: a % b
    • Exponent: a ^ b
  • Unary
    • Negation: -x
    • Absolute value: +x
    • Not: !x
  • Comparing
    • Equals: a == b
    • Not equals: a != b
    • Less than: a < b
    • Greater than: a > b
    • Less than or equal to: a <= b
    • Greater than or equal to: a >= b
  • Boolean
    • And: a && b
    • Or: a || b

Conditional Statements

if (/* condition */) {
  ...
} else if (/* condition */) {
  ...
} else {
  ...
}

Loops

While loop:

this i = 0

while (i < 10) {
  print(i)
  i += 1
}

For loop:

this i

for (i in 1 to 10) {
  print(i)
}
this alphabet = 'abcdefghijklmnopqrstuvwxyz'
this letter

for (letter in alphabet) {
  printf('The letter is %0.', [letter])
}

Functions

Functions can be created by a variety of methods:

fun foo(bar) {
  ret bar * 2
}
this foo = fun (bar) {
  ret bar * 2
}
this foo = (bar) -> {
  ret bar * 2
}
this foo = (bar) -> (bar * 2)

They have their own scope:

fun scope() {
  this x = 2
  x = 5
  y = 6
}

this x = 1
this y = 1

scope()

print(x, y) // 1, 6

Imports

Currently very limited, importing allows you to import functions and variables from other Talon scripts.

// a.tal

import('b.tal')

print(add(1, 2)) // 3
// b.tal

fun add(a, b) {
  ret a + b
}

Hopefully namespaces (which aren't yet supported) will be implemented in the future.

import('b.tal', 'b')

print(b.add(1, 2))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

talonlang-1.2.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

talonlang-1.2.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file talonlang-1.2.0.tar.gz.

File metadata

  • Download URL: talonlang-1.2.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for talonlang-1.2.0.tar.gz
Algorithm Hash digest
SHA256 60bfe8f8b92036c39dd53e24ca589224a12952b5ecacf7052fe1a7d9cc36d939
MD5 71534ed7d8234829100afd726f5a5f7a
BLAKE2b-256 21e32433e14060a53966d3167e06cf08be503c27c4e8fadff783a20f3ed17d02

See more details on using hashes here.

File details

Details for the file talonlang-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: talonlang-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for talonlang-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f44e819ab6b45bed88e6c252e48ef895ec3e9746f244da757a802d0a239d125
MD5 37d903a10468a334095da3d73370a18f
BLAKE2b-256 cf7a379984cb4114477f25e3900e77493ff16b95f175a24cae779d0a048f14e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page